How to collect two nets' parameters


#1

Hi.
I am try to connect two network structure manually,
eg: net1=resnet101()
net2=FasterRCNN(rpn,classifier,regressor)

I want to :
all_params = (net1+net2).collect_params()
trainer = gluon.Trainer(all_params,‘sgd’)
like this.

How to do this?
Thank you for your time and consideration. :wink: :wink:


#2

Assuming you want to treat the outputs of both networks independently, then you can do the following:

class CombinedModel(gluon.nn.HybridBlock):
  def __init__(self):
    super(CombinedModel, self).__init__()
    with self.name_scope():
          self.net1 = models.resnet50_v2(pretrained=False)
          self.net2 = models.resnet50_v1(pretrained=False)

  def hybrid_forward(self, F, x):
          output1 = self.net1(x)
          output2 = self.net2(x)
          return output1, output2

Then in your training loop, you could do something like this:

model = CombinedModel()
model.collect_params().initialize(mx.init.Xavier('gaussian'), ctx=mx.cpu())
optimizer = gluon.Trainer(model.collect_params(),'adam')

for epoch in range(num_epochs):
    for image_batch, label in dataloader:

        image_batch = image_batch.as_in_context(ctx)
        label = label.as_in_context(ctx)
        batch = image_batch.shape[0]

        with mx.autograd.record():
            output1, output2 = model(img)
            loss1 = loss_function(output2, label)
            loss2 = loss_function(output1, label)

If the output of one network shall be the input of the other one, then you have to change the hybrid_forward and training loop.