From 3225174de689bcd3b41c7c740a0bac74f788ceb0 Mon Sep 17 00:00:00 2001 From: TobeyQin Date: Sun, 11 Feb 2018 14:08:32 +0800 Subject: [PATCH 1/2] add caffe inference test file: imagenet_test.py --- mmdnn/conversion/caffe/caffe_emitter.py | 9 +++++---- mmdnn/conversion/examples/imagenet_test.py | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mmdnn/conversion/caffe/caffe_emitter.py b/mmdnn/conversion/caffe/caffe_emitter.py index 0b0ced67..de22ce64 100644 --- a/mmdnn/conversion/caffe/caffe_emitter.py +++ b/mmdnn/conversion/caffe/caffe_emitter.py @@ -42,7 +42,6 @@ def header_code(self): from caffe import to_proto from six import text_type as _text_type -n = caffe.NetSpec() __weights_dict = dict() @@ -59,14 +58,15 @@ def load_weights(weight_file): def KitModel(weight_file = None): - + n = caffe.NetSpec() """ @property def end_code(self): - return """ + return """ return n + def make_net(prototxt): - KitModel() + n = KitModel() with open(prototxt, 'w') as fpb: print(n.to_proto(), file=fpb) @@ -89,6 +89,7 @@ def gen_weight(weight_file, model, prototxt): if 'bias' in __weights_dict[key]: net.params[key][1].data.flat = __weights_dict[key]['bias'] net.save(model) + return net diff --git a/mmdnn/conversion/examples/imagenet_test.py b/mmdnn/conversion/examples/imagenet_test.py index 198babc9..53a97338 100644 --- a/mmdnn/conversion/examples/imagenet_test.py +++ b/mmdnn/conversion/examples/imagenet_test.py @@ -15,6 +15,7 @@ class TestKit(object): truth = { 'caffe' : { 'alexnet' : [(821, 0.25088307), (657, 0.20857951), (744, 0.096812263), (595, 0.066312768), (847, 0.053720973)], + #'alexnet' : [(657, 0.41121086), (744, 0.20789686), (847, 0.086725488), (821, 0.059082959), (595, 0.058017101)], 'vgg19' : [(21, 0.37522122), (144, 0.28500062), (23, 0.099720284), (134, 0.036305398), (22, 0.033559237)], 'inception_v1' : [(21, 0.93591732), (23, 0.037170019), (22, 0.014315935), (128, 0.005050648), (749, 0.001965977)], 'resnet152' : [(144, 0.93159181), (23, 0.033074539), (21, 0.028599562), (99, 0.001878676), (146, 0.001557963)], From 9c30d7075c9c323a94968e37b36ab5840bd49d2d Mon Sep 17 00:00:00 2001 From: TobeyQin Date: Sun, 11 Feb 2018 14:10:03 +0800 Subject: [PATCH 2/2] add caffe inference test file: imagenet_test.py --- .../examples/caffe/imagenet_test.py | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 mmdnn/conversion/examples/caffe/imagenet_test.py diff --git a/mmdnn/conversion/examples/caffe/imagenet_test.py b/mmdnn/conversion/examples/caffe/imagenet_test.py new file mode 100644 index 00000000..f251eeee --- /dev/null +++ b/mmdnn/conversion/examples/caffe/imagenet_test.py @@ -0,0 +1,77 @@ +#---------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +#---------------------------------------------------------------------------------------------- + +import argparse +import numpy as np +import sys +import os +from six import text_type as _text_type +from mmdnn.conversion.examples.imagenet_test import TestKit +import caffe + + +class TestCaffe(TestKit): + + def __init__(self): + super(TestCaffe, self).__init__() + + self.truth['caffe']['alexnet'] = [(657, 0.41121054), (744, 0.20789708), (847, 0.086725503), (821, 0.05908291), (595, 0.058017164)] + + if self.args.dump: + self.dump_net = self.args.dump + '.prototxt' + self.dump_weight = self.args.dump + '.caffemodel' + else: + self.dump_net = 'tmp.prototxt' + self.dump_weight = 'tmp.caffemodel' + + self.MainModel.make_net(self.dump_net) + self.MainModel.gen_weight(self.args.w, self.dump_weight, self.dump_net) + self.model = caffe.Net(self.dump_net, self.dump_weight, caffe.TEST) + + def preprocess(self, image_path): + x = super(TestCaffe, self).preprocess(image_path) + # caffe uses NCHW + x = np.transpose(x, [2, 0, 1]) + self.data = np.expand_dims(x, 0) + + + def print_result(self): + self.model.blobs['data'].data[...] = self.data + predict = self.model.forward()['prob'][0] + super(TestCaffe, self).print_result(predict) + + + def print_intermediate_result(self, layer_name, if_transpose = False): + intermediate_output = self.model.blobs[layer_name].data[0] + super(TestCaffe, self).print_intermediate_result(intermediate_output, if_transpose) + + + def inference(self, image_path): + self.preprocess(image_path) + + # self.print_intermediate_result('', False) + + self.print_result() + + self.test_truth() + + # delete tmp model files + if os.path.isfile(self.dump_net): + os.remove(self.dump_net) + if os.path.isfile(self.dump_weight): + os.remove(self.dump_weight) + + + def dump(self): + print ('Caffe model files are saved as [{}] and [{}], generated by [{}.py] and [{}].'.format( + self.dump_net, self.dump_weight, self.args.n, self.args.w)) + + +if __name__=='__main__': + tester = TestCaffe() + if tester.args.dump: + tester.dump() + else: + tester.inference(tester.args.image)