0

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 must do something wrong.

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]))

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

This is my figure of cat which is downloaded from the ImageNet. 

enter image description here

These are my outputs.

probability=0.218258, class=n01519563 cassowary probability=0.172373, class=n01519873 emu, Dromaius novaehollandiae, Emu novaehollandiae probability=0.128973, class=n01521399 rhea, Rhea americana probability=0.105253, class=n01518878 ostrich, Struthio camelus probability=0.051424, class=n01517565 ratite, ratite bird, flightless bird

halfer
  • 19,824
  • 17
  • 99
  • 186
Blue Bird
  • 318
  • 2
  • 9

1 Answers1

0

Reading your code:

path=‘http://data.mxnet.io/models/imagenet-11k/’

I think you might be using the synset of the ImageNet 11k (11000 classes) rather than the 1k (1000) classes. That would explain the mismatch.

The correct synset is here: http://data.mxnet.io/models/imagenet/synset.txt

Thomas
  • 676
  • 3
  • 18
  • Thank you very much! Another mistake that I have made 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)) – Blue Bird May 19 '18 at 08:11
  • No problem, you can use `mx.image.imread` in mxnet to load an image as a NDArray directly. – Thomas May 19 '18 at 13:53