Esempio n. 1
0
def test_yolo_darknet():
    MODEL_NAME = "yolov2-tiny"
    CFG_NAME = MODEL_NAME + '.cfg'
    WEIGHTS_NAME = MODEL_NAME + '.weights'
    REPO_URL = 'https://github.com/dmlc/web-data/blob/master/darknet/'
    CFG_URL = 'https://github.com/pjreddie/darknet/raw/master/cfg/' + CFG_NAME + '?raw=true'
    WEIGHTS_URL = 'https://pjreddie.com/media/files/' + WEIGHTS_NAME

    cfg_path = download.download_testdata(CFG_URL, CFG_NAME, module="darknet")
    weights_path = download.download_testdata(WEIGHTS_URL,
                                              WEIGHTS_NAME,
                                              module="darknet")

    # Download and Load darknet library
    DARKNET_LIB = 'libdarknet2.0.so'

    DARKNET_URL = REPO_URL + 'lib/' + DARKNET_LIB + '?raw=true'

    lib_path = "/Users/seankinzer/.tvm_test_data/darknet/libdarknet2.0.so"
    print(lib_path)
    DARKNET_LIB = __darknetffi__.dlopen(lib_path)
    net = DARKNET_LIB.load_network(cfg_path.encode('utf-8'),
                                   weights_path.encode('utf-8'), 0)

    mod, params = relay.frontend.from_darknet(net,
                                              dtype='float32',
                                              shape=(1, 3, 416, 416))
    return mod, params
def get_darknet(
        cfg_path='/hdd02/zhangyiyang/darknet/cfg/yolov3-tiny.cfg',
        weights_path='/hdd02/zhangyiyang/data/darknet/yolov3-tiny.weights',
        lib_path='/home/ubuntu/.tvm_test_data/darknet/libdarknet2.0.so',):
    DARKNET_LIB = __darknetffi__.dlopen(lib_path)
    net = DARKNET_LIB.load_network(cfg_path.encode(
        'utf-8'), weights_path.encode('utf-8'), 0)
    input_shape = [1, net.c, net.h, net.w]
    mod, params = relay.frontend.from_darknet(
        net, dtype='float32', shape=input_shape)
    print("successfully load darknet relay")
    return mod, params, input_shape
Esempio n. 3
0
def get_network(batch_size):
    """Get the symbol definition and random weight of a network"""
    MODEL_NAME = 'yolov3'
    REPO_URL = 'https://github.com/dmlc/web-data/blob/master/darknet/'
    
    DARKNET_LIB = 'libdarknet2.0.so'
    DARKNET_URL = REPO_URL + 'lib/' + DARKNET_LIB + '?raw=true'
    lib_path = download_testdata(DARKNET_URL, DARKNET_LIB, module="darknet")

    DARKNET_LIB = __darknetffi__.dlopen(lib_path)
    net = DARKNET_LIB.load_network(cfg_path.encode('utf-8'), weights_path.encode('utf-8'), 0)
    dtype = 'float32'
    data = np.empty([1, net.c, net.h, net.w], dtype)
    shape_dict = {'data': data.shape}
    print("Converting darknet to relay functions...")
    mod, params = relay.frontend.from_darknet(net, dtype=dtype, shape=data.shape)

    input_shape = data.shape

    return mod, params, input_shape
Esempio n. 4
0
# Using Darknet library load downloaded vision model and compile with Relay.
# The compilation steps are:
#
# 1. Front end translation from Darknet into Relay module.
# 2. Apply 8-bit quantization: here we skip the first conv layer,
#    and dense layer which will both be executed in fp32 on the CPU.
# 3. Perform graph packing to alter the data layout for tensorization.
# 4. Perform constant folding to reduce number of operators (e.g. eliminate batch norm multiply).
# 5. Perform relay build to object file.
# 6. Load the object file onto remote (FPGA device).
# 7. Generate graph runtime, `m`.
#

