Iterate Folder that contains Images

idriss: <p>Hi guys , Im new to pytorch<br>
I want to load my data from a folder that contains 9 images, but I can’t view my 9 images, I only managed to view 1 single image which changes each time when I compile my program</p>
<pre><code class="lang-auto">class Data_set_Papy(Dataset):
    def __init__(self , csv_file  ,root_directory_image , transform=None , target_transform=None , train= True):
  
        self.annotations=pandas.read_csv(csv_file)
        self.root_directory_image=root_directory_image
        self.transform=transform 
        self.target_transform=target_transform
        self.train=train


    def __len__(self):
        return len(self.annotations) # return the lenght of our csv file 



    
    def __getitem__(self, index) : # returnin a sprecific image and correspanding target to that image 
        our_image_path= os.path.join( self.root_directory_image, self.annotations.iloc[index , 0 ]) # colom zero the first column with a random index
        image= imread(our_image_path)
        y_label= torch.tensor(int(self.annotations.iloc[index , 1 ]))

        if self.transform:
            image=self.transform(image)
        if self.target_transform:
            image=self.target_transform(image)

        return image , y_label




training_data = Data_set_Papy(
    
    csv_file='index_papyrus.csv',
    root_directory_image='input_frags_papyrus',
    transform=  None,
    target_transform=None,
    train=True

)
testing_data = Data_set_Papy(
    csv_file='index_papyrus.csv',
    root_directory_image='input_frags_papyrus',
    transform= None,
    target_transform=None,
    train=False

)

train_dataloader=DataLoader(training_data , batch_size=1 ,  shuffle=True)
test_dataloader=DataLoader(testing_data, batch_size= 1, shuffle= False)

for data in train_dataloader:
    print(data)
    break
plt.imshow(data[0][0])
plt.show()
</code></pre>

Andrei_Cristea: <p>Hello, the code you sent will only return 1 image since you specified <code>batch_size=1</code> in the definition of <code>train_dataloader</code>, which means every time you iterate over the dataloader it will return a single image, and you have a <code>break</code> in your loop, which means you stop after that first single iteration (and therefore image). So if you’d like to see more than one image, remove the <code>break</code> from the loop.</p>

idriss: <p>thank you for your answer, I tried to remove the break, I always have an image which is displayed, I tried to change the batch size  and I have an   error:RuntimeError: stack expects each tensor to be equal size, but got [850, 480, 3] at entry 0 and [503, 625, 3] at entry 1</p>
<p>that why  I put a batch size which is equal  1</p>

Andrei_Cristea: <p>In terms of the display, in the code you pasted you only run one display command (which is <code>plt.imshow(data[0][0])</code>) and you run it <em>after</em> the loop has completed, so it’s expected that it will only display one image (specifically it’s displaying the image corresponding to the last datapoint it iterated through).</p>
<p>Regarding the RuntimeError, that’s likely because your images are of different shape, and indeed to include them in the same batch you must resize them to match. You can do that by passing a Resize transform to your <code>training_data</code> constructor, and you have to experiment with what common size works best.</p>
<pre><code class="lang-auto">from torchvision import transforms
...
training_data = Data_set_Papy(
    csv_file='index_papyrus.csv',
    root_directory_image='input_frags_papyrus',
    transform=  transforms.Resize((224, 224)),  # this
    target_transform=None,
    train=True
)
</code></pre>

idriss: <p>So I changed the transformation block as you told me , changed the way of loading the data, I also changed the batch size = 32,<br>
I still have the  problem of  viewing  just a single image  images=image[2][0] if I want the image with position 3, and i can just just change the index to have the other position .<br>
I try to find a solution to load all my images with a loop but I don’t know how, with regard to the error it is fixed, I leave you with the modifications I made in the code, and a screen of what I have in the compiler.</p>
<pre><code class="lang-auto">class Data_set_Papy(Dataset):
    def __init__(self , csv_file  ,root_directory_image , transform=None , target_transform=None , train= True):
  
        self.annotations=pandas.read_csv(csv_file)
        self.root_directory_image=root_directory_image
        self.transform=transform 
        self.target_transform=target_transform
        self.train=train


    def __len__(self):
        return len(self.annotations) # return the lenght of our csv file 



    
    def __getitem__(self, index) : # returnin a sprecific image and correspanding target to that image 
        our_image_path= os.path.join( self.root_directory_image, self.annotations.iloc[index , 0 ]) # colom zero the first column with a random index
        image= imread(our_image_path)
        y_label= torch.tensor(int(self.annotations.iloc[index , 1 ]))

        if self.transform:
            image=self.transform(image)
        if self.target_transform:
            image=self.target_transform(image)

        return image , y_label



