Spaces:
Sleeping
Sleeping
Commit ·
629a11e
1
Parent(s): fee5b68
Full Unet class assembled with everything
Browse files
model.py
CHANGED
|
@@ -77,4 +77,58 @@ class up_sample(torch.nn.Module):
|
|
| 77 |
super().__init__()
|
| 78 |
self.conv = nn.ConvTranspose2d(channels, channels, 4, stride=2, padding=1)
|
| 79 |
def forward(self, x):
|
| 80 |
-
return self.conv(x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
super().__init__()
|
| 78 |
self.conv = nn.ConvTranspose2d(channels, channels, 4, stride=2, padding=1)
|
| 79 |
def forward(self, x):
|
| 80 |
+
return self.conv(x)
|
| 81 |
+
|
| 82 |
+
#Assembling Unet class
|
| 83 |
+
class Unet(torch.nn.Module):
|
| 84 |
+
def __init__(self, in_channels=3, time_dim=128):
|
| 85 |
+
super().__init__()
|
| 86 |
+
#Time embedding diemension
|
| 87 |
+
self.time_emb = Time_embeddings(in_dim=time_dim, out_dim=256)
|
| 88 |
+
self.inc = nn.Conv2d(3, 64, 3, padding=1)
|
| 89 |
+
|
| 90 |
+
#Encoder class
|
| 91 |
+
self.resblock1 = resblock(64, 128, time_emb_dim=256)
|
| 92 |
+
self.down1 = down_sample(128)
|
| 93 |
+
|
| 94 |
+
self.resblock2 = resblock(128, 256, time_emb_dim=256)
|
| 95 |
+
self.down2 = down_sample(256)
|
| 96 |
+
|
| 97 |
+
#Bottleneck
|
| 98 |
+
self.bot1 = resblock(256,256, time_emb_dim=256)
|
| 99 |
+
self.bot2 = resblock(256,256, time_emb_dim=256)
|
| 100 |
+
|
| 101 |
+
#Decoder class
|
| 102 |
+
self.up1 = up_sample(256)
|
| 103 |
+
self.resblock3 = resblock(512, 128, time_emb_dim=256)
|
| 104 |
+
|
| 105 |
+
self.up2 = up_sample(128)
|
| 106 |
+
self.resblock4 = resblock(256, 64, time_emb_dim=256)
|
| 107 |
+
|
| 108 |
+
#Final output layer
|
| 109 |
+
self.outc = nn.Conv2d(64, in_channels, 3, padding=1)
|
| 110 |
+
|
| 111 |
+
def forward(self, x, t):
|
| 112 |
+
|
| 113 |
+
t_emb = self.time_emb(t)
|
| 114 |
+
x1 = self.inc(x)
|
| 115 |
+
|
| 116 |
+
x2 = self.resblock1(x1, t_emb)
|
| 117 |
+
x3 = self.down1(x2)
|
| 118 |
+
x4 = self.resblock2(x3, t_emb)
|
| 119 |
+
x5 = self.down2(x4)
|
| 120 |
+
|
| 121 |
+
#bottle neck
|
| 122 |
+
x5 = self.bot1(x5, t_emb)
|
| 123 |
+
x5 = self.bot2(x5, t_emb)
|
| 124 |
+
|
| 125 |
+
#decoder
|
| 126 |
+
x6 = self.up1(x5)
|
| 127 |
+
x6 = torch.cat([x4, x6], dim=1)
|
| 128 |
+
x6 = self.resblock3(x6, t_emb)
|
| 129 |
+
|
| 130 |
+
x7 = self.up2(x6)
|
| 131 |
+
x7 = torch.cat([x2, x7], dim=1)
|
| 132 |
+
x7 = self.resblock4(x7, t_emb)
|
| 133 |
+
|
| 134 |
+
return self.outc(x7)
|