Resnet 18 classification

linda125542: <p>Please can you help meeeeee</p>
<pre><code class="lang-auto">class ResBlock(nn.Module):
    def __init__(self, in_channels, out_channels, downsample):
        super().__init__()
        if downsample:
            self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=2, padding=1)
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=2),
                nn.BatchNorm2d(out_channels)
            )
        else:
            self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
            self.shortcut = nn.Sequential()

        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.bn2 = nn.BatchNorm2d(out_channels)

    def forward(self, input):
        shortcut = self.shortcut(input)
        input = nn.ReLU()(self.bn1(self.conv1(input)))
        input = nn.ReLU()(self.bn2(self.conv2(input)))
        input = input + shortcut
        return nn.ReLU()(input)



class ResNet18(nn.Module):
    def __init__(self, in_channels, resblock, outputs=2):
        super().__init__()
        self.layer0 = nn.Sequential(
            nn.Conv2d(in_channels, 64, kernel_size=7, stride=2, padding=3),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU()
        )

        self.layer1 = nn.Sequential(
            resblock(64, 64, downsample=False),
            resblock(64, 64, downsample=False)
        )

        self.layer2 = nn.Sequential(
            resblock(64, 128, downsample=True),
            resblock(128, 128, downsample=False)
        )

        self.layer3 = nn.Sequential(
            resblock(128, 256, downsample=True),
            resblock(256, 256, downsample=False)
        )


        self.layer4 = nn.Sequential(
            resblock(256, 256, downsample=True),
            resblock(256, 256, downsample=False)
        )

        self.gap = torch.nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Linear(512, 2)

    def forward(self,x):
        x = self.layer0(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        
        x = self.gap(x)
        x = torch.flatten(x)
        x = x.view(x.size(0), -1)
      
        x = self.fc(x)

        return x 
</code></pre>
<p>model =  ResNet18(1, ResBlock, outputs=2)</p>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (512x1 and 512x2)</p>
<p>help meee plzzzz!!</p>

Matias_Vasquez: <p>When you want to do matrix multiplication then inner dimensions must be the same.</p>
<pre><code class="lang-python">mat1 = torch.rand(512, 1)
mat2 = torch.rand(512, 2)

# This will give you an error because the dimensions look like this
# (512, 1) x (512, 2)
mat1 @ mat2

# If you want to multiply them, you can transpose the first mat to look like this
# (1, 512) x (512, 2)
# Now the inner dimension is the same (512)
mat1.T @ mat2
</code></pre>
<p>I am assuming you get the error when you do <code>x = self.fc(x)</code>, because <code>fc</code> is of the size <code>512</code>.</p>
<p>You could try and transpose <code>x</code> when giving it to <code>fc</code>.</p>
<pre><code class="lang-python">def forward(self, x):
    ...
    x = self.fc(x.T) # ←
    return x
</code></pre>

linda125542: <p><a class="mention" href="/u/matias_vasquez">@Matias_Vasquez</a>  thank for your reply, But also not working</p>
<pre><code class="lang-auto">class ResBlock(nn.Module):
    def __init__(self, in_channels, out_channels, downsample):
        super().__init__()
        if downsample:
            self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=2, padding=1)
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=2),
                nn.BatchNorm2d(out_channels)
            )
        else:
            self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
            self.shortcut = nn.Sequential()

        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.bn2 = nn.BatchNorm2d(out_channels)

    def forward(self, input):
        shortcut = self.shortcut(input)
        input = nn.ReLU()(self.bn1(self.conv1(input)))
        input = nn.ReLU()(self.bn2(self.conv2(input)))
        input = input + shortcut
        return nn.ReLU()(input)



class ResNet18(nn.Module):
    def __init__(self, in_channels, resblock, outputs=2):
        super().__init__()
        self.layer0 = nn.Sequential(
            nn.Conv2d(in_channels, 64, kernel_size=7, stride=2, padding=3),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU()
        )

        self.layer1 = nn.Sequential(
            resblock(64, 64, downsample=False),
            resblock(64, 64, downsample=False)
        )

        self.layer2 = nn.Sequential(
            resblock(64, 128, downsample=True),
            resblock(128, 128, downsample=False)
        )

        self.layer3 = nn.Sequential(
            resblock(128, 256, downsample=True),
            resblock(256, 256, downsample=False)
        )


        self.layer4 = nn.Sequential(
            resblock(256, 256, downsample=True),
            resblock(256, 256, downsample=False)
        )

        self.gap = torch.nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Linear(512, 2)

    def forward(self,x):
        x = self.layer0(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        
        x = self.gap(x)
        x = torch.flatten(x)
        x = x.view(x.size(0), -1)
      
        x = self.fc(x.T)

        return x
</code></pre>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x1651456 and 512x2)</p>
<p>help me plzz</p>

