Spaces:
Sleeping
Sleeping
Commit ·
1c37637
1
Parent(s): 2aa8392
Resblock for the model
Browse files
model.py
CHANGED
|
@@ -40,3 +40,27 @@ class Time_embeddings(torch.nn.Module):
|
|
| 40 |
def forward(self, timesteps: torch.Tensor) -> torch.Tensor:
|
| 41 |
time_emb = self.Sinusoidal_waves(timesteps)
|
| 42 |
return self.mlp(time_emb)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def forward(self, timesteps: torch.Tensor) -> torch.Tensor:
|
| 41 |
time_emb = self.Sinusoidal_waves(timesteps)
|
| 42 |
return self.mlp(time_emb)
|
| 43 |
+
|
| 44 |
+
#Resblock
|
| 45 |
+
class resblock(torch.nn.Module):
|
| 46 |
+
def __init__(self, in_channels, out_channels, time_emb_dim):
|
| 47 |
+
super().__init__()
|
| 48 |
+
self.conv1 = nn.Conv2d(in_channels, out_channels, 3, padding=1)
|
| 49 |
+
self.conv2 = nn.Conv2d(out_channels, out_channels, 3, padding=1)
|
| 50 |
+
self.norm1 = nn.GroupNorm(8, out_channels)
|
| 51 |
+
self.norm2 = nn.GroupNorm(8, out_channels)
|
| 52 |
+
self.silu = nn.SiLU()
|
| 53 |
+
self.time_mlp = nn.Linear(time_emb_dim, out_channels)
|
| 54 |
+
#Matching in channels and out channels for skip connection
|
| 55 |
+
self.residual_conv = nn.Conv2d(in_channels, out_channels, 1) if in_channels != out_channels else nn.Identity()
|
| 56 |
+
|
| 57 |
+
def forward(self, x, time_dim):
|
| 58 |
+
h = self.norm1(self.conv1(x))
|
| 59 |
+
h = self.silu(h)
|
| 60 |
+
#Tensor broadcasting (b, out_channels) -> (b, out_channels, 1, 1)
|
| 61 |
+
time_proj = self.silu(self.time_mlp(time_dim))[:, :, None, None]
|
| 62 |
+
h = h + time_proj
|
| 63 |
+
h = self.norm2(self.conv2(h))
|
| 64 |
+
h = self.silu(h)
|
| 65 |
+
return h + self.residual_conv(x)
|
| 66 |
+
|