def test_extractor_index(): with pytest.raises(TypeError) as execinfo: ex = ncnn.Extractor() assert "No constructor" in str(execinfo.value) dr = ncnn.DataReaderFromEmpty() net = ncnn.Net() net.load_param("tests/test.param") net.load_model(dr) in_mat = ncnn.Mat((227, 227, 3)) ex = net.create_extractor() ex.set_light_mode(True) ex.set_num_threads(2) ex.set_blob_allocator(alloctor) ex.set_workspace_allocator(alloctor) ex.input(0, in_mat) ret, out_mat = ex.extract(1) assert (ret == 0 and out_mat.dims == 3 and out_mat.w == 225 and out_mat.h == 225 and out_mat.c == 3) ret, out_mat = ex.extract(2) assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
def __init__(self, img_width=416, img_height=416, num_threads=1, use_gpu=False): self.img_width = img_width self.img_height = img_height self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [1.0, 1.0, 1.0] self.norm_vals = [0.007843, 0.007843, 0.007843] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # original pretrained model from https://github.com/eric612/MobileNet-YOLO # https://github.com/eric612/MobileNet-YOLO/blob/master/models/yolov2/mobilenet_yolo_deploy.prototxt # https://github.com/eric612/MobileNet-YOLO/blob/master/models/yolov2/mobilenet_yolo_deploy_iter_80000.caffemodel # the ncnn model https://github.com/caishanli/pyncnn-assets/tree/master/models self.net.load_param(get_model_file("mobilenet_yolo.param")) self.net.load_model(get_model_file("mobilenet_yolo.bin")) self.class_names = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
def __init__(self, target_size=304, num_threads=1, use_gpu=False): self.target_size = target_size self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [103.9, 116.7, 123.6] self.norm_vals = [0.017, 0.017, 0.017] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # model is converted from https://github.com/eric612/MobileNet-YOLO # and can be downloaded from https://drive.google.com/open?id=1Wt6jKv13sBRMHgrGAJYlOlRF-o80pC0g # the ncnn model https://github.com/caishanli/pyncnn-assets/tree/master/models self.net.load_param(get_model_file("pelee.param")) self.net.load_model(get_model_file("pelee.bin")) self.class_names = [ "background", "person", "rider", "car", "bus", "truck", "bike", "motor", "traffic light", "traffic sign", "train", ]
def __init__(self): self.net = ncnn.Net() self.param = "model/mobilefacenets.param" self.model = "model/mobilefacenets.bin" self.net.load_param(self.param) self.net.load_model(self.model) self.num_threads = 2
def __init__(self, img_width=600, img_height=600, num_threads=1, use_gpu=False, max_per_image=100, confidence_thresh=0.05, nms_threshold=0.3): self.img_width = img_width self.img_height = img_height self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [102.9801, 115.9465, 122.7717] self.norm_vals = [] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # original pretrained model from https://github.com/rbgirshick/py-faster-rcnn # py-faster-rcnn/models/pascal_voc/ZF/faster_rcnn_alt_opt/faster_rcnn_test.pt # https://dl.dropboxusercontent.com/s/o6ii098bu51d139/faster_rcnn_models.tgz?dl=0 # ZF_faster_rcnn_final.caffemodel # the ncnn model https://github.com/caishanli/pyncnn-assets/tree/master/models self.net.load_param(get_model_file("ZF_faster_rcnn_final.param")) self.net.load_model(get_model_file("ZF_faster_rcnn_final.bin")) self.max_per_image = max_per_image self.confidence_thresh = confidence_thresh self.nms_threshold = nms_threshold self.class_names = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
def __init__(self, img_width=300, img_height=300, num_threads=1, use_gpu=False): self.img_width = img_width self.img_height = img_height self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [123.675, 116.28, 103.53] self.norm_vals = [1.0, 1.0, 1.0] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # converted ncnn model from https://github.com/ujsyehao/mobilenetv3-ssd # the ncnn model https://github.com/caishanli/pyncnn-assets/tree/master/models self.net.load_param(get_model_file("mobilenetv3_ssdlite_voc.param")) self.net.load_model(get_model_file("mobilenetv3_ssdlite_voc.bin")) self.class_names = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ]
def __init__(self, img_width=300, img_height=300, num_threads=1, use_gpu=False): self.img_width = img_width self.img_height = img_height self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [104.0, 117.0, 123.0] self.norm_vals = [] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # original pretrained model from https://github.com/chuanqi305/SqueezeNet-SSD # squeezenet_ssd_voc_deploy.prototxt # https://drive.google.com/open?id=0B3gersZ2cHIxdGpyZlZnbEQ5Snc # the ncnn model https://github.com/caishanli/pyncnn-assets/tree/master/models self.net.load_param(get_model_file("squeezenet_ssd_voc.param")) self.net.load_model(get_model_file("squeezenet_ssd_voc.bin")) self.class_names = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ]
def test_extractor(): with pytest.raises(TypeError, match="No constructor"): ex = ncnn.Extractor() dr = ncnn.DataReaderFromEmpty() net = ncnn.Net() net.load_param("tests/test.param") net.load_model(dr) in_mat = ncnn.Mat((227, 227, 3)) with net.create_extractor() as ex: ex.set_light_mode(True) ex.set_num_threads(2) ex.set_blob_allocator(alloctor) ex.set_workspace_allocator(alloctor) ex.input("data", in_mat) ret, out_mat = ex.extract("conv0_fwd") assert (ret == 0 and out_mat.dims == 3 and out_mat.w == 225 and out_mat.h == 225 and out_mat.c == 3) ret, out_mat = ex.extract("output") assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
def test_extractor_index(): with pytest.raises(TypeError, match="No constructor"): ex = ncnn.Extractor() dr = ncnn.DataReaderFromEmpty() net = ncnn.Net() net.load_param("tests/test.param") net.load_model(dr) in_mat = ncnn.Mat((227, 227, 3)) ex = net.create_extractor() ex.set_light_mode(True) ex.set_num_threads(2) ex.set_blob_allocator(alloctor) ex.set_workspace_allocator(alloctor) ex.input(0, in_mat) ret, out_mat = ex.extract(1) assert (ret == 0 and out_mat.dims == 3 and out_mat.w == 225 and out_mat.h == 225 and out_mat.c == 3) ret, out_mat = ex.extract(2) assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 # not use with sentence, call clear manually to ensure ex destruct before net ex.clear()
def __init__(self, img_width=300, img_height=300, num_threads=1, use_gpu=False): self.img_width = img_width self.img_height = img_height self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [127.5, 127.5, 127.5] self.norm_vals = [0.007843, 0.007843, 0.007843] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # model is converted from https://github.com/chuanqi305/MobileNet-SSD # and can be downloaded from https://drive.google.com/open?id=0ByaKLD9QaPtucWk0Y0dha1VVY0U # the ncnn model https://github.com/caishanli/pyncnn-assets/tree/master/models self.net.load_param(get_model_file("mobilenet_ssd_voc_ncnn.param")) self.net.load_model(get_model_file("mobilenet_ssd_voc_ncnn.bin")) self.class_names = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ]
def __init__(self, target_size=224, max_per_image=100, confidence_thresh=0.6, nms_threshold=0.3, num_threads=1, use_gpu=False): self.target_size = target_size self.max_per_image = max_per_image self.confidence_thresh = confidence_thresh self.nms_threshold = nms_threshold self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [102.9801, 115.9465, 122.7717] self.norm_vals = [] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # original pretrained model from https://github.com/YuwenXiong/py-R-FCN # https://github.com/YuwenXiong/py-R-FCN/blob/master/models/pascal_voc/ResNet-50/rfcn_end2end/test_agnostic.prototxt # https://1drv.ms/u/s!AoN7vygOjLIQqUWHpY67oaC7mopf # resnet50_rfcn_final.caffemodel # the ncnn model https://github.com/caishanli/pyncnn-assets/tree/master/models self.net.load_param(get_model_file("rfcn_end2end.param")) self.net.load_model(get_model_file("rfcn_end2end.bin")) self.class_names = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ]
def __init__(self, img_width=352, img_height=352, num_threads=1, use_gpu=False): self.img_width = img_width self.img_height = img_height self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [127.5, 127.5, 127.5] self.norm_vals = [0.007843, 0.007843, 0.007843] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # original pretrained model from https://github.com/eric612/MobileNet-YOLO # param : https://drive.google.com/open?id=1V9oKHP6G6XvXZqhZbzNKL6FI_clRWdC- # bin : https://drive.google.com/open?id=1DBcuFCr-856z3FRQznWL_S5h-Aj3RawA # the ncnn model https://github.com/caishanli/pyncnn-assets/tree/master/models self.net.load_param(get_model_file("mobilenetv2_yolov3.param")) self.net.load_model(get_model_file("mobilenetv2_yolov3.bin")) self.class_names = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
def __init__(self, target_width=192, target_height=256, num_threads=1, use_gpu=False): self.target_width = target_width self.target_height = target_height self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [0.485 * 255.0, 0.456 * 255.0, 0.406 * 255.0] self.norm_vals = [ 1 / 0.229 / 255.0, 1 / 0.224 / 255.0, 1 / 0.225 / 255.0 ] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # the simple baseline human pose estimation from gluon-cv # https://gluon-cv.mxnet.io/build/examples_pose/demo_simple_pose.html # mxnet model exported via # pose_net.hybridize() # pose_net.export('pose') # then mxnet2ncnn # the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models self.net.load_param(get_model_file("pose.param")) self.net.load_model(get_model_file("pose.bin"))
def test_net_vulkan(): if not hasattr(ncnn, "get_gpu_count"): return dr = ncnn.DataReaderFromEmpty() net = ncnn.Net() net.opt.use_vulkan_compute = True ret = net.load_param("tests/test.param") net.load_model(dr) assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3 in_mat = ncnn.Mat((227, 227, 3)) ex = net.create_extractor() ex.input("data", in_mat) ret, out_mat = ex.extract("output") assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 net.clear() assert len(net.blobs()) == 0 and len(net.layers()) == 0 # ensure ex release before net when use vulkan ex = None net = None
def benchmark(comment, _in, opt): _in.fill(0.01) g_blob_pool_allocator.clear() g_workspace_pool_allocator.clear() if opt.use_vulkan_compute: g_blob_vkallocator.clear() g_staging_vkallocator.clear() net = ncnn.Net() net.opt = opt if net.opt.use_vulkan_compute: net.set_vulkan_device(g_vkdev) net.load_param(param_root + comment + ".param") dr = ncnn.DataReaderFromEmpty() net.load_model(dr) input_names = net.input_names() output_names = net.output_names() if g_enable_cooling_down: time.sleep(10) # warm up for i in range(g_warmup_loop_count): # test with statement with net.create_extractor() as ex: ex.input(input_names[0], _in) ex.extract(output_names[0]) time_min = sys.float_info.max time_max = -sys.float_info.max time_avg = 0.0 for i in range(g_loop_count): start = time.time() # test net keep alive until ex freed ex = net.create_extractor() ex.input(input_names[0], _in) ex.extract(output_names[0]) end = time.time() timespan = end - start time_min = timespan if timespan < time_min else time_min time_max = timespan if timespan > time_max else time_max time_avg += timespan time_avg /= g_loop_count print("%20s min = %7.2f max = %7.2f avg = %7.2f" % (comment, time_min * 1000, time_max * 1000, time_avg * 1000))
def benchmark(comment, _in, opt): _in.fill(0.01) g_blob_pool_allocator.clear() g_workspace_pool_allocator.clear() if opt.use_vulkan_compute: g_blob_vkallocator.clear() g_staging_vkallocator.clear() net = ncnn.Net() net.opt = opt if net.opt.use_vulkan_compute: net.set_vulkan_device(g_vkdev) net.load_param(param_root + comment + ".param") dr = ncnn.DataReaderFromEmpty() net.load_model(dr) if g_enable_cooling_down: time.sleep(10) # warm up for i in range(g_warmup_loop_count): ex = net.create_extractor() ex.input("data", _in) ex.extract("output") time_min = sys.float_info.max time_max = -sys.float_info.max time_avg = 0.0 for i in range(g_loop_count): start = time.time() ex = net.create_extractor() ex.input("data", _in) ex.extract("output") end = time.time() timespan = end - start time_min = timespan if timespan < time_min else time_min time_max = timespan if timespan > time_max else time_max time_avg += timespan # extractor need relese manually when build ncnn with vuklan, # due to python relese ex after net, but in extractor.destruction use net ex = None time_avg /= g_loop_count print("%20s min = %7.2f max = %7.2f avg = %7.2f" % (comment, time_min * 1000, time_max * 1000, time_avg * 1000))
def test_vulkan_device_index(): if not hasattr(ncnn, "get_gpu_count"): return net = ncnn.Net() assert net.vulkan_device() is None net.set_vulkan_device(0) assert net.vulkan_device() is not None
def test_vulkan_device_vkdev(): if not hasattr(ncnn, "get_gpu_count"): return net = ncnn.Net() assert net.vulkan_device() is None vkdev = ncnn.get_gpu_device(0) net.set_vulkan_device(vkdev) assert net.vulkan_device() is not None
def test_custom_layer(): class CustomLayer(ncnn.Layer): customLayers = [] def __init__(self): ncnn.Layer.__init__(self) self.one_blob_only = True self.customLayers.append(self) def forward(self, bottom_blob, top_blob, opt): x = np.array(bottom_blob) x += 1 top_blob.clone_from(ncnn.Mat(x), opt.blob_allocator) if top_blob.empty(): return -100 return 0 def CustomLayer_layer_creator(): return CustomLayer() def CustomLayer_layer_destroyer(layer): for i in range(len(CustomLayer.customLayers)): if CustomLayer.customLayers[i] == layer: del CustomLayer.customLayers[i] break dr = ncnn.DataReaderFromEmpty() net = ncnn.Net() net.register_custom_layer("CustomLayer", CustomLayer_layer_creator, CustomLayer_layer_destroyer) ret = net.load_param("tests/custom_layer.param") net.load_model(dr) assert ret == 0 and len(net.blobs()) == 2 and len(net.layers()) == 2 in_mat = ncnn.Mat(1) in_mat.fill(1.0) ex = net.create_extractor() ex.input("data", in_mat) ret, out_mat = ex.extract("output") assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 and out_mat[ 0] == 2.0 ex.clear() net.clear() assert len(net.blobs()) == 0 and len(net.layers()) == 0
def __init__(self, model_param, model_bin, *args, **kwargs): import ncnn super(NanoDetNCNN, self).__init__(*args, **kwargs) print(f'Using ncnn as inference backend') print(f'Using param: {model_param}, bin: {model_bin}') # load model self.model_param = model_param self.model_bin = model_bin self.net = ncnn.Net() self.net.load_param(model_param) self.net.load_model(model_bin) self.input_name = "input.1"
def __init__(self, target_size=227, num_threads=1, use_gpu=False): self.target_size = target_size self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [104.0, 117.0, 123.0] self.norm_vals = [] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models self.net.load_param(get_model_file("squeezenet_v1.1.param")) self.net.load_model(get_model_file("squeezenet_v1.1.bin"))
def __init__(self, prob_threshold=0.8, nms_threshold=0.4, num_threads=1, use_gpu=False): self.prob_threshold = prob_threshold self.nms_threshold = nms_threshold self.num_threads = num_threads self.use_gpu = use_gpu self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # model is converted from # https://github.com/deepinsight/insightface/tree/master/RetinaFace#retinaface-pretrained-models # https://github.com/deepinsight/insightface/issues/669 # the ncnn model https://github.com/caishanli/pyncnn-assets/tree/master/models self.net.load_param(get_model_file("mnet.25-opt.param")) self.net.load_model(get_model_file("mnet.25-opt.bin"))
def __init__(self, img_width=227, img_height=227, num_threads=1, use_gpu=False): self.img_width = img_width self.img_height = img_height self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [104.0, 117.0, 123.0] self.norm_vals = [] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # the ncnn model https://github.com/caishanli/pyncnn-assets/tree/master/models self.net.load_param(get_model_file("squeezenet_v1.1.param")) self.net.load_model(get_model_file("squeezenet_v1.1.bin"))
def __init__(self, target_size=224, num_threads=1, use_gpu=False): self.target_size = target_size self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [] self.norm_vals = [1 / 255.0, 1 / 255.0, 1 / 255.0] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # https://github.com/miaow1988/ShuffleNet_V2_pytorch_caffe # models can be downloaded from https://github.com/miaow1988/ShuffleNet_V2_pytorch_caffe/releases # the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models self.net.load_param(get_model_file("shufflenet_v2_x0.5.param")) self.net.load_model(get_model_file("shufflenet_v2_x0.5.bin"))
def benchmark(comment, _in, opt): _in.fill(0.01) net = ncnn.Net() net.opt = opt net.load_param("params/" + comment + ".param") dr = ncnn.DataReaderFromEmpty() net.load_model(dr) out = ncnn.Mat() #warm up for i in range(g_warmup_loop_count): ex = net.create_extractor() ex.input("data", _in) ex.extract("output", out) time_min = sys.float_info.max time_max = -sys.float_info.max time_avg = 0.0 for i in range(g_loop_count): start = time.time() ex = net.create_extractor() ex.input("data", _in) ex.extract("output", out) end = time.time() timespan = end - start time_min = timespan if timespan < time_min else time_min time_max = timespan if timespan > time_max else time_max time_avg += timespan # extractor need relese manually when build ncnn with vuklan, # due to python relese ex after net, but in extractor.destruction use net ex = None time_avg /= g_loop_count print("%20s min = %7.2f max = %7.2f avg = %7.2f" % (comment, time_min * 1000, time_max * 1000, time_avg * 1000))
def test_net(): dr = ncnn.DataReaderFromEmpty() with ncnn.Net() as net: ret = net.load_param("tests/test.param") net.load_model(dr) assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3 in_mat = ncnn.Mat((227, 227, 3)) with net.create_extractor() as ex: ex.input("data", in_mat) ret, out_mat = ex.extract("output") assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 net.clear() assert len(net.blobs()) == 0 and len(net.layers()) == 0
def __init__(self, target_size=300, num_threads=1, use_gpu=False): self.target_size = target_size self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [127.5, 127.5, 127.5] self.norm_vals = [0.007843, 0.007843, 0.007843] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu # self.net.register_custom_layer("Silence", Noop_layer_creator) # original pretrained model from https://github.com/chuanqi305/MobileNetv2-SSDLite # https://github.com/chuanqi305/MobileNetv2-SSDLite/blob/master/ssdlite/voc/deploy.prototxt # the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models self.net.load_param(get_model_file("mobilenetv2_ssdlite_voc.param")) self.net.load_model(get_model_file("mobilenetv2_ssdlite_voc.bin")) self.class_names = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor", ]
def __init__( self, target_size=550, confidence_threshold=0.05, nms_threshold=0.5, keep_top_k=200, num_threads=1, use_gpu=False, ): self.target_size = target_size self.confidence_threshold = confidence_threshold self.nms_threshold = nms_threshold self.keep_top_k = keep_top_k self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [123.68, 116.78, 103.94] self.norm_vals = [1.0 / 58.40, 1.0 / 57.12, 1.0 / 57.38] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu self.net.opt.num_threads = self.num_threads # original model converted from https://github.com/dbolya/yolact # yolact_resnet50_54_800000.pth # the ncnn model https://github.com/caishanli/pyncnn-assets/tree/master/models self.net.load_param(get_model_file("yolact.param")) self.net.load_model(get_model_file("yolact.bin")) self.conv_ws = [69, 35, 18, 9, 5] self.conv_hs = [69, 35, 18, 9, 5] self.aspect_ratios = [1, 0.5, 2] self.scales = [24, 48, 96, 192, 384] self.priors = None self.last_img_size = None self.make_priors() self.class_names = [ "background", "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush", ]
# keras_model_in.summary() keras_model = apply_transformations(keras_model_in) adapted_keras_model = adapt_keras_model(keras_model, keras_model.name) check_transform(keras_model_in, adapted_keras_model, False) string_list, weight_list, layer_name_list = conver_model( adapted_keras_model, False, False) export_path = os.path.join(export_root, '', keras_model.name) os.makedirs(export_path, exist_ok=True) out_config_path, out_weights_path = save_config(string_list, weight_list, adapted_keras_model.name, export_path, debug=False) net = ncnn.Net() net.load_param(out_config_path) net.load_model(out_weights_path) num_threads = 4 error_th = 1.e-5 ex = net.create_extractor() ex.set_num_threads(num_threads) if type(keras_model.input_shape) != list: target_shape_list = [keras_model.input_shape] else: target_shape_list = keras_model.input_shape keras_in_list = [] for input_index, target_shape in enumerate(target_shape_list):
def __init__( self, target_size=640, prob_threshold=0.25, nms_threshold=0.45, num_threads=1, use_gpu=False, ): self.target_size = target_size self.prob_threshold = prob_threshold self.nms_threshold = nms_threshold self.num_threads = num_threads self.use_gpu = use_gpu self.mean_vals = [] self.norm_vals = [1 / 255.0, 1 / 255.0, 1 / 255.0] self.net = ncnn.Net() self.net.opt.use_vulkan_compute = self.use_gpu self.net.opt.num_threads = self.num_threads self.net.register_custom_layer("YoloV5Focus", YoloV5Focus_layer_creator, YoloV5Focus_layer_destroyer) # original pretrained model from https://github.com/ultralytics/yolov5 # the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models self.net.load_param(get_model_file("yolov5s.param")) self.net.load_model(get_model_file("yolov5s.bin")) self.grid = [make_grid(10, 6), make_grid(20, 12), make_grid(40, 24)] self.stride = np.array([32, 16, 8]) self.anchor_grid = np.array([ [116, 90, 156, 198, 373, 326], [30, 61, 62, 45, 59, 119], [10, 13, 16, 30, 33, 23], ]).reshape((3, 1, 3, 1, 1, 2)) self.class_names = [ "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush", ]