Exemple #1
0
    def __init__(self):
        super(ResNet50_mod_name, self).__init__()
        self.conv1 = nn.Conv2d(3,
                               64,
                               kernel_size=7,
                               stride=2,
                               padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3,
                                    stride=2,
                                    padding=1,
                                    return_indices=True)

        self.conv111 = nn.Conv2d(64, 64, kernel_size=1, stride=1, bias=False)
        self.bn111 = nn.BatchNorm2d(64)
        self.conv112 = nn.Conv2d(64,
                                 64,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn112 = nn.BatchNorm2d(64)
        self.conv113 = nn.Conv2d(64, 256, kernel_size=1, stride=1, bias=False)
        self.bn113 = nn.BatchNorm2d(256)
        # Downsample
        self.conv114 = nn.Conv2d(64, 256, kernel_size=1, stride=1, bias=False)
        self.bn114 = nn.BatchNorm2d(256)

        self.conv121 = nn.Conv2d(256, 64, kernel_size=1, stride=1, bias=False)
        self.bn121 = nn.BatchNorm2d(64)
        self.conv122 = nn.Conv2d(64,
                                 64,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn122 = nn.BatchNorm2d(64)
        self.conv123 = nn.Conv2d(64,
                                 256,
                                 kernel_size=1,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False)
        self.bn123 = nn.BatchNorm2d(256)

        self.conv131 = nn.Conv2d(256, 64, kernel_size=1, stride=1, bias=False)
        self.bn131 = nn.BatchNorm2d(64)
        self.conv132 = nn.Conv2d(64,
                                 64,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn132 = nn.BatchNorm2d(64)
        self.conv133 = nn.Conv2d(64,
                                 256,
                                 kernel_size=1,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False)
        self.bn133 = nn.BatchNorm2d(256)

        self.conv211 = nn.Conv2d(256, 128, kernel_size=1, stride=1, bias=False)
        self.bn211 = nn.BatchNorm2d(128)
        self.conv212 = nn.Conv2d(128,
                                 128,
                                 kernel_size=3,
                                 stride=2,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn212 = nn.BatchNorm2d(128)
        self.conv213 = nn.Conv2d(128, 512, kernel_size=1, stride=1, bias=False)
        self.bn213 = nn.BatchNorm2d(512)
        # Downsample
        self.conv214 = nn.Conv2d(256, 512, kernel_size=1, stride=2, bias=False)
        self.bn214 = nn.BatchNorm2d(512)

        self.conv221 = nn.Conv2d(512, 128, kernel_size=1, stride=1, bias=False)
        self.bn221 = nn.BatchNorm2d(128)
        self.conv222 = nn.Conv2d(128,
                                 128,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn222 = nn.BatchNorm2d(128)
        self.conv223 = nn.Conv2d(128, 512, kernel_size=1, stride=1, bias=False)
        self.bn223 = nn.BatchNorm2d(512)

        self.conv231 = nn.Conv2d(512, 128, kernel_size=1, stride=1, bias=False)
        self.bn231 = nn.BatchNorm2d(128)
        self.conv232 = nn.Conv2d(128,
                                 128,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn232 = nn.BatchNorm2d(128)
        self.conv233 = nn.Conv2d(128, 512, kernel_size=1, stride=1, bias=False)
        self.bn233 = nn.BatchNorm2d(512)

        self.conv241 = nn.Conv2d(512, 128, kernel_size=1, stride=1, bias=False)
        self.bn241 = nn.BatchNorm2d(128)
        self.conv242 = nn.Conv2d(128,
                                 128,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn242 = nn.BatchNorm2d(128)
        self.conv243 = nn.Conv2d(128, 512, kernel_size=1, stride=1, bias=False)
        self.bn243 = nn.BatchNorm2d(512)

        self.conv311 = nn.Conv2d(512, 256, kernel_size=1, stride=1, bias=False)
        self.bn311 = nn.BatchNorm2d(256)
        self.conv312 = nn.Conv2d(256,
                                 256,
                                 kernel_size=3,
                                 stride=2,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn312 = nn.BatchNorm2d(256)
        self.conv313 = nn.Conv2d(256,
                                 1024,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn313 = nn.BatchNorm2d(1024)
        # Downsample
        self.conv314 = nn.Conv2d(512,
                                 1024,
                                 kernel_size=1,
                                 stride=2,
                                 bias=False)
        self.bn314 = nn.BatchNorm2d(1024)

        self.conv321 = nn.Conv2d(1024,
                                 256,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn321 = nn.BatchNorm2d(256)
        self.conv322 = nn.Conv2d(256,
                                 256,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn322 = nn.BatchNorm2d(256)
        self.conv323 = nn.Conv2d(256,
                                 1024,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn323 = nn.BatchNorm2d(1024)

        self.conv331 = nn.Conv2d(1024,
                                 256,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn331 = nn.BatchNorm2d(256)
        self.conv332 = nn.Conv2d(256,
                                 256,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn332 = nn.BatchNorm2d(256)
        self.conv333 = nn.Conv2d(256,
                                 1024,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn333 = nn.BatchNorm2d(1024)

        self.conv341 = nn.Conv2d(1024,
                                 256,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn341 = nn.BatchNorm2d(256)
        self.conv342 = nn.Conv2d(256,
                                 256,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn342 = nn.BatchNorm2d(256)
        self.conv343 = nn.Conv2d(256,
                                 1024,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn343 = nn.BatchNorm2d(1024)

        self.conv351 = nn.Conv2d(1024,
                                 256,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn351 = nn.BatchNorm2d(256)
        self.conv352 = nn.Conv2d(256,
                                 256,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn352 = nn.BatchNorm2d(256)
        self.conv353 = nn.Conv2d(256,
                                 1024,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn353 = nn.BatchNorm2d(1024)

        self.conv361 = nn.Conv2d(1024,
                                 256,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn361 = nn.BatchNorm2d(256)
        self.conv362 = nn.Conv2d(256,
                                 256,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn362 = nn.BatchNorm2d(256)
        self.conv363 = nn.Conv2d(256,
                                 1024,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn363 = nn.BatchNorm2d(1024)

        self.conv411 = nn.Conv2d(1024,
                                 512,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn411 = nn.BatchNorm2d(512)
        self.conv412 = nn.Conv2d(512,
                                 512,
                                 kernel_size=3,
                                 stride=2,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn412 = nn.BatchNorm2d(512)
        self.conv413 = nn.Conv2d(512,
                                 2048,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn413 = nn.BatchNorm2d(2048)
        # Downsample
        self.conv414 = nn.Conv2d(1024,
                                 2048,
                                 kernel_size=1,
                                 stride=2,
                                 bias=False)
        self.bn414 = nn.BatchNorm2d(2048)

        self.conv421 = nn.Conv2d(2048,
                                 512,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn421 = nn.BatchNorm2d(512)
        self.conv422 = nn.Conv2d(512,
                                 512,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn422 = nn.BatchNorm2d(512)
        self.conv423 = nn.Conv2d(512,
                                 2048,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn423 = nn.BatchNorm2d(2048)

        self.conv431 = nn.Conv2d(2048,
                                 512,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn431 = nn.BatchNorm2d(512)
        self.conv432 = nn.Conv2d(512,
                                 512,
                                 kernel_size=3,
                                 stride=1,
                                 groups=1,
                                 dilation=1,
                                 bias=False,
                                 padding=1)
        self.bn432 = nn.BatchNorm2d(512)
        self.conv433 = nn.Conv2d(512,
                                 2048,
                                 kernel_size=1,
                                 stride=1,
                                 bias=False)
        self.bn433 = nn.BatchNorm2d(2048)

        self.unpool = nn.MaxUnpool2d(kernel_size=3, stride=2, padding=1)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.unsample = nn.Upsample(size=7, mode='nearest')
Exemple #2
0
    def __init__(self):
        super(SegNet, self).__init__()
        # initialise network parameters
        filter = [64, 128, 256, 512, 512]
        self.class_nb = 13

        # define encoder decoder layers
        self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])])
        self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])])
        for i in range(4):
            self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]]))
            self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]]))

        # define convolution layer
        self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])])
        self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])])
        for i in range(4):
            if i == 0:
                self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]]))
                self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]]))
            else:
                self.conv_block_enc.append(nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]),
                                                         self.conv_layer([filter[i + 1], filter[i + 1]])))
                self.conv_block_dec.append(nn.Sequential(self.conv_layer([filter[i], filter[i]]),
                                                         self.conv_layer([filter[i], filter[i]])))

        # define task attention layers
        self.encoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])])
        self.decoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])])
        self.encoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[1]])])
        self.decoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[0]])])

        for j in range(3):
            if j < 2:
                self.encoder_att.append(nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])]))
                self.decoder_att.append(nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])]))
            for i in range(4):
                self.encoder_att[j].append(self.att_layer([2 * filter[i + 1], filter[i + 1], filter[i + 1]]))
                self.decoder_att[j].append(self.att_layer([filter[i + 1] + filter[i], filter[i], filter[i]]))

        for i in range(4):
            if i < 3:
                self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 2]]))
                self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i]]))
            else:
                self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]]))
                self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]]))

        self.pred_task1 = self.conv_layer([filter[0], self.class_nb], pred=True)
        self.pred_task2 = self.conv_layer([filter[0], 1], pred=True)
        self.pred_task3 = self.conv_layer([filter[0], 3], pred=True)

        # define pooling and unpooling functions
        self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True)
        self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2)

        self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5, -0.5]))

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.xavier_normal_(m.weight)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.xavier_normal_(m.weight)
                nn.init.constant_(m.bias, 0)
Exemple #3
0
 def __init__(self, in_size, out_size):
     super(segnetUp3, self).__init__()
     self.unpool = nn.MaxUnpool2d(2, 2)
     self.conv1 = conv2DBatchNormRelu(in_size, in_size, 3, 1, 1)
     self.conv2 = conv2DBatchNormRelu(in_size, in_size, 3, 1, 1)
     self.conv3 = conv2DBatchNormRelu(in_size, out_size, 3, 1, 1)
