I am reading the 1st 15 columns of a csv file for the prediction of a model. The 15th column has the scientific name of the plant that I am using in the task. When I run the code it shows an error “no such file or directory “Alliaria petiolata”” which is the scientific name of the plant. I don’t know how to solve this problem as it is not a file or directory but just an entry in a columns. The code is
with torch.no_grad():
for data in testloader:
images = data
output = model(images)
class_probs_batch = [F.softmax(el, dim=0) for el in output]
_, class_preds_batch = torch.max(output, 1)
class_probs.append(class_probs_batch)
class_preds.append(class_preds_batch)
and a custom Dataset class
class CustomDataSet(Dataset):
def __init__(self, csv_file, root_dir, transform):
self.root_dir = root_dir
self.transforms = transform
self.dataframe = pd.read_csv(csv_file)
def __len__(self):
return len(self.dataframe)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_path = self.dataframe.iloc[idx, 15]
image = Image.open(img_path).convert("RGB")
#image = Image.open(img_path)
tensor_image = self.transforms(image)
return tensor_image
The error is:
Traceback (most recent call last)
Input In [6], in <cell line: 1>()
1 with torch.no_grad():
----> 2 for data in testloader:
3 images = data
4 output = model(images)
File ~/.conda/envs/flowering/lib/python3.10/site-packages/torch/utils/data/dataloader.py:530, in _BaseDataLoaderIter.__next__(self)
528 if self._sampler_iter is None:
529 self._reset()
--> 530 data = self._next_data()
531 self._num_yielded += 1
532 if self._dataset_kind == _DatasetKind.Iterable and \
533 self._IterableDataset_len_called is not None and \
534 self._num_yielded > self._IterableDataset_len_called:
File ~/.conda/envs/flowering/lib/python3.10/site-packages/torch/utils/data/dataloader.py:570, in _SingleProcessDataLoaderIter._next_data(self)
568 def _next_data(self):
569 index = self._next_index() # may raise StopIteration
--> 570 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
571 if self._pin_memory:
572 data = _utils.pin_memory.pin_memory(data)
File ~/.conda/envs/flowering/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py:49, in _MapDatasetFetcher.fetch(self, possibly_batched_index)
47 def fetch(self, possibly_batched_index):
48 if self.auto_collation:
---> 49 data = [self.dataset[idx] for idx in possibly_batched_index]
50 else:
51 data = self.dataset[possibly_batched_index]
File ~/.conda/envs/flowering/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py:49, in <listcomp>(.0)
47 def fetch(self, possibly_batched_index):
48 if self.auto_collation:
---> 49 data = [self.dataset[idx] for idx in possibly_batched_index]
50 else:
51 data = self.dataset[possibly_batched_index]
Input In [2], in CustomDataSet.__getitem__(self, idx)
18 idx = idx.tolist()
19 img_path = self.dataframe.iloc[idx, 15]
---> 20 image = Image.open(img_path).convert("RGB")
21 #image = Image.open(img_path)
23 tensor_image = self.transforms(image)
File ~/.conda/envs/flowering/lib/python3.10/site-packages/PIL/Image.py:3092, in open(fp, mode, formats)
3089 filename = fp
3091 if filename:
-> 3092 fp = builtins.open(filename, "rb")
3093 exclusive_fp = True
3095 try:
FileNotFoundError: [Errno 2] No such file or directory: 'Alliaria petiolata'