我需要定义一个pytorch数据集类,该类从列中读取绝对路径以提取图像。当我尝试使用多个软件包时,会出现错误。以下是我知道的每个程序包的错误:
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
下面是我的pytorch
自定义数据集。
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列如下所示:
D:/Models/Photos/Train/train_img.jpg
完整追溯到下面:
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
答案 0 :(得分:1)
single positional indexer is out-of-bounds
告诉您您正在尝试访问不存在的列。确保您尝试访问的列存在