def forward(self, x): # Stage 1 x11 = F.relu(self.bn11(self.conv11(x))) x12 = F.relu(self.bn12(self.conv12(x11))) x1p, id1 = F.max_pool2d(x12,kernel_size=2, stride=1,return_indices=True) # Stage 2 x21 = F.relu(self.bn21(self.conv21(x1p))) x22 = F.relu(self.bn22(self.conv22(x21))) x2p, id2 = F.max_pool2d(x22,kernel_size=2, stride=1,return_indices=True) # Stage 3 x31 = F.relu(self.bn31(self.conv31(x2p))) x32 = F.relu(self.bn32(self.conv32(x31))) x33 = F.relu(self.bn33(self.conv33(x32))) x3p, id3 = F.max_pool2d(x33,kernel_size=2, stride=1,return_indices=True) # Stage 4 x41 = F.relu(self.bn41(self.conv41(x3p))) x42 = F.relu(self.bn42(self.conv42(x41))) x43 = F.relu(self.bn43(self.conv43(x42))) x4p, id4 = F.max_pool2d(x43,kernel_size=2, stride=1,return_indices=True) # Stage 5 x51 = F.relu(self.bn51(self.conv51(x4p))) x52 = F.relu(self.bn52(self.conv52(x51))) x53 = F.relu(self.bn53(self.conv53(x52))) x5p, id5 = F.max_pool2d(x53,kernel_size=2, stride=1,return_indices=True) # Stage 5d x5d = F.max_unpool2d(x5p, id5, kernel_size=2, stride=1) x53d = F.relu(self.bn53d(self.conv53d(x5d))) x52d = F.relu(self.bn52d(self.conv52d(x53d))) x51d = F.relu(self.bn51d(self.conv51d(x52d))) # Stage 4d x4d = F.max_unpool2d(x51d, id4, kernel_size=2, stride=1) x43d = F.relu(self.bn43d(self.conv43d(x4d))) x42d = F.relu(self.bn42d(self.conv42d(x43d))) x41d = F.relu(self.bn41d(self.conv41d(x42d))) # Stage 3d x3d = F.max_unpool2d(x41d, id3, kernel_size=2, stride=1) x33d = F.relu(self.bn33d(self.conv33d(x3d))) x32d = F.relu(self.bn32d(self.conv32d(x33d))) x31d = F.relu(self.bn31d(self.conv31d(x32d))) # Stage 2d x2d = F.max_unpool2d(x31d, id2, kernel_size=2, stride=1) x22d = F.relu(self.bn22d(self.conv22d(x2d))) x21d = F.relu(self.bn21d(self.conv21d(x22d))) # Stage 1d x1d = F.max_unpool2d(x21d, id1, kernel_size=2, stride=1) x12d = F.relu(self.bn12d(self.conv12d(x1d))) x11d = self.conv11d(x12d) x11d = self.sigmoid(x11d) return x11d
def forward(self, x, i1, i2, i3): x = self.dfc3(x) #x = F.relu(x) x = F.relu(self.bn3(x)) x = self.dfc2(x) x = F.relu(self.bn2(x)) #x = F.relu(x) x = self.dfc1(x) x = F.relu(self.bn1(x)) #x = F.relu(x) #print(x.size()) x = x.view(batch_size, 256, 6, 6) #print x x = self.dconv5( F.max_unpool2d(x, i3, kernel_size=(3, 3), stride=(2, 2))) x = F.relu(x) x = F.relu(self.dconv4(x)) x = F.relu(self.dconv3(x)) x = self.dconv2( F.max_unpool2d(x, i2, kernel_size=(3, 3), stride=(2, 2))) x = F.relu(x) x = self.dconv1(F.max_unpool2d(x, i1, kernel_size=3, stride=2)) #print x x = F.sigmoid(x) #print x return x
def forward(self, x): # encode dim0 = x.size() x = self.relu(self.conv1(x)) x1, i1 = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True) dim1 = x1.size() x1 = self.relu(self.conv2(x1)) x2, i2 = F.max_pool2d(x1, kernel_size=2, stride=2, return_indices=True) dim2 = x2.size() x2 = self.relu(self.conv3(x2)) x3, i3 = F.max_pool2d(x2, kernel_size=2, stride=2, return_indices=True) dim3 = x3.size() x3 = self.relu(self.conv4(x3)) x4, i4 = F.max_pool2d(x3, kernel_size=2, stride=2, return_indices=True) # decode x4 = F.max_unpool2d(x4, i4, kernel_size=2, stride=2, output_size=dim3) x3 = self.relu(self.dec4(x4)) x3 = F.max_unpool2d(x3, i3, kernel_size=2, stride=2, output_size=dim2) x2 = self.relu(self.dec3(x3)) x2 = F.max_unpool2d(x2, i2, kernel_size=2, stride=2, output_size=dim1) x1 = self.relu(self.dec2(x2)) x1 = self.relu(self.dec20(x1)) x1 = self.relu(self.dec21(x1)) x1 = self.relu(self.dec22(x1)) x1 = F.max_unpool2d(x1, i1, kernel_size=2, stride=2, output_size=dim0) x = self.sigm(self.dec1(x1)) return x
def forward(self, x): x = self.conv1(x) x, idx1 = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True) x = self.conv2(x) x, idx2 = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True) x = self.conv3(x) x, idx3 = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True) x = self.conv4(x) x, idx4 = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True) x = self.conv5(x) x = self.deconv5(x) x = F.max_unpool2d(x, idx4, kernel_size=2, stride=2) x = self.deconv4(x) x = F.max_unpool2d(x, idx3, kernel_size=2, stride=2) x = self.deconv3(x) x = F.max_unpool2d(x, idx2, kernel_size=2, stride=2) x = self.deconv2(x) x = F.max_unpool2d(x, idx1, kernel_size=2, stride=2) x = self.deconv1(x) x = self.deconv0(x) return x
def forward(self, x): # encode dim0 = x.size() x = self.relu(self.conv1(x)) x1, i1 = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True) dim1 = x1.size() x1 = self.relu(self.conv2(x1)) x2, i2 = F.max_pool2d(x1, kernel_size=2, stride=2, return_indices=True) dim2 = x2.size() x2 = self.relu(self.conv3(x2)) x3, i3 = F.max_pool2d(x2, kernel_size=2, stride=2, return_indices=True) dim3 = x3.size() x3 = self.relu(self.conv4(x3)) x4, i4 = F.max_pool2d(x3, kernel_size=2, stride=2, return_indices=True) # hidden with fixed size x_hid, i_hid = self.hid_repr(x4) # expand x_hid = F.max_unpool2d(x_hid, i_hid, kernel_size=2, stride=2) x3 = F.interpolate(x_hid, dim2[-2:]) # decode # x2 = F.max_unpool2d(x3, i2, kernel_size=2, stride=2, output_size=dim1) x2 = self.relu(self.dec2(x3)) x2 = self.relu(self.dec20(x2)) x2 = self.relu(self.dec21(x2)) x2 = self.relu(self.dec22(x2)) x1 = F.max_unpool2d(x1, i1, kernel_size=2, stride=2, output_size=dim0) x = self.sigm(self.dec1(x1)) return x
def forward(self, x, size_bf_pool5, global_pool_index, index_pool1): x = F.relu(x) x_re = self.fc_re(x) x_re = self.fc_rebn(x_re) x_re = F.relu(x_re) x_re = x_re.view(x.size(0), -1, 1, 1) x_re = F.max_unpool2d(x_re, global_pool_index, size_bf_pool5[2:], stride=size_bf_pool5[2:]) x_re = self.upconv5(x_re, output_size=(x_re.size(2) * 2, x_re.size(3) * 2)) x_re = self.upconv5_bn(x_re) x_re = F.relu(x_re) x_re = self.upconv4(x_re, output_size=(x_re.size(2) * 2, x_re.size(3) * 2)) x_re = self.upconv4_bn(x_re) x_re = F.relu(x_re) x_re = self.upconv3(x_re, output_size=(x_re.size(2) * 2, x_re.size(3) * 2)) x_re = self.upconv3_bn(x_re) x_re = F.relu(x_re) x_re = self.upconv2(x_re) x_re = self.upconv2_bn(x_re) x_re = F.relu(x_re) x_re = F.max_unpool2d(x_re, index_pool1, 3, 2, 1, output_size=(x_re.size(2) * 2, x_re.size(3) * 2)) # x_re = F.upsample(x_re, size=[x_re.size(2)-1, x_re.size(3)-1], mode='bilinear') x_re = self.upconv1(x_re, output_size=(x_re.size(2) * 2, x_re.size(3) * 2)) # x_re = self.upconv1_bn(x_re) # x_re = F.relu(x_re) return x_re
def forward(self, x): # encoder with fixed vgg parameters x = self.en_bloc1(x) x, id1 = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True) x = self.en_bloc2(x) x, id2 = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True) x = self.en_bloc3(x) x, id3 = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True) x = self.en_bloc4(x) x, id4 = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True) x = self.en_bloc5(x) x, id5 = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True) #decoder with learnable parameters x = F.max_unpool2d(x, id5, kernel_size=2, stride=2, output_size=id4.size()) x = self.de_bloc5(x) x = F.max_unpool2d(x, id4, kernel_size=2, stride=2, output_size=id3.size()) x = self.de_bloc4(x) x = F.max_unpool2d(x, id3, kernel_size=2, stride=2, output_size=id2.size()) x = self.de_bloc3(x) x = F.max_unpool2d(x, id2, kernel_size=2, stride=2, output_size=id1.size()) x = self.de_bloc2(x) x = F.max_unpool2d(x, id1, kernel_size=2, stride=2) x = self.de_bloc1(x) return x
def forward(self, input): x = self.e1(input) x, ind1 = F.max_pool2d(x, 2, 2, return_indices=True) x = self.e2(x) x, ind2 = F.max_pool2d(x, 2, 2, return_indices=True) x = self.e3(x) x, ind3 = F.max_pool2d(x, 2, 2, return_indices=True) x = self.e4(x) x, ind4 = F.max_pool2d(x, 2, 2, return_indices=True) x = self.e5(x) x, ind5 = F.max_pool2d(x, 2, 2, return_indices=True) x = self.e6(x) x = self.d6(x) x = F.max_unpool2d(x, ind5, 2, stride=2) x = self.d5(x) x = F.max_unpool2d(x, ind4, 2, stride=2) x = self.d4(x) x = F.max_unpool2d(x, ind3, 2, stride=2) x = self.d3(x) x = F.max_unpool2d(x, ind2, 2, stride=2) x = self.d2(x) x = F.max_unpool2d(x, ind1, 2, stride=2) x = self.d1(x) x = self.bottle(x) return x
def forward(self, images): ''' auto encoder ''' # encoder emb = F.relu(self.enc_cnn_1(images)) emb, indices1 = F.max_pool2d( emb, 2, return_indices=True) # return indices for unpooling emb = F.relu(self.enc_cnn_2(emb)) emb, indices2 = F.max_pool2d(emb, 2, return_indices=True) emb = emb.view([images.size(0), -1]) # unfolding emb = F.relu(self.enc_linear_1(emb)) emb = F.relu(self.enc_linear_2(emb)) # decoder out = F.relu(self.dec_linear_1(emb)) out = F.relu(self.dec_linear_2(out)) out = out.view([emb.shape[0], 20, 4, 4]) # folding out = F.max_unpool2d(out, indices2, 2) out = F.relu(self.dec_de_cnn_1(out)) out = F.max_unpool2d(out, indices1, 2) out = F.relu(self.dec_de_cnn_2(out)) return out, emb
def forward(self, x): size1 = x.size() x, idx1 = self.encoder1(x) size2 = x.size() x, idx2 = self.encoder2(x) size3 = x.size() x, idx3 = self.encoder3(x) size4 = x.size() x, idx4 = self.encoder4(x) x = F.max_unpool2d(x, idx4, kernel_size=2, stride=2, output_size=size4) x = self.decoder1(x) x = F.max_unpool2d(x, idx3, kernel_size=2, stride=2, output_size=size3) x = self.decoder2(x) x = F.max_unpool2d(x, idx2, kernel_size=2, stride=2, output_size=size2) x = self.decoder3(x) x = F.max_unpool2d(x, idx1, kernel_size=2, stride=2, output_size=size1) x = self.decoder4(x) return x
def forward(self, x, inds): x = F.max_unpool2d(x, inds[0], 16, 1) x = self.convs[0](x) for f, ind in zip(self.convs[1:], inds[1:]): x = F.max_unpool2d(x, ind, 2, 2) x = f(x) return x
def backward(ctx, grad_output): kernel_size, stride, padding, input_shape = ctx.hyperparam grad_output_lp, indices_full, indices_lp = ctx.saved_tensors # note here pytorch max pool layer set stride = kernel size if not specified if (stride != kernel_size) or (padding != [0, 0] and padding != (0, 0)): raise Exception("stride and padding are not fully supported yet!") grad_output_full = grad_output + grad_output_lp grad_input_full = F.max_unpool2d( grad_output_full, indices_full, kernel_size=kernel_size, stride=stride, padding=padding, output_size=input_shape) grad_input_lp = F.max_unpool2d( grad_output_lp, indices_lp, kernel_size=kernel_size, stride=stride, padding=padding, output_size=input_shape) grad_input_delta = grad_input_full - grad_input_lp grad_input_lp = None grad_grad_output_lp = None grad_kernel_size = None grad_stride = None grad_padding = None return grad_input_delta, grad_input_lp, grad_grad_output_lp, grad_kernel_size, grad_stride, grad_padding
def forward(self, x): x1 = self.bn1(F.relu(self.conv1(x))) x1 = self.bn11(F.relu(self.conv11(x1))) x1p, id1 = F.max_pool2d(x1,kernel_size=2, stride=2,return_indices=True) x2 = self.bn2(F.relu(self.conv2(x1p))) x2 = self.bn22(F.relu(self.conv22(x2))) x2p, id2 = F.max_pool2d(x2,kernel_size=2, stride=2,return_indices=True) x3 = self.bn3(F.relu(self.conv3(x2p))) x3p, id3 = F.max_pool2d(x3,kernel_size=2, stride=2,return_indices=True) x4 = self.bn4(F.relu(self.dropout1(self.conv4(x3p)))) # encode x4d = self.bn4d(F.relu(self.conv4d(x4))) x3d = F.max_unpool2d(x4d, id3, kernel_size=2, stride=2,output_size=x3.size()) x3d = self.bn3d(F.relu(self.conv3d(x3d))) x3d = self.bn3dd(F.relu(self.conv3dd(x3d))) x2d = F.max_unpool2d(x3d, id2, kernel_size=2, stride=2,output_size=x2.size()) x2d = self.bn2d(F.relu(self.conv2d(x2d))) x2d = self.bn2dd(F.relu(self.conv2dd(x2d))) x1d = F.max_unpool2d(x2d, id1, kernel_size=2, stride=2,output_size=x1.size()) x1d = F.tanh(self.conv1d(x1d)) return x1d
def upsample(d): e5 = self.enc5( F.max_unpool2d(d, m5, kernel_size=2, stride=2, output_size=x5.size())) e4 = self.enc4( F.max_unpool2d(e5, m4, kernel_size=2, stride=2, output_size=x4.size())) e3 = self.enc3( F.max_unpool2d(e4, m3, kernel_size=2, stride=2, output_size=x3.size())) e2 = self.enc2( F.max_unpool2d(e3, m2, kernel_size=2, stride=2, output_size=x2.size())) e1 = F.max_unpool2d(e2, m1, kernel_size=2, stride=2, output_size=x1.size()) return e1
def forward(self, x, ind1, ind2, ind3, ind4, ind5): # Stage 5d x = F.max_unpool2d(x, ind5, 2, 2) x = F.relu(self.bn53d(self.conv53d(x)), inplace=True) x = F.relu(self.bn52d(self.conv52d(x)), inplace=True) x = F.relu(self.bn51d(self.conv51d(x)), inplace=True) # Stage 4d x = F.max_unpool2d(x, ind4, 2, 2) x = F.relu(self.bn43d(self.conv43d(x)), inplace=True) x = F.relu(self.bn42d(self.conv42d(x)), inplace=True) x = F.relu(self.bn41d(self.conv41d(x)), inplace=True) # Stage 3d x = F.max_unpool2d(x, ind3, 2, 2) x = F.relu(self.bn33d(self.conv33d(x)), inplace=True) x = F.relu(self.bn32d(self.conv32d(x)), inplace=True) x = F.relu(self.bn31d(self.conv31d(x)), inplace=True) # Stage 2d x = F.max_unpool2d(x, ind2, 2, 2) x = F.relu(self.bn22d(self.conv22d(x)), inplace=True) x = F.relu(self.bn21d(self.conv21d(x)), inplace=True) # Stage 1d x = F.max_unpool2d(x, ind1, 2, 2) x = F.relu(self.bn12d(self.conv12d(x)), inplace=True) x = F.relu(self.bn11d(self.conv11d(x)), inplace=True) return self.conv_final(x)
def forward(self, x): x, indices1 = F.max_pool2d(self.conv1(x), 2, return_indices=True) x = F.relu(x) x, indices2 = F.max_pool2d(self.conv2_drop(self.conv2(x)), 2, return_indices=True) # x, indices2 = F.max_pool2d(self.conv2(x), 2, return_indices=True) x = F.relu(x) x = x.view(-1, 320) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) if self.supervised: return F.log_softmax(x) # Decoder x = self.dfc2(x) x = self.dfc1(x) x = x.view(-1, 20, 4, 4) x = F.relu( self.dconv2( F.max_unpool2d(x, kernel_size=2, indices=indices2, stride=2))) x = F.relu( self.dconv1( F.max_unpool2d(x, kernel_size=2, indices=indices1, stride=2))) return x
def forward(self, x): x, indices1 = F.max_pool2d(F.relu(self.encoder_conv1(x)), kernel_size=2, stride=2, return_indices=True) x, indices2 = F.max_pool2d(F.relu(self.encoder_conv2(x)), kernel_size=2, stride=2, return_indices=True) x, indices3 = F.max_pool2d(F.relu(self.encoder_conv3(x)), kernel_size=2, stride=2, return_indices=True) x = F.relu(self.encoder_conv4(x)) x = F.max_unpool2d(F.relu(self.decoder_deconv4(x)), indices=indices3, kernel_size=2, stride=2) x = F.max_unpool2d(F.relu(self.decoder_deconv3(x)), indices=indices2, kernel_size=2, stride=2) x = F.max_unpool2d(F.relu(self.decoder_deconv2(x)), indices=indices1, kernel_size=2, stride=2) x = F.relu(self.decoder_deconv1(x)) return x
def forward(self, x): dim_0 = x.size() x = self.encoder_0(x) x, i0 = self.max_pool(x) dim_1 = x.size() x = self.encoder_1(x) x, i1 = self.max_pool(x) dim_2 = x.size() x = self.encoder_2(x) x, i2 = self.max_pool(x) dim_3 = x.size() x = self.encoder_3(x) x, i3 = self.max_pool(x) dim_4 = x.size() x = self.encoder_4(x) x, i4 = self.max_pool(x) x = F.max_unpool2d(x, indices=i4, kernel_size=(2, 2), stride=(2, 2), output_size=dim_4) x = self.decoder_4(x) x = F.max_unpool2d(x, indices=i3, kernel_size=(2, 2), stride=(2, 2), output_size=dim_3) x = self.decoder_3(x) x = F.max_unpool2d(x, indices=i2, kernel_size=(2, 2), stride=(2, 2), output_size=dim_2) x = self.decoder_2(x) x = F.max_unpool2d(x, indices=i1, kernel_size=(2, 2), stride=(2, 2), output_size=dim_1) x = self.decoder_1(x) x = F.max_unpool2d(x, indices=i0, kernel_size=(2, 2), stride=(2, 2), output_size=dim_0) x = self.decoder_0(x) x = self.final_layer(x) return x
def forward(self, x): if self.return_indices: # encode x1, indices1 = self.backbone.res1(x) x2, indices2 = self.backbone.res2(x1) x3, indices3 = self.backbone.res3(x2) x4, indices4 = self.backbone.res4(x3) x5, indices5 = self.backbone.res5(x4) # decode dx5 = self.decode.res5(F.max_unpool2d(x5, indices5, 2, 2)) if self.use_shortcut: dx5 += x4 dx4 = self.decode.res4(F.max_unpool2d(dx5, indices4, 2, 2)) if self.use_shortcut: dx4 += x3 dx3 = self.decode.res3(F.max_unpool2d(dx4, indices3, 2, 2)) if self.use_shortcut: dx3 += x2 dx2 = self.decode.res2(F.max_unpool2d(dx3, indices2, 2, 2)) if self.use_shortcut: dx2 += x1 dx1 = self.decode.res1(F.max_unpool2d(dx2, indices1, 2, 2)) else: x1 = self.backbone.res1(x) x2 = self.backbone.res2(x1) x3 = self.backbone.res3(x2) x4 = self.backbone.res4(x3) x5 = self.backbone.res5(x4) # decode dx5 = self.decode.res5(F.interpolate(x5, scale_factor=2)) if self.use_shortcut: dx5 += x4 dx4 = self.decode.res4(F.interpolate(dx5, scale_factor=2)) if self.use_shortcut: dx4 += x3 dx3 = self.decode.res3(F.interpolate(dx4, scale_factor=2)) if self.use_shortcut: dx3 += x2 dx2 = self.decode.res2(F.interpolate(dx3, scale_factor=2)) if self.use_shortcut: dx2 += x1 dx1 = self.decode.res1(F.interpolate(dx2, scale_factor=2)) dx1 = dx1.permute(0, 2, 3, 1).contiguous().view(-1, self.num_classes) return dx1
def forward(self, x): # Encoder phase conv1 = self.conv1(x) conv1 = F.relu(self.conv1_bn(conv1)) conv1, ind1 = self.pool1(conv1) conv2 = self.conv2(conv1) conv2 = F.relu(self.conv2_bn(conv2)) conv2, ind2 = self.pool2(conv2) conv3 = self.conv3(conv2) conv3 = F.relu(self.conv3_bn(conv3)) conv3, ind3 = self.pool3(conv3) conv4 = self.conv4(conv3) conv4 = F.relu(self.conv4_bn(conv4)) conv4, ind4 = self.pool4(conv4) # Decoder phase decod1 = F.max_unpool2d(conv4, ind4, kernel_size=2, stride=2, output_size=conv3.size()) decod1 = self.decoder1(decod1) decod1 = F.relu(self.decoder1_bn(decod1)) decod2 = F.max_unpool2d(decod1, ind3, kernel_size=2, stride=2, output_size=conv2.size()) decod2 = self.decoder2(decod2) decod2 = F.relu(self.decoder2_bn(decod2)) decod3 = F.max_unpool2d(decod2, ind2, kernel_size=2, stride=2, output_size=conv1.size()) decod3 = self.decoder3(decod3) decod3 = F.relu(self.decoder3_bn(decod3)) decod4 = F.max_unpool2d(decod3, ind1, kernel_size=2, stride=2, output_size=x.size()) decod4 = self.decoder4(decod4) decod4 = F.relu(self.decoder4_bn(decod4)) output = decod4.view(-1, self.n_classes, self.input_width * self.input_height) output = self.softmax(output) return output
def forward(self, x): # maxpool = nn.MaxPool2d(2, 2, return_indices=True) # maxunpool = nn.MaxUnpool2d(2, 2) x = F.relu(self.bn11(self.conv11(x))) x = F.relu(self.bn12(self.conv12(x))) size1 = x.size() x, id1 = F.max_pool2d(x, 2, 2, return_indices=True) x = F.relu(self.bn21(self.conv21(x))) x = F.relu(self.bn22(self.conv22(x))) size2 = x.size() x, id2 = F.max_pool2d(x, 2, 2, return_indices=True) x = F.relu(self.bn31(self.conv31(x))) x = F.relu(self.bn32(self.conv32(x))) x = F.relu(self.bn33(self.conv33(x))) size3 = x.size() x, id3 = F.max_pool2d(x, 2, 2, return_indices=True) x = F.relu(self.bn41(self.conv41(x))) x = F.relu(self.bn42(self.conv42(x))) x = F.relu(self.bn43(self.conv43(x))) size4 = x.size() x, id4 = F.max_pool2d(x, 2, 2, return_indices=True) x = F.relu(self.bn51(self.conv51(x))) x = F.relu(self.bn52(self.conv52(x))) x = F.relu(self.bn53(self.conv53(x))) size5 = x.size() x, id5 = F.max_pool2d(x, 2, 2, return_indices=True) # Decoding x = F.max_unpool2d(x, id5, 2, 2, output_size=size5) x = F.relu(self.bn53d(self.conv53d(x))) x = F.relu(self.bn52d(self.conv52d(x))) x = F.relu(self.bn51d(self.conv51d(x))) x = F.max_unpool2d(x, id4, 2, 2, output_size=size4) x = F.relu(self.bn43d(self.conv43d(x))) x = F.relu(self.bn42d(self.conv42d(x))) x = F.relu(self.bn41d(self.conv41d(x))) x = F.max_unpool2d(x, id3, 2, 2, output_size=size3) x = F.relu(self.bn33d(self.conv33d(x))) x = F.relu(self.bn32d(self.conv32d(x))) x = F.relu(self.bn31d(self.conv31d(x))) x = F.max_unpool2d(x, id2, 2, 2, output_size=size2) x = F.relu(self.bn22d(self.conv22d(x))) x = F.relu(self.bn21d(self.conv21d(x))) x = F.max_unpool2d(x, id1, 2, 2, output_size=size1) x = F.relu(self.bn12d(self.conv12d(x))) x = self.conv11d(x) return x
def forward(self, x): _orig_shape = x.shape x = x.reshape(-1, self.channels, self.input_size, self.input_size) x, idx1 = F.max_pool2d(F.relu(self.upconv1(x)), 2, return_indices=True) x, idx2 = F.max_pool2d(F.relu(self.upconv2(x)), 2, return_indices=True) _nonflat_shape = x.shape x = x.view(-1, self.num_flat_features(x)) x = self.mlp(x).reshape(_nonflat_shape) x = F.relu(self.dnconv1(F.max_unpool2d(x, idx2, kernel_size=2))) x = torch.tanh(self.dnconv2(F.max_unpool2d(x, idx1, kernel_size=2))) return x.reshape(_orig_shape)
def getEmbedding(self, x): # Encoder L1 = inputImg.size() x1_0 = F.relu(self.econv1_0(x)) x1_1 = F.relu(self.econv1_1(x1_0)) x1, indices1 = F.max_pool2d(x1_1, kernel_size=3, stride=2, return_indices=True) L2 = x1.size() x2_0 = F.relu(self.econv2_0(x1)) x2_1 = F.relu(self.econv2_1(x2_0)) x2, indices2 = F.max_pool2d(x2_1, kernel_size=3, stride=2, return_indices=True) L3 = x2.size() x3_0 = F.relu(self.econv3_0(x2)) x3_1 = F.relu(self.econv3_1(x3_0)) x3, indices3 = F.max_pool2d(x3_1, kernel_size=3, stride=2, return_indices=True) # Decoder x3_0d = F.max_unpool2d(x3, indices3, kernel_size=3, stride=2, output_size=L3) x3_1d = F.relu(self.dconvtr1_0(x3_0d)) x3_2d = F.relu(self.dconvtr1_1(x3_1d)) x2_0d = F.max_unpool2d(x2, indices2, kernel_size=3, stride=2, output_size=L2) x2_1d = F.relu(self.dconvtr2_0(x2_0d)) x2_2d = F.relu(self.dconvtr2_1(x2_1d)) x1_0d = F.max_unpool2d(x1, indices1, kernel_size=3, stride=2, output_size=L1) x1_1d = F.relu(self.dconvtr1_0(x1_0d)) x1_2d = F.relu(self.dconvtr1_1(x1_1d)) return x1_2d
def forward(self, x): x = self.encoder1(x) size1 = x.size() x, idx1 = F.max_pool2d(x, kernel_size=(2, 2), stride=(2, 2), return_indices=True) x = self.encoder2(x) size2 = x.size() x, idx2 = F.max_pool2d(x, kernel_size=(2, 2), stride=(2, 2), return_indices=True) x = self.encoder3(x) size3 = x.size() x, idx3 = F.max_pool2d(x, kernel_size=(2, 2), stride=(2, 2), return_indices=True) # x = self.encoder4(x) # size4 = x.size() # x, idx4 = F.max_pool2d(x, kernel_size=(2, 2), stride=(2, 2), return_indices=True) # x = self.encoder5(x) # size5 = x.size() # x, idx5 = F.max_pool2d(x, kernel_size=(2, 2), stride=(2, 2), return_indices=True) # x = self.decoder5(F.max_unpool2d(x, idx5, kernel_size=(2, 2), stride=(2, 2), output_size = size5)) # x = self.decoder4(F.max_unpool2d(x, idx4, kernel_size=(2, 2), stride=(2, 2), output_size = size4)) x = self.decoder3( F.max_unpool2d(x, idx3, kernel_size=(2, 2), stride=(2, 2), output_size=size3)) x = self.decoder2( F.max_unpool2d(x, idx2, kernel_size=(2, 2), stride=(2, 2), output_size=size2)) x = self.decoder1( F.max_unpool2d(x, idx1, kernel_size=(2, 2), stride=(2, 2), output_size=size1)) x = F.softmax(x, dim=1) return x
def forward(self, x): x1_1 = F.relu(self.conv1_1(x)) x1_2 = F.relu(self.conv1_2(x1_1)) x1_p, id1 = F.max_pool2d(x1_2, kernel_size=2, stride=2, return_indices=True) x2_1 = F.relu(self.conv2_1(x1_p)) x2_2 = F.relu(self.conv2_2(x2_1)) x2_p, id2 = F.max_pool2d(x2_2, kernel_size=2, stride=2, return_indices=True) x3_1 = F.relu(self.conv3_1(x2_p)) x3_2 = F.relu(self.conv3_2(x3_1)) x3_3 = F.relu(self.conv3_3(x3_2)) x3_p, id3 = F.max_pool2d(x3_3, kernel_size=2, stride=2, return_indices=True) x4_1 = F.relu(self.conv4_1(x3_p)) x4_2 = F.relu(self.conv4_2(x4_1)) x4_3 = F.relu(self.conv4_3(x4_2)) xd1_1 = F.max_unpool2d(x4_3, id3, kernel_size=2, stride=2) x5_1 = F.relu(self.conv5_1(xd1_1)) x5_2 = F.relu(self.conv5_2(x5_1)) x5_3 = F.relu(self.conv5_3(x5_2)) xd2_1 = F.max_unpool2d(x5_3, id2, kernel_size=2, stride=2) x6_1 = F.relu(self.conv6_1(xd2_1)) x6_2 = F.relu(self.conv6_2(x6_1)) xd3_1 = F.max_unpool2d(x6_2, id1, kernel_size=2, stride=2) x7_1 = F.relu(self.conv7_1(xd3_1)) x7_2 = F.relu(self.conv7_2(x7_1)) x7_3 = F.relu(self.conv7_3(x7_2)) return x7_3
def forward(self, x): x, i1 = self.encoder1(x) x, i2 = self.encoder2(x) x, i3 = self.encoder3(x) x, i4 = self.encoder4(x) x, i5 = self.encoder5(x) x = self.decoder1(F.max_unpool2d(x, i5, kernel_size=2)) x = self.decoder2(F.max_unpool2d(x, i4, kernel_size=2)) x = self.decoder3(F.max_unpool2d(x, i3, kernel_size=2)) x = self.decoder4(F.max_unpool2d(x, i2, kernel_size=2)) x = self.decoder5(F.max_unpool2d(x, i1, kernel_size=2)) x = F.sigmoid(x) return x
def forward(self, x): x, id = self.encoder(x) x = F.max_unpool2d(x, id[4], kernel_size=2, stride=2) x = self.deco1(x) x = F.max_unpool2d(x, id[3], kernel_size=2, stride=2) x = self.deco2(x) x = F.max_unpool2d(x, id[2], kernel_size=2, stride=2) x = self.deco3(x) x = F.max_unpool2d(x, id[1], kernel_size=2, stride=2) x = self.deco4(x) x = F.max_unpool2d(x, id[0], kernel_size=2, stride=2) x = self.deco5(x) return x
def forward(self, x): inputsize = x.size() # Encoder x, indices = self.first_conv(x) x = self.encoder(x) # Decoder x = self.decoder(x) h_diff = ceil((x.size()[2] - indices.size()[2]) / 2) w_diff = ceil((x.size()[3] - indices.size()[3]) / 2) if indices.size()[2] % 2 == 1: x = x[:, :, h_diff:x.size()[2] - (h_diff - 1), w_diff:x.size()[3] - (w_diff - 1)] else: x = x[:, :, h_diff:x.size()[2] - h_diff, w_diff:x.size()[3] - w_diff] x = F.max_unpool2d(x, indices, kernel_size=2, stride=2) x = self.last_conv(x) if inputsize != x.size(): h_diff = (x.size()[2] - inputsize[2]) // 2 w_diff = (x.size()[3] - inputsize[3]) // 2 x = x[:, :, h_diff:x.size()[2] - h_diff, w_diff:x.size()[3] - w_diff] if h_diff % 2 != 0: x = x[:, :, :-1, :] if w_diff % 2 != 0: x = x[:, :, :, :-1] return x
def forward(self,x): ## Assumion X of size 240x320x3 pool1, indices1 = F.max_pool2d(self.conv1(x), 2, 2, return_indices=True) ## 120x160x64 pool2, indices2 = F.max_pool2d(self.conv2(pool1), 2,2,return_indices=True) ## 60x80x128 pool3, indices3 = F.max_pool2d(self.conv3(pool2), 2,2, return_indices=True) ## 30x40x256 pool4, indices4 = F.max_pool2d(self.conv4(pool3), 2, 2, return_indices=True) ## 15x20x512 pool5, indices5 = F.max_pool2d(self.conv5(pool4), 2, 2, return_indices=True) ## 7x10x512 dec1 = self.dec512(F.max_unpool2d(pool5,indices5,2, 2, output_size=pool4.size())) #15x20x512 dec2 = self.dec256(F.max_unpool2d(dec1, indices4,2,2)) ## 30x40x256 dec3 = self.dec128(F.max_unpool2d(dec2, indices3, 2, 2)) ## 60x80x128 dec4 = self.dec64(F.max_unpool2d(dec3, indices2, 2, 2)) ## 120x160x64 dec5 = self.final(F.max_unpool2d(dec4, indices1, 2, 2)) ## 240x320x1 return self.activation(dec5)
def forward(self, x): D = self.D h = [None] * int(D + 1) i = [None] * int(D / 2 - 1) s = [None] * int(D / 2 - 1) h[0] = F.relu(self.conv[0](x)) for k in range(int(D / 2) - 1): h[k + 1] = F.relu(self.bn[k](self.conv[k + 1](h[k]))) s[k] = h[k + 1].shape #needed for unpool layers h[k + 1], i[k] = F.max_pool2d(h[k + 1], (2, 2), return_indices=True) k = int(D / 2) - 1 h[k + 1] = F.relu(self.bn[k](self.conv[k + 1](h[k]))) k = int(D / 2) h[k + 1] = F.relu(self.bn[k](self.conv[k + 1](h[k]))) h[k + 1] = (h[k + 1] + h[k - 1]) / np.sqrt(2) for k in range(int(D / 2) + 1, D): l = D - k - 1 h[k] = F.max_unpool2d(h[k], i[l], (2, 2), output_size=s[l]) h[k + 1] = F.relu(self.bn[k](self.conv[k + 1](h[k]))) h[k + 1] = (h[k + 1] + h[l]) / np.sqrt(2) y = self.conv[D + 1](h[D]) + x return y
def forward(self, x, indices, size): if self.dropout is not None: x = self.dropout(x) unpooled = F.max_unpool2d(x, indices, 2, 2, 0, size) # print('unpooled ', torch.min(unpooled), torch.max(unpooled), torch.isnan(unpooled).any(), unpooled.size()) return self.decode(unpooled)