Hello guys,
I am trying to predict from an URL using the method from pretrained model for resnet (https://mxnet.incubator.apache.org/tutorials/python/predict_image.html). I modified the code to fit my parameters from the trained dataset.
The code is below:
Load the model:
symbol, arg_params, aux_params = mx.model.load_checkpoint(‘food_classification_resnet-18’,30)
net = mx.mod.Module(symbol=symbol, context=ctx)
net.bind(for_training=False, data_shapes=[(‘data’, (1,3,299,299))],
label_shapes=train_itr.provide_label)
net.set_params(arg_params, aux_params)
Predict:
%matplotlib inline
import urllib.request
from collections import namedtuple
Batch = namedtuple(‘Batch’, [‘data’])
with open(‘classes.txt’, ‘r’) as f:
labels = [l.rstrip() for l in f]
def get_image(url, show=False):
# download and show the image
fname = mx.test_utils.download(url)
img = mx.image.imread(fname)
if img is None:
return None
if show:
plt.imshow(img.asnumpy())
plt.axis(‘off’)
# convert into format (batch, RGB, width, height)
img = mx.image.imresize(img, 299, 299) # resize
img = img.transpose((1,2,0)) # Channel first
img = img.expand_dims(axis=0) # batchify
return img
def predict(url):
img = get_image(url, show=True)
net.forward(Batch([img]))
prob = net.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' %(prob[i], labels[i]))
The error I am getting is:
TypeError Traceback (most recent call last)
in ()
----> 1 predict(‘http://www.sakinahalalgrill.com/wp-content/uploads/2018/04/chicken-samosa.jpg’)
in predict(url)
32 a = np.argsort(prob)[::-1]
33 for i in a[0:5]:
—> 34 print(‘probability: %f’ %(prob[i], labels[i]))
TypeError: not all arguments converted during string formatting.
Can I get help on this error. Really appreciate any help with this error.
Srinivas