Add layer to Faster-RCNN model

Stylianos_Charalambo: <p>Hello, I have a pretrained Faster-RCNN model that I would like to customise by adding a layer after the output of the backbone. Can anyone help me? I chose not to create a backbone and then use it as an input to FasterRCNN function because I have the faster Rcnn pretrained on its own.</p>

Dwight_Foster: <p>You can do something like this</p>
<pre><code class="lang-auto">model.backbone.add_module("NAME", nn.Linear(512,3))
</code></pre>
<p>you can also put any layers in the second argument/</p>

Stylianos_Charalambo: <p>Hi, thank you for your message. I should have mentioned the architecture I was thinking of. So I need to use another input other than the images, that will pass through some layers and then get concatenated with the output of the backbone and continue to the FPN as normal.</p>

Dwight_Foster: <p>Oh ok so you could just create a different sequential model and pass the second inputs into that. Then you would have to do the</p>
<pre><code class="lang-auto">model.backbone
</code></pre>
<p>and the other layers to make the input go through each individual part of the model. For example</p>
<pre><code class="lang-auto">x = model.transform(x)
x = model.backbone(x)
x1 = OTHERMODEL(x1)
x = cat(x,x1)
</code></pre>
<p>then pass the x into the fpn, rpn, roi, roi heads, and box predictors</p>

Stylianos_Charalambo: <p>Great! That’s exactly what I wanted. One last thing… How do I specify the multiple inputs for the faster rcnn model? I know my questions may sound silly, I am just used a beginner in pytorch.</p>

Dwight_Foster: <p>What do you mean by multiple inputs do you mean one input for the backbone and one for the other sequential model. If so you can do something like this</p>
<pre><code class="lang-auto">def forward(self, x, x1):
     x = model.backbone(x)
     x1 = SECONDMODEL(x1)
...
</code></pre>

Stylianos_Charalambo: <p>Yeah, I want to modify the faster rcnn model, use another input that will pass through some layers, concatenate that with the output of the backbone and continue through the next layers of faster rcnn</p>

Dwight_Foster: <p>Ya so just do what I said above. Just pass one input into the fasterrcnn backbone and then have another sequential model and pass an input into that and concatenate them together and then pass them through the next layers.</p>

Stylianos_Charalambo: <p>Hi, I have implemented the model but I am having trouble with the tensor shapes. Could you please help?  The 2nd input tensor has 3 columns.</p>
<pre><code class="lang-auto">        meta_array_shape = 3 
        backbone_output_shape = (512,3)
        
        self.meta_model = nn.Sequential(
            nn.Linear(meta_array_shape, backbone_output_shape),
            nn.BatchNorm1d(backbone_output_shape),
            nn.ReLU(),
            nn.Dropout(p=0.5)
        )
</code></pre>
<pre><code class="lang-auto">        X = self.faster_rcnn.backbone(X)

        # Metadata Net
        Y = self.meta_model(meta_in)
        
        # Concat Feature Maps
        X = torch.cat(X, Y)
</code></pre>

Dwight_Foster: <p>Ok well the first problem is in cat you need to do this</p>
<pre><code class="lang-auto">X = torch.cat((X,Y))
</code></pre>
<p>the two tensors you need to cat needs to be in their own parenthesis. Can you print out the shapes of the X and Y tensors going into the cat.</p>