Combining Two classes and trained them Parallel and Combined Manner.!.!

sgaur: <p>Hi Guys,</p>
<p>Here’s my problem I created two Classes and now want to train them parallel and Combined manner using another Class .</p>
<p><strong>Parallel Manner Structure</strong><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cd3081fdb9f6359f57c619444d248440ca753544.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/cd3081fdb9f6359f57c619444d248440ca753544" title="a"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cd3081fdb9f6359f57c619444d248440ca753544_2_499x375.jpeg" alt="a" data-base62-sha1="thbO9jXUJwunYIVvHQeAdM0XKWo" width="499" height="375" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cd3081fdb9f6359f57c619444d248440ca753544_2_499x375.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cd3081fdb9f6359f57c619444d248440ca753544_2_748x562.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/d/cd3081fdb9f6359f57c619444d248440ca753544.jpeg 2x" data-dominant-color="FCFCFC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">a</span><span class="informations">800×600 13.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<strong>Stacked Manner Structure</strong><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/8/88c8cf01e3e79db9218e58d2cff79d97c1a0f949.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/88c8cf01e3e79db9218e58d2cff79d97c1a0f949" title="a"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/8/88c8cf01e3e79db9218e58d2cff79d97c1a0f949_2_499x375.jpeg" alt="a" data-base62-sha1="jw3ajUE6spKHmP1AbbKkLeFBqXL" width="499" height="375" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/8/88c8cf01e3e79db9218e58d2cff79d97c1a0f949_2_499x375.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/8/8/88c8cf01e3e79db9218e58d2cff79d97c1a0f949_2_748x562.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/8/88c8cf01e3e79db9218e58d2cff79d97c1a0f949.jpeg 2x" data-dominant-color="FCFCFC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">a</span><span class="informations">800×600 14.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<pre><code class="lang-auto">class InceptionA(nn.Module):
    def __init__(self):
        super(InceptionA,self).__init__()
        
        ##Branch 1
        self.conv1x1   =  nn.Sequential(nn.Conv2d(in_channels=1,out_channels= 32 , kernel_size = 1),
                                        nn.BatchNorm2d(32, eps=0.001),
                                        nn.ReLU())

       
        ##Branch 2
        self.conv1x1_1 =  nn.Sequential(nn.Conv2d(in_channels=1,out_channels= 32 , kernel_size = 1),
                                        nn.BatchNorm2d(32, eps=0.001),
                                        nn.ReLU())
        self.conv5x5   =  nn.Sequential(nn.Conv2d(in_channels=32,out_channels= 64 , kernel_size = 5,padding =2),
                                        nn.BatchNorm2d(64, eps=0.001),
                                        nn.ReLU())
        
        ##Branch 3
        self.conv1x1_2 =  nn.Sequential(nn.Conv2d(in_channels=1,out_channels= 64 , kernel_size = 1),
                                        nn.BatchNorm2d(64, eps=0.001),
                                        nn.ReLU())
        self.conv3x3   =  nn.Sequential(nn.Conv2d(in_channels=64,out_channels= 128 , kernel_size = 3),
                                         nn.BatchNorm2d(128, eps=0.001),
                                         nn.ReLU())
        
        self.conv3x3_1 =  nn.Sequential(nn.Conv2d(in_channels=128,out_channels= 196 , kernel_size = 3,padding=2),
                                        nn.BatchNorm2d(196, eps=0.001),
                                        nn.ReLU())
        
        ##Branch 4        
        self.pooling   =  nn.MaxPool2d(kernel_size=3, stride=1, padding=1)
                                        
        self.conv1x1_3 =  nn.Sequential(nn.Conv2d(in_channels=1,out_channels= 64 , kernel_size = 1 ,padding = 0),
                                        nn.BatchNorm2d(64, eps=0.001),
                                        nn.ReLU())
                                        
        
    def forward(self,x):
        
        ## 1st branch
        inp1 = self.conv1x1(x)
        
        ## 2nd Branch
        inp2 = self.conv1x1_1(x)
        inp2 = self.conv5x5(inp2)
        
        ## 3rd Branch
        inp3 = self.conv1x1_2(x)
        inp3   = self.conv3x3(inp3)
        inp3 = self.conv3x3_1(inp3)
        
        ## 4th Branch
        inp4 = self.pooling(x)
        inp4  = self.conv1x1_3(inp4)
        
        output = [inp1,inp2,inp3,inp4]
        out_1 = torch.cat(output,1)
        return out_1
</code></pre>
<pre><code class="lang-auto">class InceptionB(nn.Module):
    def __init__(self):
        super(InceptionB,self).__init__()
            
        ## Branch 1
        self.conv3x3 = nn.Sequential(nn.Conv2d(in_channels = 1,out_channels = 384,kernel_size = 3,stride =2,padding = 0),##13x13x64,
                                     nn.BatchNorm2d(384, eps=0.001),
                                     nn.ReLU())
            
        ## Branch 2
        self.conv1x1 = nn.Sequential(nn.Conv2d(in_channels=1,out_channels=64,kernel_size=1,stride=1,padding=0),##28x28x64,
                                     nn.BatchNorm2d(64, eps=0.001),
                                     nn.ReLU())
                                     
        self.conv3x3_1 = nn.Sequential(nn.Conv2d(in_channels=64,out_channels=96,kernel_size=3,stride=1,padding=1),##28x28x96
                                       nn.BatchNorm2d(96, eps=0.001),
                                       nn.ReLU())
        self.conv3x3_2 = nn.Sequential(nn.Conv2d(in_channels=96,out_channels=128,kernel_size=3,stride=2,padding=0),
                                       nn.BatchNorm2d(128, eps=0.001),
                                       nn.ReLU())
            
        ## Branch 3            
        self.pooling   =  nn.MaxPool2d(kernel_size=3, stride=2, padding=0)## 1*13*13
            
    def forward(self,x):
        inp1 = self.conv3x3(x)
        
        inp2 = self.conv1x1(x)
        inp2 = self.conv3x3_1(inp2)
        inp2 = self.conv3x3_2(inp2)
        
        inp3 = self.pooling(x)
        
        output = [inp1,inp2,inp3]
        out2 = torch.cat(output,1)
        
        return out2
    
</code></pre>
<p><strong>Presently My output From Class InceptionA is = torch.Size([128, 356, 28, 28]) where 128 is Batch Size.</strong><br>
<strong>and class InceptionB =torch.Size([128, 513, 13, 13])</strong></p>
<p>Thanks in advance.!!!</p>
<p>Stay Safe.</p>

ptrblck: <p>How would you like to combine the outputs from <code>A</code> and <code>B</code> in the first approach?<br>
If you would like to concatenate both activations in the channel dimension, you would have to make sure that the spatial and batch dimensions match.<br>
You could use a pooling operation on the output of <code>A</code>, such that its spatial shape equals the output of <code>B</code>.<br>
Afterwards you could use <code>x = torch.cat((out_a, out_b), dim=1)</code> to create the input for <code>C</code>.</p>
<p>For the second approach you would have to make sure <code>InceptionB</code> takes an input of <code>[batch_size, 256, 28, 28]</code>.<br>
The inception model from <code>torchvision</code> accepts an input in the shape <code>[batch_size, 3, 229, 229]</code>, so you might need to adapt the architecture of your model.</p>

sgaur: <p>Thanks that’s what I am looking for but I need to create another class which uses these 2-classes so I can use that third class is backpropogation. It is moreover OOPS concept problem.</p>