Images getting saved randomly while applying transforms

username: premtibadiya
cooked: <p>I am trying to create to data using torch transform. But when I am trying to save the images are saving in random manner.<br>
import torch</p>
<p>import torchvision</p>
<p>import torchvision.transforms as transforms</p>
<p>from torchvision.utils import save_image</p>
<p>import cv2</p>
<p>from skimage.util import random_noise</p>
<p>my_transforms = transforms.Compose([</p>
<pre><code>transforms.ToTensor(),

transforms.Resize((30,120)),

transforms.ColorJitter(brightness=0.5),

#transforms.RandomPerspective(distortion_scale=0.5, p=1, fill=1),

#transforms.RandomHorizontalFlip(p=0.1),

#transforms.RandomAffine(degrees=(10), scale=(.9, 1.1), shear=0),

#transforms.GaussianBlur(kernel_size=1, sigma=(0.1, 2.0)),

#transforms.Pad(padding=5, fill=1, padding_mode='constant')



])
</code></pre>
<p>dataset = torchvision.datasets.ImageFolder(root=“W:/2/…”,transform=my_transforms)</p>
<p>loader = torch.utils.data.DataLoader(dataset=dataset, batch_size=1, shuffle=False)</p>

username: ptrblck
cooked: <p>Could you post the code, which shows how the images are saved?<br>
Also, which order do you expect to see and which one is currently used while saving?<br>
Since you are using an <code>ImageFolder</code>, did you check in which order the folder names are loaded?</p>

