Custom dataset weird probelm

Nathan_Drake: <pre><code class="lang-auto">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
</code></pre>
<p>I tried to write a custom dataset according the tutorial below.</p>
<blockquote>
<p><a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">Writing Custom Datasets, DataLoaders and Transforms — PyTorch Tutorials 2.1.1+cu121 documentation</a></p>
</blockquote>
<p>And I got this error:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:/Users/Nathan/pytorch/hierarchy/CIFAR10-tutorial.py", line 102, in &lt;module&gt;
    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'
</code></pre>
<p>until I comment the if statement</p>
<p>FaceLandmarksDataset works well with the if statement but my CsvDataset did not!<br>
<img src="https://discuss.pytorch.org/images/emoji/apple/upside_down_face.png?v=12" title=":upside_down_face:" class="emoji only-emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20"></p>

ptrblck: <p>You have a small typo in the line of code.<br>
<code>torch.is_tensor</code> should work, while you are writing <code>is_tentor</code> <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

Nathan_Drake: <p>Thanks! I will increase my font size. <img src="https://discuss.pytorch.org/images/emoji/apple/rofl.png?v=9" title=":rofl:" class="emoji" alt=":rofl:"></p>

Moore: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>. I have a possibly unrelated question regarding this tutorial’s code.<br>
Why is the following code snippet included in the <code>__getitem__</code> function:</p>
<pre><code class="lang-python">...
    if torch.is_tensor(idx):
        idx = idx.tolist()
...
</code></pre>
<p>In my understanding, the method can only read one sample, as only one image file can only be read by <code>io.imread()</code>. Giving multiple indices should raise an error. The <code>tolist()</code> method here seems a bit confusing. Could you give some comments on that? Thanks for your reply in advance.</p>

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