sad- commented on issue #10334: How to subtract mean during prediction with 
pretrained model (python)
URL: 
https://github.com/apache/incubator-mxnet/issues/10334#issuecomment-406034046
 
 
   hi @johnbroughton2017 ,
   It looks like in the c++ example the mean image data was loaded from a file. 
See this 
[line](https://github.com/apache/incubator-mxnet/blob/master/example/image-classification/predict-cpp/image-classification-predict.cc#L195)
 and this 
[snippet](https://github.com/apache/incubator-mxnet/blob/master/example/image-classification/predict-cpp/image-classification-predict.cc#L238-L255).
   
   If you have a saved mean image file, you can load it in during prediction 
using the [load method 
](https://mxnet.incubator.apache.org/api/python/ndarray/sparse.html#mxnet.ndarray.load)
 and depending on the dimensions of the mean array you can subtract it from the 
image before or after the resize step. i.e
   
   `img_mean = mx.ndarray.load("path/to/img_mean_file/")[0]`
   `img = img - img_mean #ensure that img and img_mean have the compatible 
dimensions`
   `img = mx.image.imresize(img, 224, 224) # resize`
   `img = img.transpose((2, 0, 1)) # Channel first`
   `img = img.expand_dims(axis=0) # batchify`
   
   Alternatively, you can declare a mean array using something like:
   `img_mean = mx.ndarray.array([mean_r, mean_g, mean_b]).reshape(3, 1, 1)`
    
   Where, mean_r, mean_g, mean_b are the means for each channel.
   
   Then, you can continue with 
   
   `img = img - img_mean`
   `img = mx.image.imresize(img, 224, 224) # resize`
   `img = img.transpose((2, 0, 1)) # Channel first`
   `img = img.expand_dims(axis=0) # batchify`
   
   as needed.
   

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to