Skip to content

Instantly share code, notes, and snippets.

@nervetumer
Last active May 10, 2016 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nervetumer/a66cb01b9055351a87e959bf16fb473c to your computer and use it in GitHub Desktop.
Save nervetumer/a66cb01b9055351a87e959bf16fb473c to your computer and use it in GitHub Desktop.
example of running inference on an image using neon

This is an example on how to take a trained model from the neon Model Zoo model and apply it to custom data.

This example works with the Alexnet image classification model in the neon Model Zoo. The readme on that same page has a link to the trained Alexnet model file. Download those trained weights to your local directory.

This script requires the neon, PIL, scipy and numpy are installed in the system. neon must be installed and if it was installed into a virtualenv the virtualenv must be activated. An example jpeg file with an image of a dog (german short hair pointer) was used here and can be download from this link. In order to get the class names for the predictions the meta data from the ILSVCR2012 dev kit will be needed.

To run this model through neon, the image must be put into BGR order, the channel means from the trianing image set for each color channel need to be subtracted, and the image array needs to be swapped from (H, W, C) to (C, H, W) and flattened.

#!/usr/bin/env python
# ----------------------------------------------------------------------------
# Copyright 2015 Nervana Systems Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------
from neon.backends import gen_backend
from neon.util.persist import load_obj
from neon.models.model import Model
import Image
import PIL.ImageOps as ImageOps
import numpy as np
# download image from https://s3-us-west-1.amazonaws.com/nervana-modelzoo/example_images/german_pointer.jpg
#load image
jpgfile = Image.open("german_pointer.jpg")
# reshape to be 224 x 224
im_rs = ImageOps.fit(jpgfile, (224, 224))
#preprocess the image
# put into BGR order
im = np.array(im_rs)
im = im[:, :, ::-1]
# subtract the I1K means (see the macrobatch meta data file)
# R_mean 104.412277
# G_mean 119.213318
# B_mean 126.806091
im = im - np.array([126.806091, 119.213318, 104.412277])
# reorder to be (C, H, W) and flatten
im = im.transpose((2, 0, 1)).flatten()
# create a neon backend, using 32 images per minibatch here
be = gen_backend(backend='gpu', batch_size=32)
# make an image buffer on host, pad out to 32 images since batch size is 32
host_buf = np.zeros((3*224*224, be.bsz))
# set the first image to be the jpeg data loaded above
host_buf[:, 0] = im.copy()
dev_buf = be.zeros((3*224*224, be.bsz))
# copy images to device buffer
dev_buf[:] = host_buf
# load up the neon model
model_dict = load_obj('alexnet.p')
model = Model(model_dict)
# set up model buffers by ginving the input image shape
# here that would be (3, 224, 224)
model.initialize(model_dict['train_input_shape'])
out = model.fprop(dev_buf).get()
out = out[:,0] # get the predictions for all 1000 classes for the first image
# find the top-5 categories
catg = np.argsort(out)[-5:][::-1]
# load the synsets from the ILSVCR 2012 dev kit
# there may be a nicer way to load these:
# may need to add the path to the devkit
from scipy.io import loadmat
meta = loadmat('ILSVRC2012_devkit_t12/data/meta.mat')
#get the class names
names = [meta['synsets'][ind][0][2][0] for ind in catg]
print 'Top 5 classes for this image:'
print names
# for the record this dog is part German shorthair pointer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment