I wrote a following script to predict image label for resent 18.
import mxnet as mx
model_name = 'resnet-18'
path='http://data.mxnet.io/models/imagenet/resnet/'
[mx.test_utils.download(path+'18-layers/resnet-18-symbol.json'),
mx.test_utils.download(path+'18-layers/resnet-18-0000.params'),
mx.test_utils.download(path+'synset.txt')]
sym, arg_params, aux_params = mx.model.load_checkpoint(model_name, 0)
mod = mx.mod.Module(symbol=sym, context=mx.cpu(), label_names=None)
mod.bind(for_training=False, data_shapes=[('data', (1,3,224,224))],
label_shapes=mod._label_shapes)
mod.set_params(arg_params, aux_params, allow_missing=True)
with open('synset.txt', 'r') as f:
labels = [l.rstrip() for l in f]
%matplotlib inline
import matplotlib.pyplot as plt
import cv2
import numpy as np
# define a simple data batch
from collections import namedtuple
Batch = namedtuple('Batch', ['data'])
def get_image(url, show=False):
# download and show the image
fname = mx.test_utils.download(url)
img = cv2.cvtColor(cv2.imread(fname), cv2.COLOR_BGR2RGB)
if img is None:
return None
if show:
plt.imshow(img)
plt.axis('off')
# convert into format (batch, RGB, width, height)
img = cv2.resize(img, (224, 224))
img = np.swapaxes(img, 0, 2)
img = np.swapaxes(img, 1, 2)
img = img[np.newaxis, :]
return img
def predict(url):
img = get_image(url, show=True)
# compute the predict probabilities
mod.forward(Batch([mx.nd.array(img)]))
prob = mod.get_outputs()[0].asnumpy()
# print the top-5
prob = np.squeeze(prob)
a = np.argsort(prob)[::-1]
for i in a[0:5]:
print('probability=%f, class=%s' %(prob[i], labels[i]))
predict('http://writm.com/wp-content/uploads/2016/08/Cat-hd-wallpapers.jpg')
Output
probability=0.244390, class=n01514668 cock
probability=0.170342, class=n01514752 gamecock, fighting cock
probability=0.145019, class=n01495493 angel shark, angelfish, Squatina squatina, monkfish
probability=0.059832, class=n01540233 grosbeak, grossbeak
probability=0.051555, class=n01517966 carinate, carinate bird, flying bird
I am getting completly wrong output.
Same code I tried with Resent 152., Output I got is correct
probability=0.692327, class=n02122948 kitten, kitty
probability=0.043847, class=n01323155 kit
probability=0.030002, class=n01318894 pet
probability=0.029693, class=n02122878 tabby, queen
probability=0.026972, class=n01322221 baby