2016-02-22 83 views
1

我正在嘗試在Python 3中爲R描述所有內容。但到目前爲止,我還沒有做進一步的工作。在Python中使用MXNet預先訓練的圖像分類模型

R中的教程描述如下:http://mxnet.readthedocs.org/en/latest/R-package/classifyRealImageWithPretrainedModel.html

我怎麼可以這樣做在Python?使用下面的模式: https://github.com/dmlc/mxnet-model-gallery/blob/master/imagenet-1k-inception-bn.md

親切的問候, 凱文

+0

僅供參考,將R文檔移動到這裏:http://mxnet.io/tutorials/r /classifyRealImageWithPretrainedModel.html – Leopd

回答

0

目前,您可以使用Python mxnet做更多的事情比使用R.我使用的膠子API,這使得編寫代碼更簡單,它允許加載預訓練模型。

您參考的教程中使用的模型是Inception model。所有可用的預訓練模型的列表可以找到here

本教程的其餘操作是數據標準化和擴充。你可以做一些類似他們如何規範它的API頁面上的新數據的標準化:

image = image/255 
normalized = mx.image.color_normalize(image, 
             mean=mx.nd.array([0.485, 0.456, 0.406]), 
             std=mx.nd.array([0.229, 0.224, 0.225])) 

可能增強的名單可here

這是您可運行的示例。我也只有一個擴充,如果你想要做他們的越多,你可以添加更多的參數mx.image.CreateAugmenter

%matplotlib inline 
import mxnet as mx 
from mxnet.gluon.model_zoo import vision 
from matplotlib.pyplot import imshow 

def plot_mx_array(array, clip=False): 
    """ 
    Array expected to be 3 (channels) x heigh x width, and values are floats between 0 and 255. 
    """ 
    assert array.shape[2] == 3, "RGB Channel should be last" 
    if clip: 
     array = array.clip(0,255) 
    else: 
     assert array.min().asscalar() >= 0, "Value in array is less than 0: found " + str(array.min().asscalar()) 
     assert array.max().asscalar() <= 255, "Value in array is greater than 255: found " + str(array.max().asscalar()) 
    array = array/255 
    np_array = array.asnumpy() 
    imshow(np_array) 


inception_model = vision.inception_v3(pretrained=True) 

with open("/Volumes/Unix/workspace/MxNet/2018-02-20T19-43-45/types_of_data_augmentation/output_4_0.png", 'rb') as open_file: 
    encoded_image = open_file.read() 
    example_image = mx.image.imdecode(encoded_image) 
    example_image = example_image.astype("float32") 
    plot_mx_array(example_image) 


augmenters = mx.image.CreateAugmenter(data_shape=(1, 100, 100)) 

for augementer in augmenters: 
    example_image = augementer(example_image) 

plot_mx_array(example_image) 

example_image = example_image/255 
normalized_image = mx.image.color_normalize(example_image, 
             mean=mx.nd.array([0.485, 0.456, 0.406]), 
             std=mx.nd.array([0.229, 0.224, 0.225])) 
相關問題