Exemple #4
0
    def __init__(self):
        super(FCN, self).__init__()

        self.conv1_1 = nn.Conv2d(3,
                                 64,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.conv1_2 = nn.Conv2d(64,
                                 64,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.max1 = nn.MaxPool2d(kernel_size=(2, 2),
                                 stride=2,
                                 return_indices=True)

        self.conv2_1 = nn.Conv2d(64,
                                 128,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.conv2_2 = nn.Conv2d(128,
                                 128,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.max2 = nn.MaxPool2d(kernel_size=(2, 2),
                                 stride=2,
                                 return_indices=True)

        self.conv3_1 = nn.Conv2d(128,
                                 256,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.conv3_2 = nn.Conv2d(256,
                                 256,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.conv3_3 = nn.Conv2d(256,
                                 256,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.max3 = nn.MaxPool2d(kernel_size=(2, 2),
                                 stride=2,
                                 return_indices=True)

        self.conv4_1 = nn.Conv2d(256,
                                 512,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.conv4_2 = nn.Conv2d(512,
                                 512,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.conv4_3 = nn.Conv2d(512,
                                 512,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.max4 = nn.MaxPool2d(kernel_size=(2, 2),
                                 stride=2,
                                 return_indices=True)

        self.conv5_1 = nn.Conv2d(512,
                                 512,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.conv5_2 = nn.Conv2d(512,
                                 512,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.conv5_3 = nn.Conv2d(512,
                                 512,
                                 kernel_size=(3, 3),
                                 stride=1,
                                 padding=1)
        self.max5 = nn.MaxPool2d(kernel_size=(2, 2),
                                 stride=2,
                                 return_indices=True)

        self.deconv1_1 = nn.ConvTranspose2d(512,
                                            512,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.deconv1_2 = nn.ConvTranspose2d(512,
                                            512,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.deconv1_3 = nn.ConvTranspose2d(512,
                                            512,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.unpool1 = nn.MaxUnpool2d((2, 2), stride=2)

        self.deconv2_1 = nn.ConvTranspose2d(512,
                                            512,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.deconv2_2 = nn.ConvTranspose2d(512,
                                            512,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.deconv2_3 = nn.ConvTranspose2d(512,
                                            512,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.unpool2 = nn.MaxUnpool2d(2, stride=2)

        self.deconv3_1 = nn.ConvTranspose2d(512,
                                            256,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.deconv3_2 = nn.ConvTranspose2d(256,
                                            256,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.deconv3_3 = nn.ConvTranspose2d(256,
                                            256,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.unpool3 = nn.MaxUnpool2d(2, stride=2)

        self.deconv4_1 = nn.ConvTranspose2d(256,
                                            128,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.deconv4_2 = nn.ConvTranspose2d(128,
                                            128,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.unpool4 = nn.MaxUnpool2d(2, stride=2)

        self.deconv5_1 = nn.ConvTranspose2d(128,
                                            64,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.deconv5_2 = nn.ConvTranspose2d(64,
                                            64,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
        self.unpool5 = nn.MaxUnpool2d(2, stride=2)

        self.deconv6_1 = nn.ConvTranspose2d(64,
                                            2,
                                            kernel_size=(3, 3),
                                            stride=1,
                                            padding=1)
 def __init__(self, in_size, out_size):
     super(segnetUp1, self).__init__()
     self.unpool = nn.MaxUnpool2d(2, 2)
     self.conv = conv2DBatchNormRelu(in_size, out_size, k_size=5, stride=1, padding=2, with_relu=False)
Exemple #6
0
 def __init__(self, params, se_block_type=None):
     super(DecoderBlock, self).__init__(params, se_block_type=se_block_type)
     self.unpool = nn.MaxUnpool2d(kernel_size=params['pool'],
                                  stride=params['stride_pool'])
    img_tensor = torch.ones((1, 1, 4, 4))
    avgpool_layer = nn.AvgPool2d((2, 2), stride=(2, 2), divisor_override=3)
    img_pool = avgpool_layer(img_tensor)

    print("raw_img:\n{0}\npooling_img:\n{1}".format(img_tensor, img_pool))

# maxunpool 最大上采样
# maxpool中的return_indices以tensor形式返回所有最大值的下标
flag = 0
if flag:
    img_tensor = torch.randint(high=5, size=(1, 1, 4, 4), dtype=torch.float)
    maxpool_layer = nn.MaxPool2d((2, 2), stride=(2, 2), return_indices=True)
    img_pool, indices = maxpool_layer(img_tensor)

    img_reconstruct = torch.randn_like(img_pool, dtype=torch.float)
    maxunpool_layer = nn.MaxUnpool2d((2, 2), stride=(2, 2))
    img_unpool = maxunpool_layer(img_reconstruct, indices)

    print("raw_img:\n{0}\nimg_pool:\n{1}".format(img_tensor, img_pool))
    print("img_reconstruct:\n{0}\nimg_unpool:\n{1}".format(
        img_reconstruct, img_unpool))

# 线性变换 linear
flag = 0
if flag:
    inputs = torch.tensor([[1., 2, 3]])
    linear_layer = nn.Linear(3, 4)
    linear_layer.weight.data = torch.tensor([[1., 1., 1.], [2., 2., 2.],
                                             [3., 3., 3.], [4., 4., 4.]])

    linear_layer.bias.data.fill_(0.5)
Exemple #8
0
    def __init__(self, num_classes):
        super(DeconvNet,self).__init__()
        
        layers = make_layers()

        self.features_map1 = layers[0]
        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True, return_indices=True) #

        self.features_map2 = layers[1]
        self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True, return_indices=True) #

        self.features_map3 = layers[2]
        self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True, return_indices=True) #
        
        self.features_map4 = layers[3]
        self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True, return_indices=True) #

        self.features_map5 = layers[4]
        self.pool5 = nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True, return_indices=True) #
         
        
        # fc6 ~ fc7
        self.fc = nn.Sequential(nn.Conv2d(512, 4096, kernel_size = 7),
                                  nn.ReLU(inplace=True),
                                  nn.Dropout(),
                                  nn.Conv2d(4096, 4096, kernel_size = 1),
                                  nn.ReLU(inplace=True),
                                  nn.Dropout()
                                )
        
        # Deconv
        self.fc_deconv = DCB(4096, 512, 7, 1,0)
        self.unpool5 = nn.MaxUnpool2d(2, stride=2)
        self.deconv5 =  nn.Sequential(
                                        DCB(512, 512, 3, 1, 1),
                                        DCB(512, 512, 3, 1, 1),
                                        DCB(512, 512, 3, 1, 1)
                                    )
        self.unpool4 = nn.MaxUnpool2d(2, stride=2)
        self.deconv4 =  nn.Sequential(
                                DCB(512, 512, 3, 1, 1),
                                DCB(512, 512, 3, 1, 1),
                                DCB(512, 256, 3, 1, 1)
                            )
        
        self.unpool3 = nn.MaxUnpool2d(2, stride=2)
        self.deconv3 =  nn.Sequential(
                                DCB(256, 256, 3, 1, 1),
                                DCB(256, 256, 3, 1, 1),
                                DCB(256, 128, 3, 1, 1)
                            )
        
        self.unpool2 = nn.MaxUnpool2d(2, stride=2)
        self.deconv2 =  nn.Sequential(
                                DCB(128, 128, 3, 1, 1),
                                DCB(128, 64, 3, 1, 1)
                            )

        self.unpool1 = nn.MaxUnpool2d(2, stride=2)
        self.deconv1 =  nn.Sequential(
                                DCB(64, 64, 3, 1, 1),
                                DCB(64, 64, 3, 1, 1)
                            )

        self.score = nn.Conv2d(64,
                                    num_classes,
                                    kernel_size=1,
                                    stride=1,
                                    padding=0,
                                    dilation=1)
Exemple #9
0
    def __init__(self):
        super(encoder_decoder, self).__init__()

        #Defining the Encoder Network
        self.conv11 = nn.Conv2d(in_channels=3,
                                out_channels=64,
                                kernel_size=3,
                                stride=1,
                                padding=1)
        self.conv12 = nn.Conv2d(in_channels=64,
                                out_channels=64,
                                kernel_size=3,
                                stride=1,
                                padding=1)
        self.bn1 = nn.BatchNorm2d(64)
        self.conv21 = nn.Conv2d(in_channels=64,
                                out_channels=128,
                                kernel_size=3,
                                stride=1,
                                padding=1)
        self.conv22 = nn.Conv2d(in_channels=128,
                                out_channels=128,
                                kernel_size=3,
                                stride=1,
                                padding=1)
        self.bn2 = nn.BatchNorm2d(128)
        self.conv31 = nn.Conv2d(in_channels=128,
                                out_channels=256,
                                kernel_size=3,
                                stride=1,
                                padding=1)
        self.conv32 = nn.Conv2d(in_channels=256,
                                out_channels=256,
                                kernel_size=3,
                                stride=1,
                                padding=1)
        self.bn3 = nn.BatchNorm2d(256)
        self.conv41 = nn.Conv2d(in_channels=256,
                                out_channels=512,
                                kernel_size=3,
                                stride=1,
                                padding=1)
        self.conv42 = nn.Conv2d(in_channels=512,
                                out_channels=512,
                                kernel_size=3,
                                stride=1,
                                padding=1)
        self.bn4 = nn.BatchNorm2d(512)

        #Defining Relu and Maxpool Layer
        self.prelu = nn.PReLU()
        self.maxpool = nn.MaxPool2d(kernel_size=2,
                                    stride=2,
                                    return_indices=True)

        #Defining Decoder Network
        self.deconv41 = nn.ConvTranspose2d(in_channels=512,
                                           out_channels=512,
                                           kernel_size=3,
                                           stride=1,
                                           padding=1)
        self.deconv31 = nn.ConvTranspose2d(in_channels=512,
                                           out_channels=256,
                                           kernel_size=3,
                                           stride=1,
                                           padding=1)
        self.deconv32 = nn.ConvTranspose2d(in_channels=256,
                                           out_channels=256,
                                           kernel_size=3,
                                           stride=1,
                                           padding=1)
        self.deconv21 = nn.ConvTranspose2d(in_channels=256,
                                           out_channels=128,
                                           kernel_size=3,
                                           stride=1,
                                           padding=1)
        self.deconv22 = nn.ConvTranspose2d(in_channels=128,
                                           out_channels=128,
                                           kernel_size=3,
                                           stride=1,
                                           padding=1)
        self.deconv11 = nn.ConvTranspose2d(in_channels=128,
                                           out_channels=64,
                                           kernel_size=3,
                                           stride=1,
                                           padding=1)
        self.deconv12 = nn.ConvTranspose2d(in_channels=64,
                                           out_channels=64,
                                           kernel_size=3,
                                           stride=1,
                                           padding=1)
        self.deconv13 = nn.ConvTranspose2d(in_channels=64,
                                           out_channels=11,
                                           kernel_size=3,
                                           stride=1,
                                           padding=1)
        self.bn5 = nn.BatchNorm2d(11)
        #Defining UnMaxpool Layer
        self.maxunpool = nn.MaxUnpool2d(kernel_size=2, stride=2)

        #Defining Last Pooling Layers
        self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.avgpool = nn.AvgPool2d(kernel_size=2, stride=2)

        #Inittializing the module
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal(m.weight, mode='fan_out')
                if m.bias is not None:
                    nn.init.constant(m.bias, 0)
            elif isinstance(m, nn.ConvTranspose2d):
                nn.init.kaiming_normal(m.weight, mode='fan_out')
                if m.bias is not None:
                    nn.init.constant(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant(m.weight, 1)
                nn.init.constant(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal(m.weight, 0, 0.01)
                nn.init.constant(m.bias, 0)
Exemple #10
0
    def __init__(self,
                 state_size=((imgh, imgw), img_in_channels),
                 latent_size=latent_size,
                 conv_channels=conv_channels,
                 kernel_size=kernel_size):
        super(VAE, self).__init__()

        self.state_size, self.channels_in = state_size

        self.conv1 = nn.Conv2d(self.channels_in,
                               conv_channels,
                               kernel_size=kernel_size,
                               stride=1,
                               padding=0)

        self.bn1 = nn.BatchNorm2d(num_features=conv_channels)

        self.pool1 = nn.MaxPool2d((2, 2), return_indices=True)
        self.conv2 = nn.Conv2d(conv_channels,
                               conv_channels * 2,
                               kernel_size=kernel_size,
                               stride=1,
                               padding=1)
        self.bn2 = nn.BatchNorm2d(num_features=conv_channels * 2)
        self.pool2 = nn.MaxPool2d((2, 2), return_indices=True)
        self.conv3 = nn.Conv2d(conv_channels * 2,
                               conv_channels * 2,
                               kernel_size=kernel_size,
                               stride=1,
                               padding=1)
        self.bn3 = nn.BatchNorm2d(num_features=conv_channels * 2)
        self.pool3 = nn.MaxPool2d((2, 2), return_indices=True)

        self.flat_size = (-1,
                          conv_channels * 2 * ceil(self.state_size[0] / 8) *
                          ceil(self.state_size[1] / 8))

        self.fc21 = nn.Linear(
            conv_channels * 2 * ceil(self.state_size[0] / 8) *
            ceil(self.state_size[1] / 8), latent_size)
        self.fc22 = nn.Linear(
            conv_channels * 2 * ceil(self.state_size[0] / 8) *
            ceil(self.state_size[1] / 8), latent_size)

        self.fc3 = nn.Linear(
            latent_size, conv_channels * 2 * ceil(self.state_size[0] / 8) *
            ceil(self.state_size[1] / 8))

        self.unflatt_size = (-1, conv_channels * 2,
                             ceil(self.state_size[0] / 8),
                             ceil(self.state_size[1] / 8))

        self.unpool1 = nn.MaxUnpool2d(kernel_size)
        self.uncov1 = nn.Conv2d(conv_channels * 2,
                                conv_channels * 2,
                                kernel_size=kernel_size,
                                padding=0)
        self.upsample1 = nn.Upsample(scale_factor=2, mode='nearest')

        self.unpool2 = nn.MaxUnpool2d(kernel_size)
        self.uncov2 = nn.Conv2d(conv_channels * 2,
                                conv_channels,
                                kernel_size=kernel_size,
                                padding=1)
        self.upsample2 = nn.Upsample(scale_factor=2, mode='nearest')

        self.unpool3 = nn.MaxUnpool2d(kernel_size)
        self.uncov3 = nn.Conv2d(conv_channels,
                                self.channels_in,
                                kernel_size=kernel_size,
                                padding=1)
        self.upsample3 = nn.Upsample(scale_factor=2, mode='nearest')
Exemple #11
0
    def __init__(self, in_channels, outsize):
        super(ResblocksDeconv, self).__init__()
#         conv 1_1:
        self.conv1_1 = nn.Conv2d(in_channels, 64, kernel_size=3, stride=1, padding=1)
        self.bn1_1 = nn.BatchNorm2d(64)
#         conv 1_2
        self.conv1_2 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)
        self.bn1_2 = nn.BatchNorm2d(64)
#         pool 1
        self.maxp1 = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True)
#         120 x 120
#         conv2_1
        self.conv2_1 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
        self.bn2_1 = nn.BatchNorm2d(128)
#         conv2_2
        self.conv2_2 = nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1)
        self.bn2_2 = nn.BatchNorm2d(128)
#         pool2
        self.maxp2 = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True)

#         60 x 60
#         conv3_1
        self.conv3_1 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
        self.bn3_1 = nn.BatchNorm2d(256)
#         conv3_2
        self.conv3_2 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1)
        self.bn3_2 = nn.BatchNorm2d(256)
#         conv3_3
        self.conv3_3 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1)
        self.bn3_3 = nn.BatchNorm2d(256)
#         pool3
        self.maxp3 = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True)   
    
    
#         30 x 30
#         conv4_1
        self.conv4_1 = nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1)
        self.bn4_1 = nn.BatchNorm2d(512)
#         conv4_2
        self.conv4_2 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
        self.bn4_2 = nn.BatchNorm2d(512)
#         conv4_3
        self.conv4_3 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
        self.bn4_3 = nn.BatchNorm2d(512)
#         pool4
        self.maxp4 = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True)   
    
#         15 x 15 
#         conv5_1
        self.conv5_1 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
        self.bn5_1 = nn.BatchNorm2d(512)
#         conv5_2
        self.conv5_2 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
        self.bn5_2 = nn.BatchNorm2d(512)
#         conv5_3
        self.conv5_3 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
        self.bn5_3 = nn.BatchNorm2d(512)
#         pool5
        self.maxp5 = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True)           
     
#         7 x 7 
        self.residualBlock_1 = ResidualBlock(512, 512)
        self.residualBlock_2 = ResidualBlock(512, 512)
        self.residualBlock_3 = ResidualBlock(512, 512)
        self.residualBlock_4 = ResidualBlock(512, 512)
        self.residualBlock_5 = ResidualBlock(512, 512)
        
        
#         7 x 7 
#         unpool5
        self.unpool5 = nn.MaxUnpool2d(2,2) 

#         _ x _
#         deconv5_1
        self.deconv5_1 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
        self.debn5_1 = nn.BatchNorm2d(512)
#         conv3_2
        self.deconv5_2 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
        self.debn5_2 = nn.BatchNorm2d(512)
#         conv3_3
        self.deconv5_3 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
        self.debn5_3 = nn.BatchNorm2d(512)
        
        
#         _ x _ 
#         unpool4
        self.unpool4 = nn.MaxUnpool2d(2,2) #paper did unpoolsize 28 (we want 30 x 30)
#         _ x _
#         deconv4_1
        self.deconv4_1 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
        self.debn4_1 = nn.BatchNorm2d(512)
#         deconv4_2
        self.deconv4_2 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
        self.debn4_2 = nn.BatchNorm2d(512)
#         conv4_3
        self.deconv4_3 = nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1)
        self.debn4_3 = nn.BatchNorm2d(256)        
        
#         _ x _ 
#         unpool3
        self.unpool3 = nn.MaxUnpool2d(2,2) #paper did unpoolsize 56 (we want 60 x 60)
#         _ x _
#         deconv3_1
        self.deconv3_1 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1)
        self.debn3_1 = nn.BatchNorm2d(256)
#         deconv3_2
        self.deconv3_2 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1)
        self.debn3_2 = nn.BatchNorm2d(256)
#         deconv3_3
        self.deconv3_3 = nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1)
        self.debn3_3 = nn.BatchNorm2d(128)
        
        
#         _ x _ 
#         unpool2
        self.unpool2 = nn.MaxUnpool2d(2,2) #paper did unpoolsize 112 (we want 120 x 120)
#         _ x _
#         deconv3_1
        self.deconv2_1 = nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1)
        self.debn2_1 = nn.BatchNorm2d(128)
#         deconv3_2
        self.deconv2_2 = nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1)
        self.debn2_2 = nn.BatchNorm2d(64)
        
        
#         _ x _ 
#         unpool1
        self.unpool1 = nn.MaxUnpool2d(2,2) #paper did unpoolsize 224 (we want 240 x 240)
#         _ x _
#         deconv3_1
        self.deconv1_1 = nn.Conv2d(64, 32, kernel_size=3, stride=1, padding=1)
        self.debn1_1 = nn.BatchNorm2d(32)
#         deconv3_2
        self.deconv1_2 = nn.Conv2d(32, 3, kernel_size=3, stride=1, padding=1)
#         self.debn1_2 = nn.BatchNorm2d(3)
        

        self.in_channels = in_channels
        self.outsize = outsize
Exemple #12
0
 def __append_layer(self, net_style, args_dict):
     args_values_list = list(args_dict.values())
     if net_style == "Conv2d":
         self.layers.append(
             nn.Conv2d(args_values_list[0], args_values_list[1],
                       args_values_list[2], args_values_list[3],
                       args_values_list[4], args_values_list[5],
                       args_values_list[6], args_values_list[7]))
     elif net_style == "MaxPool2d":
         self.layers.append(
             nn.MaxPool2d(args_values_list[0], args_values_list[1],
                          args_values_list[2], args_values_list[3],
                          args_values_list[4], args_values_list[5]))
     elif net_style == "Linear":
         self.layers.append(
             nn.Linear(args_values_list[0], args_values_list[1],
                       args_values_list[2]))
     elif net_style == "reshape":
         # 如果是特殊情况 reshape,就直接将目标向量尺寸传入
         # print(type(args_values_list[0]))
         self.layers.append(args_values_list[0])
     elif net_style == "Conv1d":
         self.layers.append(
             nn.Conv1d(args_values_list[0], args_values_list[1],
                       args_values_list[2], args_values_list[3],
                       args_values_list[4], args_values_list[5],
                       args_values_list[6], args_values_list[7]))
     elif net_style == "Conv3d":
         self.layers.append(
             nn.Conv3d(args_values_list[0], args_values_list[1],
                       args_values_list[2], args_values_list[3],
                       args_values_list[4], args_values_list[5],
                       args_values_list[6], args_values_list[7]))
     elif net_style == "ConvTranspose1d":
         self.layers.append(
             nn.ConvTranspose1d(args_values_list[0], args_values_list[1],
                                args_values_list[2], args_values_list[3],
                                args_values_list[4], args_values_list[5],
                                args_values_list[6], args_values_list[7],
                                args_values_list[8]))
     elif net_style == "ConvTranspose2d":
         self.layers.append(
             nn.ConvTranspose2d(args_values_list[0], args_values_list[1],
                                args_values_list[2], args_values_list[3],
                                args_values_list[4], args_values_list[5],
                                args_values_list[6], args_values_list[7],
                                args_values_list[8]))
     elif net_style == "ConvTranspose3d":
         self.layers.append(
             nn.ConvTranspose3d(args_values_list[0], args_values_list[1],
                                args_values_list[2], args_values_list[3],
                                args_values_list[4], args_values_list[5],
                                args_values_list[6], args_values_list[7],
                                args_values_list[8]))
     elif net_style == "Unfold":
         self.layers.append(
             nn.Unfold(args_values_list[0], args_values_list[1],
                       args_values_list[2], args_values_list[3]))
     elif net_style == "Fold":
         self.layers.append(
             nn.Unfold(args_values_list[0], args_values_list[1],
                       args_values_list[2], args_values_list[3],
                       args_values_list[4]))
     elif net_style == "MaxPool1d":
         self.layers.append(
             nn.MaxPool1d(args_values_list[0], args_values_list[1],
                          args_values_list[2], args_values_list[3],
                          args_values_list[4], args_values_list[5]))
     elif net_style == "MaxPool3d":
         self.layers.append(
             nn.MaxPool3d(args_values_list[0], args_values_list[1],
                          args_values_list[2], args_values_list[3],
                          args_values_list[4], args_values_list[5]))
     elif net_style == "MaxUnpool1d":
         self.layers.append(
             nn.MaxUnpool1d(args_values_list[0], args_values_list[1],
                            args_values_list[2]))
     elif net_style == "MaxUnpool2d":
         self.layers.append(
             nn.MaxUnpool2d(args_values_list[0], args_values_list[1],
                            args_values_list[2]))
     elif net_style == "MaxUnpool3d":
         self.layers.append(
             nn.MaxUnpool3d(args_values_list[0], args_values_list[1],
                            args_values_list[2]))
     elif net_style == "AvgPool1d":
         self.layers.append(
             nn.AvgPool1d(args_values_list[0], args_values_list[1],
                          args_values_list[2], args_values_list[3],
                          args_values_list[4]))
     elif net_style == "AvgPool2d":
         self.layers.append(
             nn.AvgPool2d(args_values_list[0], args_values_list[1],
                          args_values_list[2], args_values_list[3],
                          args_values_list[4]))
     elif net_style == "AvgPool3d":
         self.layers.append(
             nn.AvgPool3d(args_values_list[0], args_values_list[1],
                          args_values_list[2], args_values_list[3],
                          args_values_list[4]))
     elif net_style == "FractionalMaxPool2d":
         self.layers.append(
             nn.FractionalMaxPool2d(args_values_list[0],
                                    args_values_list[1],
                                    args_values_list[2],
                                    args_values_list[3],
                                    args_values_list[4]))
     elif net_style == "LPPool1d":
         self.layers.append(
             nn.LPPool1d(args_values_list[0], args_values_list[1],
                         args_values_list[2], args_values_list[3]))
     elif net_style == "LPPool2d":
         self.layers.append(
             nn.LPPool2d(args_values_list[0], args_values_list[1],
                         args_values_list[2], args_values_list[3]))
     elif net_style == "AdaptiveMaxPool1d":
         self.layers.append(
             nn.AdaptiveMaxPool1d(args_values_list[0], args_values_list[1]))
     elif net_style == "AdaptiveMaxPool2d":
         self.layers.append(
             nn.AdaptiveMaxPool2d(args_values_list[0], args_values_list[1]))
     elif net_style == "AdaptiveMaxPool3d":
         self.layers.append(
             nn.AdaptiveMaxPool3d(args_values_list[0], args_values_list[1]))
     elif net_style == "AdaptiveAvgPool1d":
         self.layers.append(nn.AdaptiveAvgPool1d(args_values_list[0]))
     elif net_style == "AdaptiveAvgPool2d":
         self.layers.append(nn.AdaptiveAvgPool2d(args_values_list[0]))
     elif net_style == "AdaptiveAvgPool3d":
         self.layers.append(nn.AdaptiveAvgPool3d(args_values_list[0]))
     elif net_style == "ReflectionPad1d":
         self.layers.append(nn.ReflectionPad1d(args_values_list[0]))
     elif net_style == "ReflectionPad2d":
         self.layers.append(nn.ReflectionPad2d(args_values_list[0]))
     elif net_style == "ReplicationPad1d":
         self.layers.append(nn.ReplicationPad1d(args_values_list[0]))
     elif net_style == "ReplicationPad2d":
         self.layers.append(nn.ReplicationPad2d(args_values_list[0]))
     elif net_style == "ReplicationPad3d":
         self.layers.append(nn.ReplicationPad3d(args_values_list[0]))
     elif net_style == "ZeroPad2d":
         self.layers.append(nn.ZeroPad2d(args_values_list[0]))
     elif net_style == "ConstantPad1d":
         self.layers.append(
             nn.ConstantPad1d(args_values_list[0], args_values_list[1]))
     elif net_style == "ConstantPad2d":
         self.layers.append(
             nn.ConstantPad2d(args_values_list[0], args_values_list[1]))
     elif net_style == "ConstantPad3d":
         self.layers.append(
             nn.ConstantPad3d(args_values_list[0], args_values_list[1]))
     elif net_style == "ELU":
         self.layers.append(nn.ELU(args_values_list[0],
                                   args_values_list[1]))
     elif net_style == "Hardshrink":
         self.layers.append(nn.Hardshrink(args_values_list[0]))
     elif net_style == "Hardtanh":
         self.layers.append(
             nn.Hardtanh(args_values_list[0], args_values_list[1],
                         args_values_list[2], args_values_list[3],
                         args_values_list[4]))
     elif net_style == "LeakyReLU":
         self.layers.append(
             nn.LeakyReLU(args_values_list[0], args_values_list[1]))
     elif net_style == "LogSigmoid":
         self.layers.append(nn.LogSigmoid())
     elif net_style == "PReLU":
         self.layers.append(
             nn.PReLU(args_values_list[0], args_values_list[1]))
     elif net_style == "ReLU":
         self.layers.append(nn.ReLU(args_values_list[0]))
     elif net_style == "ReLU6":
         self.layers.append(nn.ReLU6(args_values_list[0]))
     elif net_style == "RReLU":
         self.layers.append(
             nn.RReLU(args_values_list[0], args_values_list[1],
                      args_values_list[2]))
     elif net_style == "SELU":
         self.layers.append(nn.SELU(args_values_list[0]))
     elif net_style == "CELU":
         self.layers.append(
             nn.CELU(args_values_list[0], args_values_list[1]))
     elif net_style == "Sigmoid":
         self.layers.append(nn.Sigmoid())
     elif net_style == "Softplus":
         self.layers.append(
             nn.Softplus(args_values_list[0], args_values_list[1]))
     elif net_style == "Softshrink":
         self.layers.append(nn.Softshrink(args_values_list[0]))
     elif net_style == "Softsign":
         self.layers.append(nn.Softsign())
     elif net_style == "Tanh":
         self.layers.append(nn.Tanh())
     elif net_style == "Tanhshrink":
         self.layers.append(nn.Tanhshrink())
     elif net_style == "Threshold":
         self.layers.append(
             nn.Threshold(args_values_list[0], args_values_list[1],
                          args_values_list[2]))
     elif net_style == "Softmin":
         self.layers.append(nn.Softmin(args_values_list[0]))
     elif net_style == "Softmax":
         self.layers.append(nn.Softmax(args_values_list[0]))
     elif net_style == "Softmax2d":
         self.layers.append(nn.Softmax2d())
     elif net_style == "LogSoftmax":
         self.layers.append(nn.LogSoftmax(args_values_list[0]))
     elif net_style == "AdaptiveLogSoftmaxWithLoss":
         self.layers.append(
             nn.AdaptiveLogSoftmaxWithLoss(args_values_list[0],
                                           args_values_list[1],
                                           args_values_list[2],
                                           args_values_list[3],
                                           args_values_list[4]))
     elif net_style == "BatchNorm1d":
         self.layers.append(
             nn.BatchNorm1d(args_values_list[0], args_values_list[1],
                            args_values_list[2], args_values_list[3],
                            args_values_list[4]))
     elif net_style == "BatchNorm2d":
         self.layers.append(
             nn.BatchNorm2d(args_values_list[0], args_values_list[1],
                            args_values_list[2], args_values_list[3],
                            args_values_list[4]))
     elif net_style == "BatchNorm3d":
         self.layers.append(
             nn.BatchNorm3d(args_values_list[0], args_values_list[1],
                            args_values_list[2], args_values_list[3],
                            args_values_list[4]))
     elif net_style == "GroupNorm":
         self.layers.append(
             nn.GroupNorm(args_values_list[0], args_values_list[1],
                          args_values_list[2], args_values_list[3]))
     elif net_style == "InstanceNorm1d":
         self.layers.append(
             nn.InstanceNorm1d(args_values_list[0], args_values_list[1],
                               args_values_list[2], args_values_list[3],
                               args_values_list[4]))
     elif net_style == "InstanceNorm2d":
         self.layers.append(
             nn.InstanceNorm2d(args_values_list[0], args_values_list[1],
                               args_values_list[2], args_values_list[3],
                               args_values_list[4]))
     elif net_style == "InstanceNorm3d":
         self.layers.append(
             nn.InstanceNorm3d(args_values_list[0], args_values_list[1],
                               args_values_list[2], args_values_list[3],
                               args_values_list[4]))
     elif net_style == "LayerNorm":
         self.layers.append(
             nn.LayerNorm(args_values_list[0], args_values_list[1],
                          args_values_list[2]))
     elif net_style == "LocalResponseNorm":
         self.layers.append(
             nn.LocalResponseNorm(args_values_list[0], args_values_list[1],
                                  args_values_list[2], args_values_list[3]))
     elif net_style == "Linear":
         self.layers.append(
             nn.Linear(args_values_list[0], args_values_list[1],
                       args_values_list[2]))
     elif net_style == "Dropout":
         self.layers.append(
             nn.Dropout(args_values_list[0], args_values_list[1]))
     elif net_style == "Dropout2d":
         self.layers.append(
             nn.Dropout2d(args_values_list[0], args_values_list[1]))
     elif net_style == "Dropout3d":
         self.layers.append(
             nn.Dropout3d(args_values_list[0], args_values_list[1]))
     elif net_style == "AlphaDropout":
         self.layers.append(
             nn.AlphaDropout(args_values_list[0], args_values_list[1]))
Exemple #13
0
    def __init__(self, n_classes=6, learned_billinear=False):
        super(patch_deconvnet, self).__init__()
        self.learned_billinear = learned_billinear
        self.n_classes = n_classes
        self.unpool = nn.MaxUnpool2d(2, stride=2)
        self.conv_block1 = nn.Sequential(

            # conv1_1
            nn.Conv2d(1, 64, 3, padding=1),
            nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # conv1_2
            nn.Conv2d(64, 64, 3, padding=1),
            nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # pool1
            nn.MaxPool2d(2, stride=2, return_indices=True, ceil_mode=True),
        )
        # it returns outputs and pool_indices_1

        # 48*48

        self.conv_block2 = nn.Sequential(

            # conv2_1
            nn.Conv2d(64, 128, 3, padding=1),
            nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # conv2_2
            nn.Conv2d(128, 128, 3, padding=1),
            nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # pool2
            nn.MaxPool2d(2, stride=2, return_indices=True, ceil_mode=True),
        )
        # it returns outputs and pool_indices_2

        # 24*24

        self.conv_block3 = nn.Sequential(

            # conv3_1
            nn.Conv2d(128, 256, 3, padding=1),
            nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # conv3_2
            nn.Conv2d(256, 256, 3, padding=1),
            nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # conv3_3
            nn.Conv2d(256, 256, 3, padding=1),
            nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # pool3
            nn.MaxPool2d(2, stride=2, return_indices=True, ceil_mode=True),
        )
        # it returns outputs and pool_indices_3

        # 12*12

        self.conv_block4 = nn.Sequential(

            # conv4_1
            nn.Conv2d(256, 512, 3, padding=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # conv4_2
            nn.Conv2d(512, 512, 3, padding=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # conv4_3
            nn.Conv2d(512, 512, 3, padding=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # pool4
            nn.MaxPool2d(2, stride=2, return_indices=True, ceil_mode=True),
        )
        # it returns outputs and pool_indices_4

        # 6*6

        self.conv_block5 = nn.Sequential(

            # conv5_1
            nn.Conv2d(512, 512, 3, padding=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # conv5_2
            nn.Conv2d(512, 512, 3, padding=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # conv5_3
            nn.Conv2d(512, 512, 3, padding=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # pool5
            nn.MaxPool2d(2, stride=2, return_indices=True, ceil_mode=True),
        )
        # it returns outputs and pool_indices_5

        # 3*3

        self.conv_block6 = nn.Sequential(

            # fc6
            nn.Conv2d(512, 4096, 3),
            # set the filter size and nor padding to make output into 1*1
            nn.BatchNorm2d(4096, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),
        )

        # 1*1

        self.conv_block7 = nn.Sequential(

            # fc7
            nn.Conv2d(4096, 4096, 1),
            # set the filter size to make output into 1*1
            nn.BatchNorm2d(4096, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),
        )

        self.deconv_block8 = nn.Sequential(

            # fc6-deconv
            nn.ConvTranspose2d(4096, 512, 3, stride=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),
        )

        # 3*3

        self.unpool_block9 = nn.Sequential(

            # unpool5
            nn.MaxUnpool2d(2, stride=2), )
        # usage unpool(output, indices)

        # 6*6

        self.deconv_block10 = nn.Sequential(

            # deconv5_1
            nn.ConvTranspose2d(512, 512, 3, stride=1, padding=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # deconv5_2
            nn.ConvTranspose2d(512, 512, 3, stride=1, padding=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # deconv5_3
            nn.ConvTranspose2d(512, 512, 3, stride=1, padding=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),
        )

        self.unpool_block11 = nn.Sequential(

            # unpool4
            nn.MaxUnpool2d(2, stride=2), )

        # 12*12

        self.deconv_block12 = nn.Sequential(

            # deconv4_1
            nn.ConvTranspose2d(512, 512, 3, stride=1, padding=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # deconv4_2
            nn.ConvTranspose2d(512, 512, 3, stride=1, padding=1),
            nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # deconv4_3
            nn.ConvTranspose2d(512, 256, 3, stride=1, padding=1),
            nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),
        )

        self.unpool_block13 = nn.Sequential(

            # unpool3
            nn.MaxUnpool2d(2, stride=2), )

        # 24*24

        self.deconv_block14 = nn.Sequential(

            # deconv3_1
            nn.ConvTranspose2d(256, 256, 3, stride=1, padding=1),
            nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # deconv3_2
            nn.ConvTranspose2d(256, 256, 3, stride=1, padding=1),
            nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # deconv3_3
            nn.ConvTranspose2d(256, 128, 3, stride=1, padding=1),
            nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),
        )

        self.unpool_block15 = nn.Sequential(

            # unpool2
            nn.MaxUnpool2d(2, stride=2), )

        # 48*48

        self.deconv_block16 = nn.Sequential(

            # deconv2_1
            nn.ConvTranspose2d(128, 128, 3, stride=1, padding=1),
            nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # deconv2_2
            nn.ConvTranspose2d(128, 64, 3, stride=1, padding=1),
            nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),
        )

        self.unpool_block17 = nn.Sequential(

            # unpool1
            nn.MaxUnpool2d(2, stride=2), )

        # 96*96

        self.deconv_block18 = nn.Sequential(

            # deconv1_1
            nn.ConvTranspose2d(64, 64, 3, stride=1, padding=1),
            nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),

            # deconv1_2
            nn.ConvTranspose2d(64, 64, 3, stride=1, padding=1),
            nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True),
            nn.ReLU(inplace=True),
        )

        self.seg_score19 = nn.Sequential(

            # seg-score
            nn.Conv2d(64, self.n_classes, 1), )

        if self.learned_billinear:
            raise NotImplementedError
    def __init__(self, num_classes, init_weights=True, pretrained=True):
        super(DeconvNet, self).__init__()

        layers = make_layers(pretrained)

        self.conv1 = layers[0]
        self.pool1 = nn.MaxPool2d(kernel_size=2,
                                  stride=2,
                                  padding=0,
                                  return_indices=True)

        self.conv2 = layers[1]
        self.pool2 = nn.MaxPool2d(kernel_size=2,
                                  stride=2,
                                  padding=0,
                                  return_indices=True)

        self.conv3 = layers[2]
        self.pool3 = nn.MaxPool2d(kernel_size=2,
                                  stride=2,
                                  padding=0,
                                  return_indices=True)

        self.conv4 = layers[3]
        self.pool4 = nn.MaxPool2d(kernel_size=2,
                                  stride=2,
                                  padding=0,
                                  return_indices=True)

        self.conv5 = layers[4]
        self.pool5 = nn.MaxPool2d(kernel_size=2,
                                  stride=2,
                                  padding=0,
                                  return_indices=True)

        self.conv67 = nn.Sequential(layers[5], nn.BatchNorm2d(4096), nn.ReLU(),
                                    layers[6], nn.BatchNorm2d(4096), nn.ReLU())

        self.deconv67 = nn.Sequential(
            nn.ConvTranspose2d(4096, 512, kernel_size=7, stride=1, padding=0),
            nn.BatchNorm2d(512), nn.ReLU())

        self.unpool5 = nn.MaxUnpool2d(kernel_size=2, stride=2)
        self.deconv5 = nn.Sequential(
            nn.ConvTranspose2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512), nn.ReLU(),
            nn.ConvTranspose2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512), nn.ReLU(),
            nn.ConvTranspose2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512), nn.ReLU())

        self.unpool4 = nn.MaxUnpool2d(kernel_size=2, stride=2)
        self.deconv4 = nn.Sequential(
            nn.ConvTranspose2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512), nn.ReLU(),
            nn.ConvTranspose2d(512, 512, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(512), nn.ReLU(),
            nn.ConvTranspose2d(512, 256, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(256), nn.ReLU())

        self.unpool3 = nn.MaxUnpool2d(kernel_size=2, stride=2)
        self.deconv3 = nn.Sequential(
            nn.ConvTranspose2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(256), nn.ReLU(),
            nn.ConvTranspose2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(256), nn.ReLU(),
            nn.ConvTranspose2d(256, 128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128), nn.ReLU())

        self.unpool2 = nn.MaxUnpool2d(kernel_size=2, stride=2)
        self.deconv2 = nn.Sequential(
            nn.ConvTranspose2d(128, 128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128), nn.ReLU(),
            nn.ConvTranspose2d(128, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64), nn.ReLU())

        self.unpool1 = nn.MaxUnpool2d(kernel_size=2, stride=2)
        self.deconv1 = nn.Sequential(
            nn.ConvTranspose2d(64, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64), nn.ReLU(),
            nn.ConvTranspose2d(64, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64), nn.ReLU(),
            nn.ConvTranspose2d(64,
                               num_classes,
                               kernel_size=1,
                               stride=1,
                               padding=0))

        if init_weights:
            self._initialize_weights()
Exemple #15
0
    def __init__(self, encoder_block, decoder_block, layers, num_classes=1000):
        self.inplanes = 64
        super(ResAE, self).__init__()
        self.conv1 = nn.Conv2d(3,
                               64,
                               kernel_size=7,
                               stride=2,
                               padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu1 = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3,
                                    stride=2,
                                    padding=1,
                                    return_indices=True)

        self.layer1 = self._make_encoder_layer(encoder_block, 64, layers[0])
        self.fc_drop1 = nn.Dropout(p=0.5)
        self.layer2 = self._make_encoder_layer(encoder_block,
                                               128,
                                               layers[1],
                                               stride=2)
        self.fc_drop2 = nn.Dropout(p=0.5)
        self.layer3 = self._make_encoder_layer(encoder_block,
                                               256,
                                               layers[2],
                                               stride=2)
        self.fc_drop3 = nn.Dropout(p=0.5)
        self.layer4 = self._make_encoder_layer(encoder_block,
                                               512,
                                               layers[3],
                                               stride=2)

        self.avgpool = nn.AvgPool2d(7)
        self.fc_drop4 = nn.Dropout(p=0.5)
        self.fc = nn.Linear(2048 * 1 * 1, p_transform["n_labels"])

        self.fc_drop5 = nn.Dropout(p=0.5)
        self.layer5 = self._make_decoder_layer(decoder_block,
                                               512,
                                               layers[3],
                                               outplanes=1024,
                                               stride=2)
        self.fc_drop6 = nn.Dropout(p=0.5)
        self.layer6 = self._make_decoder_layer(decoder_block,
                                               256,
                                               layers[2],
                                               outplanes=512,
                                               stride=2)
        self.fc_drop7 = nn.Dropout(p=0.5)
        self.layer7 = self._make_decoder_layer(decoder_block,
                                               128,
                                               layers[1],
                                               outplanes=256,
                                               stride=2)
        self.fc_drop8 = nn.Dropout(p=0.5)
        self.layer8 = self._make_decoder_layer(decoder_block,
                                               64,
                                               layers[0],
                                               outplanes=64,
                                               stride=1)

        self.max_unpool = nn.MaxUnpool2d(kernel_size=3, stride=2, padding=1)
        self.deconv1 = nn.ConvTranspose2d(64,
                                          64,
                                          7,
                                          stride=2,
                                          padding=3,
                                          output_padding=1,
                                          bias=False)
        self.bn2 = nn.BatchNorm2d(64)
        self.relu2 = nn.ReLU(inplace=True)
        self.c1_conv = nn.Conv2d(64,
                                 p_transform['channels'],
                                 kernel_size=1,
                                 stride=1,
                                 padding=0,
                                 bias=False)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()
Exemple #16
0
 def __init__(self, kernel_size, stride):
     super(CropMaxUnpool2d, self).__init__()
     self.unpool = nn.MaxUnpool2d(kernel_size, stride)
Exemple #17
0
 def __init__(self, pooling):
     super(StatefulMaxUnpool2d, self).__init__()
     self.pooling = pooling
     self.unpooling = nn.MaxUnpool2d(pooling.kernel_size, pooling.stride, pooling.padding)
Exemple #18
0
    def __init__(self, logsigma=True):
        """

        :param logsigma: for uncert weighting
        """
        super().__init__()
        filter = [64, 128, 256, 512, 1024]
        self.class_nb = 13

        # define encoder decoder layers
        self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])])
        self.decoder_block = nn.ModuleList(
            [self.conv_layer([filter[0], filter[0]])])
        for i in range(4):
            self.encoder_block.append(
                self.conv_layer([filter[i], filter[i + 1]]))
            self.decoder_block.append(
                self.conv_layer([filter[i + 1], filter[i]]))

        # define convolution layer
        self.conv_block_enc = nn.ModuleList(
            [self.conv_layer([filter[0], filter[0]])])
        self.conv_block_dec = nn.ModuleList(
            [self.conv_layer([filter[0], filter[0]])])
        for i in range(4):
            if i == 0:
                self.conv_block_enc.append(
                    self.conv_layer([filter[i + 1], filter[i + 1]]))
                self.conv_block_dec.append(
                    self.conv_layer([filter[i], filter[i]]))
            else:
                self.conv_block_enc.append(
                    nn.Sequential(
                        self.conv_layer([filter[i + 1], filter[i + 1]]),
                        self.conv_layer([filter[i + 1], filter[i + 1]])))
                self.conv_block_dec.append(
                    nn.Sequential(self.conv_layer([filter[i], filter[i]]),
                                  self.conv_layer([filter[i], filter[i]])))

        # define task specific layers
        self.pred_task1 = nn.Sequential(
            nn.Conv2d(in_channels=filter[0],
                      out_channels=filter[0],
                      kernel_size=3,
                      padding=1),
            nn.Conv2d(in_channels=filter[0],
                      out_channels=self.class_nb,
                      kernel_size=1,
                      padding=0))
        self.pred_task2 = nn.Sequential(
            nn.Conv2d(in_channels=filter[0],
                      out_channels=filter[0],
                      kernel_size=3,
                      padding=1),
            nn.Conv2d(in_channels=filter[0],
                      out_channels=1,
                      kernel_size=1,
                      padding=0))
        self.pred_task3 = nn.Sequential(
            nn.Conv2d(in_channels=filter[0],
                      out_channels=filter[0],
                      kernel_size=3,
                      padding=1),
            nn.Conv2d(in_channels=filter[0],
                      out_channels=3,
                      kernel_size=1,
                      padding=0))

        # define pooling and unpooling functions
        self.down_sampling = nn.MaxPool2d(kernel_size=2,
                                          stride=2,
                                          return_indices=True)
        self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2)

        self.logsigma = None
        if logsigma:
            self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5, -0.5]))

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.xavier_normal_(m.weight)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.xavier_normal_(m.weight)
                nn.init.constant_(m.bias, 0)
