def _main(): parser = argparse.ArgumentParser() parser.add_argument('-n', '--network', type=_text_type, help='Model Type', required=True, choices = _default_model_info.keys()) parser.add_argument('-i', '--image', default=None, type=_text_type, help='Test Image Path') parser.add_argument('-o', '--output_dir', default='./', type=_text_type, help='Caffe Checkpoint file name') args = parser.parse_args() if not download_file(_default_model_info[args.network]['prototxt'], directory=args.output_dir): return -1 if not download_file(_default_model_info[args.network]['caffemodel'], directory=args.output_dir): return -1 print("Model {} saved.".format(args.network)) if args.image: # Yuhao TODO: inference code pass return 0
def download(cls, architecture, path='./'): if cls.sanity_check(architecture): cfg_name = architecture + ".cfg" architecture_file = download_file( cls.architecture_map[architecture]['config'], directory=path, local_fname=cfg_name) if not architecture_file: return None weight_name = architecture + ".weights" weight_file = download_file( cls.architecture_map[architecture]['weights'], directory=path, local_fname=weight_name) if not weight_file: return None print("Darknet Model {} saved as [{}] and [{}].".format( architecture, architecture_file, weight_file)) return (architecture_file, weight_file) else: return None
def _main(): parser = argparse.ArgumentParser() parser.add_argument('-n', '--network', type=_text_type, help='Model Type', required=True, choices=DEFAULT_MODEL_INFO.keys()) parser.add_argument('-i', '--image', default=None, type=_text_type, help='Test Image Path') parser.add_argument('-o', '--output_dir', default='./', type=_text_type, help='Caffe Checkpoint file name') args = parser.parse_args() arch_fn = download_file(DEFAULT_MODEL_INFO[args.network]['prototxt'], directory=args.output_dir) if not arch_fn: return -1 weight_fn = download_file(DEFAULT_MODEL_INFO[args.network]['caffemodel'], directory=args.output_dir) if not weight_fn: return -1 print("Model {} saved.".format(args.network)) if args.image: import caffe import numpy as np from mmdnn.conversion.examples.imagenet_test import TestKit net = caffe.Net(arch_fn, weight_fn, caffe.TEST) func = TestKit.preprocess_func['caffe'][args.network] img = func(args.image) img = np.transpose(img, (2, 0, 1)) img = np.expand_dims(img, 0) net.blobs['data'].data[...] = img predict = np.squeeze(net.forward()['prob'][0]) predict = np.squeeze(predict) top_indices = predict.argsort()[-5:][::-1] result = [(i, predict[i]) for i in top_indices] print(result) print(np.sum(result)) return 0
def _main(): parser = argparse.ArgumentParser() parser.add_argument('-n', '--network', type=_text_type, help='Model Type', required=True, choices=_default_model_info.keys()) parser.add_argument('-i', '--image', default=None, type=_text_type, help='Test Image Path') parser.add_argument('-o', '--output_dir', default='./', type=_text_type, help='Tensorflow Checkpoint file name') args = parser.parse_args() if not download_file(_default_model_info[args.network]['symbol'], directory=args.output_dir): return -1 if not download_file(_default_model_info[args.network]['params'], directory=args.output_dir): return -1 print("Model {} saved.".format(args.network)) file_name = _default_model_info[args.network]['params'].split('/')[-1] prefix, epoch_num = file_name[:-7].rsplit('-', 1) sym, arg_params, aux_params = mx.model.load_checkpoint(args.output_dir + prefix, int(epoch_num)) model = mx.mod.Module(symbol=sym) model.bind(for_training=False, data_shapes=[('data', (1, 3, _default_model_info[args.network]['image_size'], _default_model_info[args.network]['image_size']))]) model.set_params(arg_params, aux_params, allow_missing=True, allow_extra=True) if args.image: import numpy as np # need to be updated network = _search_preprocess_key(args.network) func = TestKit.preprocess_func['mxnet'][network] img = func(args.image) img = np.swapaxes(img, 0, 2) img = np.swapaxes(img, 1, 2) img = np.expand_dims(img, axis=0) model.forward(Batch([mx.nd.array(img)])) predict = model.get_outputs()[0].asnumpy() predict = np.squeeze(predict) top_indices = predict.argsort()[-5:][::-1] result = [(i, predict[i]) for i in top_indices] print(result) return 0
def download(cls, architecture, path="./"): if cls.sanity_check(architecture): architecture_file = download_file(cls.architecture_map[architecture]['symbol'], directory=path) if not architecture_file: return None weight_file = download_file(cls.architecture_map[architecture]['params'], directory=path) if not weight_file: return None print("MXNet Model {} saved as [{}] and [{}].".format(architecture, architecture_file, weight_file)) return (architecture_file, weight_file) else: return None
def download(cls, architecture, path="./"): if cls.sanity_check(architecture): prototxt_name = architecture + "-deploy.prototxt" architecture_file = download_file(cls.architecture_map[architecture]['prototxt'], directory=path ,local_fname=prototxt_name) if not architecture_file: return None weight_file = download_file(cls.architecture_map[architecture]['caffemodel'], directory=path) if not weight_file: return None print("Caffe Model {} saved as [{}] and [{}].".format(architecture, architecture_file, weight_file)) return (architecture_file, weight_file) else: return None
def download(cls, architecture, path="./"): if cls.sanity_check(architecture): architecture_file = download_file( cls.architecture_map[architecture]['url'], directory=path, auto_unzip=True) if not architecture_file: return None tf.reset_default_graph() if 'ckpt' in cls.architecture_map[architecture]['filename']: cls.handle_checkpoint(architecture, path) elif cls.architecture_map[architecture]['filename'].endswith('pb'): cls.handle_frozen_graph(architecture, path) else: raise ValueError("Unknown file name [{}].".format( cls.architecture_map[architecture]['filename'])) return architecture_file else: return None
def download(cls, architecture, path="./"): if cls.sanity_check(architecture): prototxt_name = architecture + "-deploy.prototxt" architecture_file = download_file(cls.architecture_map[architecture]['prototxt'], directory=path, local_fname=prototxt_name) if not architecture_file: return None weight_name = architecture + ".caffemodel" weight_file = download_file(cls.architecture_map[architecture]['caffemodel'], directory=path, local_fname=weight_name) if not weight_file: return None print("Caffe Model {} saved as [{}] and [{}].".format(architecture, architecture_file, weight_file)) return (architecture_file, weight_file) else: return None
def inference(cls, architecture, files, model_path, image_path): import numpy as np if cls.sanity_check(architecture): download_file(cls._base_model_url + "cfg/coco.data", directory='./') download_file(cls._base_model_url + "data/coco.names", directory='./data/') net = cdarknet.load_net(files[0], files[1], 0) meta = cdarknet.load_meta("coco.data") r = cdarknet.detect(net, meta, image_path) # print(r) return r else: return None
def _main(): parser = argparse.ArgumentParser() parser.add_argument('-n', '--network', type=_text_type, help='Model Type', required=True, choices=DEFAULT_MODEL_INFO.keys()) parser.add_argument('-i', '--image', default=None, type=_text_type, help='Test Image Path') parser.add_argument('-o', '--output_dir', default='./', type=_text_type, help='Caffe Checkpoint file name') args = parser.parse_args() arch_fn = download_file(DEFAULT_MODEL_INFO[args.network]['prototxt'], directory=args.output_dir) if not arch_fn: return -1 weight_fn = download_file(DEFAULT_MODEL_INFO[args.network]['caffemodel'], directory=args.output_dir) if not weight_fn: return -1 print("Model {} saved.".format(args.network)) if args.image: import caffe import numpy as np from mmdnn.conversion.examples.imagenet_test import TestKit net = caffe.Net(arch_fn.encode("utf-8"), weight_fn.encode("utf-8"), caffe.TEST) # net = caffe.Net(arch_fn, weight_fn, caffe.TEST) func = TestKit.preprocess_func['caffe'][args.network] img = func(args.image) img = np.transpose(img, (2, 0, 1)) img = np.expand_dims(img, 0) net.blobs['data'].data[...] = img predict = np.squeeze(net.forward()['prob'][0]) predict = np.squeeze(predict) top_indices = predict.argsort()[-5:][::-1] result = [(i, predict[i]) for i in top_indices] print(result) print(np.sum(result)) return 0
def _main(): parser = argparse.ArgumentParser() parser.add_argument('-n', '--network', type=_text_type, help='Model Type', required=True, choices=MODEL_URL.keys()) parser.add_argument('-i', '--image', default=None, type=_text_type, help='Test Image Path') parser.add_argument('-o', '--output_dir', default='./', type=_text_type, help='Caffe Checkpoint file name') args = parser.parse_args() fn = download_file(MODEL_URL[args.network], directory=args.output_dir) if not fn: return -1 model = C.Function.load(fn) if len(model.outputs) > 1: for idx, output in enumerate(model.outputs): if len(output.shape) > 0: eval_node = idx break model = C.as_composite(model[eval_node].owner) model.save(fn) print("Model {} is saved as {}.".format(args.network, fn)) if args.image: import numpy as np from mmdnn.conversion.examples.imagenet_test import TestKit func = TestKit.preprocess_func['cntk'][args.network] img = func(args.image) img = np.transpose(img, (2, 0, 1)) predict = model.eval({model.arguments[0]: [img]}) predict = np.squeeze(predict) top_indices = predict.argsort()[-5:][::-1] result = [(i, predict[i]) for i in top_indices] print(result) print(np.sum(result)) return 0
def download(cls, architecture, path='./'): if cls.sanity_check(architecture): architecture_file = download_file( cls.architecture_map[architecture], directory=path) if not architecture_file: return None print('Coreml model {} is saved in [{}]'.format( architecture, path)) return architecture_file else: return None
def download(cls, architecture, path="./"): if cls.sanity_check(architecture): architecture_file = download_file(cls.architecture_map[architecture], directory=path) model = C.Function.load(architecture_file) if len(model.outputs) > 1: for idx, output in enumerate(model.outputs): if len(output.shape) > 0: eval_node = idx break model = C.as_composite(model[eval_node].owner) model.save(architecture_file) print("Cntk Model {} saved as [{}].".format(architecture, architecture_file)) return architecture_file else: return None
def _main(): parser = argparse.ArgumentParser() parser.add_argument('-n', '--network', type=_text_type, help='Model Type', required=True, choices=MODEL_URL.keys()) parser.add_argument('-i', '--image', default=None, type=_text_type, help='Test Image Path') parser.add_argument('-o', '--output_dir', default='./', type=_text_type, help='Caffe Checkpoint file name') args = parser.parse_args() fn = download_file(MODEL_URL[args.network], directory=args.output_dir) if not fn: return -1 model = C.Function.load(fn) if len(model.outputs) > 1: for idx, output in enumerate(model.outputs): if len(output.shape) > 0: eval_node = idx break model = C.as_composite(model[eval_node].owner) model.save(fn) print("Model {} is saved as {}.".format(args.network, fn)) if args.image: import numpy as np from mmdnn.conversion.examples.imagenet_test import TestKit func = TestKit.preprocess_func['cntk'][args.network] img = func(args.image) img = np.transpose(img, (2, 0, 1)) predict = model.eval({model.arguments[0]:[img]}) predict = np.squeeze(predict) top_indices = predict.argsort()[-5:][::-1] result = [(i, predict[i]) for i in top_indices] print(result) print(np.sum(result)) return 0
def download(cls, architecture, path="./"): if cls.sanity_check(architecture): architecture_file = download_file(cls.architecture_map[architecture]['url'], directory=path, auto_unzip=True) if not architecture_file: return None if cls.architecture_map[architecture]['filename'].endswith('ckpt'): cls.handle_checkpoint(architecture, path) elif cls.architecture_map[architecture]['filename'].endswith('pb'): cls.handle_frozen_graph(architecture, path) else: raise ValueError("Unknown file name [{}].".format(cls.architecture_map[architecture]['filename'])) return architecture_file else: return None
def download(cls, architecture, path="./"): if cls.sanity_check(architecture): architecture_file = download_file( cls.architecture_map[architecture], directory=path) model = C.Function.load(architecture_file) if len(model.outputs) > 1: for idx, output in enumerate(model.outputs): if len(output.shape) > 0: eval_node = idx break model = C.as_composite(model[eval_node].owner) model.save(architecture_file) print("Cntk Model {} saved as [{}].".format( architecture, architecture_file)) return architecture_file else: return None
def download(cls, architecture, path="./"): if cls.sanity_check(architecture): reset_parser() DATA_DIM = 3 * paddle_extractor._image_size * paddle_extractor._image_size # Use 3 * 331 * 331 or 3 * 299 * 299 for Inception-ResNet-v2. CLASS_DIM = paddle_extractor.class_dim_map[architecture] image = paddle.layer.data( name="image", type=paddle.data_type.dense_vector(DATA_DIM)) if 'resnet' in architecture: from mmdnn.conversion.examples.paddle.models import resnet depth = int(architecture.strip('resnet')) out = resnet.resnet_imagenet(image, class_dim=CLASS_DIM, depth=depth) elif architecture == 'vgg16': from mmdnn.conversion.examples.paddle.models import vgg out = vgg.vgg16(image, class_dim=CLASS_DIM) else: print("Not support for {} yet.", architecture) return None architecture_file = path + architecture + '.bin' paddle_extractor.dump_v2_config(out, architecture_file, True) weight_file = download_file( cls.architecture_map[architecture]['params'], directory=path, local_fname=architecture + '.tar.gz') if not weight_file: return None print("MXNet Model {} saved as [{}] and [{}].".format( architecture, architecture_file, weight_file)) return (architecture_file, weight_file) else: return None
def download(cls, architecture, path="./"): if architecture in cls.thirdparty_map: weight_file = download_file(cls.thirdparty_map[architecture], directory=path) return weight_file elif cls.sanity_check(architecture): output_filename = path + 'imagenet_{}.h5'.format(architecture) if os.path.exists(output_filename) == False: model = cls.architecture_map[architecture]() model.save(output_filename) print("Keras model {} is saved in [{}]".format( architecture, output_filename)) K.clear_session() del model return output_filename else: print("File [{}] existed, skip download.".format( output_filename)) return output_filename else: return None
def _main(): parser = argparse.ArgumentParser() parser.add_argument('-n', '--network', type=_text_type, help='Model Type', required=True, choices=MODEL_URL.keys()) parser.add_argument('-i', '--image', default=None, type=_text_type, help='Test Image Path') parser.add_argument('-o', '--output_dir', default='./', type=_text_type, help='Paddlepaddle parameters file name') args = parser.parse_args() fn = download_file(MODEL_URL[args.network], local_fname=architecture + '.tar.gz', directory=args.output_dir) if not fn: return -1 DATA_DIM = 3 * IMG_SIZE * IMG_SIZE # Use 3 * 331 * 331 or 3 * 299 * 299 for Inception-ResNet-v2. CLASS_DIM = CLASS_DIMS[args.network] # refer to https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/v2/tests/test_rnn_layer.py#L35 reset_parser() # refer to https://github.com/PaddlePaddle/Paddle/issues/7403 paddle.init(use_gpu=False, trainer_count=1) image = paddle.layer.data(name="image", type=paddle.data_type.dense_vector(DATA_DIM)) if 'resnet' in architecture: from mmdnn.conversion.examples.paddle.models import resnet depth = int(architecture.strip('resnet')) out = resnet.resnet_imagenet(image, class_dim=CLASS_DIM, depth=depth) elif architecture == 'vgg16': from mmdnn.conversion.examples.paddle.models import vgg out = vgg.vgg16(image, class_dim=CLASS_DIM) else: print("Not support for {} yet.", architecture) return None dump_v2_config(out, args.output_dir + architecture + '.bin') print("Model {} is saved as {} and {}.".format( args.network, args.output_dir + architecture + '.bin', fn)) if args.image: import numpy as np from mmdnn.conversion.examples.imagenet_test import TestKit func = TestKit.preprocess_func['paddle'][args.network] img = func(args.image) img = np.transpose(img, (2, 0, 1)) test_data = [(img.flatten(), )] with gzip.open(parameters_file, 'r') as f: parameters = paddle.parameters.Parameters.from_tar(f) predict = paddle.infer(output_layer=out, parameters=parameters, input=test_data) predict = np.squeeze(predict) top_indices = predict.argsort()[-5:][::-1] result = [(i, predict[i]) for i in top_indices] print(result) print(np.sum(result)) return 0
def _main(): parser = argparse.ArgumentParser() parser.add_argument('-n', '--network', type=_text_type, help='Model Type', required=True, choices=_default_model_info.keys()) parser.add_argument('-i', '--image', default=None, type=_text_type, help='Test Image Path') parser.add_argument('-o', '--output_dir', default='./', type=_text_type, help='Tensorflow Checkpoint file name') args = parser.parse_args() if not download_file(_default_model_info[args.network]['symbol'], directory=args.output_dir): return -1 if not download_file(_default_model_info[args.network]['params'], directory=args.output_dir): return -1 print("Model {} saved.".format(args.network)) file_name = _default_model_info[args.network]['params'].split('/')[-1] prefix, epoch_num = file_name[:-7].rsplit('-', 1) sym, arg_params, aux_params = mx.model.load_checkpoint( args.output_dir + prefix, int(epoch_num)) model = mx.mod.Module(symbol=sym) model.bind(for_training=False, data_shapes=[ ('data', (1, 3, _default_model_info[args.network]['image_size'], _default_model_info[args.network]['image_size'])) ]) model.set_params(arg_params, aux_params, allow_missing=True, allow_extra=True) if args.image: import numpy as np # need to be updated network = _search_preprocess_key(args.network) func = TestKit.preprocess_func['mxnet'][network] img = func(args.image) img = np.swapaxes(img, 0, 2) img = np.swapaxes(img, 1, 2) img = np.expand_dims(img, axis=0) model.forward(Batch([mx.nd.array(img)])) predict = model.get_outputs()[0].asnumpy() predict = np.squeeze(predict) top_indices = predict.argsort()[-5:][::-1] result = [(i, predict[i]) for i in top_indices] print(result) return 0