Esempio n. 1
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
        ]
Esempio n. 2
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)
Esempio n. 3
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:])
Esempio n. 4
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)
Esempio n. 5
0
 def __init__(self,mi,mo):
     #mi => input no of filters
     #mo => output filters for each layer
     self.f = tf.nn.relu
     self.session = None
     
     #define layers 
     self.conv1 = ConvLayer(1,mi,mo[0],stride = 1,padding = 'VALID')
     self.bn1 = BatchNormLayer(mo[0])
     self.conv2 = ConvLayer(3,mo[0],mo[1],stride = 1,padding = 'SAME')
     self.bn2 = BatchNormLayer(mo[1])
     self.conv3 = ConvLayer(1,mo[1],mo[2],stride = 1,padding = 'VALID')
     self.bn3 = BatchNormLayer(mo[2])
     
     self.layers = [self.conv1,self.bn1,
                    self.conv2,self.bn2,
                    self.conv3,self.bn3]
Esempio n. 6
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])
Esempio n. 7
0
class IdentityBlock(object):
    # X -> CL -> BN -> relu -> CL -> BN -> relu -> CL -> BN_final
    # return relu(BN_final + X) 
    def __init__(self,mi,mo):
        #mi => input no of filters
        #mo => output filters for each layer
        self.f = tf.nn.relu
        self.session = None
        
        #define layers 
        self.conv1 = ConvLayer(1,mi,mo[0],stride = 1,padding = 'VALID')
        self.bn1 = BatchNormLayer(mo[0])
        self.conv2 = ConvLayer(3,mo[0],mo[1],stride = 1,padding = 'SAME')
        self.bn2 = BatchNormLayer(mo[1])
        self.conv3 = ConvLayer(1,mo[1],mo[2],stride = 1,padding = 'VALID')
        self.bn3 = BatchNormLayer(mo[2])
        
        self.layers = [self.conv1,self.bn1,
                       self.conv2,self.bn2,
                       self.conv3,self.bn3]
        
    def forward(self,X):
        FX = self.conv1.forward(X)
        FX = self.bn1.forward(FX)
        FX = self.f(FX)
        FX = self.conv2.forward(FX)
        FX = self.bn2.forward(FX)
        FX = self.f(FX)
        FX = self.conv3.forward(FX)
        FX = self.bn3.forward(FX)
        
        return self.f(X + FX)
    
    def get_params(self):
        params = []
        for layer in self.layers:
            params += layer.get_params()
        return params
        
    def set_session(self,session):
        self.session = session
        self.conv1.session = session
        self.bn1.session = session
        self.conv2.session = session
        self.bn2.session = session
        self.conv3.session = session
        self.bn3.session = session
        
    def copyFromKerasLayers(self,layers):
        self.conv1.copyFromKerasLayers(layers[0])
        self.bn1.copyFromKerasLayers(layers[1])
        self.conv2.copyFromKerasLayers(layers[3])
        self.bn2.copyFromKerasLayers(layers[4])
        self.conv3.copyFromKerasLayers(layers[6])
        self.bn3.copyFromKerasLayers(layers[7])