Image reading error during the training

Hi , I am doing image classification problem, I read the images and i did some operation after that I building a model , but it showing some path error. please help me how to fix it.
Here my code:

import torch
from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader
from torchvision import transforms
from torch import nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import pandas as pd
import numpy as np 


from PIL import Image
from sklearn.preprocessing import MultiLabelBinarizer
IMG_PATH = 'dataset/Train Images/'
IMG_EXT = '.jpg'
TRAIN_DATA = 'dataset/train.csv'

class Dataset_get(Dataset):
  def __init__(self, csv_path, img_path, img_ext, transform=None):
    train = pd.read_csv(csv_path)
    self.mlb = MultiLabelBinarizer()
    self.img_path = img_path
    self.img_ext = img_ext
    self.transform = transform

    self.X_train = train['Image']
    self.y_train = self.mlb.fit_transform(train['Class'].str.split()).astype(np.float32)

  def __getitem__(self, index):
    img = Image.open(self.img_path + self.X_train[index] + self.img_ext)
    img = img.convert('RGB')
    if self.transform is not None:
      img = self.transform(img)
        
      label = torch.from_numpy(self.y_train[index])
    return img, label

  def __len__(self):
      return len(self.X_train.index)
transformations = transforms.Compose([transforms.Scale(32),transforms.ToTensor()])

dset_train = Dataset_get(TRAIN_DATA,IMG_PATH,IMG_EXT,transformations)
train_loader = DataLoader(dset_train,
                          batch_size=8,
                          shuffle=True,
                          num_workers=4 
                         )
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 32, kernel_size=3)
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3)
        self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(2304, 256)
        self.fc2 = nn.Linear(256, 4)

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(x.size(0), -1) # Flatten layer
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.softmax(x)

model = Net() 
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)
def train(epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        
        data, target = Variable(data), Variable(target)
        optimizer.zero_grad()
        output = model(data)
        loss = F.binary_cross_entropy(output, target)
        loss.backward()
        optimizer.step()
        if batch_idx % 4 == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.data[0]))

for epoch in range(1, 2):
    train(epoch)

here the error:

FileNotFoundError: Caught FileNotFoundError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "<ipython-input-43-627de6d2c863>", line 13, in __getitem__
    img = Image.open(self.img_path + self.X_train[index] + self.img_ext)
  File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 2766, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'dataset/Train Images/image5233.jpg.jpg'

Based on the error message, it seems you don’t need to add the file extension again, as the file path already contains the .jpg extension:

img = Image.open(self.img_path + self.X_train[index] + self.img_ext)

Hi ptrblck,
I removed the img_ext, but it is showing some another error, please help me , how to fix it this error.

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-18-b0b29ceedffa> in <module>()
      1 for epoch in range(1, 2):
----> 2     train(epoch)

4 frames
/usr/local/lib/python3.6/dist-packages/torch/_utils.py in reraise(self)
    392             # (https://bugs.python.org/issue2651), so we work around it.
    393             msg = KeyErrorMessage(msg)
--> 394         raise self.exc_type(msg)

RuntimeError: Caught RuntimeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 79, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 79, in <listcomp>
    return [default_collate(samples) for samples in transposed]
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/collate.py", line 55, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 48 and 41 in dimension 2 at /pytorch/aten/src/TH/generic/THTensor.cpp:612

The image tensor need to have the same number of channels as well as the same spatial size, which is apparently not the case for your samples.
You could use torchvision.transforms.Resize to create the same spatial size.