Alexnet Training Dimensions Error

Hi Guys,

I am new to DeepLearning using pytorch previously i have used matlab for deep learning,currently i want to perform simple image classification task using alexnet in pytorch. I have mantained images list and their classes within a csv file. i don’t wanted to use transform and augmentation for my dataset. i somehow wrote a little code but getting a dimentionality error and i am unable to understand this. below is my code and output error. your help would be highly appriciated.

import pandas as pd
import os
import numpy as np
from torchvision.models  import AlexNet
import torch
import cv2

DATASET_DIR = 'dataset/'
labels_csv = pd.read_csv('dataset/labelednum.csv', sep="delimiter", dtype="str")
images_data = []
labels_data = []

for index,line in labels_csv.iterrows():
    
    line_split = line[0].split(",")
    file_path = DATASET_DIR + "/" + line_split[0]
    
    if os.path.exists(file_path):
        
        img = cv2.cvtColor(cv2.imread(file_path, 1), cv2.COLOR_BGR2RGB)
        img = res = cv2.resize(img,(64,64))
        images_data.append(img)
        labels_data.append(line_split[1:])

images_data_tensor = torch.tensor(images_data)

net = AlexNet()
output = net(images_data_tensor)

Output Error :

RuntimeError                              Traceback (most recent call last)
<ipython-input-3-0f4c1ee226de> in <module>
      1 print(net)
      2 
----> 3 output = net(images_data_tensor)

C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

C:\ProgramData\Anaconda3\lib\site-packages\torchvision\models\alexnet.py in forward(self, x)
     42 
     43     def forward(self, x):
---> 44         x = self.features(x)
     45         x = self.avgpool(x)
     46         x = x.view(x.size(0), 256 * 6 * 6)

C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
     90     def forward(self, input):
     91         for module in self._modules.values():
---> 92             input = module(input)
     93         return input
     94 

C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    491             result = self._slow_forward(*input, **kwargs)
    492         else:
--> 493             result = self.forward(*input, **kwargs)
    494         for hook in self._forward_hooks.values():
    495             hook_result = hook(self, input, result)

C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
    336                             _pair(0), self.dilation, self.groups)
    337         return F.conv2d(input, self.weight, self.bias, self.stride,
--> 338                         self.padding, self.dilation, self.groups)
    339 
    340 

RuntimeError: Given groups=1, weight of size 64 3 11 11, expected input[4563, 64, 64, 3] to have 3 channels, but got 64 channels instead

The conv layers in PyTorch expect the input to have the channels in dim1 (channel_first).
Based on the error message it looks like you are passing the input as channel_last.
You could permute it using:

images_data_tensor = images_data_tensor.permute(0, 3, 1, 2).contiguous()

Thank you for your response, i have found this in documentation so i appended this line in my code

img = cv2.cvtColor(cv2.imread(file_path, 1), cv2.COLOR_BGR2RGB)
img = img.transpose((2, 0, 1))