Image Folder with no subfolders

Ayshine: <p>Hi,<br>
I am trying to load images from a folder with no subfolders and having the error below. When I try to read an image from the folder I can load .jpg file with no error. My file format meets the pytorch image format criteria. Also, transforms.Compose just works fine (this must mean nothing wrong with my pytorch installation) Is there any other way to load image data or anything I can do to solve this error.<br>
Thanks in advance!<br>
Ayshine</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/4/41f8b5479c899d2c5981524aa9329be4a45813dd.png" data-download-href="https://discuss.pytorch.org/uploads/default/41f8b5479c899d2c5981524aa9329be4a45813dd" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/4/41f8b5479c899d2c5981524aa9329be4a45813dd.png" alt="image" data-base62-sha1="9pBTcdZWAm0U3YSWOPIT4VljnyJ" width="690" height="340" data-dominant-color="F6F7F7"><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">image</span><span class="informations">1037×512 30.3 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>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/e/efccb6c51365593d1b47580477eb0319e1e33eb3.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/efccb6c51365593d1b47580477eb0319e1e33eb3" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/e/efccb6c51365593d1b47580477eb0319e1e33eb3_2_690x466.jpeg" alt="image" data-base62-sha1="ydmIcVCcy618w3Mp58sJcijgWk3" width="690" height="466" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/e/efccb6c51365593d1b47580477eb0319e1e33eb3_2_690x466.jpeg, https://discuss.pytorch.org/uploads/default/original/2X/e/efccb6c51365593d1b47580477eb0319e1e33eb3.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/e/efccb6c51365593d1b47580477eb0319e1e33eb3.jpeg 2x" data-dominant-color="D8D8DB"><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">image</span><span class="informations">721×487 125 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>

ptrblck: <p><code>torchvision.datasets.ImageFolder</code> expects subfolders representing the classes containing images of the corresponding class.<br>
If you just would like to load a single image, you could load it with e.g. <code>PIL.Image.open</code> and pass it to your transform.<br>
However, if you don’t want to change your code, just move your image to a subfolder and <code>ImageFolder</code> should work.</p>

Ayshine: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>Unfortunately I need to load 25000 image dataset. I had a couple of issues about labels, too. May be I can write a script to arrange them in folders.</p>

ptrblck: <p>How are the images labeled at the moment? Are the labels somehow written into the file names or do you have a separate list with the image paths and the corresponding label?</p>

Ayshine: <p>There are couple of label files that are all in .mat file. I couldn’t find the way to open them so started converting two relevant files to .csv files. One of those files labels images if there is one person or not and the other one classifies them in terms of main activity and the sub activity.</p>

ptrblck: <p>You can load <code>.mat</code> files with <a href="https://docs.scipy.org/doc/scipy/reference/tutorial/io.html">scipy.io.loadmat</a>.<br>
If you could create one .csv file containing the image path and both labels, we could write a <code>Dataset</code> and load all corresponding images and labels using this file without moving your data.</p>

Ayshine: <p>That would be great! Let me try to merge them all in one .csv then come back here.</p>

ptrblck: <p>Sure! If you’ve created the .csv, could you post a few lines of the file so that we can write a dummy <code>Dataset</code> using your format?<br>
Also, let me know, if you need some help creating the .csv from your MATLAB files.</p>

Ayshine: <p>Thanks for the offer helping with MATLAB files. Now, my problem became more about reading MATLAB files. I am working on this dataset: <a href="http://human-pose.mpi-inf.mpg.de/#download" class="inline-onebox" rel="noopener nofollow ugc">MPII Human Pose Database</a><br>
If you are also familiar with the paper I would really appreciate your recommendations about how to feed this dataset to a deep learning model built using pytorch.I could fınd the fırst ımage file name as follows.I’ll try to find a way to retrieve the rest.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/2X/5/5f6574e712e232ac81c6e74d0183a8388fa007d3.png" alt="image" data-base62-sha1="dBUJerHx9PdaoDXCfPNDWlolSEj" width="593" height="141"></p>

ptrblck: <p>I had a look at the labels of the dataset, and it seems <code>loadmat</code> wraps the data in a ton of unnecessary nested arrays. I’ll try to load it with Octave and see how far I come.</p>

