Simple image classification into Cats and Dogs

The code is incomplete as I’m trying to fix the error.

→ I read image after image
→ All the images are rgb 64x64, image reading code is at top, I’m using a single example for training.

from torch.nn import Module, Conv2d, Linear, MaxPool2d, AdaptiveAvgPool2d
from torch.nn.functional import relu
import numpy as np
import cv2
import os
import torch

data_dir = '/home/salahuddin/projects/datasets/catsanddogs/train/mix'
def load_data(data_dir):
    X, y = [], []
    for file_name in os.listdir(data_dir):
        if file_name.endswith('.jpg'):
            img = cv2.imread(os.path.join(data_dir, file_name))
            img = cv2.resize(img, (64, 64))
            # img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
            img = torch.tensor(img)
            # print(img)
            X.append(img)
            if 'cat' in file_name:
                y.append(1)
                # print("Cat")
            else:
                y.append(0)
    # X = torch.tensor(X)
    y = torch.tensor(y)
    y = y.reshape(-1, 1)
    y = y.tolist()
    return X, y



class Network(Module):
    def __init__(self):
        super(Network, self).__init__()
        self.conv1 = Conv2d(in_channels=64, out_channels=64, kernel_size=5)
        self.conv2 = Conv2d(in_channels=64, out_channels=128, kernel_size=5)
        self.conv3 = Conv2d(in_channels=128, out_channels=256, kernel_size=5) 

        self.maxPooling = MaxPool2d(kernel_size=4)
        # self.adPooling = AdaptiveAvgPool2d(256).squeeze()
        self.fc1 = Linear(in_features=256, out_features=128)
        self.fc2 = Linear(in_features=128, out_features=64)
        self.out = Linear(in_features=64, out_features=2)

    def forward(self, x):
        x = self.conv1(x)
        x = self.maxPooling(x)
        x = relu(x)

        x = self.conv2(3)
        x = self.maxPooling(x)
        x = relu(x)

        x = self.conv3(x)
        x = self.maxPooling(x)
        x = relu(x)


        x = x.view(1, x.size()[0], -1)                                                                                                                                                                                                                                                                                                      
        x = self.adPooling(x)

        x = self.fc1(x)
        x = relu(x)
        x = self.fc2(x)
        x = relu(x)
        x = self.out(x)
        return x

model = Network()
learning_rate = 0.1
optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)
criterion = torch.nn.MSELoss()

train_loader = load_data(data_dir=data_dir)
# X, y = train_loader[0], train_loader[1]
# print(len(train_loader[0]), len(train_loader[0]))
n_epochs=3
cost_list=[]
accuracy_list=[]
# N_test=len(validation_dataset)
COST=0

def train_model(n_epochs):
    for epoch in range(n_epochs):
        COST=0
        for X, y in zip(train_loader[0], train_loader[1]):
            
            print(y)
            optimizer.zero_grad()
            # print(type(X))
            z = model(X)

            loss = criterion(z, y)
            loss.backward()
            optimizer.step()
            COST+=loss.data
            print(COST)
        
        # cost_list.append(COST)
        # correct=0
        # #perform a prediction on the validation  data  
        # for x_test, y_test in validation_loader:
        #     z = model(x_test)
        #     _, yhat = torch.max(z.data, 1)
        #     correct += (yhat == y_test).sum().item()
        # accuracy = correct / N_test
        # accuracy_list.append(accuracy)
     
train_model(n_epochs)

However I get the following error :

File Traceback (most recent call last):
File “/home/salahuddin/projects/Deeplearning_Practice/pytorch/Deep-Neural-Networks-with-PyTorch/Week6/catsvsdogscnn.py”, line 134, in
train_model(n_epochs)
File “/home/salahuddin/projects/Deeplearning_Practice/pytorch/Deep-Neural-Networks-with-PyTorch/Week6/catsvsdogscnn.py”, line 116, in train_model
z = model(X)
File “/home/salahuddin/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “/home/salahuddin/projects/Deeplearning_Practice/pytorch/Deep-Neural-Networks-with-PyTorch/Week6/catsvsdogscnn.py”, line 71, in forward
x = self.conv1(x)
File “/home/salahuddin/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 889, in _call_impl
result = self.forward(*input, **kwargs)
File “/home/salahuddin/anaconda3/lib/python3.9/site-packages/torch/nn/modules/conv.py”, line 399, in forward
return self._conv_forward(input, self.weight, self.bias)
File “/home/salahuddin/anaconda3/lib/python3.9/site-packages/torch/nn/modules/conv.py”, line 395, in _conv_forward
return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 64, 5, 5], but got 3-dimensional input of size [64, 64, 3] instead

Hi, Salahuddin,
As per the error, it looks like x is a 4-dimensional input of weight [64, 64, 5, 5], but you are passing 3 dim. So I think in the forward function 4th line, you should write:

x = self.conv2(x)

Instead of self.conv2(3). This will resolve the error partly. Another thing I noticed, the 11th line same func, adPooling(), is not defined anywhere. It will cause errors. Please correct it to AdaptiveAvgPool2d.

Please let me know if there are any more errors. Thanks!