class FaceLandmarksDataset(Dataset):
"""Face Landmarks dataset."""
def __init__(self, csv_file, root_dir, transform=None):
self.landmarks_frame = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.landmarks_frame)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_name = os.path.join(self.root_dir,
self.landmarks_frame.iloc[idx, 0])
image = io.imread(img_name)
landmarks = self.landmarks_frame.iloc[idx, 1:]
landmarks = np.array([landmarks])
landmarks = landmarks.astype('float').reshape(-1, 2)
sample = {'image': image, 'landmarks': landmarks}
if self.transform:
sample = self.transform(sample)
return sample
class CsvDataset(Dataset):
def __init__(self, path, csv_file, transform=None):
self.root = os.getcwd()
for i in path: # deal with system difference
self.root = os.path.join(self.root, i)
self.csv = pd.read_csv(os.path.join(self.root, csv_file))
self.transform = transform
def __len__(self):
return len(self.csv)
def __getitem__(self, idx):
# if torch.is_tentor(idx):
# idx = idx.tolist()
img_path = os.path.join(self.root, self.csv.iloc[idx, 0])
img = io.imread(img_path)
label = self.csv.iloc[idx, 1:]
label = np.array([label])
label = label.astype('float').reshape(-1, 2)
sample = {'image':img, 'label':label}
if self.transform:
sample = self.transform(sample)
return sample
I tried to write a custom dataset according the tutorial below.
https://pytorch.org/tutorials/beginner/data_loading_tutorial.html
And I got this error:
Traceback (most recent call last):
File "C:/Users/Nathan/pytorch/hierarchy/CIFAR10-tutorial.py", line 102, in <module>
sample = face_dataset[i]
File "C:/Users/Nathan/pytorch/hierarchy/CIFAR10-tutorial.py", line 82, in __getitem__
if torch.is_tentor(idx):
AttributeError: module 'torch' has no attribute 'is_tentor'
until I comment the if statement
FaceLandmarksDataset works well with the if statement but my CsvDataset did not!