'ResNetV1' object has no attribute 'classes'

Trained with ‘sh train_resnet18_v1.sh’ from
https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/logs/classification/imagenet/resnet18_v1.sh

Training completes but I can’t seem to use the generated model, but it
works with the ‘built-in’ params:

~/scratch/Gluon$ python3 demo_imagenet.py --model resnet18_v1 --input-pic ~/Pictures/sebastian-piton-goralski-gaudi3.jpg
The input picture is classified to be
[church], with probability 0.369.
[bell cote], with probability 0.351.
[castle], with probability 0.123.
[monastery], with probability 0.048.
[tile roof], with probability 0.029.
~/scratch/Gluon$ python3 demo_imagenet.py --model resnet18_v1 --input-pic ~/Pictures/sebastian-piton-goralski-gaudi3.jpg --saved-params params_resnet18_v1/0.2930-imagenet-resnet18_v1-118-best.params
The input picture is classified to be
Traceback (most recent call last):
File “demo_imagenet.py”, line 40, in
(net.classes[ind[i].asscalar()], nd.softmax(pred)[0][ind[i]].asscalar()))
AttributeError: ‘ResNetV1’ object has no attribute ‘classes’
~/scratch/Gluon$ python3 demo_imagenet.py --model resnet18_v1 --input-pic ~/Pictures/sebastian-piton-goralski-gaudi3.jpg --saved-params params_resnet18_v1/imagenet-resnet18_v1-119.params
The input picture is classified to be
Traceback (most recent call last):
File “demo_imagenet.py”, line 40, in
(net.classes[ind[i].asscalar()], nd.softmax(pred)[0][ind[i]].asscalar()))
AttributeError: ‘ResNetV1’ object has no attribute ‘classes’

What am I doing wrong ?

Hi,

you downloaded the demo_imagenet.py from this page right?
https://gluon-cv.mxnet.io/build/examples_classification/demo_imagenet.html#sphx-glr-build-examples-classification-demo-imagenet-py

It points to an old version of that script apparently, the most recent version looks not to have this bug anymore:
https://raw.githubusercontent.com/dmlc/gluon-cv/25b1717635803712193b28ed54ca4aa2c752f975/docs/tutorials/classification/demo_imagenet.py

hth,
Lieven

Yes as to the origin of the demo_imagenet.py script.

I assume you meant to point to https://github.com/dmlc/gluon-cv/blob/master/scripts/classification/imagenet/demo_imagenet.py as the docs/tutorials link is a prototype web page. My version of demo_imagenet.py does not differ from this file. I have also verified that the training script train_imagenet.py which I used is identical to the git version.

What is imagenet_labels.txt for, is it silently referred to during training ? None of the scripts appear to refer to it, but would absence of this file during training cause this issue ?

Strange, I had been looking at this version, which looks like the original commit:

Here you see that the classnames are loaded from imagenet_labels.txt and then passed as ‘classes’ argument to the model.

Not sure what happened in the meantime, but that demo script certainly isn’t working anymore.
L.

From that version I just pasted in the class_names initialization and replaced the net.classes reference with class_names, and this lets me get results.

Thank you!

FYI, the issue turns out to be that the Imagenet classnames are only loaded when we are using the pretrained parameters.

I have proposed a simple fix to the demo script here:

L.