Ayshine: <p>Oh, Thanks! I managed to gather all the important ınfo to one CSV file. Here is a couple of lines.</p>
<blockquote>
<p>image_path,train_test,category,activity,single_person</p>
<p>029122914.jpg,1,‘occupation’, ‘truck driving, loading and unloading truck, tying down load, standing, walking and carrying heavy lo’,1<br>
061185289.jpg,1,‘occupation’, ‘truck driving, loading and unloading truck, tying down load, standing, walking and carrying heavy lo’,1<br>
013949386.jpg,1,‘occupation’, ‘truck driving, loading and unloading truck, tying down load, standing, walking and carrying heavy lo’,1<br>
029214465.jpg,1,‘occupation’, ‘truck driving, loading and unloading truck, tying down load, standing, walking and carrying heavy lo’,1</p>
</blockquote>

ptrblck: <p>Awesome!<br>
Did you manage to create the <code>Dataset</code>?<br>
If not, could you explain the format the the labels a bit?<br>
The first column obviously contains the image names. Which column refers to the labels and what kind of ino do you need for your training?</p>

Ayshine: <p>Sorry, I should’ve been more clear</p>
<p><strong>image_path</strong>: the name of the images<br>
<strong>train_test</strong>: if 1 the image is training data 0 the image is<br>
<strong>category</strong>: the main category of the action<br>
<strong>activity</strong>: detailed activity of the person or people in the image<br>
<strong>single_person</strong>: contains rectangle id  <code>ridx</code>  of  <em>sufficiently separated</em>  individuals (this explanation is from the database page I haven’t figured out how to use this information)</p>
<p>It looks like test data does not have any category, activity or single_person information. After filtering train data I no longer need this column. For my initial model, I only take <strong>category</strong> column as the label but <strong>single_person</strong> information is also crucial for my use case. I might also drop the <strong>activity</strong> column  In Sum, for my ultimate goal is a binary decision between the states below:</p>
<ol>
<li>There is one person on the image doing a regular activity</li>
<li>There are multiple people doing something else or The figure in the image does not belong to one person doing a regular activity.</li>
</ol>
<p>I might be heading nowhere by using this dataset, but I’d like to share my use case to explain more clearly why I chose the dataset in the first place.</p>

ptrblck: <p>OK, I see.<br>
Could you save the .csv with <code>;</code> as a separator, as this will make the reading a bit easier, since <code>activity</code> uses <code>,</code> inside the text?</p>
<p>Let’s make sure I understand your use case.<br>
The “regular activity” is defined by <code>category</code>? If so, I assume you would like to group the activities into regular and not regular. Which categories would belong to which class?<br>
Could you post an example for each case?</p>
<p>If <code>single_person</code> is set to 0, we know, that there are more than one person in the image.<br>
How do you know the person in the image is not doing the regular activity?</p>

Ayshine: <p>Now, I saved the file with <code>;</code> separator. The three most important information I have image_path, category and single_person. After a little further reading single person has a list having ids of the coordinates of the head rectangle. If 0 then no one in the image, else if 1 there is one person, else more there are more people. I can use this column as my label and look for if there is one person in the image or not then use category as further info. In this case, I have 3 distinct labels.</p>
<pre><code class="lang-auto">image_path,train_test,category,single_person
030424224.jpg;1;miscellaneous;[1,2]
052475643.jpg;1;miscellaneous;[1,2]
</code></pre>

Ayshine: <p>I decided to come up with a model have classes 0, 1 or 2 (2 for 2 or more people) and moved my images to data folder using this code.It took seconds</p>
<pre><code class="lang-auto">i = 0
import shutil
from pathlib import Path

for i in range(len(filenames)):
    if train_test[i] != '0':
        my_file = Path('images/'+ filenames[i])
        if my_file.is_file():
            if len(sps[i]) == 0:
                shutil.copyfile('images/'+ filenames[i], 'data/0/'+filenames[i]) 
            elif len(sps[i]) == 1:
                shutil.copyfile('images/'+ filenames[i], 'data/1/'+filenames[i]) 
            else :
                shutil.copyfile('images/'+ filenames[i], 'data/2/'+filenames[i]) 
</code></pre>
<p>removed couple of things so my data in .csv file looks like this</p>
<pre><code class="lang-auto">015601864.jpg;1;sports;curling;12
015599452.jpg;1;sports;curling;3
</code></pre>
<p>Writing this if it would help someone else too and marking this reply as solution.</p>

Steven_Mugisha: <p>Considering that testing images are in most cases not categorized into classes as train images are, does pytorch have a way of loading such images or do I have to write a custom loop to load all of them.</p>

ptrblck: <p>A custom <code>Dataset</code> would probably be “cleaner”, but you could also load all images from a single folder and just discard the target.</p>