Reputation: 1495
I need to define a pytorch dataset class that reads the absolute path from a column to pull the image. When I try this with multiple packages, I get errors. Below are the errors for each package I'm aware of:
Pathlib.Path
: TypeError: expected str, bytes or os.PathLike object, not method
glob.glob
: TypeError: expected str, bytes or os.PathLike object, not method
os.path
: TypeError: 'module' object is not callable
Below is my pytorch
custom dataset.
class image_Dataset(Dataset):
'''
image data class
'''
def __init__(self, data, transform = None):
'''
Args:
------------------------------------------------------------
data = dataframe
image = column in dataframe with absolute path to the image
label = column in dataframe that is the target classification variable
policy = ID variable
'''
self.image_frame = data
self.transform = transform
def __len__(self):
return len(self.image_frame)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
label = self.image_frame.iloc[idx, 16]
pic = Path(self.image_frame.iloc[idx,19])
img = Image.open(pic)
policy = self.image_frame.iloc[idx, 1]
sample = {'image': img, 'policy': policy, 'label':label}
if self.transform:
image = self.transform(image)
return image, label, policy
path column in my dataset values looks like the following:
D:/Models/Photos/Train/train_img.jpg
Full trace back below:
IndexError Traceback (most recent call last)
<ipython-input-261-38143c041795> in <module>
7
8 for i in range(len(roof_data_test)):
----> 9 sample = roof_data_test[i]
10
11 print(i, sample[0].shape, sample[1], sample[2])
<ipython-input-260-1bd25b938fea> in __getitem__(self, idx)
25
26 label = self.image_frame.iloc[idx, 16]
---> 27 pic = Path(self.image_frame.iloc[idx,19])
28 img = Image.open(pic)
29 policy = self.image_frame.iloc[idx, 1]
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
1492 except (KeyError, IndexError, AttributeError):
1493 pass
-> 1494 return self._getitem_tuple(key)
1495 else:
1496 # we by definition only have the 0th axis
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup)
2141 def _getitem_tuple(self, tup):
2142
-> 2143 self._has_valid_tuple(tup)
2144 try:
2145 return self._getitem_lowerdim(tup)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _has_valid_tuple(self, key)
221 raise IndexingError('Too many indexers')
222 try:
--> 223 self._validate_key(k, i)
224 except ValueError:
225 raise ValueError("Location based indexing can only have "
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis)
2068 return
2069 elif is_integer(key):
-> 2070 self._validate_integer(key, axis)
2071 elif isinstance(key, tuple):
2072 # a tuple should already have been caught by this point
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_integer(self, key, axis)
2137 len_axis = len(self.obj._get_axis(axis))
2138 if key >= len_axis or key < -len_axis:
-> 2139 raise IndexError("single positional indexer is out-of-bounds")
2140
2141 def _getitem_tuple(self, tup):
IndexError: single positional indexer is out-of-bounds
Upvotes: 0
Views: 404
Reputation: 389
single positional indexer is out-of-bounds
is telling you that your are trying to access a column that is not there. Make sure that the columns you are trying to access exist
Upvotes: 1