Simple image classification into Cats and Dogs

Salahuddin_Khan: <p>The code is incomplete as I’m trying to fix the error.</p>
<p> → I read image after image<br>
 → All the images are rgb 64x64, image reading code is at top, I’m using a single example for training.</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>However I get the following error :</p>
<p>File Traceback (most recent call last):<br>
File “/home/salahuddin/projects/Deeplearning_Practice/pytorch/Deep-Neural-Networks-with-PyTorch/Week6/catsvsdogscnn.py”, line 134, in <br>
train_model(n_epochs)<br>
File “/home/salahuddin/projects/Deeplearning_Practice/pytorch/Deep-Neural-Networks-with-PyTorch/Week6/catsvsdogscnn.py”, line 116, in train_model<br>
z = model(X)<br>
File “/home/salahuddin/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 889, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/home/salahuddin/projects/Deeplearning_Practice/pytorch/Deep-Neural-Networks-with-PyTorch/Week6/catsvsdogscnn.py”, line 71, in forward<br>
x = self.conv1(x)<br>
File “/home/salahuddin/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 889, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/home/salahuddin/anaconda3/lib/python3.9/site-packages/torch/nn/modules/conv.py”, line 399, in forward<br>
return self._conv_forward(input, self.weight, self.bias)<br>
File “/home/salahuddin/anaconda3/lib/python3.9/site-packages/torch/nn/modules/conv.py”, line 395, in _conv_forward<br>
return F.conv2d(input, weight, bias, self.stride,<br>
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 64, 5, 5], but got 3-dimensional input of size [64, 64, 3] instead</p>

khushi-411: <p>Hi, Salahuddin,<br>
As per the error, it looks like <code>x</code> is a 4-dimensional input of weight [64, 64, 5, 5], but you are passing 3 dim. So I think in the <code>forward</code> function 4th line, you should write:</p>
<pre><code class="lang-python">x = self.conv2(x)
</code></pre>
<p>Instead of <code>self.conv2(3)</code>. This will resolve the error partly. Another thing I noticed, the 11th line same func, <code>adPooling()</code>, is not defined anywhere. It will cause errors. Please correct it to <code>AdaptiveAvgPool2d</code>.</p>
<p>Please let me know if there are any more errors. Thanks!</p>