Next(iter(dataloader)) error. kindly help

username: ggroom
cooked: <p>Hi,</p>
<p>upon create the dataloader, i try to iterate it ( image, labels = next(iter(dataloader)) ) to check the content and got the following error:</p>
<p>TypeError: pic should be PIL Image or ndarray. Got &lt;class ‘torch.Tensor’&gt;</p>
<p>For train data transform as followings:<br>
train_transform = transforms.Compose(<br>
[<br>
transforms.Grayscale(3),<br>
transforms.Resize((img_size,img_size)),<br>
transforms.ToTensor(),<br>
transforms.Normalize((0.1307,0.1307,0.1307),(0.3081,0.3081,0.3081))<br>
]<br>
)</p>
<p>i have created the image dataset  as img_dataset with images and csv (image file name and label) using above train_transform<br>
i then create split data by calling train_sampler = SubsetRandomSampler(…)<br>
and dataloader = DataLoader(img_dataset, batch_size=64, sampler = train_sampler)</p>
<p>I understand that upon using train_transform, the images loaded will be convert to tensor.  which is what i need before fitting to model.  However, when I do the iteration, it states that it should be PIL Image or ndarray.  i have checked on line for a while but all the sample codes from the web are similar to my code.  why in my case, there is a strange error like this.  and how can i solve it?</p>
<p>kindly advice.</p>

username: Matias_Vasquez
cooked: <p>My guess is that you already have the image as a tensor and try to perform the <code>ToTensor()</code> operation again.</p>
<p>Can you post how you create your dataset?</p>

username: ggroom
cooked: <p>ToTensor() is working fine.  the image is in jpg format.</p>
<p>torch.manual_seed(0)<br>
train_transform = transforms.Compose(<br>
[<br>
transforms.Grayscale(3),<br>
transforms.Resize((img_size,img_size)),<br>
transforms.ToTensor(),<br>
transforms.Normalize((0.5,0.5,0.5),(1.0,1.0,1.0))<br>
]<br>
)</p>
<p>class myImageDataset(Dataset):<br>
def <strong>init</strong>(self,ann_file,img_dir,transform=None):<br>
self.img_labels = pd.read_csv(ann_file)<br>
self.img_dir = img_dir<br>
self.transform = transform</p>
<pre><code>def __len__(self):
    return len(self.img_labels)  #this create upper bound, the max number of instances

def __getitem__(self, idx):
    img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx,0])
    img = read_image(img_path)
    label = self.img_labels.iloc[idx,1]
    if self.transform:
        img = self.transform(img)
    return img,label
</code></pre>
<p>img_dataset = myImageDataset(ann_file = “train.csv”,<br>
img_dir = “train_images/”,transform=train_transform)</p>
<p>dataset_size = len(img_dataset)<br>
indices = list(range(dataset_size))<br>
split = int(np.floor(validation_split*dataset_size))<br>
if shuffle_dataset:<br>
np.random.seed(random_seed)<br>
np.random.shuffle(indices)</p>
<p>train_ind = indices[split:]<br>
val_ind = indices[:split]</p>
<p>print(train_ind[45])<br>
train_sampler = SubsetRandomSampler(train_ind)<br>
valid_sampler = SubsetRandomSampler(val_ind)<br>
train_loader = DataLoader(img_dataset, batch_size = batch_size, sampler = train_sampler)<br>
valid_loader = DataLoader(img_dataset, batch_size = batch_size, sampler = valid_sampler)</p>
<p>img_,labels = next(iter(train_loader) #########&gt;&gt; this is where the error pop up</p>

username: Matias_Vasquez
cooked: <p>If you are using <code>torchvision.io.read_image</code>,  the output is already a tensor. So when you call <code>ToTensor</code> in your transform in the <code>__getitem__</code> method, that is why you get the error.</p>
<p><a href="https://pytorch.org/vision/master/generated/torchvision.io.read_image.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/vision/master/generated/torchvision.io.read_image.html</a></p>
<p>Could you print the <code>type</code> for the img variable before you do the transform? Just to be sure it is either PIL or ndarray?</p>

username: ggroom
cooked: <p>thanks Matrias_Vasquez</p>