예제 #1
0
    def __init__(self, n_channles, n_filters, activation, num_sigmas, block_depth=2):
        super().__init__()
        self.activation = activation
        self.channels_to_filters = nn.Conv2d(n_channles, n_filters, kernel_size=3, padding=1)

        self.output_1_layer = ConvBlock(n_filters, n_filters,
                                        conv_before=False,
                                        activation=activation,
                                        num_sigmas=num_sigmas,
                                        kernel_size=3,
                                        residual=True,
                                        padding=1,
                                        block_depth=block_depth)
        self.output_2_layer = ConvBlock(n_filters, 2 * n_filters,
                                        conv_before=False,
                                        activation=activation,
                                        num_sigmas=num_sigmas,
                                        kernel_size=3,
                                        residual=True,
                                        block_depth=block_depth,
                                        pooling=True)
        self.output_3_layer = ConvBlock(2 * n_filters, 2 * n_filters,
                                        conv_before=False,
                                        activation=activation,
                                        num_sigmas=num_sigmas,
                                        kernel_size=3,
                                        dilation=2,
                                        padding=2,
                                        residual=True,
                                        block_depth=block_depth)
        self.output_4_layer = ConvBlock(2 * n_filters, 2 * n_filters,
                                        conv_before=False,
                                        activation=activation,
                                        num_sigmas=num_sigmas,
                                        kernel_size=3,
                                        dilation=4,
                                        padding=4,
                                        residual=True,
                                        block_depth=block_depth)

        self.refine_block4 = RefineBlock(2 * n_filters, 2 * n_filters, activation,
                                         num_sigmas, num_inputs=1)
        self.refine_block3 = RefineBlock(2 * n_filters, 2 * n_filters, activation,
                                         num_sigmas, num_inputs=2)
        self.refine_block2 = RefineBlock(2 * n_filters, 2 * n_filters, activation,
                                         num_sigmas, num_inputs=2)
        self.refine_block1 = RefineBlock(2 * n_filters, 2 * n_filters, activation,
                                         num_sigmas, num_inputs=2,
                                         in_channels_high=n_filters)

        self.output_layer = SequentialWithSigmas(
            ConditionalInstanceNormalizationPlusPlus(2 * n_filters, num_sigmas),
            activation(),
            nn.Conv2d(2 * n_filters, n_channles, kernel_size=3, padding=1)
        )
예제 #2
0
    def __init__(self):

        self.conv_layer1 = ConvLayer(7, 3, 64, stride=2, padding='SAME')
        self.batch_norm1 = BatchNormLayer(64)
        self.relu_layer1 = ReluLayer()
        self.max_pool1 = MaxPoolLayer(3)
        self.conv_block1 = ConvBlock(64, mo=[64, 64, 256], stride=1)

        self.layers = [
            self.conv_layer1, self.batch_norm1, self.relu_layer1,
            self.max_pool1, self.conv_block1
        ]
예제 #3
0
 def __init__(self):
     #layers for the partial resnet
     self.layers = [
         ConvLayer(d = 7,mi=3,mo=64,stride = 2,padding='SAME'),
         BatchNormLayer(64),
         ReluLayer(),
         MaxPoolLayer(3),
         ConvBlock(mi = 64,fm_sizes = [64,64,256],stride = 1)
     ]
     
     self.input_1 = tf.placeholder(dtype = tf.float32,shape = [None,224,224,3])
     self.output1 = self.forward(self.input_1)
예제 #4
0
    def add_block_layer(self, n_layers, stride, out_channels):
        # stride_for_each_layer_list = [stride] concatonated with [1, 1, .....]
        stride_for_each_layer_list = [stride] + [1] * (n_layers - 1)
        layers = []
        for stride in stride_for_each_layer_list:
            layers.append(
                ConvBlock(self.in_channels,
                          out_channels,
                          kernel_size=3,
                          stride=stride,
                          padding=1))
            self.in_channels = out_channels

        return nn.Sequential(*layers)
