Targets attribute ImageFolder


#1

While trying to access the Targets attribute of the ImageFolder-

valdata = datasets.ImageFolder("../input/chest_xray/chest_xray/val", transform = transform)
valloader = torch.utils.data.DataLoader(valdata, shuffle = True, batch_size = 64 )

print(valdata.class_to_idx)
print(valdata.classes)
print(valdata.targets)
for i in valdata.samples:
    print(i)

I get this error-

{'NORMAL': 0, 'PNEUMONIA': 1}
['NORMAL', 'PNEUMONIA']
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-46-a7c7858221ef> in <module>()
     20 print(valdata.class_to_idx)
     21 print(valdata.classes)
---> 22 print(valdata.targets)
     23 for i in valdata.samples:
     24     print(i)

AttributeError: 'ImageFolder' object has no attribute 'targets'

#2

It looks like the targets attribute was added in this PR 9 months ago. Could you try to update torchvision and try your code again?


#3

Thanks @ptrblck

Actually, I was using Pytorch on a Kaggle Kernel.
Is there any permanent fix- so that I need not update the libraries every time I use a Kaggle Kernel?


#4

I’m not sure if Kaggle Kernels stay updated or if you would have to update certain packages in each run.
Could you try it out and let us know?

Alternatively, you might just get all targets using this line of code manually:

targets = [s[1] for s in valdata.samples]

#5

Thanks @ptrblck for help