transform = transforms.Compose(
 [
    transforms.ToPILImage(),
    transforms.Resize(255),
     transforms.CenterCrop(224),
     transforms.ToTensor()])
training_data = Data_set_Papy(
    
    csv_file='index_papyrus.csv',
    root_directory_image='input_frags_papyrus',
    transform= transform,
    target_transform=None,
    train=True

)
testing_data = Data_set_Papy(
    csv_file='index_papyrus.csv',
    root_directory_image='input_frags_papyrus',
    transform= transform,
    target_transform=None,
    train=False

)

train_dataloader=DataLoader(training_data , batch_size=32 ,  shuffle=True)
test_dataloader=DataLoader(testing_data, batch_size= 1, shuffle= False)

images, labels = next(iter(train_dataloader))
  
# print the total no of samples
print('Number of samples: ', len(images))
image = images[2][0]  # load 3rd sample 
plt.imshow(image, cmap='gray')
plt.show()

# print the size o  f image
print("Image Size: ", image.size())
  
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/e/7ec68672d230bbde3f68e08967edf04249e92065.png" alt="samp" data-base62-sha1="i5vuYcOZslDy4oILkPNxTW2KRIV" width="658" height="117"></p>
<p>that the modification<br>
i have 8 sample with a size of (224,224)</p>

Andrei_Cristea: <p>Glad the error is gone!</p>
<p>Regarding wanting to see the 3rd image, right now your <code>train_dataloader</code> has <code>shuffle=True</code> which means it’s randomizing the order of the data, so you won’t be able to index the result from the dataloader and get the image you want.</p>
<p>Try this instead:</p>
<pre><code class="lang-auto">image = training_data[2][0]  # load directly from the dataset, not from the dataloader
plt.imshow(image, cmap='gray')
plt.show()
</code></pre>

idriss: <p>i mean if i want the third image , with the previous code its works , but i want all the images in one time ! i want to see all the 9 images</p>

Andrei_Cristea: <p>OK, try this:</p>
<pre><code class="lang-auto">from matplotlib import pyplot as plt

fig, axes = plt.subplots(nrows=3, ncols=3)  # set up a 3x3 grid to visualize
image_list = [x[0] for x in training_data]
for img, ax in zip(image_list, axes.ravel()):
    ax.imshow(img)
plt.gcf().set_size_inches(10, 10)
</code></pre>

idriss: <p>seems working but  have this at the end of my image  position 8<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/0/e0de13e78e0a320f50dc499802ed141d2c9b269f.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e0de13e78e0a320f50dc499802ed141d2c9b269f" title="Capture d’écran 2022-05-19 150935"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e0de13e78e0a320f50dc499802ed141d2c9b269f_2_690x478.jpeg" alt="Capture d’écran 2022-05-19 150935" data-base62-sha1="w5gKFE8Dn6b6Zh6irVZbfrz5z8X" width="690" height="478" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e0de13e78e0a320f50dc499802ed141d2c9b269f_2_690x478.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e0de13e78e0a320f50dc499802ed141d2c9b269f_2_1035x717.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e0de13e78e0a320f50dc499802ed141d2c9b269f_2_1380x956.jpeg 2x" data-dominant-color="D4DBE2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Capture d’écran 2022-05-19 150935</span><span class="informations">1942×1347 362 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

idriss: <p>i figure out that my first image in my folder dosent load  that what explain the  empty</p>

Andrei_Cristea: <p>Glad it worked, good luck with your project, seems very cool.</p>

idriss: <p>thanks , you dont have an idea why my first image of my folder dosent load ?</p>

idriss: <p>i fixed the problem , thank you so much for your time !</p>

Andrei_Cristea: <p>In your output it says “Number of samples: 8” indicating you only have 8 images. So it seems like your dataset actually has 8 images, not 9 as you said previously. You can display them more nicely using <code>plt.subplots(nrows=2, ncols=4)</code> and then <code>plt.gcf().set_size_inches(12, 6)</code> now that you know they’re just 8 of them.</p>
<p>If you were expecting 9 images total, check your csv file and make sure all 9 are in there.</p>

idriss: <p>that what i did thank you so much , i learned a lot of things<br>
best !</p>