The built-in VGG16 network is not working. Please, help me


#1

I would like to test the trained built-in VGG16 network in MxNet. The experiment is to feed the network with an image from ImageNet. Then, I would like to see whether the result is correct. However, the results are always error! Hi, how stupid the network is! Well, that cannot be true. I am stupid, and I must do something wrong. Can you help me?

from mxnet.gluon.model_zoo.vision import vgg16

from mxnet.image import color_normalize
import mxnet as mx

import numpy as np
import cv2

path=‘http://data.mxnet.io/models/imagenet-11k/
data_dir = ‘F:/Temps/Models_tmp/’
k = ‘synset.txt’

#gluon.utils.download(path+k, data_dir+k)

img_dir = ‘F:/Temps/DataSets/ImageNet/’

img = cv2.imread(img_dir + ‘cat.jpg’)
img = mx.nd.array(img)
img,_ = mx.image.center_crop(img,(224,224))

img = img/255
img = color_normalize(img,mean=mx.nd.array([0.485, 0.456, 0.406]),std=mx.nd.array([0.229, 0.224, 0.225]))
img = mx.nd.transpose(img, axes=(2, 0, 1))
img = img.expand_dims(axis=0)

with open(data_dir + ‘synset.txt’, ‘r’) as f:
labels = [l.rstrip() for l in f]

aVGG = vgg16(pretrained=True,root=‘F:/Temps/Models_tmp/’)
features = aVGG.forward(img)
features = mx.ndarray.softmax(features)

features = features.asnumpy()

features = np.squeeze(features)

a = np.argsort(features)[::-1]
for i in a[0:5]:
print(‘probability=%f, class=%s’ %(features[i], labels[i]))

cat


#2

The outputs from color_normalize seems not right for the absolute values of some numbers are greater than one.


#3

The first mistake is that I have used the wrong file ‘synset.tx’. It should be one located at ‘http://data.mxnet.io/models/imagenet/synset.txt’.
Another one is to import a BGR data rather than an RGB data.
img = cv2.imread(img_dir + ‘cat.jpg’)
It should be:
_img = cv2.imread(img_dir + ‘n01751748_838.JPEG’) _
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
or
img = plt.imread(img_dir + ‘n01751748_838.JPEG’)
It would be better if there is something like ‘image.load_img’ as we do in keras.
img = image.load_img(img_path + ‘cat.jpg’, target_size=(224, 224))


#4

Thanks BluebirdHouse for sharing your findings, glad you found a solution. FYI There is mx.image.imread that let’s you do what you just described.