def main(model_str, output_path): if output_path.exists(): del_dir(output_path) output_path.mkdir() output_path_str = os.fspath(output_path) print(model_str) print("getting model...") net, params = get_model(model_str) try: os.mkdir(model_str) except FileExistsError: pass print("building...") with tvm.transform.PassContext(opt_level=3): graph, lib, params = relay.build(net, target, target_host=target_host, params=params) print("dumping lib...") lib.export_library(output_path_str + '/' + 'deploy_lib_cpu.so', ndk.create_shared) print("dumping graph...") with open(output_path_str + '/' + 'deploy_graph.json', 'w') as f: f.write(graph) print("dumping params...") with open(output_path_str + '/' + 'deploy_param.params', 'wb') as f: f.write(relay.save_param_dict(params)) print("dumping labels...") synset_url = ''.join(['https://gist.githubusercontent.com/zhreshold/', '4d0b62f3d01426887599d4f7ede23ee5/raw/', '596b27d23537e5a1b5751d2b0481ef172f58b539/', 'imagenet1000_clsid_to_human.txt']) synset_path = output_path_str + '/image_net_labels' download(synset_url, output_path_str + '/image_net_labels') with open(synset_path) as fi: synset = eval(fi.read()) with open(output_path_str + '/image_net_labels.json', "w") as fo: json.dump(synset, fo, indent=4) os.remove(synset_path)
def download_bitstream(): """Downloads a cached bitstream corresponding to the current config """ env = get_env() success = False bit = get_bitstream_path() url = os.path.join(BITSTREAM_URL, env.TARGET) url = os.path.join(url, env.HW_VER) url = os.path.join(url, env.BITSTREAM) try: download(url, bit) except urllib2.HTTPError as err: if err.code == 404: raise RuntimeError( # Raise error - the solution when this happens it to build your # own bitstream and add it to your $VTA_CACHE_PATH "{} is not available. It appears that this configuration \ bistream has not been cached. Please compile your own bitstream (see hardware \ compilation guide to get Xilinx toolchains setup) and add it to your \ $VTA_CACHE_PATH. Alternatively edit your config.json back to its default \ settings. You can see the list of available bitstreams under {}" .format(url, BITSTREAM_URL)) else: raise RuntimeError( # This could happen when trying to access the URL behind a proxy "Something went wrong when trying to access {}. Check your \ internet connection or proxy settings." .format(url)) return success
def download_bitstream(): """Downloads a cached bitstream corresponding to the current config """ env = get_env() success = False bit = get_bitstream_path() url = os.path.join(BITSTREAM_URL, env.TARGET) url = os.path.join(url, env.HW_VER) url = os.path.join(url, env.BITSTREAM + ".bit") try: download(url, bit) except urllib2.HTTPError as err: if err.code == 404: raise RuntimeError( # Raise error - the solution when this happens it to build your # own bitstream and add it to your $VTA_CACHE_PATH "{} is not available. It appears that this configuration \ bistream has not been cached. Please compile your own bitstream (see hardware \ compilation guide to get Xilinx toolchains setup) and add it to your \ $VTA_CACHE_PATH. Alternatively edit your config.json back to its default \ settings. You can see the list of available bitstreams under {}" .format(url, BITSTREAM_URL)) else: raise RuntimeError( # This could happen when trying to access the URL behind a proxy "Something went wrong when trying to access {}. Check your \ internet connection or proxy settings." .format(url)) return success
def get_single_image_input(img_url, save_dir='/tmp'): img_name = img_url.split("/")[-1] img_path = os.path.join(save_dir, img_name) download(img_url, img_path) orig_img = Image.open(img_path) orig_img = orig_img.resize((img_width, img_height), Image.LANCZOS) img = np.array(orig_img)[:, :, (0, 1, 2)].astype('uint8') return img, orig_img, img_path
def test_detection_models(): img = "test_street_small.jpg" img_url = ("https://raw.githubusercontent.com/dmlc/web-data/" "master/gluoncv/detection/street_small.jpg") download(img_url, img) input_shape = (1, 3, in_size, in_size) target = "llvm" input_name = "input0" shape_list = [(input_name, input_shape)] score_threshold = 0.9 scripted_model = generate_jit_model(1) mod, params = relay.frontend.from_pytorch(scripted_model, shape_list) with tvm.transform.PassContext(opt_level=3, disabled_pass=["FoldScaleAxis"]): vm_exec = relay.vm.compile(mod, target=target, params=params) ctx = tvm.cpu() vm = VirtualMachine(vm_exec, ctx) data = process_image(img) pt_res = scripted_model(data) data = data.detach().numpy() vm.set_input("main", **{input_name: data}) tvm_res = vm.run() # Note: due to accumulated numerical error, we can't directly compare results # with pytorch output. Some boxes might have a quite tiny difference in score # and the order can become different. We just measure how many valid boxes # there are for input image. pt_scores = pt_res[1].detach().numpy().tolist() tvm_scores = tvm_res[1].asnumpy().tolist() num_pt_valid_scores = num_tvm_valid_scores = 0 for score in pt_scores: if score >= score_threshold: num_pt_valid_scores += 1 else: break for score in tvm_scores: if score >= score_threshold: num_tvm_valid_scores += 1 else: break assert num_pt_valid_scores == num_tvm_valid_scores, ( "Output mismatch: Under score threshold {}, Pytorch has {} valid " "boxes while TVM has {}.".format(score_threshold, num_pt_valid_scores, num_tvm_valid_scores))
def test_mnist(): DEBUG = False tempdir_root = None if DEBUG: tempdir_root = os.path.join( curr_path, f"workspace", "test_mnist", datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S"), ) curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) model_path = os.path.join(curr_path, "models/mnist.tflite") build_dir = tvm.contrib.utils.tempdir(tempdir_root) model_url = "https://storage.googleapis.com/download.tensorflow.org/models/tflite/digit_classifier/mnist.tflite" download(model_url, model_path) check_network(build_dir.path, "mnist", model_path, build_dir.path)
def test_detection_models(): img = "test_street_small.jpg" img_url = ( "https://raw.githubusercontent.com/dmlc/web-data/" "master/gluoncv/detection/street_small.jpg" ) download(img_url, img) input_shape = (1, 3, in_size, in_size) input_name = "input0" shape_list = [(input_name, input_shape)] scripted_model = generate_jit_model(1) mod, params = relay.frontend.from_pytorch(scripted_model, shape_list) data = process_image(img) data_np = data.detach().numpy() with torch.no_grad(): pt_res = scripted_model(data) for target in ["llvm", "cuda"]: with tvm.transform.PassContext(opt_level=3): vm_exec = relay.vm.compile(mod, target=target, params=params) ctx = tvm.context(target, 0) vm = VirtualMachine(vm_exec, ctx) vm.set_input("main", **{input_name: data_np}) tvm_res = vm.run() # Bounding boxes tvm.testing.assert_allclose( pt_res[0].cpu().numpy(), tvm_res[0].asnumpy(), rtol=1e-5, atol=1e-5 ) # Scores tvm.testing.assert_allclose( pt_res[1].cpu().numpy(), tvm_res[1].asnumpy(), rtol=1e-5, atol=1e-5 ) # Class ids np.testing.assert_equal(pt_res[2].cpu().numpy(), tvm_res[2].asnumpy()) score_threshold = 0.9 print("Num boxes:", pt_res[0].cpu().numpy().shape[0]) print("Num valid boxes:", np.sum(pt_res[1].cpu().numpy() >= score_threshold))
def get_maskrcnn_input(in_size: int) -> np.ndarray: """ This function gets a real image with multiple objects of interest and returns it. """ input_shape = (1, 3, in_size, in_size) img_path = "test_street_small.jpg" img_url = ("https://raw.githubusercontent.com/dmlc/web-data/" "master/gluoncv/detection/street_small.jpg") download(img_url, img_path) import cv2 img = cv2.imread(img_path).astype("float32") img = cv2.resize(img, (in_size, in_size)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.transpose(img / 255.0, [2, 0, 1]) img = np.expand_dims(img, axis=0) return img
def download_model(): url = "https://github.com/uwsaml/web-data/raw/master/vta/models/" categ_fn = 'synset.txt' graph_fn = 'resnet18_qt8.json' params_fn = 'resnet18_qt8.params' data_dir = '_data' if not os.path.exists(data_dir): os.makedirs(data_dir) for file in [categ_fn, graph_fn, params_fn]: if not os.path.isfile(file): download(os.path.join(url, file), os.path.join(data_dir, file)) sym = nnvm.graph.load_json(open(os.path.join(data_dir, graph_fn)).read()) params = nnvm.compiler.load_param_dict( open(os.path.join(data_dir, params_fn), 'rb').read()) return sym, params
def load_model(): """ Load VTA Model """ load_vta_library() with open("./build/model/graph.json", "r") as graphfile: graph = graphfile.read() lib = tvm.runtime.load_module("./build/model/lib.so") model = graph_runtime.create(graph, lib, CTX) with open("./build/model/params.params", "rb") as paramfile: param_bytes = paramfile.read() categ_url = "https://github.com/uwsaml/web-data/raw/master/vta/models/" categ_fn = "synset.txt" download.download(join(categ_url, categ_fn), categ_fn) synset = eval(open(categ_fn).read()) return model, param_bytes, synset
def build_inputs(opts): from tvm.contrib import download from PIL import Image import numpy as np build_dir = os.path.abspath(opts.out_dir) # Download test image image_url = 'https://homes.cs.washington.edu/~moreau/media/vta/cat.jpg' image_fn = os.path.join(build_dir, "cat.png") download.download(image_url, image_fn) image = Image.open(image_fn).resize((224, 224)) def transform_image(image): image = np.array(image) - np.array([123., 117., 104.]) image /= np.array([58.395, 57.12, 57.375]) image = image.transpose((2, 0, 1)) image = image[np.newaxis, :] return image x = transform_image(image) print('x', x.shape) with open(os.path.join(build_dir, "cat.bin"), "wb") as fp: fp.write(x.astype(np.float32).tobytes())
lib = remote.load_module("graphlib.o") # Graph runtime m = graph_runtime.create(graph, lib, ctx) ###################################################################### # Perform image classification inference # -------------------------------------- # We run classification on an image sample from ImageNet # We just need to download the categories files, `synset.txt` # and an input test image. # Download ImageNet categories categ_url = "https://github.com/uwsaml/web-data/raw/master/vta/models/" categ_fn = "synset.txt" download.download(join(categ_url, categ_fn), categ_fn) synset = eval(open(categ_fn).read()) # Download test image image_url = 'https://homes.cs.washington.edu/~moreau/media/vta/cat.jpg' image_fn = 'cat.png' download.download(image_url, image_fn) # Prepare test image for inference image = Image.open(image_fn).resize((224, 224)) plt.imshow(image) plt.show() image = np.array(image) - np.array([123., 117., 104.]) image /= np.array([58.395, 57.12, 57.375]) image = image.transpose((2, 0, 1)) image = image[np.newaxis, :]
def run_model(model, params, input_dtype, input_shape, evaluate=False): # init input data data = np.empty(input_shape, dtype=input_dtype) # compile kernels with history best records print("Compile with [%s]..." % log_file) with autotvm.apply_history_best(log_file): with relay.build_config(opt_level=4): graph, lib, params = relay.build(model, target=target, params=params) [neth, netw] = input_shape[2:] ###################################################################### # Execute on TVM Runtime # ---------------------- # The process is no different from other examples. from tvm.contrib import graph_runtime module = graph_runtime.create(graph, lib, ctx) module.set_input('data', tvm.nd.array(data.astype(input_dtype))) module.set_input(**params) num_outputs = module.get_num_outputs() outputs = [[] for i in range(num_outputs)] # evaluate if (evaluate): print("Evaluate inference time cost...") ftimer = module.module.time_evaluator("run", ctx, number=1, repeat=5) prof_res = np.array( ftimer().results) * 1000 # convert to millisecond print("Mean inference time (std dev): %.2f ms (%.2f ms)" % (np.mean(prof_res), np.std(prof_res))) thresh = 0.30 nms_thresh = 0.35 coco_name = 'coco.names' coco_url = 'https://github.com/siju-samuel/darknet/blob/master/data/' + coco_name + '?raw=true' font_name = 'arial.ttf' font_url = 'https://github.com/siju-samuel/darknet/blob/master/data/' + font_name + '?raw=true' download(coco_url, coco_name) font_path = download_testdata(font_url, font_name, module='data') with open(coco_name) as f: content = f.readlines() fpos = 0 names = [x.strip() for x in content] vcap = cv2.VideoCapture(VIDEO_FILE) nframes = int(vcap.get(cv2.CAP_PROP_FRAME_COUNT)) vcap.set(cv2.CAP_PROP_POS_FRAMES, 0) while (True): ret, frame = vcap.read() if not ret: break img = np.array(frame) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.divide(img, 255.0) # prepare for network (resized) data = cv2.resize(img, (neth, netw), interpolation=cv2.INTER_AREA) data = data.transpose((2, 0, 1)) data = np.flip(data, 0) # prepare for display (original) img = img.transpose((2, 0, 1)) img = np.flip(img, 0) # set inputs sys.stdout.write("\r") sys.stdout.flush() sys.stdout.write("Inference on frame %i/%i" % (fpos, nframes)) sys.stdout.flush() module.set_input('data', tvm.nd.array(data.astype(input_dtype))) # execute module.run() tvm_out = [] if MODEL_NAME == 'yolov2': layer_out = {} layer_out['type'] = 'Region' # Get the region layer attributes (n, out_c, out_h, out_w, classes, coords, background) layer_attr = m.get_output(2).asnumpy() layer_out['biases'] = m.get_output(1).asnumpy() out_shape = (layer_attr[0], layer_attr[1] // layer_attr[0], layer_attr[2], layer_attr[3]) layer_out['output'] = m.get_output(0).asnumpy().reshape( out_shape) layer_out['classes'] = layer_attr[4] layer_out['coords'] = layer_attr[5] layer_out['background'] = layer_attr[6] tvm_out.append(layer_out) elif MODEL_NAME == 'yolov3-tiny': for i in range(2): layer_out = {} layer_out['type'] = 'Yolo' # Get the yolo layer attributes (n, out_c, out_h, out_w, classes, total) layer_attr = module.get_output(i * 4 + 3).asnumpy() layer_out['biases'] = module.get_output(i * 4 + 2).asnumpy() layer_out['mask'] = module.get_output(i * 4 + 1).asnumpy() out_shape = (layer_attr[0], layer_attr[1] // layer_attr[0], layer_attr[2], layer_attr[3]) layer_out['output'] = module.get_output( i * 4).asnumpy().reshape(out_shape) layer_out['classes'] = layer_attr[4] tvm_out.append(layer_out) elif MODEL_NAME == 'yolov3': for i in range(3): layer_out = {} layer_out['type'] = 'Yolo' # Get the yolo layer attributes (n, out_c, out_h, out_w, classes, total) layer_attr = module.get_output(i * 4 + 3).asnumpy() layer_out['biases'] = module.get_output(i * 4 + 2).asnumpy() layer_out['mask'] = module.get_output(i * 4 + 1).asnumpy() out_shape = (layer_attr[0], layer_attr[1] // layer_attr[0], layer_attr[2], layer_attr[3]) layer_out['output'] = module.get_output( i * 4).asnumpy().reshape(out_shape) layer_out['classes'] = layer_attr[4] tvm_out.append(layer_out) else: raise ValueError("Unsupported model: " + MODEL_NAME) _, im_h, im_w = data.shape dets = tvm.relay.testing.yolo_detection.fill_network_boxes( (netw, neth), (im_w, im_h), thresh, 1, tvm_out) last_layer = net.layers[net.n - 1] tvm.relay.testing.yolo_detection.do_nms_sort( dets, last_layer.classes, nms_thresh) tvm.relay.testing.yolo_detection.draw_detections( font_path, img, dets, thresh, names, last_layer.classes) cv2.imshow('VIDEO', img.transpose(1, 2, 0)) cv2.waitKey(1) fpos = fpos + 1
lib = remote.load_module("graphlib.o") # Graph runtime m = graph_runtime.create(graph, lib, ctx) ###################################################################### # Perform image classification inference # -------------------------------------- # We run classification on an image sample from ImageNet # We just need to download the categories files, `synset.txt` # and an input test image. # Download ImageNet categories categ_url = "https://github.com/uwsaml/web-data/raw/master/vta/models/" categ_fn = "synset.txt" download.download(join(categ_url, categ_fn), categ_fn) synset = eval(open(categ_fn).read()) # Download test image image_url = 'https://homes.cs.washington.edu/~moreau/media/vta/cat.jpg' response = requests.get(image_url) # Prepare test image for inference image = Image.open(BytesIO(response.content)).resize((224, 224)) plt.imshow(image) plt.show() image = np.array(image) - np.array([123., 117., 104.]) image /= np.array([58.395, 57.12, 57.375]) image = image.transpose((2, 0, 1)) image = image[np.newaxis, :] image = np.repeat(image, env.BATCH, axis=0)
# Obtain ResNet model and download them into _data dir url = "https://github.com/uwsaml/web-data/raw/master/vta/models/" categ_fn = 'synset.txt' graph_fn = 'resnet18_qt8.json' params_fn = 'resnet18_qt8.params' # Create data dir data_dir = "_data/" if not os.path.exists(data_dir): os.makedirs(data_dir) # Download files for file in [categ_fn, graph_fn, params_fn]: if not os.path.isfile(file): download(os.path.join(url, file), os.path.join(data_dir, file)) # Read in ImageNet Categories synset = eval(open(os.path.join(data_dir, categ_fn)).read()) # Download pre-tuned op parameters of conv2d for ARM CPU used in VTA autotvm.tophub.check_backend('vta') ###################################################################### # Setup the Pynq Board's RPC Server # --------------------------------- # Build the RPC server's VTA runtime and program the Pynq FPGA. # Measure build start time reconfig_start = time.time()
from nnvm.testing.darknet import __darknetffi__ # Model name MODEL_NAME = 'yolo' ###################################################################### # Download required files # ----------------------- # Download cfg and weights file if first time. CFG_NAME = MODEL_NAME + '.cfg' WEIGHTS_NAME = MODEL_NAME + '.weights' REPO_URL = 'https://github.com/siju-samuel/darknet/blob/master/' CFG_URL = REPO_URL + 'cfg/' + CFG_NAME + '?raw=true' WEIGHTS_URL = 'https://pjreddie.com/media/files/' + WEIGHTS_NAME download(CFG_URL, CFG_NAME) download(WEIGHTS_URL, WEIGHTS_NAME) # 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) download(DARKNET_URL, DARKNET_LIB)
def test_ssd(): model_name = "ssd_resnet50_512" model_file = "%s.zip" % model_name test_image = "dog.jpg" dshape = (1, 3, 512, 512) ###################################################################### # Download MXNet SSD pre-trained model and demo image # --------------------------------------------------- # Pre-trained model available at # https://github.com/apache/incubator-\mxnet/tree/master/example/ssd model_url = "https://github.com/zhreshold/mxnet-ssd/releases/download/v0.6/" \ "resnet50_ssd_512_voc0712_trainval.zip" image_url = "https://cloud.githubusercontent.com/assets/3307514/20012567/" \ "cbb60336-a27d-11e6-93ff-cbc3f09f5c9e.jpg" inference_symbol_folder = "c1904e900848df4548ce5dfb18c719c7-a28c4856c827fe766aa3da0e35bad41d44f0fb26" inference_symbol_url = "https://gist.github.com/kevinthesun/c1904e900848df4548ce5dfb18c719c7/" \ "archive/a28c4856c827fe766aa3da0e35bad41d44f0fb26.zip" dir = "ssd_model" if not os.path.exists(dir): os.makedirs(dir) model_file_path = "%s/%s" % (dir, model_file) test_image_path = "%s/%s" % (dir, test_image) inference_symbol_path = "%s/inference_model.zip" % dir download(model_url, model_file_path) download(image_url, test_image_path) download(inference_symbol_url, inference_symbol_path) zip_ref = zipfile.ZipFile(model_file_path, 'r') zip_ref.extractall(dir) zip_ref.close() zip_ref = zipfile.ZipFile(inference_symbol_path) zip_ref.extractall(dir) zip_ref.close() ###################################################################### # Convert and compile model with NNVM for CPU. sym = mx.sym.load("%s/%s/ssd_resnet50_inference.json" % (dir, inference_symbol_folder)) _, arg_params, aux_params = load_checkpoint( "%s/%s" % (dir, model_name), 0) net, params = from_mxnet(sym, arg_params, aux_params) shape_dict = {"data": dshape} with nnvm.compiler.build_config(opt_level=3): image, tvm_output = heterogeneous_ssd(net, ['nms'], shape_dict, params, test_image_path) ##################################################################### # Display result class_names = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] def display(img, out, thresh=0.5): import random import matplotlib as mpl import matplotlib.pyplot as plt mpl.rcParams['figure.figsize'] = (10, 10) pens = dict() plt.clf() plt.imshow(img) for det in out: cid = int(det[0]) if cid < 0: continue score = det[1] if score < thresh: continue if cid not in pens: pens[cid] = (random.random(), random.random(), random.random()) scales = [img.shape[1], img.shape[0]] * 2 xmin, ymin, xmax, ymax = [ int(p * s) for p, s in zip(det[2:6].tolist(), scales)] rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, edgecolor=pens[cid], linewidth=3) plt.gca().add_patch(rect) text = class_names[cid] plt.gca().text(xmin, ymin - 2, '{:s} {:.3f}'.format(text, score), bbox=dict(facecolor=pens[cid], alpha=0.5), fontsize=12, color='white') plt.show() image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) display(image, tvm_output.asnumpy()[0], thresh=0.45)
target = 'llvm' ctx = tvm.cpu(0) ###################################################################### # Prepare cfg and weights file # ---------------------------- # Pretrained model available https://pjreddie.com/darknet/imagenet/ # Download cfg and weights file first time. cfg_name = model_name + '.cfg' weights_name = model_name + '.weights' cfg_url = 'https://github.com/siju-samuel/darknet/blob/master/cfg/' + \ cfg_name + '?raw=true' weights_url = 'http://pjreddie.com/media/files/' + weights_name + '?raw=true' download(cfg_url, cfg_name) download(weights_url, weights_name) ###################################################################### # Download and Load darknet library # --------------------------------- darknet_lib = 'libdarknet.so' darknetlib_url = 'https://github.com/siju-samuel/darknet/blob/master/lib/' + \ darknet_lib + '?raw=true' download(darknetlib_url, darknet_lib) #if the file doesnt exist, then exit normally. if os.path.isfile('./' + darknet_lib) is False: exit(0)
from tvm.contrib.download import download from tvm.contrib import graph_runtime current_milli_time = lambda: int(round(time.time() * 1000)) test_image = "dog.jpg" dshape = (1, 3, 512, 512) #dshape = (1, 3, 608, 608) dtype = "float32" test_image_npy = "{}.{}.npy".format(test_image, dshape[2]) image_url = "https://cloud.githubusercontent.com/assets/3307514/20012567/" \ "cbb60336-a27d-11e6-93ff-cbc3f09f5c9e.jpg" download(image_url, test_image) # Preprocess image if os.path.isfile(test_image_npy): print("File {} exists, skip image preprocessing.".format(test_image_npy)) img_data = np.load(test_image_npy) else: import cv2 test_image_path = test_image image = cv2.imread(test_image_path) img_data = cv2.resize(image, (dshape[2], dshape[3])) img_data = img_data[:, :, (2, 1, 0)].astype(np.float32) img_data -= np.array([123, 117, 104]) img_data = np.transpose(np.array(img_data), (2, 0, 1)) img_data = np.expand_dims(img_data, axis=0)
out = 'yolov3.tx2.gpu' graph = load_tvm_graph('graph/{}'.format(out)) params = load_tvm_params('params/{}'.format(out)) lib = load_tvm_module('so/{}'.format(out), 'ptx') [neth, netw] = shape['data'][2:] # Current image shape is 608x608 ###################################################################### # Load a test image # -------------------------------------------------------------------- test_image = 'dog.jpg' print("Loading the test image...") img_url = 'https://github.com/siju-samuel/darknet/blob/master/data/' + \ test_image + '?raw=true' download(img_url, test_image) data = nnvm.testing.darknet.load_image(test_image, netw, neth) ###################################################################### # Execute on TVM Runtime # ---------------------- # The process is no different from other examples. from tvm.contrib import graph_runtime m = graph_runtime.create(graph, lib, ctx) # set inputs m.set_input('data', tvm.nd.array(data.astype(dtype))) m.set_input(**params) # execute print("Running the test image...")
def test_detection_models(): img = "test_street_small.jpg" img_url = ("https://raw.githubusercontent.com/dmlc/web-data/" "master/gluoncv/detection/street_small.jpg") download(img_url, img) input_shape = (1, 3, in_size, in_size) input_name = "input0" shape_list = [(input_name, input_shape)] scripted_model = generate_jit_model(1) mod, params = relay.frontend.from_pytorch(scripted_model, shape_list) data = process_image(img) data_np = data.detach().numpy() with torch.no_grad(): pt_res = scripted_model(data) def compile_and_run_vm(mod, params, data_np, target): with tvm.transform.PassContext(opt_level=3): vm_exec = relay.vm.compile(mod, target=target, params=params) dev = tvm.device(target, 0) vm = VirtualMachine(vm_exec, dev) vm.set_input("main", **{input_name: data_np}) return vm.run() for target in ["llvm"]: tvm_res = compile_and_run_vm(mod, params, data_np, target) # Bounding boxes tvm.testing.assert_allclose(pt_res[0].cpu().numpy(), tvm_res[0].numpy(), rtol=1e-5, atol=1e-5) # Scores tvm.testing.assert_allclose(pt_res[1].cpu().numpy(), tvm_res[1].numpy(), rtol=1e-5, atol=1e-5) # Class ids np.testing.assert_equal(pt_res[2].cpu().numpy(), tvm_res[2].numpy()) score_threshold = 0.9 print("Num boxes:", pt_res[0].cpu().numpy().shape[0]) print("Num valid boxes:", np.sum(pt_res[1].cpu().numpy() >= score_threshold)) before = mod["main"] mod = rewrite_nms_to_batched_nms(mod) after = mod["main"] assert not tvm.ir.structural_equal(after, before) # TODO(masahi): It seems this rewrite causes flaky segfaults on CI # See https://github.com/apache/tvm/issues/7363 # before = mod["main"] # mod = rewrite_batched_nms_with_max_out_size(mod) # after = mod["main"] # assert not tvm.ir.structural_equal(after, before) before = mod["main"] mod = rewrite_scatter_to_gather( mod, 4) # num_scales is 4 for maskrcnn_resnet50_fpn after = mod["main"] assert not tvm.ir.structural_equal(after, before) tvm_res_after_rewrite = compile_and_run_vm(mod, params, data_np, "llvm") # Results should be equivalent after rewriting for res1, res2 in zip(tvm_res, tvm_res_after_rewrite): tvm.testing.assert_allclose(res1.numpy(), res2.numpy())
model.eval() inp = torch.Tensor(np.random.uniform(0.0, 250.0, size=(1, 3, in_size, in_size))) with torch.no_grad(): out = model(inp) script_module = do_trace(model, inp) ###################################################################### # Download a test image and pre-process # ------------------------------------- img_path = "test_street_small.jpg" img_url = ( "https://raw.githubusercontent.com/dmlc/web-data/" "master/gluoncv/detection/street_small.jpg" ) download(img_url, img_path) img = cv2.imread(img_path).astype("float32") img = cv2.resize(img, (in_size, in_size)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.transpose(img / 255.0, [2, 0, 1]) img = np.expand_dims(img, axis=0) ###################################################################### # Import the graph to Relay # ------------------------- input_name = "input0" shape_list = [(input_name, input_shape)] mod, params = relay.frontend.from_pytorch(script_module, shape_list) ######################################################################
boxes, probs = nnvm.testing.yolo2_detection.get_region_boxes( region_layer, im_w, im_h, net.w, net.h, thresh, probs, boxes, 1, tvm_out) boxes, probs = nnvm.testing.yolo2_detection.do_nms_sort( boxes, probs, region_layer.w*region_layer.h*region_layer.n, region_layer.classes, 0.3) coco_name = 'coco.names' coco_url = 'https://github.com/siju-samuel/darknet/blob/master/data/' + coco_name + '?raw=true' font_name = 'arial.ttf' font_url = 'https://github.com/siju-samuel/darknet/blob/master/data/' + font_name + '?raw=true' from tvm.contrib.download import download download(coco_url, coco_name) download(font_url, font_name) with open(coco_name) as f: content = f.readlines() names = [x.strip() for x in content] nnvm.testing.yolo2_detection.draw_detections( img, region_layer.w*region_layer.h*region_layer.n, thresh, boxes, probs, names, region_layer.classes) #cv2.imshow('detection',img.transpose(1, 2, 0)) cv2.imwrite('detection.png',np.flip(img, 0).transpose(1, 2, 0)*255) #plt.imshow(img.transpose(1, 2, 0))
model_url = "https://github.com/zhreshold/mxnet-ssd/releases/download/v0.6/" \ "resnet50_ssd_512_voc0712_trainval.zip" image_url = "https://cloud.githubusercontent.com/assets/3307514/20012567/" \ "cbb60336-a27d-11e6-93ff-cbc3f09f5c9e.jpg" inference_symbol_folder = "c1904e900848df4548ce5dfb18c719c7-a28c4856c827fe766aa3da0e35bad41d44f0fb26" inference_symbol_url = "https://gist.github.com/kevinthesun/c1904e900848df4548ce5dfb18c719c7/" \ "archive/a28c4856c827fe766aa3da0e35bad41d44f0fb26.zip" dir = "ssd_model" if not os.path.exists(dir): os.makedirs(dir) model_file_path = "%s/%s" % (dir, model_file) test_image_path = "%s/%s" % (dir, test_image) inference_symbol_path = "%s/inference_model.zip" % dir download(model_url, model_file_path) download(image_url, test_image_path) download(inference_symbol_url, inference_symbol_path) zip_ref = zipfile.ZipFile(model_file_path, 'r') zip_ref.extractall(dir) zip_ref.close() zip_ref = zipfile.ZipFile(inference_symbol_path) zip_ref.extractall(dir) zip_ref.close() ###################################################################### # Convert and compile model with NNVM for CPU. sym = mx.sym.load("%s/%s/ssd_resnet50_inference.json" % (dir, inference_symbol_folder)) _, arg_params, aux_params = load_checkpoint("%s/%s" % (dir, model_name), 0)
# Obtain ResNet model and download them into _data dir url = "https://github.com/uwsaml/web-data/raw/master/vta/models/" categ_fn = 'synset.txt' graph_fn = 'resnet18_qt8.json' params_fn = 'resnet18_qt8.params' # Create data dir data_dir = "_data/" if not os.path.exists(data_dir): os.makedirs(data_dir) # Download files for file in [categ_fn, graph_fn, params_fn]: if not os.path.isfile(file): download(os.path.join(url, file), os.path.join(data_dir, file)) # Read in ImageNet Categories synset = eval(open(os.path.join(data_dir, categ_fn)).read()) # Download pre-tuned op parameters of conv2d for ARM CPU used in VTA autotvm.tophub.check_backend('vta') ###################################################################### # Setup the Pynq Board's RPC Server # --------------------------------- # Build the RPC server's VTA runtime and program the Pynq FPGA. # Measure build start time reconfig_start = time.time()
model_url = "https://github.com/zhreshold/mxnet-ssd/releases/download/v0.6/" \ "resnet50_ssd_512_voc0712_trainval.zip" image_url = "https://cloud.githubusercontent.com/assets/3307514/20012567/" \ "cbb60336-a27d-11e6-93ff-cbc3f09f5c9e.jpg" inference_symbol_folder = "c1904e900848df4548ce5dfb18c719c7-a28c4856c827fe766aa3da0e35bad41d44f0fb26" inference_symbol_url = "https://gist.github.com/kevinthesun/c1904e900848df4548ce5dfb18c719c7/" \ "archive/a28c4856c827fe766aa3da0e35bad41d44f0fb26.zip" dir = "ssd_model" if not os.path.exists(dir): os.makedirs(dir) model_file_path = "%s/%s" % (dir, model_file) test_image_path = "%s/%s" % (dir, test_image) inference_symbol_path = "%s/inference_model.zip" % dir download(model_url, model_file_path) download(image_url, test_image_path) download(inference_symbol_url, inference_symbol_path) zip_ref = zipfile.ZipFile(model_file_path, 'r') zip_ref.extractall(dir) zip_ref.close() zip_ref = zipfile.ZipFile(inference_symbol_path) zip_ref.extractall(dir) zip_ref.close() ###################################################################### # Convert and compile model with NNVM or Relay for CPU. sym = mx.sym.load("%s/%s/ssd_resnet50_inference.json" % (dir, inference_symbol_folder)) _, arg_params, aux_params = load_checkpoint("%s/%s" % (dir, model_name), 0)
from nnvm.testing.darknet import __darknetffi__ # Model name MODEL_NAME = 'yolov3' ###################################################################### # Download required files # ----------------------- # Download cfg and weights file if first time. CFG_NAME = MODEL_NAME + '.cfg' WEIGHTS_NAME = MODEL_NAME + '.weights' REPO_URL = 'https://github.com/siju-samuel/darknet/blob/master/' CFG_URL = REPO_URL + 'cfg/' + CFG_NAME + '?raw=true' WEIGHTS_URL = 'https://pjreddie.com/media/files/' + WEIGHTS_NAME download(CFG_URL, CFG_NAME) download(WEIGHTS_URL, WEIGHTS_NAME) # 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) download(DARKNET_URL, DARKNET_LIB)
exec_flavor = "vulkan" model_name = 'extraction' ###################################################################### # Prepare cfg and weights file # ---------------------------- # Pretrained model available https://pjreddie.com/darknet/imagenet/ # Download cfg and weights file first time. cfg_name = model_name + '.cfg' weights_name = model_name + '.weights' cfg_url = 'https://github.com/siju-samuel/darknet/blob/master/cfg/' + \ cfg_name + '?raw=true' weights_url = 'http://pjreddie.com/media/files/' + weights_name + '?raw=true' download(cfg_url, cfg_name) download(weights_url, weights_name) ###################################################################### # Download and Load darknet library # --------------------------------- dtype = 'float32' darknet_lib = 'libdarknet.so' darknetlib_url = 'https://github.com/siju-samuel/darknet/blob/master/lib/' + \ darknet_lib + '?raw=true' download(darknetlib_url, darknet_lib) #if the file doesnt exist, then exit normally. if os.path.isfile('./' + darknet_lib) is False: exit(0)