You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/08/07 11:38:57 UTC

[GitHub] prathik-naidu opened a new issue #12063: MxNet Pre-trained Prediction Doc Not Working

prathik-naidu opened a new issue #12063: MxNet Pre-trained Prediction Doc Not Working
URL: https://github.com/apache/incubator-mxnet/issues/12063
 
 
   ## Description
   The predict image doc page: [https://mxnet.incubator.apache.org/tutorials/python/predict_image.html](url) doesn't work right off the shelf (returns incorrect predictions).
   
   ## Problem
   The tutorial doesn't normalize the images – causing incorrect predictions to be made. For newcomers to MxNet/deep learning, I think this is a simple, but useful addition to add in.
   
   ## Solution
   Add in a color normalization in the get_image method as follows:
   
   ```python
   def get_image(url, show=False):
       # download and show the image. Remove query string from the file name.
       fname = mx.test_utils.download(url, fname=url.split('/')[-1].split('?')[0])
       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, 224, 224) # resize
       #---------------------------
       img = img.astype(float)/255
       img = mx.image.color_normalize(img,
                  mean=mx.nd.array([0.485, 0.456, 0.406]).astype(float), 
                  std=mx.nd.array([0.229, 0.224, 0.225]).astype(float)
       #---------------------------
       img = img.transpose((2, 0, 1)) # Channel first
       img = img.expand_dims(axis=0) # batchify
       return img
   ```
   
   
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services