Matias_Vasquez: <p>To avoid having these problems, you can use a <code>nn.LazyLinear()</code> layer.</p>
<p>This will take care of the input size for you.</p>
<pre><code class="lang-python">class ResNet18(nn.Module):
    def __init__(self, in_channels, resblock, outputs=2):
        super().__init__()
        self.layer0 = nn.Sequential(
            nn.Conv2d(in_channels, 64, kernel_size=7, stride=2, padding=3),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU()
        )

        self.layer1 = nn.Sequential(
            resblock(64, 64, downsample=False),
            resblock(64, 64, downsample=False)
        )

        self.layer2 = nn.Sequential(
            resblock(64, 128, downsample=True),
            resblock(128, 128, downsample=False)
        )

        self.layer3 = nn.Sequential(
            resblock(128, 256, downsample=True),
            resblock(256, 256, downsample=False)
        )


        self.layer4 = nn.Sequential(
            resblock(256, 256, downsample=True),
            resblock(256, 256, downsample=False)
        )

        self.gap = torch.nn.AdaptiveAvgPool2d(1)
        self.fc = nn.LazyLinear(out_features=2)

    def forward(self,x):
        x = self.layer0(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        
        x = self.gap(x)
        x = torch.flatten(x)
        x = self.fc(x)

        return x
</code></pre>

linda125542: <p><a class="mention" href="/u/matias_vasquez">@Matias_Vasquez</a></p>
<p>error : ValueError: Expected input batch_size (1651456) to match target batch_size (6451).</p>
<pre><code class="lang-auto">def train(epoch):
  
    model.train()
    tr_loss = 0
    # getting the training set
    x_train, y_train = Variable(train_x), Variable(train_y)
    # getting the validation set
    x_val, y_val = Variable(val_x), Variable(val_y)
    # converting the data into GPU format
    if torch.cuda.is_available():
        #model.cuda()
        x_train = x_train.cuda()
        y_train = y_train.cuda()
        x_val = x_val.cuda()
        y_val = y_val.cuda()

    # clearing the Gradients of the model parameters
    optimizer.zero_grad()

    # prediction for training and validation set
    output_train = model(x_train.float())
    output_val = model(x_val.float())

    # computing the training and validation loss
    loss_train = criterion(output_train, y_train.long())
    loss_val = criterion(output_val, y_val.long())
    train_losses.append(loss_train)
    val_losses.append(loss_val)
    
    # computing the updated weights of all the model parameters
    loss_train.backward()
    optimizer.step()
    tr_loss = loss_train.item()
    
</code></pre>

Matias_Vasquez: <p>Hi,</p>
<p>not quite sure where your error is coming from, but it seems that it is a new one. It also seems that it is coming from one of the losses computed. You might want to look at the shape of your labels (<code>y_train</code>, <code>y_val</code>) and at the predictions (<code>output_train</code>, <code>output_val</code>).</p>
<p>There are some other stuff that you need to take care.</p>
<ul>
<li>
<code>Variable</code> has been deprecated and you should not use it.</li>
<li>Not sure what <code>train_x</code> and <code>train_y</code> are, but you should use a <code>DataLoader</code> for managing your <code>Dataset</code>
</li>
<li>You should <strong>NOT</strong> do training and validation on the same step. Passing data through the model while on <code>train</code> mode will modify the gradients. You should train for an epoch and after said epoch you can validate the performance.</li>
</ul>
<p>The best you can do is look at a tutorial such as the one linked below and follow it step by step.</p>
<p><a href="https://pytorch.org/tutorials/beginner/introyt/trainingyt.html#the-training-loop" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/beginner/introyt/trainingyt.html#the-training-loop</a></p>
<p>If you prefer, it even has a video going step by step</p>
<div class="youtube-onebox lazy-video-container" data-video-id="jF43_wj_DCQ" data-video-title="Training with PyTorch" data-provider-name="youtube">
  <a href="https://www.youtube.com/watch?v=jF43_wj_DCQ" target="_blank" rel="noopener nofollow ugc">
    <img class="youtube-thumbnail" src="https://img.youtube.com/vi/jF43_wj_DCQ/maxresdefault.jpg" title="Training with PyTorch" width="690" height="388">
  </a>
</div>

<p>Here is another tutorial to finetune a pretrained version of the same model you are using (<code>resnet18</code>).</p>
<p><a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html</a></p>