|
| 1 | +import matplotlib.pyplot as plt |
| 2 | + |
| 3 | +from mxnet import gluon, nd, image |
| 4 | +from mxnet.gluon.data.vision import transforms |
| 5 | +from gluoncv import utils |
| 6 | +from gluoncv.model_zoo import get_model |
| 7 | + |
| 8 | +# Download and show the example image |
| 9 | +url = 'https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/classification/plane-draw.jpeg' |
| 10 | +im_fname = utils.download(url) |
| 11 | + |
| 12 | +img = image.imread(im_fname) |
| 13 | + |
| 14 | +plt.imshow(img.asnumpy()) |
| 15 | +plt.show() |
| 16 | + |
| 17 | +# Transform the image: |
| 18 | +transform_fn = transforms.Compose([ |
| 19 | + transforms.Resize(32), |
| 20 | + transforms.CenterCrop(32), |
| 21 | + transforms.ToTensor(), |
| 22 | + transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]) |
| 23 | +]) |
| 24 | + |
| 25 | +img = transform_fn(img) |
| 26 | +plt.imshow(nd.transpose(img, (1,2,0)).asnumpy()) |
| 27 | +plt.show() |
| 28 | + |
| 29 | +# Load a pre-trained model |
| 30 | +net = get_model('cifar_resnet110_v1', classes=10, pretrained=True) |
| 31 | + |
| 32 | +# Finally, prepare the image and feed it to the model |
| 33 | +pred = net(img.expand_dims(axis=0)) |
| 34 | + |
| 35 | +class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', |
| 36 | + 'dog', 'frog', 'horse', 'ship', 'truck'] |
| 37 | +ind = nd.argmax(pred, axis=1).astype('int') |
| 38 | +print('The input picture is classified as [%s], with probability %.3f.'% |
| 39 | + (class_names[ind.asscalar()], nd.softmax(pred)[0][ind].asscalar())) |
0 commit comments