username: premtibadiya
cooked: <p>I have saved images number wise (0.jpg,1.jpg) and the images are getting loaded like this :-<br>
(‘W:/2/med-ROI\images\0.jpg’,)<br>
(‘W:/2/med-ROI\images\1.jpg’,)<br>
(‘W:/2/med-ROI\images\10.jpg’,)<br>
(‘W:/2/med-ROI\images\100.jpg’,)<br>
(‘W:/2/med-ROI\images\101.jpg’,)<br>
(‘W:/2/med-ROI\images\102.jpg’,)<br>
(‘W:/2/med-ROI\images\103.jpg’,)<br>
(‘W:/2/med-ROI\images\104.jpg’,)<br>
(‘W:/2/med-ROI\images\105.jpg’,)<br>
(‘W:/2/med-ROI\images\106.jpg’,)<br>
(‘W:/2/med-ROI\images\107.jpg’,)<br>
(‘W:/2/med-ROI\images\108.jpg’,)<br>
(‘W:/2/med-ROI\images\109.jpg’,)<br>
(‘W:/2/med-ROI\images\11.jpg’,)<br>
(‘W:/2/med-ROI\images\110.jpg’,)<br>
(‘W:/2/med-ROI\images\111.jpg’,)<br>
(‘W:/2/med-ROI\images\112.jpg’,)<br>
(‘W:/2/med-ROI\images\113.jpg’,)<br>
(‘W:/2/med-ROI\images\114.jpg’,)<br>
(‘W:/2/med-ROI\images\115.jpg’,)<br>
(‘W:/2/med-ROI\images\116.jpg’,)<br>
(‘W:/2/med-ROI\images\117.jpg’,)<br>
(‘W:/2/med-ROI\images\118.jpg’,)<br>
(‘W:/2/med-ROI\images\119.jpg’,)<br>
(‘W:/2/med-ROI\images\12.jpg’,)<br>
(‘W:/2/med-ROI\images\120.jpg’,)<br>
(‘W:/2/med-ROI\images\121.jpg’,)<br>
(‘W:/2/med-ROI\images\122.jpg’,)<br>
(‘W:/2/med-ROI\images\123.jpg’,)<br>
(‘W:/2/med-ROI\images\124.jpg’,)<br>
(‘W:/2/med-ROI\images\125.jpg’,)<br>
(‘W:/2/med-ROI\images\126.jpg’,)<br>
(‘W:/2/med-ROI\images\127.jpg’,)<br>
(‘W:/2/med-ROI\images\128.jpg’,)<br>
(‘W:/2/med-ROI\images\129.jpg’,)<br>
(‘W:/2/med-ROI\images\13.jpg’,)<br>
(‘W:/2/med-ROI\images\130.jpg’,)<br>
(‘W:/2/med-ROI\images\131.jpg’,)<br>
(‘W:/2/med-ROI\images\132.jpg’,)<br>
(‘W:/2/med-ROI\images\133.jpg’,)<br>
(‘W:/2/med-ROI\images\134.jpg’,)<br>
(‘W:/2/med-ROI\images\135.jpg’,)<br>
(‘W:/2/med-ROI\images\136.jpg’,)<br>
(‘W:/2/med-ROI\images\137.jpg’,)<br>
(‘W:/2/med-ROI\images\138.jpg’,)<br>
(‘W:/2/med-ROI\images\139.jpg’,)<br>
(‘W:/2/med-ROI\images\14.jpg’,)<br>
(‘W:/2/med-ROI\images\140.jpg’,)<br>
(‘W:/2/med-ROI\images\141.jpg’,)<br>
(‘W:/2/med-ROI\images\142.jpg’,)<br>
(‘W:/2/med-ROI\images\143.jpg’,)<br>
(‘W:/2/med-ROI\images\144.jpg’,)<br>
(‘W:/2/med-ROI\images\145.jpg’,)<br>
(‘W:/2/med-ROI\images\146.jpg’,)<br>
(‘W:/2/med-ROI\images\147.jpg’,)<br>
(‘W:/2/med-ROI\images\148.jpg’,)<br>
(‘W:/2/med-ROI\images\149.jpg’,)<br>
(‘W:/2/med-ROI\images\15.jpg’,)<br>
(‘W:/2/med-ROI\images\150.jpg’,)<br>
(‘W:/2/med-ROI\images\151.jpg’,)<br>
(‘W:/2/med-ROI\images\152.jpg’,)<br>
(‘W:/2/med-ROI\images\153.jpg’,)<br>
(‘W:/2/med-ROI\images\154.jpg’,)<br>
(‘W:/2/med-ROI\images\155.jpg’,)<br>
(‘W:/2/med-ROI\images\156.jpg’,)<br>
(‘W:/2/med-ROI\images\157.jpg’,)<br>
(‘W:/2/med-ROI\images\158.jpg’,)<br>
(‘W:/2/med-ROI\images\159.jpg’,)<br>
(‘W:/2/med-ROI\images\16.jpg’,)<br>
(‘W:/2/med-ROI\images\160.jpg’,)<br>
(‘W:/2/med-ROI\images\161.jpg’,)<br>
(‘W:/2/med-ROI\images\162.jpg’,)<br>
(‘W:/2/med-ROI\images\163.jpg’,)<br>
(‘W:/2/med-ROI\images\164.jpg’,)<br>
(‘W:/2/med-ROI\images\165.jpg’,)<br>
(‘W:/2/med-ROI\images\166.jpg’,)<br>
(‘W:/2/med-ROI\images\167.jpg’,)<br>
(‘W:/2/med-ROI\images\168.jpg’,)<br>
(‘W:/2/med-ROI\images\169.jpg’,)<br>
(‘W:/2/med-ROI\images\17.jpg’,)<br>
(‘W:/2/med-ROI\images\170.jpg’,)<br>
(‘W:/2/med-ROI\images\171.jpg’,)<br>
(‘W:/2/med-ROI\images\172.jpg’,)<br>
(‘W:/2/med-ROI\images\173.jpg’,)<br>
(‘W:/2/med-ROI\images\174.jpg’,)<br>
(‘W:/2/med-ROI\images\175.jpg’,)<br>
(‘W:/2/med-ROI\images\176.jpg’,)<br>
(‘W:/2/med-ROI\images\177.jpg’,)<br>
(‘W:/2/med-ROI\images\178.jpg’,)<br>
(‘W:/2/med-ROI\images\179.jpg’,)<br>
(‘W:/2/med-ROI\images\18.jpg’,)<br>
(‘W:/2/med-ROI\images\180.jpg’,)<br>
(‘W:/2/med-ROI\images\181.jpg’,)<br>
(‘W:/2/med-ROI\images\182.jpg’,)<br>
(‘W:/2/med-ROI\images\183.jpg’,)<br>
(‘W:/2/med-ROI\images\184.jpg’,)<br>
(‘W:/2/med-ROI\images\185.jpg’,)<br>
(‘W:/2/med-ROI\images\186.jpg’,)<br>
(‘W:/2/med-ROI\images\187.jpg’,)<br>
(‘W:/2/med-ROI\images\188.jpg’,)<br>
(‘W:/2/med-ROI\images\189.jpg’,)<br>
(‘W:/2/med-ROI\images\19.jpg’,)<br>
(‘W:/2/med-ROI\images\190.jpg’,)<br>
(‘W:/2/med-ROI\images\191.jpg’,)<br>
(‘W:/2/med-ROI\images\192.jpg’,)<br>
(‘W:/2/med-ROI\images\193.jpg’,)<br>
(‘W:/2/med-ROI\images\194.jpg’,)<br>
(‘W:/2/med-ROI\images\195.jpg’,)<br>
(‘W:/2/med-ROI\images\196.jpg’,)<br>
(‘W:/2/med-ROI\images\197.jpg’,)<br>
(‘W:/2/med-ROI\images\198.jpg’,)<br>
(‘W:/2/med-ROI\images\199.jpg’,)<br>
(‘W:/2/med-ROI\images\2.jpg’,)<br>
(‘W:/2/med-ROI\images\20.jpg’,)<br>
(‘W:/2/med-ROI\images\200.jpg’,)<br>
(‘W:/2/med-ROI\images\201.jpg’,)<br>
(‘W:/2/med-ROI\images\202.jpg’,)<br>
(‘W:/2/med-ROI\images\203.jpg’,)<br>
(‘W:/2/med-ROI\images\204.jpg’,)<br>
(‘W:/2/med-ROI\images\205.jpg’,)<br>
(‘W:/2/med-ROI\images\206.jpg’,)<br>
(‘W:/2/med-ROI\images\207.jpg’,)<br>
(‘W:/2/med-ROI\images\208.jpg’,)<br>
(‘W:/2/med-ROI\images\209.jpg’,)</p>
<p>This are paths of how my images are getting loaded</p>

username: ptrblck
cooked: <p>I’m not sure if the sorting makes the trouble, but as you can see you would have to prepend zeros to the file names in case you want the sorting to be executed as:</p>
<pre><code class="lang-python">000.jpg
001.jpg
002.jpg
...
010.jpg
011.jpg
...
020.jpg
...
</code></pre>

username: premtibadiya
cooked: <p>Is there no other way? Because right now I have images in thousands then it can increase to tens of thousands then again I have to label all.</p>

username: premtibadiya
cooked: <p>I tried by creating my custom dataset loader from the csv file. But it is giving me some error.<br>
Code of the custom dataset loader.<br>
class CustomDataset(torch.utils.data.Dataset):</p>
<pre><code>def __init__(self, csv_path, images_folder, transform = None):

    self.df = pd.read_csv(csv_path)

    self.images_folder = images_folder

    self.transform = transform

def __len__(self):

    return len(self.df)

def __getitem__(self, index):

    filename = self.df[index, "Images"]

    image = PIL.Image.open(os.path.join(self.images_folder, filename))

    if self.transform is not None:

        image = self.transform(image)

    return image
</code></pre>
<p>I am getting this error:-<br>
KeyError: (0, ‘Images’)</p>