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'