예제 #5
0
    def __init__(self):
        #1st block

        self.conv1 = ConvLayer(7, 3, 64, stride=2, padding='SAME')
        self.bn1 = BatchNormLayer(64)
        self.activation1 = ReluLayer()
        self.max_pool1 = MaxPoolLayer(3, stride=2)

        #2nd Block
        self.conv_block2a = ConvBlock(64, [64, 64, 256], stride=1)
        self.identity_block2b = IdentityBlock(256, [64, 64, 256])
        self.identity_block2c = IdentityBlock(256, [64, 64, 256])

        #3rd Block
        self.conv_block3a = ConvBlock(256, [128, 128, 512], stride=2)
        self.identity_block3b = IdentityBlock(512, [128, 128, 512])
        self.identity_block3c = IdentityBlock(512, [128, 128, 512])
        self.identity_block3d = IdentityBlock(512, [128, 128, 512])

        #4th Block
        self.conv_block4a = ConvBlock(512, [256, 256, 1024], stride=2)
        self.identity_block4b = IdentityBlock(1024, [256, 256, 1024])
        self.identity_block4c = IdentityBlock(1024, [256, 256, 1024])
        self.identity_block4d = IdentityBlock(1024, [256, 256, 1024])
        self.identity_block4e = IdentityBlock(1024, [256, 256, 1024])
        self.identity_block4f = IdentityBlock(1024, [256, 256, 1024])

        #5th Block
        self.conv_block5a = ConvBlock(1024, [512, 512, 2048], stride=2)
        self.identity_block5b = IdentityBlock(2048, [512, 512, 2048])
        self.identity_block5c = IdentityBlock(2048, [512, 512, 2048])

        #Final block
        self.avg_poolf = AvgPoolLayer(7, stride=7)
        self.flattenf = FlattenLayer()
        self.dense_layerf = DenseLayer(2048, 1000)
예제 #6
0
class PartialResNet(object):
    def __init__(self):

        self.conv_layer1 = ConvLayer(7, 3, 64, stride=2, padding='SAME')
        self.batch_norm1 = BatchNormLayer(64)
        self.relu_layer1 = ReluLayer()
        self.max_pool1 = MaxPoolLayer(3)
        self.conv_block1 = ConvBlock(64, mo=[64, 64, 256], stride=1)

        self.layers = [
            self.conv_layer1, self.batch_norm1, self.relu_layer1,
            self.max_pool1, self.conv_block1
        ]

    def forward(self, X):
        FX = self.conv_layer1.forward(X)
        FX = self.batch_norm1.forward(FX)
        FX = self.relu_layer1.forward(FX)
        FX = self.max_pool1.forward(FX)
        FX = self.conv_block1.forward(FX)
        return FX

    def get_params(self):
        all_params = []
        all_params += self.conv_layer1.get_params()
        all_params += self.batch_norm1.get_params()
        all_params += self.conv_block1.get_params()
        return all_params

    def set_session(self, session):
        self.session = session
        self.conv_layer1.session = session
        self.batch_norm1.session = session
        self.conv_block1.set_session(session)

    def copyFromKerasLayers(self, layers):
        self.conv_layer1.copyFromKerasLayers(layers[1])
        self.batch_norm1.copyFromKerasLayers(layers[2])
        self.conv_block1.copyFromKerasLayers(layers[5:])