# Load pre-configured AutoTVM schedules
with autotvm.tophub.context(target):
    net = __darknetffi__.dlopen(darknet_lib_path).load_network(
        cfg_path.encode('utf-8'), weights_path.encode('utf-8'), 0)
    dshape = (env.BATCH, net.c, net.h, net.w)
    dtype = 'float32'

    # Measure build start time
    build_start = time.time()

    # Start front end compilation
    mod, params = relay.frontend.from_darknet(net, dtype=dtype, shape=dshape)

    if target.device_name == "vta":
        # Perform quantization in Relay
        # Note: We set opt_level to 3 in order to fold batch norm
        with relay.build_config(opt_level=3):
            with relay.quantize.qconfig(global_scale=33.0,
                                        skip_conv_layers=[0],
Esempio n. 5
0
by the script.
"""
import numpy as np
import tvm
from tvm.contrib import graph_runtime
from tvm.contrib.download import download_testdata
download_testdata.__test__ = False
from tvm.relay.testing.darknet import LAYERTYPE
from tvm.relay.testing.darknet import __darknetffi__
from tvm.relay.frontend.darknet import ACTIVATION
from tvm import relay

REPO_URL = 'https://github.com/dmlc/web-data/blob/master/darknet/'
DARKNET_LIB = 'libdarknet2.0.so'
DARKNETLIB_URL = REPO_URL + 'lib/' + DARKNET_LIB + '?raw=true'
LIB = __darknetffi__.dlopen(download_testdata(DARKNETLIB_URL, DARKNET_LIB, module='darknet'))

DARKNET_TEST_IMAGE_NAME = 'dog.jpg'
DARKNET_TEST_IMAGE_URL = REPO_URL + 'data/' + DARKNET_TEST_IMAGE_NAME +'?raw=true'
DARKNET_TEST_IMAGE_PATH = download_testdata(DARKNET_TEST_IMAGE_URL, DARKNET_TEST_IMAGE_NAME, module='data')

def _read_memory_buffer(shape, data, dtype='float32'):
    length = 1
    for x in shape:
        length *= x
    data_np = np.zeros(length, dtype=dtype)
    for i in range(length):
        data_np[i] = data[i]
    return data_np.reshape(shape)

def _get_tvm_output(net, data, build_dtype='float32', states=None):
Esempio n. 6
0
weights_path = download_testdata(WEIGHTS_URL, WEIGHTS_NAME, module="darknet")

# Download and Load darknet library
if sys.platform in ["linux", "linux2"]:
    DARKNET_LIB = "libdarknet2.0.so"
    DARKNET_URL = REPO_URL + "lib/" + DARKNET_LIB + "?raw=true"
elif sys.platform == "darwin":
    DARKNET_LIB = "libdarknet_mac2.0.so"
    DARKNET_URL = REPO_URL + "lib_osx/" + DARKNET_LIB + "?raw=true"
else:
    err = "Darknet lib is not supported on {} platform".format(sys.platform)
    raise NotImplementedError(err)

lib_path = download_testdata(DARKNET_URL, DARKNET_LIB, module="darknet")

DARKNET_LIB = __darknetffi__.dlopen(lib_path)
net = DARKNET_LIB.load_network(cfg_path.encode("utf-8"), weights_path.encode("utf-8"), 0)
dtype = "float32"
batch_size = 1

data = np.empty([batch_size, net.c, net.h, net.w], dtype)
shape_dict = {"data": data.shape}
print("Converting darknet to relay functions...")
mod, params = relay.frontend.from_darknet(net, dtype=dtype, shape=data.shape)

######################################################################
# Import the graph to Relay
# -------------------------
# compile the model
target = tvm.target.Target("llvm", host="llvm")
dev = tvm.cpu(0)
Esempio n. 7
0
IMAGE_NAME = "dog.jpg"
IMAGE_URL = REPO_URL + "data/" + IMAGE_NAME + "?raw=true"
img_path = download_testdata(IMAGE_URL, IMAGE_NAME, module="darknet")
## Download and Load darknet library
if sys.platform in ["linux", "linux2"]:
    DARKNET_LIB = "libdarknet2.0.so"
    DARKNET_URL = REPO_URL + "lib/" + DARKNET_LIB + "?raw=true"
elif sys.platform == "darwin":
    DARKNET_LIB = "libdarknet_mac2.0.so"
    DARKNET_URL = REPO_URL + "lib_osx/" + DARKNET_LIB + "?raw=true"
else:
    err = "Darknet lib is not supported on {} platform".format(sys.platform)
    raise NotImplementedError(err)

lib_path = download_testdata(DARKNET_URL, DARKNET_LIB, module="darknet")
darknet_lib = __darknetffi__.dlopen(lib_path)
net = darknet_lib.load_network(cfg_path.encode("utf-8"),
                               weights_path.encode("utf-8"), 0)

# Get moduleIR and params
dtype = "float32"
batch_size = 1
data = np.empty([batch_size, net.c, net.h, net.w], dtype=dtype)
shape = {"data": data.shape}
mod, params = relay.frontend.from_darknet(net, shape=data.shape, dtype=dtype)

# compile the module
target = "llvm"
target_host = "llvm"
ctx = tvm.cpu(0)
data = np.empty([batch_size, net.c, net.h, net.w], dtype)
Esempio n. 8
0
    def __init__(self, config):
        cfg_path = config['cfg_path']
        weights_path = config['weights_path']
        device_type = config['device_type']
        autotune = config['autotune']
        log_file = config['log_file']
        self.thresh = config['thresh']
        self.nms_thresh = config['nms_thresh']

        DARKNET_URL = 'https://github.com/dmlc/web-data/blob/master/darknet/lib/libdarknet2.0.so?raw=true'
        lib_path = download_testdata(DARKNET_URL,
                                     'libdarknet2.0.so',
                                     module="darknet")
        DARKNET_LIB = __darknetffi__.dlopen(lib_path)
        self.net = DARKNET_LIB.load_network(cfg_path.encode('utf-8'),
                                            weights_path.encode('utf-8'), 0)

        dtype = 'float32'
        data = np.empty([1, self.net.c, self.net.h, self.net.w], dtype)
        self.shape = {'data': data.shape}

        # convert darknet to relay functions
        mod, params = relay.frontend.from_darknet(self.net,
                                                  dtype=dtype,
                                                  shape=data.shape)

        # import graph to relay
        if device_type == 'cpu':
            target = 'llvm'
            ctx = tvm.cpu(0)
            if autotune:
                if not os.path.isfile(log_file):
                    err = "Autotvm log file does not exist."
                    raise NotImplementedError(err)
                with autotvm.apply_history_best(log_file):
                    with relay.build_config(opt_level=3):
                        graph, lib, params = relay.build_module.build(
                            mod, target=target, params=params)
            else:
                with relay.build_config(opt_level=3):
                    graph, lib, params = relay.build_module.build(
                        mod, target=target, params=params)
        elif device_type == 'cuda-cudnn':
            target = 'cuda -libs=cudnn'
            ctx = tvm.gpu()
            if autotune:
                if not os.path.isfile(log_file):
                    err = "Autotvm log file does not exist."
                    raise NotImplementedError(err)
                with autotvm.apply_history_best(log_file):
                    with relay.build_config(opt_level=3):
                        graph, lib, params = relay.build_module.build(
                            mod, target=target, params=params)
            else:
                with relay.build_config(opt_level=3):
                    graph, lib, params = relay.build_module.build(
                        mod, target=target, params=params)
        elif device_type == 'cuda':
            target = tvm.target.cuda()
            ctx = tvm.gpu()
            if autotune:
                if not os.path.isfile(log_file):
                    err = "Autotvm log file does not exist."
                    raise NotImplementedError(err)
                with autotvm.apply_history_best(log_file):
                    with relay.build_config(opt_level=3):
                        graph, lib, params = relay.build_module.build(
                            mod, target=target, params=params)
            else:
                with relay.build_config(opt_level=3):
                    graph, lib, params = relay.build_module.build(
                        mod, target=target, params=params)
        else:
            err = "Device type is not supported on this platform."
            raise NotImplementedError(err)

        self.m = graph_runtime.create(graph, lib, ctx)
        self.m.set_input(**params)