Let’s take gluon.model_zoo.vision.Resnet_V2
as an example. You can create your own Block
(or HybridBlock
) with a dummy feature
and whatever type of advanced output
you need followed by that. Then after initializing your block’s parameters, simply load the pre-trained network from model-zoo and replace your networks’s feature
with the pretrained net’s feature
. Just remember that if you want to train end-to-end, parameters that are passed to Trainer
must be fetched after pre-trained feature
is added to your network:
class MyAdvancedNet(gluon.HybridBlock):
def __init__(self):
super(MyAdvancedNet, self).__init__()
with self.name_scope():
self.feature = None
self.output = gluon.nn.Dense(10)
def hybrid_forward(self, F, x):
return self.output(self.feature(x))
net = MyAdvancedNet()
net.collect_params().initialize()
pretrained_net = gluon.model_zoo.vision.resnet34_v2(pretrained=True)
net.feature = pretrained_net.features
y = net(nd.random.uniform(shape=(16, 3, 224, 224)))
print(y.shape)