예제 #7
0
class TfResNet(object):
    def __init__(self):
        #1st block

        self.conv1 = ConvLayer(7, 3, 64, stride=2, padding='SAME')
        self.bn1 = BatchNormLayer(64)
        self.activation1 = ReluLayer()
        self.max_pool1 = MaxPoolLayer(3, stride=2)

        #2nd Block
        self.conv_block2a = ConvBlock(64, [64, 64, 256], stride=1)
        self.identity_block2b = IdentityBlock(256, [64, 64, 256])
        self.identity_block2c = IdentityBlock(256, [64, 64, 256])

        #3rd Block
        self.conv_block3a = ConvBlock(256, [128, 128, 512], stride=2)
        self.identity_block3b = IdentityBlock(512, [128, 128, 512])
        self.identity_block3c = IdentityBlock(512, [128, 128, 512])
        self.identity_block3d = IdentityBlock(512, [128, 128, 512])

        #4th Block
        self.conv_block4a = ConvBlock(512, [256, 256, 1024], stride=2)
        self.identity_block4b = IdentityBlock(1024, [256, 256, 1024])
        self.identity_block4c = IdentityBlock(1024, [256, 256, 1024])
        self.identity_block4d = IdentityBlock(1024, [256, 256, 1024])
        self.identity_block4e = IdentityBlock(1024, [256, 256, 1024])
        self.identity_block4f = IdentityBlock(1024, [256, 256, 1024])

        #5th Block
        self.conv_block5a = ConvBlock(1024, [512, 512, 2048], stride=2)
        self.identity_block5b = IdentityBlock(2048, [512, 512, 2048])
        self.identity_block5c = IdentityBlock(2048, [512, 512, 2048])

        #Final block
        self.avg_poolf = AvgPoolLayer(7, stride=7)
        self.flattenf = FlattenLayer()
        self.dense_layerf = DenseLayer(2048, 1000)

    def forward(self, X):
        FX = self.conv1.forward(X)
        FX = self.bn1.forward(FX)
        FX = self.activation1.forward(FX)
        FX = self.max_pool1.forward(FX)

        FX = self.conv_block2a.forward(FX)
        FX = self.identity_block2b.forward(FX)
        FX = self.identity_block2c.forward(FX)

        FX = self.conv_block3a.forward(FX)
        FX = self.identity_block3b.forward(FX)
        FX = self.identity_block3c.forward(FX)
        FX = self.identity_block3d.forward(FX)

        FX = self.conv_block4a.forward(FX)
        FX = self.identity_block4b.forward(FX)
        FX = self.identity_block4c.forward(FX)
        FX = self.identity_block4d.forward(FX)
        FX = self.identity_block4e.forward(FX)
        FX = self.identity_block4f.forward(FX)

        FX = self.conv_block5a.forward(FX)
        FX = self.identity_block5b.forward(FX)
        FX = self.identity_block5c.forward(FX)

        FX = self.avg_poolf.forward(FX)
        FX = self.flattenf.forward(FX)
        FX = self.dense_layerf.forward(FX)

        return FX

    def get_params(self):
        params = []
        params += self.conv1.get_params()
        params += self.bn1.get_params()

        params += self.conv_block2a.get_params()
        params += self.identity_block2b.get_params()
        params += self.identity_block2c.get_params()

        params += self.conv_block3a.get_params()
        params += self.identity_block3b.get_params()
        params += self.identity_block3c.get_params()
        params += self.identity_block3d.get_params()

        params += self.conv_block4a.get_params()
        params += self.identity_block4b.get_params()
        params += self.identity_block4c.get_params()
        params += self.identity_block4d.get_params()
        params += self.identity_block4e.get_params()
        params += self.identity_block4f.get_params()

        params += self.conv_block5a.get_params()
        params += self.identity_block5b.get_params()
        params += self.identity_block5c.get_params()

        params += self.dense_layerf.get_params()

        return params

    def set_session(self, session):

        self.conv1.session = session
        self.bn1.session = session

        self.conv_block2a.set_session(session)
        self.identity_block2b.set_session(session)
        self.identity_block2c.set_session(session)

        self.conv_block3a.set_session(session)
        self.identity_block3b.set_session(session)
        self.identity_block3c.set_session(session)
        self.identity_block3d.set_session(session)

        self.conv_block4a.set_session(session)
        self.identity_block4b.set_session(session)
        self.identity_block4c.set_session(session)
        self.identity_block4d.set_session(session)
        self.identity_block4e.set_session(session)
        self.identity_block4f.set_session(session)

        self.conv_block5a.set_session(session)
        self.identity_block5b.set_session(session)
        self.identity_block5c.set_session(session)

        self.dense_layerf.session = session

    def copyFromKerasLayers(self, layers):
        self.conv1.copyFromKerasLayers(layers[1])
        self.bn1.copyFromKerasLayers(layers[2])

        self.conv_block2a.copyFromKerasLayers(layers[5:17])
        self.identity_block2b.copyFromKerasLayers(layers[17:27])
        self.identity_block2c.copyFromKerasLayers(layers[27:37])

        self.conv_block3a.copyFromKerasLayers(layers[37:49])
        self.identity_block3b.copyFromKerasLayers(layers[49:59])
        self.identity_block3c.copyFromKerasLayers(layers[59:69])
        self.identity_block3d.copyFromKerasLayers(layers[69:79])

        self.conv_block4a.copyFromKerasLayers(layers[79:91])
        self.identity_block4b.copyFromKerasLayers(layers[91:101])
        self.identity_block4c.copyFromKerasLayers(layers[101:111])
        self.identity_block4d.copyFromKerasLayers(layers[111:121])
        self.identity_block4e.copyFromKerasLayers(layers[121:131])
        self.identity_block4f.copyFromKerasLayers(layers[131:141])

        self.conv_block5a.copyFromKerasLayers(layers[141:153])
        self.identity_block5b.copyFromKerasLayers(layers[153:163])
        self.identity_block5c.copyFromKerasLayers(layers[163:173])

        self.dense_layerf.copyFromKerasLayers(layers[175])