Beispiel #1
0
def extract_model(args):
    if args.framework == 'caffe':
        from mmdnn.conversion.examples.caffe.extractor import caffe_extractor
        extractor = caffe_extractor()

    elif args.framework == 'caffe2':
        raise NotImplementedError("Caffe2 is not supported yet.")

    elif args.framework == 'keras':
        from mmdnn.conversion.examples.keras.extractor import keras_extractor
        extractor = keras_extractor()

    elif args.framework == 'tensorflow' or args.framework == 'tf':
        from mmdnn.conversion.examples.tensorflow.extractor import tensorflow_extractor
        extractor = tensorflow_extractor()

    elif args.framework == 'mxnet':
        from mmdnn.conversion.examples.mxnet.extractor import mxnet_extractor
        extractor = mxnet_extractor()

    elif args.framework == 'cntk':
        pass
    else:
        raise ValueError("Unknown framework [{}].".format(args.framework))

    files = extractor.download(args.network, args.path)

    if files and args.image:
        predict = extractor.inference(args.network, args.path, args.image)
        top_indices = predict.argsort()[-5:][::-1]
        result = [(i, predict[i]) for i in top_indices]
        print(result)
Beispiel #2
0
def extract_model(args):
    if args.framework == 'caffe':
        from mmdnn.conversion.examples.caffe.extractor import caffe_extractor
        extractor = caffe_extractor()

    elif args.framework == 'caffe2':
        raise NotImplementedError("Caffe2 is not supported yet.")

    elif args.framework == 'keras':
        from mmdnn.conversion.examples.keras.extractor import keras_extractor
        extractor = keras_extractor()

    elif args.framework == 'tensorflow' or args.framework == 'tf':
        from mmdnn.conversion.examples.tensorflow.extractor import tensorflow_extractor
        extractor = tensorflow_extractor()

    elif args.framework == 'mxnet':
        from mmdnn.conversion.examples.mxnet.extractor import mxnet_extractor
        extractor = mxnet_extractor()

    elif args.framework == 'cntk':
        from mmdnn.conversion.examples.cntk.extractor import cntk_extractor
        extractor = cntk_extractor()

    else:
        raise ValueError("Unknown framework [{}].".format(args.framework))

    files = extractor.download(args.network, args.path)

    if files and args.image:
        predict = extractor.inference(args.network, args.path, args.image)
        top_indices = predict.argsort()[-5:][::-1]
        result = [(i, predict[i]) for i in top_indices]
        print(result)
Beispiel #3
0
def extract_model(args):
    if args.framework == 'caffe':
        from mmdnn.conversion.examples.caffe.extractor import caffe_extractor
        extractor = caffe_extractor()

    elif args.framework == 'keras':
        from mmdnn.conversion.examples.keras.extractor import keras_extractor
        extractor = keras_extractor()

    elif args.framework == 'tensorflow' or args.framework == 'tf':
        from mmdnn.conversion.examples.tensorflow.extractor import tensorflow_extractor
        extractor = tensorflow_extractor()

    elif args.framework == 'mxnet':
        from mmdnn.conversion.examples.mxnet.extractor import mxnet_extractor
        extractor = mxnet_extractor()

    elif args.framework == 'cntk':
        from mmdnn.conversion.examples.cntk.extractor import cntk_extractor
        extractor = cntk_extractor()

    elif args.framework == 'pytorch':
        from mmdnn.conversion.examples.pytorch.extractor import pytorch_extractor
        extractor = pytorch_extractor()

    elif args.framework == 'darknet':
        from mmdnn.conversion.examples.darknet.extractor import darknet_extractor
        extractor = darknet_extractor()

    else:
        raise ValueError("Unknown framework [{}].".format(args.framework))

    files = extractor.download(args.network, args.path)

    if files and args.image:
        predict = extractor.inference(args.network, files, args.path,
                                      args.image)
        if type(predict) == list:
            print(predict)

        else:
            if predict.ndim == 1:
                if predict.shape[0] == 1001:
                    offset = 1
                else:
                    offset = 0
                top_indices = predict.argsort()[-5:][::-1]
                predict = [(i, predict[i]) for i in top_indices]
                predict = generate_label(predict, args.label, offset)

                for line in predict:
                    print(line)

            else:
                print(predict.shape)
                print(predict)
Beispiel #4
0
def extract_model(args):
    if args.framework == 'caffe':
        from mmdnn.conversion.caffe.transformer import CaffeTransformer
        transformer = CaffeTransformer(args.network, args.weights, "tensorflow", args.inputShape, phase = args.caffePhase)
        graph = transformer.transform_graph()
        data = transformer.transform_data()

        from mmdnn.conversion.caffe.writer import JsonFormatter, ModelSaver, PyWriter
        JsonFormatter(graph).dump(args.dstPath + ".json")
        print ("IR network structure is saved as [{}.json].".format(args.dstPath))

        prototxt = graph.as_graph_def().SerializeToString()
        with open(args.dstPath + ".pb", 'wb') as of:
            of.write(prototxt)
        print ("IR network structure is saved as [{}.pb].".format(args.dstPath))

        import numpy as np
        with open(args.dstPath + ".npy", 'wb') as of:
            np.save(of, data)
        print ("IR weights are saved as [{}.npy].".format(args.dstPath))

        return 0

    elif args.framework == 'caffe2':
        raise NotImplementedError("Caffe2 is not supported yet.")
        '''
        assert args.inputShape != None
        from dlconv.caffe2.conversion.transformer import Caffe2Transformer
        transformer = Caffe2Transformer(args.network, args.weights, args.inputShape, 'tensorflow')

        graph = transformer.transform_graph()
        data = transformer.transform_data()

        from dlconv.common.writer import JsonFormatter, ModelSaver, PyWriter
        JsonFormatter(graph).dump(args.dstPath + ".json")
        print ("IR saved as [{}.json].".format(args.dstPath))

        prototxt = graph.as_graph_def().SerializeToString()
        with open(args.dstPath + ".pb", 'wb') as of:
            of.write(prototxt)
        print ("IR saved as [{}.pb].".format(args.dstPath))

        import numpy as np
        with open(args.dstPath + ".npy", 'wb') as of:
            np.save(of, data)
        print ("IR weights saved as [{}.npy].".format(args.dstPath))

        return 0
        '''

    elif args.framework == 'keras':
        from mmdnn.conversion.examples.keras.extractor import keras_extractor
        extractor = keras_extractor()

    elif args.framework == 'tensorflow' or args.framework == 'tf':
        pass

    elif args.framework == 'mxnet':
        from mmdnn.conversion.examples.mxnet.extractor import mxnet_extractor
        extractor = mxnet_extractor()

    elif args.framework == 'cntk':
        pass
    else:
        raise ValueError("Unknown framework [{}].".format(args.framework))

    files = extractor.download(args.network,args.path)

    if files and args.image:
        predict = extractor.inference(args.network, args.path, args.image)
        top_indices = predict.argsort()[-5:][::-1]
        result = [(i, predict[i]) for i in top_indices]
        print(result)