From ae9c48af5cc380522f0f000e456f589937638534 Mon Sep 17 00:00:00 2001 From: guosheng Date: Thu, 1 Jun 2017 11:27:44 +0800 Subject: [PATCH] add caffe_predict to test --- image_classification/caffe2paddle.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/image_classification/caffe2paddle.py b/image_classification/caffe2paddle.py index 7c571e28..9157a32a 100644 --- a/image_classification/caffe2paddle.py +++ b/image_classification/caffe2paddle.py @@ -205,10 +205,35 @@ class ModelConverter(object): self.params[file_name] = (param_conf, data.flatten()) return name + def caffe_predict(self, + img, + mean_file='./caffe/imagenet/ilsvrc_2012_mean.npy'): + net = self.net + + mu = np.load(mean_file) + mu = mu.mean(1).mean(1) + + transformer = caffe.io.Transformer({ + 'data': net.blobs['data'].data.shape + }) + transformer.set_transpose('data', (2, 0, 1)) + transformer.set_mean('data', mu) + transformer.set_raw_scale('data', 255) + transformer.set_channel_swap('data', (2, 1, 0)) + im = caffe.io.load_image(img) + + net.blobs['data'].data[...] = transformer.preprocess('data', im) + out = net.forward() + + output_prob = net.blobs['prob'].data[0].flatten() + print np.sort(output_prob)[::-1] + print np.argsort(output_prob)[::-1] + print 'predicted class is:', output_prob.argmax() + if __name__ == "__main__": converter = ModelConverter("./VGG_ILSVRC_16_layers_deploy.prototxt", "./VGG_ILSVRC_16_layers.caffemodel", - "/Users/baidu/caffe/caffe/python/paddle_model", "test_vgg16.tar.gz") converter.convert() + converter.caffe_predict(img='./caffe/examples/images/cat.jpg') -- GitLab