import mxnet as mx
import numpy as np
from skimage import io, transform
prefix="model/mnist-0"
num_round=2
model = mx.model.FeedForward.load(prefix, num_round, ctx=mx.cpu(), numpy_batch_size=1)
mean_img = np.load("mean_img.npy")
mean_img = np.swapaxes(mean_img, 0, 2)
mean_img = np.swapaxes(mean_img, 1, 2)
def PreprocessImage(path):
img=io.imread(path)
short_egde = min(img.shape[:2])
yy = int((img.shape[0] - short_egde)/2)
xx = int((img.shape[1] - short_egde)/2)
crop_img = img[yy: yy+short_egde, xx:xx+short_egde]
resized_img = transform.resize(crop_img, (224, 224))
sample = np.asarray(resized_img)*256
sample = np.swapaxes(sample, 0, 2)
sample = np.swapaxes(sample, 1, 2)
normed_img = sample - mean_img
print normed_img
print normed_img.shape
normed_img = normed_img.reshape(1, 3, 224, 224)
return normed_img
batch = PreprocessImage("1999.jpg")
prob = model.predict(batch)[0]
print prob
~