MCPcopy Create free account
hub / github.com/ActiveVisionLab/DFNet / autoencoder_vgg4

Class autoencoder_vgg4

script/feature/model.py:157–212  ·  view source on GitHub ↗

vgg encoder with bilinear upsampling

Source from the content-addressed store, hash-verified

155 return feat_out, x
156
157class autoencoder_vgg4(nn.Module): # 35.54 PSNR 36.05 BCELoss (120x120)
158 ''' vgg encoder with bilinear upsampling'''
159 def __init__(self):
160 super(autoencoder_vgg4, self).__init__()
161 self.encoder = models.vgg19(pretrained=True).features
162 # receptive field not equal? so maybe it does not work very well
163 self.decoder = nn.Sequential(
164 # (b, 512, 14, 14)
165 # nn.UpsamplingBilinear2d(scale_factor=2), # upsample to feature map's size
166 nn.Conv2d(512, 512, 3, stride=1, padding=1),
167 nn.ReLU(True),
168 # (b, 256, 56, 56)
169 # nn.UpsamplingBilinear2d(scale_factor=4),
170 nn.Conv2d(512, 256, 3, stride=1, padding=1),
171 nn.ReLU(True),
172 # (b, 64, 224, 224)
173 # nn.UpsamplingBilinear2d(scale_factor=4),
174 nn.Conv2d(256, 64, 3, stride=1, padding=1),
175 nn.ReLU(True),
176 nn.Conv2d(64, 3, 3, stride=1, padding=1),
177 # nn.Tanh() # MSELoss
178 nn.Sigmoid() # BCELoss
179 )
180 def forward(self, x):
181 # pdb.set_trace()
182 feat = []
183 feat_out = []
184 for i in range(len(self.encoder)):
185 # print("layer {} encoder layer: {}".format(i, self.encoder[i]))
186 x = self.encoder[i](x)
187 if i == 35: # ReLU-36
188 feat.append(x)
189 elif i == 17: # ReLU-17
190 feat.append(x)
191 elif i == 3: # ReLU-4
192 feat.append(x)
193
194 for i in range(len(self.decoder)):
195 # print("layer {} decoder layer: {}".format(i, self.decoder[i]))
196 x = self.decoder[i](x)
197 if i == 1:
198 _, _, h, w = feat[2].shape
199 x = nn.UpsamplingBilinear2d(size=(h,w))(x)
200 x = x + feat[2]
201 feat_out.append(x)
202 elif i == 3:
203 _, _, h, w = feat[1].shape
204 x = nn.UpsamplingBilinear2d(size=(h,w))(x)
205 x = x + feat[1]
206 feat_out.append(x)
207 elif i == 5:
208 _, _, h, w = feat[0].shape
209 x = nn.UpsamplingBilinear2d(size=(h,w))(x)
210 x = x + feat[0]
211 feat_out.append(x)
212 return feat_out, x
213
214class autoencoder_vgg5(nn.Module): # 36.78 PSNR

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected