def _convert(args): if args.dstFramework == 'caffe': from mmdnn.conversion.caffe.caffe_emitter import CaffeEmitter if args.IRWeightPath is None: emitter = CaffeEmitter(args.IRModelPath) else: assert args.dstWeightPath emitter = CaffeEmitter((args.IRModelPath, args.IRWeightPath)) elif args.dstFramework == 'keras': from mmdnn.conversion.keras.keras2_emitter import Keras2Emitter emitter = Keras2Emitter((args.IRModelPath, args.IRWeightPath)) elif args.dstFramework == 'tensorflow': from mmdnn.conversion.tensorflow.tensorflow_emitter import TensorflowEmitter if args.IRWeightPath is None: # Convert network architecture only emitter = TensorflowEmitter(args.IRModelPath) else: emitter = TensorflowEmitter((args.IRModelPath, args.IRWeightPath)) elif args.dstFramework == 'cntk': from mmdnn.conversion.cntk.cntk_emitter import CntkEmitter if args.IRWeightPath is None: emitter = CntkEmitter(args.IRModelPath) else: emitter = CntkEmitter((args.IRModelPath, args.IRWeightPath)) elif args.dstFramework == 'coreml': raise NotImplementedError("CoreML emitter is not finished yet.") elif args.dstFramework == 'pytorch': if not args.dstWeightPath or not args.IRWeightPath: raise ValueError("Need to set a target weight filename.") from mmdnn.conversion.pytorch.pytorch_emitter import PytorchEmitter emitter = PytorchEmitter((args.IRModelPath, args.IRWeightPath)) elif args.dstFramework == 'mxnet': from mmdnn.conversion.mxnet.mxnet_emitter import MXNetEmitter if args.IRWeightPath is None: emitter = MXNetEmitter(args.IRModelPath) else: if args.dstWeightPath is None: raise ValueError( "MXNet emitter needs argument [dstWeightPath(dw)], like -dw mxnet_converted-0000.param" ) emitter = MXNetEmitter( (args.IRModelPath, args.IRWeightPath, args.dstWeightPath)) elif args.dstFramework == 'onnx': from mmdnn.conversion.onnx.onnx_emitter import OnnxEmitter if args.IRWeightPath is None: raise NotImplementedError("ONNX emitter needs IR weight file") else: emitter = OnnxEmitter(args.IRModelPath, args.IRWeightPath) else: assert False emitter.run(args.dstModelPath, args.dstWeightPath, args.phase) return 0
def CaffeEmit(original_framework, architecture_name, architecture_path, weight_path, image_path): import caffe from mmdnn.conversion.caffe.caffe_emitter import CaffeEmitter # IR to code converted_file = original_framework + '_caffe_' + architecture_name + "_converted" converted_file = converted_file.replace('.', '_') emitter = CaffeEmitter((architecture_path, weight_path)) emitter.run(converted_file + '.py', converted_file + '.npy', 'test') del emitter del CaffeEmitter # import converted model imported = __import__(converted_file) imported.make_net(converted_file + '.prototxt') imported.gen_weight(converted_file + '.npy', converted_file + '.caffemodel', converted_file + '.prototxt') model_converted = caffe.Net(converted_file + '.prototxt', converted_file + '.caffemodel', caffe.TEST) func = TestKit.preprocess_func[original_framework][architecture_name] img = func(image_path) img = np.transpose(img, [2, 0, 1]) input_data = np.expand_dims(img, 0) model_converted.blobs[model_converted._layer_names[0]].data[...] = input_data predict = model_converted.forward()[model_converted._layer_names[-1]][0] converted_predict = np.squeeze(predict) del model_converted del sys.modules[converted_file] os.remove(converted_file + '.py') os.remove(converted_file + '.npy') os.remove(converted_file + '.prototxt') os.remove(converted_file + '.caffemodel') return converted_predict
def CaffeEmit(original_framework, architecture_name, architecture_path, weight_path, test_input_path): try: import caffe from mmdnn.conversion.caffe.caffe_emitter import CaffeEmitter # IR to code converted_file = original_framework + '_caffe_' + architecture_name + "_converted" converted_file = converted_file.replace('.', '_') emitter = CaffeEmitter((architecture_path, weight_path)) emitter.run(converted_file + '.py', converted_file + '.npy', 'test') del emitter del CaffeEmitter # import converted model imported = imp.load_source('CaffeModel', converted_file + '.py') imported.make_net(converted_file + '.prototxt') imported.gen_weight(converted_file + '.npy', converted_file + '.caffemodel', converted_file + '.prototxt') model_converted = caffe.Net(converted_file + '.prototxt', converted_file + '.caffemodel', caffe.TEST) original_framework = checkfrozen(original_framework) func = TestKit.preprocess_func[original_framework][ architecture_name] img = func(test_input_path(architecture_name)) img = np.transpose(img, [2, 0, 1]) input_data = np.expand_dims(img, 0) model_converted.blobs[model_converted._layer_names[0]].data[ ...] = input_data predict = model_converted.forward()[ model_converted._layer_names[-1]] converted_predict = np.squeeze(predict) del model_converted del sys.modules['CaffeModel'] del caffe os.remove(converted_file + '.py') os.remove(converted_file + '.npy') os.remove(converted_file + '.prototxt') os.remove(converted_file + '.caffemodel') return converted_predict except ImportError: print("Cannot import Caffe. Caffe Emit is not tested.") return None
def start(self): print("start to do pytorch to IR") self.parse.run(self.save_root) print("done! then to do IR to caffe code") emitter = CaffeEmitter( (self.save['structurepb'], self.save['weights'])) emitter.run(self.save['caffenetwork'], self.save['caffeweights'], 'test') print("done! then to do ccode to model") MainModel = imp.load_source('MainModel', self.save['caffenetwork']) save_model( MainModel, self.save['caffenetwork'], self.save['caffeweights', self.save['caffemodel'], self.save['caffeproto']]) print("done!!!!!!")
def CaffeEmit(original_framework, architecture_name, architecture_path, weight_path, image_path): print("Testing {} from {} to Caffe.".format(architecture_name, original_framework)) # IR to code emitter = CaffeEmitter((architecture_path, weight_path)) emitter.run("converted_model.py", None, 'test') del emitter # import converted model import converted_model reload_module (converted_model) model_converted = converted_model.KitModel(TestModels.tmpdir + architecture_name + "_converted.npy") # input_tf, model_tf = model_converted func = TestKit.preprocess_func[original_framework][architecture_name] img = func(image_path)
def start(self): print("start to do pytorch to IR") self.parse.run(self.save_root + self.save_name) print("done! then to do IR to caffe code") emitter = CaffeEmitter( (self.save['structurepb'], self.save['weights'])) emitter.run(self.save['caffenetwork'], self.save['caffeweights'], 'test') print("done! then to do ccode to model") MainModel = imp.load_source('MainModel', self.save['caffenetwork']) save_model(MainModel, self.save['caffenetwork'], self.save['caffeweights'], self.save['caffemodel']) print('start to rename inputs') lines = open(self.save['caffemodel'] + '.prototxt', 'r').readlines() new_lines = rename_input(lines, self.input_shape) fp = open(self.save['caffemodel'] + '.prototxt', 'w') fp.writelines(new_lines) print("^~^^~^^~^^~^")