Exemple #19
0
    def __init__(self,input_c,output_c,P,
                 Type='downsampling',pool_size=(128,64),
                 pad=0,ratio=2):
        super(Bottleneck,self).__init__()
        self.Type=Type
        if self.Type=='downsampling':
            self.net=nn.ModuleDict({'block1':nn.Sequential(nn.Conv2d(input_c,output_c,2,stride=2),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(output_c)),
                                    'block2':nn.Sequential(nn.Conv2d(output_c,output_c,3,padding=pad),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(output_c)),
                                    'block3':nn.Sequential(nn.Conv2d(output_c,output_c,1),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(output_c)),
                                    'dropout':nn.Dropout2d(p=P),
                                    'Pooling':nn.AdaptiveMaxPool2d(pool_size,return_indices=True)})

        elif self.Type.split()[0]=='asymmetric':
            self.net=nn.ModuleDict({'block1':nn.Sequential(nn.Conv2d(input_c,input_c//ratio,1),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(input_c//ratio)),
                                    'block2':nn.Sequential(nn.Sequential(nn.Conv2d(input_c//ratio,input_c//ratio,(5,1),padding=(2,0)),
                                                           nn.Conv2d(input_c//ratio,input_c//ratio,(1,5),padding=(0,2))),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(input_c//ratio)),
                                    'block3':nn.Sequential(nn.Conv2d(input_c//ratio,output_c,1),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(output_c)),
                                    'dropout':nn.Dropout2d(p=P)})

        elif self.Type.split()[0]=='dilated':
            self.net=nn.ModuleDict({'block1':nn.Sequential(nn.Conv2d(input_c,input_c//ratio,1),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(input_c//ratio)),
                                    'block2':nn.Sequential(nn.Conv2d(input_c//ratio,input_c//ratio,3,dilation=int(Type.split()[1]),padding=pad),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(input_c//ratio)),
                                    'block3':nn.Sequential(nn.Conv2d(input_c//ratio,output_c,1),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(output_c)),
                                    'dropout':nn.Dropout2d(p=P)})
        elif self.Type=='normal':
            self.net=nn.ModuleDict({'block1':nn.Sequential(nn.Conv2d(input_c,input_c//ratio,1),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(input_c//ratio)),
                                    'block2':nn.Sequential(nn.Conv2d(input_c//ratio,input_c//ratio,3,padding=pad),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(input_c//ratio)),
                                    'block3':nn.Sequential(nn.Conv2d(input_c//ratio,output_c,1),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(output_c)),
                                    'dropout':nn.Dropout2d(p=P)})
        elif self.Type=='upsampling':
            self.net=nn.ModuleDict({'block1':nn.Sequential(nn.Conv2d(input_c,input_c//ratio,1),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(input_c//ratio)),
                                    'block2':nn.Sequential(nn.ConvTranspose2d(input_c//ratio,input_c//ratio,2,stride=2,padding=pad),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(input_c//ratio)),
                                    'block3':nn.Sequential(nn.Conv2d(input_c//ratio,output_c,1),
                                                           nn.PReLU(),
                                                           nn.BatchNorm2d(output_c)),
                                    'block4':nn.Conv2d(input_c,output_c,1),
                                    'unpooling':nn.MaxUnpool2d(2,stride=2),
                                    'dropout':nn.Dropout2d(p=P)})
Exemple #20
0
    def __init__(self,
                 pool_scales,
                 in_channels,
                 channels,
                 conv_cfg=None,
                 norm_cfg=dict(type='BN'),
                 act_cfg=dict(type='ReLU'),
                 align_corners=False):
        super().__init__()

        assert isinstance(pool_scales, (list, tuple))
        # Pyramid Pooling Module
        self.pool_scales = pool_scales
        self.in_channels = in_channels
        self.channels = channels
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.act_cfg = act_cfg
        self.align_corners = align_corners
        self.batch_norm = False

        self.ppm = []
        for scale in self.pool_scales:
            self.ppm.append(
                nn.Sequential(
                    nn.AdaptiveAvgPool2d(scale),
                    *(ConvModule(self.in_channels,
                                 self.channels,
                                 kernel_size=1,
                                 bias=True,
                                 conv_cfg=self.conv_cfg,
                                 norm_cfg=self.norm_cfg,
                                 act_cfg=self.act_cfg).children())))
        self.ppm = nn.ModuleList(self.ppm)

        # Followed the author's implementation that
        # concatenate conv layers described in the supplementary
        # material between up operations
        self.conv_up1 = nn.Sequential(*(list(
            ConvModule(self.in_channels + len(pool_scales) * 256,
                       self.channels,
                       padding=1,
                       kernel_size=3,
                       bias=True,
                       conv_cfg=self.conv_cfg,
                       norm_cfg=self.norm_cfg,
                       act_cfg=self.act_cfg).children()) + list(
                           ConvModule(self.channels,
                                      self.channels,
                                      padding=1,
                                      bias=True,
                                      kernel_size=3,
                                      conv_cfg=self.conv_cfg,
                                      norm_cfg=self.norm_cfg,
                                      act_cfg=self.act_cfg).children())))

        self.conv_up2 = nn.Sequential(*(list(
            ConvModule(self.channels * 2,
                       self.channels,
                       padding=1,
                       kernel_size=3,
                       bias=True,
                       conv_cfg=self.conv_cfg,
                       norm_cfg=self.norm_cfg,
                       act_cfg=self.act_cfg).children())))

        if (self.norm_cfg['type'] == 'BN'):
            d_up3 = 128
        else:
            d_up3 = 64

        self.conv_up3 = nn.Sequential(*(list(
            ConvModule(self.channels + d_up3,
                       64,
                       padding=1,
                       kernel_size=3,
                       bias=True,
                       conv_cfg=self.conv_cfg,
                       norm_cfg=self.norm_cfg,
                       act_cfg=self.act_cfg).children())))

        self.unpool = nn.MaxUnpool2d(2, stride=2)

        self.conv_up4 = nn.Sequential(
            *(list(
                ConvModule(64 + 3 + 3 + 2,
                           32,
                           padding=1,
                           kernel_size=3,
                           bias=True,
                           act_cfg=self.act_cfg).children()) + list(
                               ConvModule(32,
                                          16,
                                          padding=1,
                                          kernel_size=3,
                                          bias=True,
                                          act_cfg=self.act_cfg).children()) +
              list(
                  ConvModule(
                      16, 7, padding=0, kernel_size=1, bias=True,
                      act_cfg=None).children())))
Exemple #21
0
 def __init__(self, kernel_size=3, padding=1, stride=2):
     super().__init__()
     self.pool = nn.MaxPool2d(kernel_size=kernel_size, padding=1, stride=stride, return_indices=True)
     self.unpool = nn.MaxUnpool2d(kernel_size=kernel_size, padding=padding, stride=stride)
    def __init__(self):
        super(AutoEncoder, self).__init__()
        """
        self.encoder = nn.Sequential(
            nn.Conv2d(1, 16, 3, stride=1, padding=1),
            nn.BatchNorm2d(16),
            nn.ReLU(),
            nn.Conv2d(16, 32, 3, stride=1, padding=1),
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(2, stride=2, return_indices=True),

            nn.Conv2d(32, 64, 3, stride=1, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(),
#            nn.Conv2d(64, 64, 3, stride=1, padding=1),
#            nn.BatchNorm2d(64),
#            nn.ReLU(),
            nn.MaxPool2d(2, stride=2, return_indices=True),

#            nn.Linear(256*128*87, 1024),
#            nn.BatchNorm2d(1024),
#            nn.ReLU()
	)

        self.decoder = nn.Sequential(
            nn.MaxUnpool2d(2, stride=2),
#            nn.ConvTranspose2d(64, 64, 3, padding=1),
            nn.ConvTranspose2d(64, 32, 3, padding=1),
            
            nn.MaxUnpool2d(2, stride=2),
            nn.ConvTranspose2d(32, 16, 3, padding=1),
            nn.ConvTranspose2d(16, 1, 3, padding=1),

#            nn.Sigmoid()
        )
        """
        self.conv1 = nn.Conv2d(1, 64, 3, stride=1, padding=1)
        self.bn1 = nn.BatchNorm2d(64)
        self.r1 = nn.ReLU()

        self.conv2 = nn.Conv2d(64, 64, 3, stride=1, padding=1)
        self.bn2 = nn.BatchNorm2d(64)
        self.r2 = nn.ReLU()

        self.maxpool1 = nn.MaxPool2d(2, stride=2, return_indices=True)

        self.conv3 = nn.Conv2d(64, 128, 3, stride=1, padding=1)
        self.bn3 = nn.BatchNorm2d(128)
        self.r3 = nn.ReLU()

        self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)
        self.bn4 = nn.BatchNorm2d(128)
        self.r4 = nn.ReLU()

        self.maxpool2 = nn.MaxPool2d(2, stride=2, return_indices=True)

        self.conv5 = nn.Conv2d(128, 256, 3, stride=1, padding=1)
        self.bn5 = nn.BatchNorm2d(256)
        self.r5 = nn.ReLU()

        self.conv6 = nn.Conv2d(256, 64, 1, stride=1, padding=0)
        self.bn6 = nn.BatchNorm2d(64)
        self.r6 = nn.ReLU()
        self.conv7 = nn.Conv2d(64, 64, 3, stride=1, padding=1)
        self.bn7 = nn.BatchNorm2d(64)
        self.r7 = nn.ReLU()
        self.conv8 = nn.Conv2d(64, 256, 1, stride=1, padding=0)
        self.bn8 = nn.BatchNorm2d(256)
        self.r8 = nn.ReLU()

        #        self.maxpool2 = nn.MaxPool2d(2, stride=2, return_indices=True)

        #        self.linear1 = nn.Linear(50*128*87, 80)
        #        self.bn4 = nn.BatchNorm2d(80)
        #        self.r4 = nn.ReLU()
        #        self.linear2 = nn.Linear(80, 50*128*87)
        #        self.r5 = nn.ReLU()

        #        self.maxunpool2 = nn.MaxUnpool2d(2, stride=2)

        self.deconv8 = nn.ConvTranspose2d(256, 64, 1, padding=0)
        self.bn9 = nn.BatchNorm2d(64)
        self.r9 = nn.ReLU()
        self.deconv7 = nn.ConvTranspose2d(64, 64, 3, padding=1)
        self.bn10 = nn.BatchNorm2d(64)
        self.r10 = nn.ReLU()
        self.deconv6 = nn.ConvTranspose2d(64, 256, 1, padding=0)
        self.bn11 = nn.BatchNorm2d(256)
        self.r11 = nn.ReLU()

        self.deconv5 = nn.ConvTranspose2d(256, 128, 1, padding=0)
        self.bn12 = nn.BatchNorm2d(128)
        self.r12 = nn.ReLU()

        self.maxunpool2 = nn.MaxUnpool2d(2, stride=2)

        self.deconv4 = nn.ConvTranspose2d(128, 128, 3, padding=1)
        self.bn13 = nn.BatchNorm2d(128)
        self.r13 = nn.ReLU()

        self.deconv3 = nn.ConvTranspose2d(128, 64, 3, padding=1)
        self.bn14 = nn.BatchNorm2d(64)
        self.r14 = nn.ReLU()

        self.maxunpool1 = nn.MaxUnpool2d(2, stride=2)

        self.deconv2 = nn.ConvTranspose2d(64, 64, 3, padding=1)
        self.bn15 = nn.BatchNorm2d(64)
        self.r15 = nn.ReLU()

        self.deconv1 = nn.ConvTranspose2d(64, 1, 3, padding=1)
    def __init__(self):
        super(conv_deconv, self).__init__()
        self.convblock1 = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, padding=1),
            nn.BatchNorm2d(64),  #add batchnorm after each convolution,
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 64, kernel_size=3, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True))
        self.maxpool1 = nn.MaxPool2d(kernel_size=2,
                                     stride=2,
                                     return_indices=True)

        self.convblock2 = nn.Sequential(
            nn.Conv2d(64, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128),
            nn.ReLU(inplace=True), nn.Conv2d(128,
                                             128,
                                             kernel_size=3,
                                             padding=1), nn.BatchNorm2d(128),
            nn.ReLU(inplace=True))
        self.maxpool2 = nn.MaxPool2d(kernel_size=2,
                                     stride=2,
                                     return_indices=True)

        self.convblock3 = nn.Sequential(
            nn.Conv2d(128, 256, kernel_size=3, padding=1), nn.BatchNorm2d(256),
            nn.ReLU(inplace=True), nn.Conv2d(256,
                                             256,
                                             kernel_size=3,
                                             padding=1), nn.BatchNorm2d(256),
            nn.ReLU(inplace=True), nn.Conv2d(256,
                                             256,
                                             kernel_size=3,
                                             padding=1), nn.BatchNorm2d(256),
            nn.ReLU(inplace=True))

        self.maxpool3 = nn.MaxPool2d(kernel_size=2,
                                     stride=2,
                                     return_indices=True)

        self.convblock4 = nn.Sequential(
            nn.Conv2d(256, 512, kernel_size=3, padding=1), nn.BatchNorm2d(512),
            nn.ReLU(inplace=True), nn.Conv2d(512,
                                             512,
                                             kernel_size=3,
                                             padding=1), nn.BatchNorm2d(512),
            nn.ReLU(inplace=True), nn.Conv2d(512,
                                             512,
                                             kernel_size=3,
                                             padding=1), nn.BatchNorm2d(512),
            nn.ReLU(inplace=True))

        self.maxpool4 = nn.MaxPool2d(kernel_size=2,
                                     stride=2,
                                     return_indices=True)

        self.convblock5 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, padding=1), nn.BatchNorm2d(512),
            nn.ReLU(inplace=True), nn.Conv2d(512,
                                             512,
                                             kernel_size=3,
                                             padding=1), nn.BatchNorm2d(512),
            nn.ReLU(inplace=True), nn.Conv2d(512,
                                             512,
                                             kernel_size=3,
                                             padding=1), nn.ReLU(inplace=True),
            nn.BatchNorm2d(512))

        self.maxpool5 = nn.MaxPool2d(kernel_size=2,
                                     stride=2,
                                     return_indices=True)

        #kernel with the same size as image 7 for each convolution we get one value 4096 times in output with 0 padding
        self.fc6 = nn.Sequential(
            nn.Conv2d(512, 4096, kernel_size=7, padding=0), nn.ReLU(True))

        self.fc7 = nn.Sequential(
            nn.Conv2d(4096, out_channels=4096,
                      kernel_size=1),  #kernel size 1 to match each pixel 
            nn.ReLU(True))

        self.fc6_deconv = nn.Sequential(
            nn.ConvTranspose2d(4096, out_channels=512, kernel_size=7),
            nn.ReLU(True))

        self.maxunpool5 = nn.MaxUnpool2d(kernel_size=2, stride=2)

        self.deconvblock5 = nn.Sequential(
            nn.ConvTranspose2d(512, 512, kernel_size=3, padding=1),
            nn.BatchNorm2d(512), nn.ReLU(inplace=True),
            nn.ConvTranspose2d(512, 512, kernel_size=3, padding=1),
            nn.BatchNorm2d(512), nn.ReLU(inplace=True),
            nn.ConvTranspose2d(512, 512, kernel_size=3, padding=1),
            nn.BatchNorm2d(512), nn.ReLU(inplace=True))

        self.maxunpool4 = nn.MaxUnpool2d(kernel_size=2, stride=2)

        self.deconvblock4 = nn.Sequential(
            nn.ConvTranspose2d(512, 512, kernel_size=3, padding=1),
            nn.BatchNorm2d(512), nn.ReLU(inplace=True),
            nn.ConvTranspose2d(512, 512, kernel_size=3, padding=1),
            nn.BatchNorm2d(512), nn.ReLU(inplace=True),
            nn.ConvTranspose2d(512, 256, kernel_size=3, padding=1),
            nn.BatchNorm2d(256), nn.ReLU(inplace=True))

        self.maxunpool3 = nn.MaxUnpool2d(kernel_size=2, stride=2)

        self.deconvblock3 = nn.Sequential(
            nn.ConvTranspose2d(256, 256, kernel_size=3, padding=1),
            nn.BatchNorm2d(256), nn.ReLU(inplace=True),
            nn.ConvTranspose2d(256, 256, kernel_size=3, padding=1),
            nn.BatchNorm2d(256), nn.ReLU(inplace=True),
            nn.ConvTranspose2d(256, 128, kernel_size=3, padding=1),
            nn.BatchNorm2d(128), nn.ReLU(inplace=True))

        self.maxunpool2 = nn.MaxUnpool2d(kernel_size=2, stride=2)

        self.deconvblock2 = nn.Sequential(
            nn.ConvTranspose2d(128, 128, kernel_size=3, padding=1),
            nn.BatchNorm2d(128), nn.ReLU(inplace=True),
            nn.ConvTranspose2d(128, 64, kernel_size=3, padding=1),
            nn.BatchNorm2d(64), nn.ReLU(inplace=True))

        self.maxunpool1 = nn.MaxUnpool2d(kernel_size=2, stride=2)

        self.deconvblock1 = nn.Sequential(
            nn.ConvTranspose2d(64, 64, kernel_size=3, padding=1),
            nn.BatchNorm2d(64), nn.ReLU(inplace=True),
            nn.ConvTranspose2d(64, 1, kernel_size=3, padding=1),
            nn.BatchNorm2d(1), nn.ReLU(inplace=True))
Exemple #24
0
    def __init__(self,
                 input_shape,
                 block,
                 layers,
                 num_classes=1,
                 dilation=1,
                 dilation_growth=2,
                 block_up=None,
                 layers_up=None,
                 skip_connection=False,
                 inplanes=None,
                 preblock=None):
        super(ResNetDeconv, self).__init__()

        self.skip_connection = skip_connection

        if block_up is None:
            block_up = BasicBlockUp if block.__name__ == 'BasicBlock' \
                                   else BottleneckUp
        elif (block_up is not BasicBlockUp) and (block_up is not BottleneckUp):
            raise ValueError('Invalid upsampling block {}'.format(block_up))

        if layers_up is None:
            layers_up = layers[::-1]
        self.nb_blocks = len(layers_up)

        self.base = ResNet(input_shape,
                           block,
                           layers,
                           num_classes=num_classes,
                           dilation=dilation,
                           dilation_growth=dilation_growth,
                           return_indices=True,
                           return_sizes=True,
                           skip_connection=skip_connection,
                           preblock=preblock)

        last_layer = [
            obj for obj in self.base.modules() if isinstance(obj, nn.Conv2d)
        ][-1]
        self.inplanes = last_layer.out_channels

        for i, layer in enumerate(layers_up):

            if isinstance(layer, int):
                blocks = layer
                channels = 2**(6 + (len(layers_up) - 1) - i)
                stride = 2 if i < (len(layers_up) - 1) else 1
                dilate = dilation / dilation_growth**i
            elif isinstance(layer, list) and isinstance(layer[1], dict):
                blocks = layer[0]
                channels = layer[1].get('depth', 2**(6 + i))
                stride = layer[1].get('stride', 2 if i <
                                      (len(layers_up) - 1) else 1)
                block_up = layer[1].get('block', block_up)
                dilate = layer[1].get('dilation',
                                      dilation / dilation_growth**i)
            else:
                raise ValueError(
                    'Elements of {} should either be int '.format(layers) +
                    'or list of [int, dict]')

            outplanes = 64 if i == (len(layers_up) - 1) else None

            self.__dict__['_modules']['layer{}'.format(
                i + 1)] = self._make_up_layer(block_up,
                                              channels,
                                              blocks,
                                              stride=stride,
                                              outplanes=outplanes).apply(
                                                  partial(
                                                      self._nostride_dilate,
                                                      dilate=dilate))

        # this deconv/unpool should be condicioned to conv1 (preblock)
        # in ResNet which is now configurable
        if preblock is None:
            self.unpool = nn.MaxUnpool2d(kernel_size=(3, 3),
                                         stride=(2, 2),
                                         padding=1)
            self.deconv = nn.ConvTranspose2d(self.inplanes,
                                             2,
                                             kernel_size=7,
                                             stride=2,
                                             padding=3,
                                             bias=False)
        else:
            if preblock['pool'] is None:
                self.unpool = identity_up
            else:
                self.unpool = nn.MaxUnpool2d(*preblock['pool'])

            self.deconv = nn.ConvTranspose2d(self.inplanes,
                                             2,
                                             *preblock['conv'][1:],
                                             bias=False)

        self.bn = nn.BatchNorm2d(2)
        self.relu = nn.ReLU(inplace=True)
        self.classifier = nn.Conv2d(2, num_classes, kernel_size=1, padding=0)
    def __init__(self, nin=1, num_classes=2, dtype=torch.float32):
        super().__init__()
        self.projecting_factor = 4
        self.n_kernels = 16

        # Initial
        self.conv0 = nn.Conv2d(nin, 15, kernel_size=3, stride=2, padding=1)
        self.maxpool0 = nn.MaxPool2d(2, return_indices=True)

        # First group
        self.bottleNeck1_0 = BottleNeckDownSampling(self.n_kernels,
                                                    self.projecting_factor,
                                                    self.n_kernels * 4, dtype)
        self.bottleNeck1_1 = BottleNeckNormal(self.n_kernels * 4,
                                              self.n_kernels * 4,
                                              self.projecting_factor, 0.01)
        self.bottleNeck1_2 = BottleNeckNormal(self.n_kernels * 4,
                                              self.n_kernels * 4,
                                              self.projecting_factor, 0.01)
        self.bottleNeck1_3 = BottleNeckNormal(self.n_kernels * 4,
                                              self.n_kernels * 4,
                                              self.projecting_factor, 0.01)
        self.bottleNeck1_4 = BottleNeckNormal(self.n_kernels * 4,
                                              self.n_kernels * 4,
                                              self.projecting_factor, 0.01)

        # Second group
        self.bottleNeck2_0 = BottleNeckDownSampling(self.n_kernels * 4,
                                                    self.projecting_factor,
                                                    self.n_kernels * 8, dtype)
        self.bottleNeck2_1 = BottleNeckNormal(self.n_kernels * 8,
                                              self.n_kernels * 8,
                                              self.projecting_factor, 0.1)
        self.bottleNeck2_2 = BottleNeckDownSamplingDilatedConv(
            self.n_kernels * 8, self.projecting_factor, self.n_kernels * 8, 2)
        self.bottleNeck2_3 = BottleNeckNormal_Asym(self.n_kernels * 8,
                                                   self.n_kernels * 8,
                                                   self.projecting_factor, 0.1)
        self.bottleNeck2_4 = BottleNeckDownSamplingDilatedConv(
            self.n_kernels * 8, self.projecting_factor, self.n_kernels * 8, 4)
        self.bottleNeck2_5 = BottleNeckNormal(self.n_kernels * 8,
                                              self.n_kernels * 8,
                                              self.projecting_factor, 0.1)
        self.bottleNeck2_6 = BottleNeckDownSamplingDilatedConv(
            self.n_kernels * 8, self.projecting_factor, self.n_kernels * 8, 8)
        self.bottleNeck2_7 = BottleNeckNormal_Asym(self.n_kernels * 8,
                                                   self.n_kernels * 8,
                                                   self.projecting_factor, 0.1)
        self.bottleNeck2_8 = BottleNeckDownSamplingDilatedConv(
            self.n_kernels * 8, self.projecting_factor, self.n_kernels * 8, 16)

        # Third group
        self.bottleNeck3_1 = BottleNeckNormal(self.n_kernels * 8,
                                              self.n_kernels * 8,
                                              self.projecting_factor, 0.1)
        self.bottleNeck3_2 = BottleNeckDownSamplingDilatedConv(
            self.n_kernels * 8, self.projecting_factor, self.n_kernels * 8, 2)
        self.bottleNeck3_3 = BottleNeckNormal_Asym(self.n_kernels * 8,
                                                   self.n_kernels * 8,
                                                   self.projecting_factor, 0.1)
        self.bottleNeck3_4 = BottleNeckDownSamplingDilatedConv(
            self.n_kernels * 8, self.projecting_factor, self.n_kernels * 8, 4)
        self.bottleNeck3_5 = BottleNeckNormal(self.n_kernels * 8,
                                              self.n_kernels * 8,
                                              self.projecting_factor, 0.1)
        self.bottleNeck3_6 = BottleNeckDownSamplingDilatedConv(
            self.n_kernels * 8, self.projecting_factor, self.n_kernels * 8, 8)
        self.bottleNeck3_7 = BottleNeckNormal_Asym(self.n_kernels * 8,
                                                   self.n_kernels * 8,
                                                   self.projecting_factor, 0.1)
        self.bottleNeck3_8 = BottleNeckDownSamplingDilatedConvLast(
            self.n_kernels * 8, self.projecting_factor, self.n_kernels * 4, 16)

        # ### Decoding path ####
        # Unpooling 1
        self.unpool_0 = nn.MaxUnpool2d(2)

        self.bottleNeck_Up_1_0 = BottleNeckUpSampling(self.n_kernels * 8,
                                                      self.projecting_factor,
                                                      self.n_kernels * 4)
        self.PReLU_Up_1 = nn.PReLU()

        self.bottleNeck_Up_1_1 = BottleNeckNormal(self.n_kernels * 4,
                                                  self.n_kernels * 4,
                                                  self.projecting_factor, 0.1)
        self.bottleNeck_Up_1_2 = BottleNeckNormal(self.n_kernels * 4,
                                                  self.n_kernels,
                                                  self.projecting_factor, 0.1)

        # Unpooling 2
        self.unpool_1 = nn.MaxUnpool2d(2)
        self.bottleNeck_Up_2_1 = BottleNeckUpSampling(self.n_kernels * 2,
                                                      self.projecting_factor,
                                                      self.n_kernels)
        self.bottleNeck_Up_2_2 = BottleNeckNormal(self.n_kernels,
                                                  self.n_kernels,
                                                  self.projecting_factor, 0.1)
        self.PReLU_Up_2 = nn.PReLU()

        # Unpooling Last
        self.deconv3 = upSampleConv(self.n_kernels, self.n_kernels)

        self.out_025 = nn.Conv2d(self.n_kernels * 8,
                                 num_classes,
                                 kernel_size=3,
                                 stride=1,
                                 padding=1)
        self.out_05 = nn.Conv2d(self.n_kernels,
                                num_classes,
                                kernel_size=3,
                                stride=1,
                                padding=1)
        self.final = nn.Conv2d(self.n_kernels, num_classes, kernel_size=1)
Exemple #26
0
    def __init__(self, class_nb=13, task='semantic', ignore_index=-1):
        super(SegNet, self).__init__()
        # initialise network parameters
        filter = [64, 128, 256, 512, 512]
        self.class_nb = class_nb
        self.ignore_index = ignore_index
        self.task = task

        # define encoder decoder layers
        self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])])
        self.decoder_block = nn.ModuleList(
            [self.conv_layer([filter[0], filter[0]])])
        for i in range(4):
            self.encoder_block.append(
                self.conv_layer([filter[i], filter[i + 1]]))
            self.decoder_block.append(
                self.conv_layer([filter[i + 1], filter[i]]))

        # define convolution layer
        self.conv_block_enc = nn.ModuleList(
            [self.conv_layer([filter[0], filter[0]])])
        self.conv_block_dec = nn.ModuleList(
            [self.conv_layer([filter[0], filter[0]])])
        for i in range(4):
            if i == 0:
                self.conv_block_enc.append(
                    self.conv_layer([filter[i + 1], filter[i + 1]]))
                self.conv_block_dec.append(
                    self.conv_layer([filter[i], filter[i]]))
            else:
                self.conv_block_enc.append(
                    nn.Sequential(
                        self.conv_layer([filter[i + 1], filter[i + 1]]),
                        self.conv_layer([filter[i + 1], filter[i + 1]])))
                self.conv_block_dec.append(
                    nn.Sequential(self.conv_layer([filter[i], filter[i]]),
                                  self.conv_layer([filter[i], filter[i]])))

        self.encoder_att = nn.ModuleList([
            nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])
        ])
        self.decoder_att = nn.ModuleList([
            nn.ModuleList(
                [self.att_layer([2 * filter[0], filter[0], filter[0]])])
        ])
        self.encoder_block_att = nn.ModuleList(
            [self.conv_layer([filter[0], filter[1]])])
        self.decoder_block_att = nn.ModuleList(
            [self.conv_layer([filter[0], filter[0]])])

        for j in range(1):
            for i in range(4):
                self.encoder_att[j].append(
                    self.att_layer(
                        [2 * filter[i + 1], filter[i + 1], filter[i + 1]]))
                self.decoder_att[j].append(
                    self.att_layer(
                        [filter[i + 1] + filter[i], filter[i], filter[i]]))

        for i in range(4):
            if i < 3:
                self.encoder_block_att.append(
                    self.conv_layer([filter[i + 1], filter[i + 2]]))
                self.decoder_block_att.append(
                    self.conv_layer([filter[i + 1], filter[i]]))
            else:
                self.encoder_block_att.append(
                    self.conv_layer([filter[i + 1], filter[i + 1]]))
                self.decoder_block_att.append(
                    self.conv_layer([filter[i + 1], filter[i + 1]]))

        if self.task == 'semantic':
            self.pred_task = self.conv_layer([filter[0], self.class_nb],
                                             pred=True)
        if self.task == 'depth':
            self.pred_task = self.conv_layer([filter[0], 1], pred=True)
        if self.task == 'normal':
            self.pred_task = self.conv_layer([filter[0], 3], pred=True)

        # define pooling and unpooling functions
        self.down_sampling = nn.MaxPool2d(kernel_size=2,
                                          stride=2,
                                          return_indices=True)
        self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.xavier_normal_(m.weight)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.xavier_normal_(m.weight)
                nn.init.constant_(m.bias, 0)
Exemple #27
0
 def __init__(self, in_ch, out_ch, is_bn=False):
     super(SegNetUpx3, self).__init__()
     # upsampling and convolution block
     self.unpool = nn.MaxUnpool2d(2, 2)
     self.block = UNetDownx3(in_ch, out_ch, is_bn)
Exemple #28
0
    def __init__(self):
        super(EncoderDecoder, self).__init__()
        ##Encoder
        # vgg = models.vgg11_bn(pretrained=True)
        # self.features = vgg
        # self.features.features[0] = nn.Conv2d(4, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        # weight = torch.zeros([64, 4, 3, 3], dtype=torch.float)
        # weight[:, 0:3, :, :] = vgg.features[0].weight
        # state_dict = vgg.state_dict()
        # state_dict['features.0.weight'] = weight
        # self.features.load_state_dict(state_dict)

        self.layer0 = nn.Conv2d(4,
                                64,
                                kernel_size=(3, 3),
                                stride=(1, 1),
                                padding=(1, 1))
        self.layer1 = nn.BatchNorm2d(64,
                                     eps=1e-05,
                                     momentum=0.1,
                                     affine=True,
                                     track_running_stats=True)
        self.layer2 = nn.ReLU(inplace=True)
        self.layer3 = nn.MaxPool2d(kernel_size=2,
                                   stride=2,
                                   padding=0,
                                   dilation=1,
                                   ceil_mode=False,
                                   return_indices=True)

        self.layer4 = nn.Conv2d(64,
                                128,
                                kernel_size=(3, 3),
                                stride=(1, 1),
                                padding=(1, 1))
        self.layer5 = nn.BatchNorm2d(128,
                                     eps=1e-05,
                                     momentum=0.1,
                                     affine=True,
                                     track_running_stats=True)
        self.layer6 = nn.ReLU(inplace=True)
        self.layer7 = nn.MaxPool2d(kernel_size=2,
                                   stride=2,
                                   padding=0,
                                   dilation=1,
                                   ceil_mode=False,
                                   return_indices=True)

        self.layer8 = nn.Conv2d(128,
                                256,
                                kernel_size=(3, 3),
                                stride=(1, 1),
                                padding=(1, 1))
        self.layer9 = nn.BatchNorm2d(256,
                                     eps=1e-05,
                                     momentum=0.1,
                                     affine=True,
                                     track_running_stats=True)
        self.layer10 = nn.ReLU(inplace=True)
        self.layer11 = nn.Conv2d(256,
                                 256,
                                 kernel_size=(3, 3),
                                 stride=(1, 1),
                                 padding=(1, 1))
        self.layer12 = nn.BatchNorm2d(256,
                                      eps=1e-05,
                                      momentum=0.1,
                                      affine=True,
                                      track_running_stats=True)
        self.layer13 = nn.ReLU(inplace=True)
        self.layer14 = nn.MaxPool2d(kernel_size=2,
                                    stride=2,
                                    padding=0,
                                    dilation=1,
                                    ceil_mode=False,
                                    return_indices=True)

        self.layer15 = nn.Conv2d(256,
                                 512,
                                 kernel_size=(3, 3),
                                 stride=(1, 1),
                                 padding=(1, 1))
        self.layer16 = nn.BatchNorm2d(512,
                                      eps=1e-05,
                                      momentum=0.1,
                                      affine=True,
                                      track_running_stats=True)
        self.layer17 = nn.ReLU(inplace=True)
        self.layer18 = nn.Conv2d(512,
                                 512,
                                 kernel_size=(3, 3),
                                 stride=(1, 1),
                                 padding=(1, 1))
        self.layer19 = nn.BatchNorm2d(512,
                                      eps=1e-05,
                                      momentum=0.1,
                                      affine=True,
                                      track_running_stats=True)
        self.layer20 = nn.ReLU(inplace=True)
        self.layer21 = nn.MaxPool2d(kernel_size=2,
                                    stride=2,
                                    padding=0,
                                    dilation=1,
                                    ceil_mode=False,
                                    return_indices=True)

        self.layer22 = nn.Conv2d(512,
                                 512,
                                 kernel_size=(3, 3),
                                 stride=(1, 1),
                                 padding=(1, 1))
        self.layer23 = nn.BatchNorm2d(512,
                                      eps=1e-05,
                                      momentum=0.1,
                                      affine=True,
                                      track_running_stats=True)
        self.layer24 = nn.ReLU(inplace=True)
        self.layer25 = nn.Conv2d(512,
                                 512,
                                 kernel_size=(3, 3),
                                 stride=(1, 1),
                                 padding=(1, 1))
        self.layer26 = nn.BatchNorm2d(512,
                                      eps=1e-05,
                                      momentum=0.1,
                                      affine=True,
                                      track_running_stats=True)
        self.layer27 = nn.ReLU(inplace=True)
        self.layer28 = nn.MaxPool2d(kernel_size=2,
                                    stride=2,
                                    padding=0,
                                    dilation=1,
                                    ceil_mode=False,
                                    return_indices=True)

        ##Decoder
        self.deconv6 = nn.Conv2d(512, 512, kernel_size=(1, 1), stride=(1, 1))
        self.deconvBN6 = nn.BatchNorm2d(512)

        self.Unpooling5 = nn.MaxUnpool2d(2, stride=2)
        self.deconv5 = nn.Conv2d(512,
                                 512,
                                 kernel_size=(5, 5),
                                 stride=(1, 1),
                                 padding=(2, 2))
        self.deconvBN5 = nn.BatchNorm2d(512)

        self.Unpooling4 = nn.MaxUnpool2d(2, stride=2)
        self.deconv4 = nn.Conv2d(512,
                                 256,
                                 kernel_size=(5, 5),
                                 stride=(1, 1),
                                 padding=(2, 2))
        self.deconvBN4 = nn.BatchNorm2d(256)

        self.Unpooling3 = nn.MaxUnpool2d(2, stride=2)
        self.deconv3 = nn.Conv2d(256,
                                 128,
                                 kernel_size=(5, 5),
                                 stride=(1, 1),
                                 padding=(2, 2))
        self.deconvBN3 = nn.BatchNorm2d(128)

        self.Unpooling2 = nn.MaxUnpool2d(2, stride=2)
        self.deconv2 = nn.Conv2d(128,
                                 64,
                                 kernel_size=(5, 5),
                                 stride=(1, 1),
                                 padding=(2, 2))
        self.deconvBN2 = nn.BatchNorm2d(64)

        self.Unpooling1 = nn.MaxUnpool2d(2, stride=2)
        self.deconv1 = nn.Conv2d(64,
                                 64,
                                 kernel_size=(5, 5),
                                 stride=(1, 1),
                                 padding=(2, 2))
        self.deconvBN1 = nn.BatchNorm2d(64)

        self.conv_final = nn.Conv2d(64,
                                    1,
                                    kernel_size=(5, 5),
                                    stride=(1, 1),
                                    padding=(2, 2))

        self.initialize()
Exemple #29
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 internal_ratio=4,
                 dropout_prob=0,
                 bias=False,
                 relu=True):
        super().__init__()

        # Check in the internal_scale parameter is within the expected range
        # [1, channels]
        if internal_ratio <= 1 or internal_ratio > in_channels:
            raise RuntimeError(
                "Value out of range. Expected value in the "
                "interval [1, {0}], got internal_scale={1}. ".format(
                    in_channels, internal_ratio))

        internal_channels = in_channels // internal_ratio

        if relu:
            activation = nn.ReLU
        else:
            activation = nn.PReLU

        # Main branch - max pooling followed by feature map (channels) padding
        self.main_conv1 = nn.Sequential(
            nn.Conv2d(in_channels, out_channels, kernel_size=1, bias=bias),
            nn.BatchNorm2d(out_channels))

        # Remember that the stride is the same as the kernel_size, just like
        # the max pooling layers
        self.main_unpool1 = nn.MaxUnpool2d(kernel_size=2)

        # Extension branch - 1x1 convolution, followed by a regular, dilated or
        # asymmetric convolution, followed by another 1x1 convolution. Number
        # of channels is doubled.

        # 1x1 projection convolution with stride 1
        self.ext_conv1 = nn.Sequential(
            nn.Conv2d(in_channels, internal_channels, kernel_size=1,
                      bias=bias), nn.BatchNorm2d(internal_channels),
            activation())

        # Transposed convolution
        self.ext_tconv1 = nn.ConvTranspose2d(internal_channels,
                                             internal_channels,
                                             kernel_size=2,
                                             stride=2,
                                             bias=bias)
        self.ext_tconv1_bnorm = nn.BatchNorm2d(internal_channels)
        self.ext_tconv1_activation = activation()

        # 1x1 expansion convolution
        self.ext_conv2 = nn.Sequential(
            nn.Conv2d(internal_channels,
                      out_channels,
                      kernel_size=1,
                      bias=bias), nn.BatchNorm2d(out_channels))

        self.ext_regul = nn.Dropout2d(p=dropout_prob)

        # PReLU layer to apply after concatenating the branches
        self.out_activation = activation()
Exemple #30
0
    def __init__(self, out_channels=1, in_channels=3, pretrained=False):
        super(SegNet_VGG_GN, self).__init__()
        vgg_bn = vgg16_bn(pretrained=pretrained)
        encoder = list(vgg_bn.features.children())

        # Adjust the input size
        if in_channels != 3:
            encoder[0] = nn.Conv2d(in_channels,
                                   64,
                                   kernel_size=3,
                                   stride=1,
                                   padding=1)

        #
        for i in range(len(encoder)):
            if isinstance(encoder[i], nn.BatchNorm2d):
                encoder[i] = nn.GroupNorm(32, encoder[i].num_features)

        # Encoder, VGG without any maxpooling
        self.stage1_encoder = nn.Sequential(*encoder[:6])
        self.stage2_encoder = nn.Sequential(*encoder[7:13])
        self.stage3_encoder = nn.Sequential(*encoder[14:23])
        self.stage4_encoder = nn.Sequential(*encoder[24:33])
        self.stage5_encoder = nn.Sequential(*encoder[34:-1])
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True)

        # Decoder, same as the encoder but reversed, maxpool will not be used
        decoder = encoder
        decoder = [
            i for i in list(reversed(decoder))
            if not isinstance(i, nn.MaxPool2d)
        ]
        # Replace the last conv layer
        decoder[-1] = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)
        # When reversing, we also reversed conv->batchN->relu, correct it
        decoder = [
            item for i in range(0, len(decoder), 3)
            for item in decoder[i:i + 3][::-1]
        ]
        # Replace some conv layers & batchN after them
        for i, module in enumerate(decoder):
            if isinstance(module, nn.Conv2d):
                if module.in_channels != module.out_channels:
                    decoder[i + 1] = nn.GroupNorm(32, module.in_channels)
                    decoder[i] = nn.Conv2d(module.out_channels,
                                           module.in_channels,
                                           kernel_size=3,
                                           stride=1,
                                           padding=1)

        self.stage1_decoder = nn.Sequential(*decoder[0:9])
        self.stage2_decoder = nn.Sequential(*decoder[9:18])
        self.stage3_decoder = nn.Sequential(*decoder[18:27])
        self.stage4_decoder = nn.Sequential(*decoder[27:33])
        self.stage5_decoder = nn.Sequential(
            *decoder[33:],
            nn.Conv2d(64, out_channels, kernel_size=3, stride=1, padding=1))
        self.unpool = nn.MaxUnpool2d(kernel_size=2, stride=2)

        self._initialize_weights(self.stage1_decoder, self.stage2_decoder,
                                 self.stage3_decoder, self.stage4_decoder,
                                 self.stage5_decoder)