Custom dataset weird probelm

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!
:upside_down_face:

You have a small typo in the line of code.
torch.is_tensor should work, while you are writing is_tentor :wink:

Thanks! I will increase my font size. :rofl:

Hi @ptrblck. I have a possibly unrelated question regarding this tutorial’s code.
Why is the following code snippet included in the __getitem__ function:

...
    if torch.is_tensor(idx):
        idx = idx.tolist()
...

In my understanding, the method can only read one sample, as only one image file can only be read by io.imread(). Giving multiple indices should raise an error. The tolist() method here seems a bit confusing. Could you give some comments on that? Thanks for your reply in advance.

The pandas.DataFrame doesn’t seem to accept tensors as indices.
I guess the tolist() operation might not really be necessary, but instead a Python literal might work.
You could check it by using idx = idx.item() and rerun the script.

1 Like