in sagemaker/src/handwriting_line_recognition.py [0:0]
def get_body(self, resnet_layer_id):
'''
Create the feature extraction network 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.
Parameters
----------
resnet_layer_id: int
The resnet_layer_id specifies which layer to take from
the bottom of the network.
Returns
-------
network: gluon.nn.HybridSequential
The body network for feature extraction based on resnet
'''
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)
# 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.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