def get_body(self, resnet_layer_id):
        '''
        Create the feature extraction network of the SSD based on resnet34.
        The first layer of the res-net is converted into grayscale by averaging the weights of the 3 channels
        of the original resnet.

        Returns
        -------
        network: gluon.nn.HybridSequential
            The body network for feature extraction based on resnet
        '''

        pretrained = resnet34_v1(pretrained=True, ctx=ctx)
        pretrained_2 = resnet34_v1(pretrained=True, ctx=mx.cpu(0))
        first_weights = pretrained_2.features[0].weight.data().mean(
            axis=1).expand_dims(axis=1)
        # First weights could be replaced with individual channels.

        body = gluon.nn.HybridSequential()
        with body.name_scope():
            first_layer = gluon.nn.Conv2D(channels=64,
                                          kernel_size=(7, 7),
                                          padding=(3, 3),
                                          strides=(2, 2),
                                          in_channels=1,
                                          use_bias=False)
            first_layer.initialize(mx.init.Normal(), ctx=ctx)
            first_layer.weight.set_data(first_weights)
            body.add(first_layer)
            body.add(*pretrained.features[1:-resnet_layer_id])
        return body
Example #2
0
def make_cnn(ctx=mx.gpu()):
    p_dropout = 0.5
    pretrained = resnet34_v1(pretrained=True, ctx=ctx)
    pretrained_2 = resnet34_v1(pretrained=True, ctx=mx.cpu(0))
    first_weights = pretrained_2.features[0].weight.data().mean(axis=1).expand_dims(axis=1)
    # First weights could be replaced with individual channels.
        
    body = gluon.nn.HybridSequential()
    with body.name_scope():
        first_layer = gluon.nn.Conv2D(channels=64, kernel_size=(7, 7), padding=(3, 3), strides=(2, 2), in_channels=1, use_bias=False)
        first_layer.initialize(mx.init.Normal(), ctx=ctx)
        first_layer.weight.set_data(first_weights)
        body.add(first_layer)
        body.add(*pretrained.features[1:6])
        
        output = gluon.nn.HybridSequential()
        
        output.add(gluon.nn.Flatten())
        output.add(gluon.nn.Dense(64, activation='relu'))
        output.add(gluon.nn.Dropout(p_dropout))
        output.add(gluon.nn.Dense(64, activation='relu'))
        output.add(gluon.nn.Dropout(p_dropout))
        output.add(gluon.nn.Dense(4, activation='sigmoid'))

        output.collect_params().initialize(mx.init.Normal(), ctx=ctx)
        body.add(output)

    return body
Example #3
0
    def get_body(self,resnet_layer_id):

        pretrained = resnet34_v1(pretrained=True,ctx=self.ctx)
        pretrained_2 = resnet34_v1(pretrained=True,ctx=mx.cpu(0))
        first_weights = pretrained_2.features[0].weight.data().mean(axis=1).expand_dims(axis=1)
        body = gluon.nn.HybridSequential()
        with body.name_scope():
            first_layer = gluon.nn.Conv2D(channels=64,kernel_size=(7,7),padding=(3,3),strides=(2,2),in_channels=1,use_bias=False)
            first_layer.initialize(mx.init.Xavier(),ctx=self.ctx)
            first_layer.weight.set_data(first_weights)
            body.add(first_layer)
            body.add(*pretrained.features[1:-resnet_layer_id])

        return body
Example #4
0
 def __init__(self):
     super(BackboneResnet34, self).__init__()
     self.info = "resnet34"
     self.features = vision.resnet34_v1(pretrained=True).features[0:-2]
     self.append = nn.Sequential()
     self.append.add(
         nn.Conv2D(1024,
                   kernel_size=3,
                   padding=6,
                   dilation=6,
                   weight_initializer=mx.init.Xavier()), nn.BatchNorm(),
         nn.Activation("relu"),
         nn.Conv2D(1024,
                   kernel_size=1,
                   padding=0,
                   dilation=1,
                   weight_initializer=mx.init.Xavier()), nn.BatchNorm(),
         nn.Activation("relu"))
     self.extra = nn.Sequential()
     self.extra.add(
         ssd_extra_one(channels=512, stride=2, padding=1),
         ssd_extra_one(channels=256, stride=2, padding=1),
         ssd_extra_one(channels=256, stride=1,
                       padding=0),  #smaller feature map
         ssd_extra_one(channels=256, stride=1, padding=0),
     )
     self.features.collect_params().setattr("lr_mult", 0.1)
     self.extra.initialize()
     self.append.initialize()
     return
Example #5
0
        if autograd.is_training(
        ):  #for hybridBlock, must call hybridize() to make this take effect!!
            return anchors, cls_preds, bbox_pred
        return self.postprocess(anchors, cls_preds, bbox_pred)


if 0:
    net = SSD_CUSTOM(100)
    ctx = mx.gpu()
    net.collect_params().reset_ctx(ctx)
    x = nd.zeros((32, 3, 256 * 2, 256 * 2), ctx=ctx)
    y = net(x)

if 0:
    #net = get_resnet_34()
    net = vision.resnet34_v1(pretrained=True).features
    ctx = mx.gpu()
    net.collect_params().reset_ctx(ctx)
    x = nd.zeros((32, 3, 256 * 2, 256 * 2), ctx=ctx)
    y = net(x)
    for layer in net:
        x = layer(x)
        print(layer.name, x.shape)
    data = mx.sym.var("data")
    for sym in net(data).get_internals():
        print(sym)

if 0:
    # net = model_zoo.get_model('ssd_300_vgg16_atrous_voc', pretrained=False, pretrained_base=False)
    #net = vgg16_atrous_300(pretrained=True)
    net = BackboneResnet34()