Python CNN for Image recognition

Hi, I wanted to my own model and I took this example from github,

import torch
import torch.nn as nn
from import DataLoader
import torchvision.transforms as transforms
from Model import CNN
from Dataset import CatsAndDogsDataset
from tqdm import tqdm

device = ("cuda" if torch.cuda.is_available() else "cpu")

transform = transforms.Compose(
            transforms.Resize((356, 356)),
            transforms.RandomCrop((299, 299)),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),

This file has been truncated. show original

I replaced the model with the following.

model = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1),
nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1),
nn.Linear(in_features=64 * 74 * 74, out_features=256),
nn.Linear(in_features=256, out_features=1)

But I am getting the following error at

loss = criterion(outputs, labels).

ValueError: Using a target size (torch.Size([32])) that is different to the input size (torch.Size([32, 1])) is deprecated. Please ensure they have the same size.

Thank you,

Your criterion is nn.BCELoss() which expects a model output and target in the same shape.
You should thus .unsqueeze the tensor in dim1 and the error should disappear:

labels = labels.unsqueeze(1)

Also, I would recommend replacing nn.BCELoss with nn.BCEWithLogitsLoss for better numerical stability. nn.BCEWithLogitsLoss expects raw logits so remove the torch.sigmoid used on the model output.

Thank you. :pray:

I changed nn.BCELoss with nn.BCEWithLogitsLoss and added labels = labels.unsqueeze(1)

I am now getting the following error at like loss.backward()

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn


for name, param in model.named_parameters(): param.requires_grad = True

I set it true and it seems to be working. Thanks