Implement three models as one general model

Hello, I’m a little bit new to pytorch, cause I used to work with keras almost the time.
I have a question concerning Cnn’s.Let’s say for example I have image input data and then I’ll feed this data to Net01() and then I’ll apply some calculations to the output and then feed this data to Net2(), apply calculations and feed it to Net03().
So please can someone explain how will be the process or if someone can share some code to get an intuition and thank you.

Hi,

In PyTorch, every model/layer can be defined by extending nn.Module class.
For instance, you can see layers like nn.Linear or nn.Conv2d. When you define a new model, you need to follow same pattern which will be based on nn.Module. This leads to cases where we can have complete models such as resnet, vgg, etc (you can find available models here and their source code).

Here is an example:
Let’s say you have 3 models called Net1, Net2 and Net3. Now we want to have a model Combined that uses all three models in it.

class Combined(nn.Module):
  def __init__(self, # your args such as num classes):
    super(Combined, self).__init__()
    self.model1 = Net1()
    self.model2 = Net2()
    self.model3 = Net3()

  def forward(self, x):
    model1_output = self.model1(x) 
    # do your calc with model1_output 
    model2_output = self.model2(model1_output)  # or you can pass `x` 
    # do your calc with model2_output 
    model3_output = self.model3(x)  # or you can pass `x` or `model1_output`, etc
    # do your calc with model1_output 
    return model3_output  # or anything you want as the final output of your `Combined` model


Bests

Wow ! Thanks a lot
I started liking pytorch because of this forums !!

2 Likes

That’s great. :wink:
You will enjoy it much more when you get comfortable with PyTorch itself. Literally, you feel there is no limit! And of course, I learned almost everything about PyTorch by reading many of discussions here.

1 Like