# Questions about ImageFolder

**URL:** <https://discuss.pytorch.org/t/questions-about-imagefolder/774>\
**Category:** Uncategorized\
**Created:** [February 28, 2017, 3:03am UTC](https://discuss.pytorch.org/t/questions-about-imagefolder/774 "2017-02-28T03:03:55Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![Alan\_Wu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alan_wu/32/13846_2.png) [@Alan\_Wu](https://discuss.pytorch.org/u/Alan_Wu)\
**Post date:** [July 8, 2019, 6:10am UTC](https://discuss.pytorch.org/t/questions-about-imagefolder/774/22 "2019-07-08T06:10:31Z")

</div>

I figured it out:  
dataset.class\_to\_idx  
from: [https://pytorch.org/docs/stable/\_modules/torchvision/datasets/folder.html#ImageFolder](https://pytorch.org/docs/stable/_modules/torchvision/datasets/folder.html#ImageFolder)

---

<div class="post-metadata">

**Author:** ![Samuel\_T](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/samuel_t/32/12220_2.png) [@Samuel\_T](https://discuss.pytorch.org/u/Samuel_T)\
**Post date:** [November 14, 2019, 9:18pm UTC](https://discuss.pytorch.org/t/questions-about-imagefolder/774/23 "2019-11-14T21:18:59Z")

</div>

Im new to Pytorch so excuse the amateur question: I have a number of .pmg images that are located within my ImageFolde.@ncullen93 When I try to follow your steps to iterate through the image I get the following error " **OSError** : cannot identify image file \<\_io.BufferedReader name=‘C:/…/faces\abc\abc\_left\_angry\_open.pgm’\>". Is this because of the pmg format? Majority of support is around jpeg so Im wondering if this is the issue. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![shwetank](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shwetank](https://discuss.pytorch.org/u/shwetank)\
**Post date:** [November 22, 2019, 5:51am UTC](https://discuss.pytorch.org/t/questions-about-imagefolder/774/24 "2019-11-22T05:51:02Z")

</div>

Hello, Actually, there are two folder on kaggle on path “’…/input/aptos2019-blindness-detection/”, train\_images and test\_images, and imageFolder is loading the all images from both folder but i want dataset from train folder only, please help how can i achieve this?  
if i give path to tain\_images, it gives error i.e:

RuntimeError: Found 0 files in subfolders of: …/input/aptos2019-blindness-detection/train\_images  
Supported extensions are: .jpg,.jpeg,.png,.ppm,.bmp,.pgm,.tif,.tiff,.webp

but if i give path: “…/input/aptos2019-blindness-detection/”, it reads images from both folder.

i can’t even create a new folder because data is read only in kaggle

---

<div class="post-metadata">

**Author:** ![aniket132](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@aniket132](https://discuss.pytorch.org/u/aniket132)\
**Post date:** [October 3, 2020, 12:38pm UTC](https://discuss.pytorch.org/t/questions-about-imagefolder/774/26 "2020-10-03T12:38:53Z")

</div>

Hi, I have a dataset with following format -  
Root\_Folder  
Class\_1  
Sub\_class1  
img1.tiff  
img2.tiff  
…  
Sub\_class2  
img3.tiff  
…  
Sub\_class3  
.  
.  
.  
class2  
Sub\_class1  
.  
.  
And so on. When I use ImageFolder, I can get the label of parent classes.  
How do I get labels for subclasses as well?  
Do I need to build by own Custom Dataset?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 4, 2020, 9:01am UTC](https://discuss.pytorch.org/t/questions-about-imagefolder/774/27 "2020-10-04T09:01:35Z")

</div>

> [@aniket132](#):
>
> Do I need to build by own Custom Dataset?

Yes, this would be the best approach, since `ImageFolder` creates labels for the subfolders only (not their subfolders).  
To do so you could reuse the source code of `ImageFolder` if possible.

---

<div class="post-metadata">

**Author:** ![Abhiraj\_Kanse](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/abhiraj_kanse/32/32483_2.png) [@Abhiraj\_Kanse](https://discuss.pytorch.org/u/Abhiraj_Kanse)\
**Post date:** [December 18, 2020, 11:57am UTC](https://discuss.pytorch.org/t/questions-about-imagefolder/774/28 "2020-12-18T11:57:14Z")

</div>

Hi,  
I was using Image folder to create the basic cat versus dog data set from kaggle for classification but in my case the function is assigning a class for “1” to both cat and dog. I believe I have followed the convention correctly attaching the code for reference.

```auto
train_dataset = torchvision.datasets.ImageFolder('./drive/My Drive/DDP/catvdog/Data/n_train', transform=transform_train)
test_dataset = torchvision.datasets.ImageFolder('./drive/My Drive/DDP/catvdog/Data/n_test', transform=transform_test)
train_loader = DataLoader(train_dataset, batch_size=4,
                        shuffle=True, num_workers=0)
test_loader = DataLoader(test_dataset, batch_size=4,
                        shuffle=False, num_workers=0)

```

![image](https://discuss.pytorch.org/uploads/default/original/3X/f/1/f1e3a21eeba4d19bdb5c7698f3911776ab77924e.png)  
Above is my directory path.  
I don’t know what the issue is. ?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [December 19, 2020, 7:33am UTC](https://discuss.pytorch.org/t/questions-about-imagefolder/774/29 "2020-12-19T07:33:20Z")

</div>

Could you `print(train_dataset.class_to_idx)` and check the output?  
The paths look correct and `ImageFolder` so I’m unsure why both folders get the same class index.

[Previous page](https://discuss.pytorch.org/t/questions-about-imagefolder/774.md?page=1)
