How to Load images in pytorch

mudit: <p>i am new to pytorch i have one folder which contain train.csv file and train folder train.csv contain image name with corresponding labels and train contains images how to load the images and then train the model.</p>

ptrblck: <p>You could create a custom <code>Dataset</code> as explained in <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html">this tutorial</a>.<br>
In the <code>Dataset.__init__</code> method you could load the corresponding <code>csv</code> file and load each sample in <code>__getitem__</code> lazily. To do so, you could index the <code>csv</code> file (e.g. via a <code>pd.DataFrame</code>), load (and transform) the corresponding image and create the target tensor.</p>
<p>Let me know, if you get stuck somewhere.</p>

mudit: <p>sir my train.csv is in this form<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/3/f/3fd818f2447ef6b6af555daa85824398ff9e5b76.png" alt="Screenshot from 2020-05-25 22-41-18" data-base62-sha1="96N4aDkdADz1Wnp5iAniby3O1dI" width="319" height="303"><br>
do i need to convert target to one hot vector?<br>
also what is idx in <strong>getitem(idx)</strong>?</p>

ptrblck: <p>If you are working on a multi-class classification, the targets should be the class indices and should not be one-hot encoded.<br>
E.g. if your use case uses 5 targets, the valid values would be <code>[0, 1, 2, 3, 4]</code>.</p>
<p>For your input data, you could use a mapping, such that e.g. <code>manipuri</code> maps to <code>0</code>, <code>odissi</code> maps to <code>1</code>, etc.</p>
<p>The <code>Dataset.__getitem__(self, index)</code> method is called by the <code>DataLoader</code> with an index for each sample in the range <code>[0, len(dataset)]</code> and is responsible to load and return the sample for the current index.</p>

mudit: <p>sir after all the loading i trained the model the no of images in train dataset is 364 only. so i want to keep all images corresponding to different classes equally in my train dataset and validation dataset . how can i do that? i am currently using mobilenet_v2 as model since images are less should i write my own model or using this model what  can i do to increase accuracy?</p>

ptrblck: <p>364 images are not that many and your model might overfit quickly, especially since you would need to split this dataset into a training, validation, and test set.</p>
<p>You could try to use an aggressive data augmentation and observe the validation loss to make sure the model still generalizes well.</p>
<p>I don’t think that a custom model trained from scratch would be easier, so your best bet might be to try to fine tune a pretrianed model, add data augmentation, maybe increase the regularization, or in the best case collect more data.</p>

mudit: <p>how to stratify my image dataset so that my train and validation dataset has equal weightage of classes(i have 8 classes every class must be there in train as well as validation with equal ratio).<br>
Thanks for your help</p>

ptrblck: <p>You could use <code>sklearn.model_selection.train_test_split</code> with the <code>stratify</code> argument.<br>
This would return indices for the training and validation dataset, which you could then pass to a <code>Subset</code> or <code>SubsetRandomSampler</code>.</p>