示例#1
0
def demo_file(args, imagelist,imagefile):
    # 1. load the configure file
    cfg_from_file(args.confg_file)
    # 2. load detector based on the configure file
    object_detector = ObjectDetector()
    args = parse_args()
    for image_name in imagelist:
    # 3. load image
        image_path = os.path.join(imagefile,image_name)
        image = cv2.imread(image_path)

    # 4. detect
        _labels, _scores, _coords = object_detector.predict(image)

    # 5. draw bounding box on the image
        for labels, scores, coords in zip(_labels, _scores, _coords):
            cv2.rectangle(image, (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])), COLORS[labels % 3], 2)
            cv2.putText(image, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels], score=scores), (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)
    
    # 6. visualize result
        if args.display is True:
            cv2.imshow('result', image)
            cv2.waitKey(800)
        '''
    # 7. write result
       # if args.save is True:
        path = './'
        path = os.path.join(path,image_name)
        print(path)
        #path, _ = os.path.splitext(image_path)
        #print(path)
        cv2.imwrite(path, image)
        '''
        '''
示例#2
0
def test():
    confg_file = '/home/hby/mycode/ssds.pytorch-1/experiments/cfgs/my_ssdlite_mobilenetv2_6.yml'
    cfg_from_file(confg_file)

    model, priors = create_model(cfg.MODEL)
        # Utilize GPUs for computation
    use_gpu = torch.cuda.is_available()
    #half = False
    if use_gpu:
        print('Utilize GPUs for computation')
        print('Number of GPU available', torch.cuda.device_count())
        model.cuda()
        # self.model = torch.nn.DataParallel(self.model).module
        # Utilize half precision
        half = cfg.MODEL.HALF_PRECISION
        if half:
            model = model.half()

    pthfile = r'/home/hby/mycode/ssds.pytorch-1/experiments/models/ssd_mobilenet_v2_voc_6/ssd_lite_mobilenet_v2_voc_epoch_400.pth'
    checkpoint = torch.load(pthfile, map_location='cuda' if use_gpu else 'cpu')
    model.load_state_dict(checkpoint)
    
    #data type nchw
    dummy_input1 = torch.randn(1, 3, 300, 300).cuda().half()
    # dummy_input2 = torch.randn(1, 3, 64, 64)
    # dummy_input3 = torch.randn(1, 3, 64, 64)
    input_names = [ "actual_input_1"]
    output_names = [ "output1" ]
    # torch.onnx.export(model, (dummy_input1, dummy_input2, dummy_input3), "C3AE.onnx", verbose=True, input_names=input_names, output_names=output_names)
    torch.onnx.export(model, dummy_input1, "ssdmobilenet_half.onnx", verbose=True, input_names=input_names, output_names=output_names)
示例#3
0
def test():
    with torch.cuda.device(0):
        with torch.no_grad():
            args = parse_args()
            if args.config_file is not None:
                cfg_from_file(args.config_file)
        #test_model()
            s = Solver(args)
            model = s.model
            _t = Timer()

            batch_size = 16

            timing_array = []
            for i in range(1000):

                _t.tic()
                batch = torch.FloatTensor(batch_size, 3, cfg.DATASET.IMAGE_SIZE[0], cfg.DATASET.IMAGE_SIZE[1]).cuda(0)
                model = add_flops_counting_methods(model)
                model.eval().start_flops_count()
                out = model(batch)
                inf_time = _t.toc()
                timing_array.append(inf_time)

            print("Inference Time Mean: {:0.6f} Std Dev: {:0.6f}".format(np.mean(timing_array)*1000/batch_size, np.std(timing_array)*1000/batch_size))

            #print(model)


        #print('Output shape: {}'.format(list(out.shape)))
            print('Flops:  {}'.format(flops_to_string(model.compute_average_flops_cost())))
            print('Params: ' + get_model_parameters_number(model))
示例#4
0
def train():
    args = parse_args()
    if args.config_file is not None:
        print("cfg file: %s" % args.config_file)
        cfg_from_file(args.config_file)

    train_model()
示例#5
0
def time_benchmark(args, image_path):
    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load imagedemo
    image = cv2.imread(image_path)

    # 4. time test
    warmup = 20
    time_iter = 100
    print('Warmup the detector...')
    _t = list()
    for i in range(warmup+time_iter):
        _, _, _, (total_time, preprocess_time, net_forward_time, detect_time, output_time) \
            = object_detector.predict(image, check_time=True)
        if i > warmup:
            _t.append([total_time, preprocess_time, net_forward_time, detect_time, output_time])
            if i % 20 == 0: 
                print('In {}\{}, total time: {} \n preprocess: {} \n net_forward: {} \n detect: {} \n output: {}'.format(
                    i-warmup, time_iter, total_time, preprocess_time, net_forward_time, detect_time, output_time
                ))
    total_time, preprocess_time, net_forward_time, detect_time, output_time = np.sum(_t, axis=0)/time_iter
    print('In average, total time: {} \n preprocess: {} \n net_forward: {} \n detect: {} \n output: {}'.format(
        total_time, preprocess_time, net_forward_time, detect_time, output_time
    ))
示例#6
0
def demo(args, image_path):
    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load image
    image = cv2.imread(image_path)
    image256 = image[300:556]
    # 4. detect
    _labels, _scores, _coords = object_detector.predict(image256)

    # 5. draw bounding box on the image
    for labels, scores, coords in zip(_labels, _scores, _coords):
        cv2.rectangle(image256, (int(coords[0]), int(coords[1])),
                      (int(coords[2]), int(coords[3])), COLORS[labels % 3], 2)
        cv2.putText(
            image256, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels],
                                                    score=scores),
            (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)

    # 6. visualize result
    if args.display is True:
        cv2.imshow('result', image)
        cv2.waitKey(0)

    # 7. write result
    if args.save is True:
        # path, _ = os.path.splitext(image_path)

        img_name = image_path.split("/")[-1]
        cv2.imwrite(os.path.join(args.test_out_img_path, img_name), image)
示例#7
0
def test():
    with torch.cuda.device(0):
        with torch.no_grad():
            args = parse_args()
            if args.config_file is not None:
                cfg_from_file(args.config_file)
        #test_model()
            s = Solver(args)
            model = s.model
            batch = torch.FloatTensor(1, 3, 300, 300).cuda(0)

            fmap_size = _forward_features_size(model, img_size=[300, 300])

            model = add_flops_counting_methods(model)
            model.eval().start_flops_count()
            out = model(batch)

            print(model)

            #print('Output shape: {}'.format(list(out.shape)))
            print('Flops:  {}'.format(
                flops_to_string(model.compute_average_flops_cost())))
            print('Params: ' + get_model_parameters_number(model))
            print('Feature map size')
            print(fmap_size)
示例#8
0
def train():
    args = parse_args()
    # print(args)
    if args.config_file is not None:
        cfg_from_file(args.config_file)

    from lib.ssds_train import train_model

    train_model()
示例#9
0
def demo(args, image_path):

    input_size = (300, 300)

    # 加载模型 
    net = cv2.dnn.Net_readFromModelOptimizer(args.model+".xml", args.model+".bin")
    # 设置推理引擎后端
    net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
    # 设置运算设备
    net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)

    # 打开视频文件或摄像头 
    if args.demo_file:
        cap = cv2.VideoCapture(args.demo_file)
    else:
        cap = cv2.VideoCapture(0)

    # while True:

    # 读取一帧图像
    ret, frame = cap.read()
    # 将图片转换成模型输入
    blob = cv2.dnn.blobFromImage(frame, 0.007843, input_size, (127.5, 127.5, 127.5), False)

    # 转换后的待输入对象blob设置为网络输入
    net.setInput(blob)

    # # 开始进行网络推理运算
    # out = net.forward()

    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector(net)

    # # 3. load image
    # image = cv2.imread(image_path)

    # 4. detect
    _labels, _scores, _coords = object_detector.predict(frame)

    # 5. draw bounding box on the image
    for labels, scores, coords in zip(_labels, _scores, _coords):
        cv2.rectangle(frame, (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])), COLORS[labels % 3], 2)
        cv2.putText(frame, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels], score=scores), (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)
    
    # 6. visualize result
    if args.display is True:
        cv2.imshow('result', frame)
        cv2.waitKey(0)

    # 7. write result
    if args.save is True:
        path, _ = os.path.splitext(image_path)
        cv2.imwrite(path + '_result.jpg', frame)
示例#10
0
def predict():
    args = parse_args()

    cfg_from_file(args.cfg)

    detector = ObjectDetector()

    img = cv2.imread(img_f)

    _labels, _scores, _coords = detector.predict(img)
    print('labels: {}\nscores: {}\ncoords: {}'.format(_labels, _scores, _coords))
示例#11
0
def demo_live(args, video_path):
    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load video
    video = cv2.VideoCapture(video_path)

    index = -1
    while (video.isOpened()):
        index = index + 1
        sys.stdout.write('Process image: {} \r'.format(index))
        sys.stdout.flush()

        # 4. read image
        flag, image = video.read()
        if flag == False:
            print("Can not read image in Frame : {}".format(index))
            break

        # 5. detect
        _labels, _scores, _coords = object_detector.predict(image,
                                                            threshold=0.6)

        # 6. draw bounding box on the image
        for labels, scores, coords in zip(_labels, _scores, _coords):
            cv2.rectangle(image, (int(coords[0]), int(coords[1])),
                          (int(coords[2]), int(coords[3])), COLORS[labels % 3],
                          2)
            cv2.putText(
                image, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels],
                                                     score=scores),
                (int(coords[0]), int(coords[1])), FONT, 0.5,
                COLORS[labels % 3], 2)

        # 7. visualize result
        if args.display is True:
            cv2.imshow('result', image)
            cv2.waitKey(33)

        # 8. write result
        if args.save is True:
            path, _ = os.path.splitext(video_path)
            path = path + '_result'
            if not os.path.exists(path):
                os.mkdir(path)
            cv2.imwrite(path + '/{}.jpg'.format(index), image)
示例#12
0
def demo_video(args, video_path):
    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load video
    video = cv2.VideoCapture(video_path)
    index = -1
    frametime10 = np.zeros((10,1))
    while(video.isOpened()):
        #sys.stdout.write('Process image: {} \r'.format(index))
        #sys.stdout.flush()
        # 4. read image
        flag, image = video.read()
        t0 = timeit.default_timer()
        if flag == False:
            #print("Can not read image in Frame : {}".format(index))
            break

        # 5. detect
        _labels, _scores, _coords,elapsed = object_detector.predict(image)

        # 6. draw bounding box on the image
        for labels, scores, coords in zip(_labels, _scores, _coords):
            cv2.rectangle(image, (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])), COLORS[labels % 3], 5)
            cv2.putText(image, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels], score=scores), (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)
        
        #print(elapsed)
        index = index + 1
        index = index%10
        #frametime10[index] = elapsed
        #fps = 1/np.average(frametime10)
        fps = 1/elapsed
        cv2.putText(image, '{fps:%.5s}'%(fps), (int(50), int(50)), FONT, 2,COLORS[1])
        #print(fps)
        # 7. visualize result
        if args.display is True:
            image = cv2.resize(image,(540,960))
            cv2.imshow('result', image)
            key = cv2.waitKey(1) & 0xFF
            if key==ord("q"):
                break
                video.close()
            if key==ord("s"):
                key2 = cv2.waitKey(0)
示例#13
0
def demo_live(args, video_path):
    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load video
    cam = cv2.VideoCapture(0)
    #cam.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
    #cam.set(cv2.CAP_PROP_FRAME_WIDTH,1920)frametime10

    index = -1
    frametime20 = np.zeros((100,1))
    index = 0
    while True:
        #index = index + 1
        #sys.stdout.write('Process image: {} \r'.format(index))
        #sys.stdout.flush()

        # 4. read image
        #flag, image = video.read()
        retval,image = cam.read()
        #
        #image = cv2.resize(image,(192,108))
        #image = image[270:270+540,480:480+540,:]
        print(image.shape)
        if retval == False:
            print("Can not read image in Frame : {}".format(index))
            break
        t0 = timeit.default_timer()
        # 5. detect
        _labels, _scores, _coords = object_detector.predict(image)

        # 6. draw bounding box on the image
        for labels, scores, coords in zip(_labels, _scores, _coords):
            cv2.rectangle(image, (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])), COLORS[labels % 3], 2)
            cv2.putText(image, '{label}: {score:.3f}'.format(label=VOC_frametime10CLASSES[labels], score=scores), (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)
        elapsed = timeit.default_timer() - t0
        frametime20[index] = elapsed
        index = index+1
        index = index%100
        print(elapsed)
        # 7. visualize result
        cv2.imshow('result', image)
        cv2.waitKey(1)
        '''
示例#14
0
def demo_live(confg_file, index):
    # 1. load the configure file
    cfg_from_file(confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load video
    state = "init"
    cam = cv2.VideoCapture(0)
    points = []
    record_points = []
    count_frames = 0
    all_count = 0
    success_count = 0
    circle_radius = 20
    line_thickness = 16
    close_distance = 20
    wait_frames = 10
    font_scale = 1
    font_thickness = 2
    middle_state = False
    middle_frame = 0
    bias = 50
    t = 0
    while True:
        retval, frame = cam.read()
        img_h, img_w, c = frame.shape
        s_y = int((1080 - img_h) / 2)
        s_x = int((1920 - img_w) / 2)
        bg_image = np.full((1080, 1920, 3), 127, np.uint8)
        bg_image[s_y:s_y + img_h, s_x:s_x + img_w] = frame
        frame = bg_image[:,
                         int(1920 * (1 - CROP_RATE) /
                             2):int(1920 * (1 + CROP_RATE) / 2)]
        frame = cv2.flip(frame, 1)
        show_img = np.copy(frame)
        if state == "init":
            points = random_point(
                s_x - int(1920 * (1 - CROP_RATE) / 2) + bias,
                s_x + img_w - int(1920 * (1 - CROP_RATE) / 2) - bias,
                s_y + bias, s_y + img_h - bias)
            print("points:", points)
            # input()
            state = "waiting"

        for i in range(len(points) - 1):
            cv2.line(show_img, (points[i][0], points[i][1]),
                     (points[i + 1][0], points[i + 1][1]), (255, 0, 0),
                     line_thickness)
        cv2.circle(show_img, tuple(points[0]), circle_radius, (0, 255, 0), -1)
        cv2.circle(show_img, tuple(points[-1]), circle_radius, (0, 0, 255), -1)
        for i in range(1, len(points) - 1):
            cv2.circle(show_img, tuple(points[i]), circle_radius,
                       (255, 0, 255), -1)
        _labels, _scores, _coords = object_detector.predict(frame)
        hands_points = []
        for labels, scores, coords in zip(_labels, _scores, _coords):
            tmp_x = int((coords[0] + coords[2]) / 2)
            tmp_y = int((coords[1] + coords[3]) / 2)
            cv2.circle(show_img, (tmp_x, tmp_y), circle_radius, (0, 255, 255),
                       -1)
            hands_points.append([tmp_x, tmp_y])
        if state == "waiting":
            if count_frames > wait_frames:
                state = "Recording"
                t = time.time()
                count_frames = 0
            has_found = False
            for h_point in hands_points:
                if np.sqrt((h_point[0] - points[0][0])**2 +
                           (h_point[1] - points[0][1])**2) < close_distance:
                    has_found = True
                    count_frames += 1
            if has_found == False:
                count_frames = 0
        if state == "Recording":
            record_points.append(hands_points)
            cv2.putText(show_img, "Recording", (10, 150),
                        cv2.FONT_HERSHEY_COMPLEX, font_scale, (0, 0, 255),
                        font_thickness)
            if middle_frame > wait_frames:
                middle_state = True
                record_points.append("Middle")
                middle_frame = 0
            if middle_state:
                cv2.circle(show_img, tuple(points[1]), circle_radius,
                           (0, 255, 0), -1)
            if count_frames > wait_frames:
                state = "init"
                print("----------------")
                if len(points) == 3:
                    if judge(record_points, points) and middle_state:
                        success_count += 1
                        print("yes")
                    else:
                        print("no")
                else:
                    if judge(record_points, points):
                        success_count += 1
                        print("yes")
                    else:
                        print("no")
                all_count += 1
                count_frames = 0
                middle_frame = 0
                middle_state = False
                print("{}:{}".format("time", time.time() - t))

                record_points = []
            has_found = False
            middle_found = False
            for h_point in hands_points:
                if np.sqrt((h_point[0] - points[-1][0])**2 +
                           (h_point[1] - points[-1][1])**2) < close_distance:
                    has_found = True
                    count_frames += 1
                if len(points) > 2 and middle_state is False and np.sqrt(
                    (h_point[0] - points[1][0])**2 +
                    (h_point[1] - points[1][1])**2) < close_distance:
                    middle_frame += 1
                    middle_found = True
            if not has_found:
                count_frames = 0
            if not middle_found:
                middle_frame = 0
        cv2.putText(show_img, "Result:{}/{}".format(success_count, all_count),
                    (10, 50), cv2.FONT_HERSHEY_COMPLEX, font_scale,
                    (0, 0, 255), font_thickness)
        cv2.putText(show_img, "{} {}".format(state, count_frames), (10, 100),
                    cv2.FONT_HERSHEY_COMPLEX, font_scale, (0, 0, 255),
                    font_thickness)
        cv2.imshow("Frame", show_img)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
def train():
    args = parse_args()
    if args.config_file is not None:
        cfg_from_file(args.config_file)
    train_model()
def validate():
    args = parse_args()
    if args.config_file is not None:
        cfg_from_file(args.config_file)
    validate_model()
示例#17
0
def inference_img_dir(args, imgs_dir):
    if args.save:
        infer_key = imgs_dir.split("/")[-1].replace("/", "")
        model_name = args.confg_file.split("/")[-1].replace(".yml", "")
        test_img_rdir = os.path.join(args.save, infer_key)
        test_model_rdir = os.path.join(args.save, infer_key, model_name)
        test_out_img_path = os.path.join(args.save, infer_key, model_name,
                                         "det_imgs")
        pred_out_txt = os.path.join(args.save, infer_key, model_name,
                                    "pred_txt")
        model_save_path = os.path.join(args.save, infer_key, model_name,
                                       "model")
        if not os.path.exists(test_img_rdir):
            os.mkdir(test_img_rdir)
        if not os.path.exists(test_model_rdir):
            os.mkdir(test_model_rdir)
        if not os.path.exists(test_out_img_path):
            os.mkdir(test_out_img_path)
        if not os.path.exists(pred_out_txt):
            os.mkdir(pred_out_txt)
        if not os.path.exists(model_save_path):
            os.mkdir(model_save_path)

    # 1. load the configure file
    cfg_from_file(args.confg_file)
    # 2. load detector based on the configure file
    object_detector = ObjectDetector()
    for idx, img in enumerate(os.listdir(imgs_dir)):
        image_path = os.path.join(imgs_dir, img)
        # 3. load image
        image = cv2.imread(image_path)
        image256 = image[300:556]
        # 4. detect
        _labels, _scores, _coords = object_detector.predict(image256)

        # 5. draw bounding box on the image
        cxyl, stl, edl = [], [], []
        for label, score, coords in zip(_labels, _scores, _coords):
            xmin, ymin, xmax, ymax = [
                int(round(i)) for i in coords.cpu().numpy()
            ]
            cx = int(round((xmin + xmax) / 2))
            cy = int(round((ymin + ymax) / 2))
            min_d = min(xmax - xmin, ymax - ymin)
            if score >= 0.45:
                if label == 0:
                    cxyl.append([(cx, cy),
                                 [[xmin, ymin], [xmin, ymax], [xmax, ymax],
                                  [xmax, ymin]]])
                    cv2.circle(image256, (cx, cy), int(min_d / 4), (0, 0, 255),
                               2)
                    # cv2.rectangle(img,(xmin, ymin),(xmax, ymax),(0, 255, 255))
            if score >= 0.45:
                if label == 1:
                    stl.append((cx, cy))
                #     cv2.circle(image256, (cx, cy), 5, (255, 0, 0), 2)
                # elif label == 2:
                #     cv2.circle(image256, (cx, cy), 5, (0, 255, 0), 2)
                # cv2.line(img, (xmin, ymin), (xmax, ymax), (155, 155, 155), 2)
                # cv2.line(image256, (xmin + 20, ymin + 20), (xmax - 20, ymax - 20), (0, 255, 0), 2)
                # elif label == 4:
                #     # cv2.line(img, (xmin, ymin), (xmax, ymax), (0, 155, 155), 2)
                #     cv2.line(image256, (xmin + 20, ymin + 20), (xmax - 20, ymax - 20), (0, 155, 155), 2)
                # elif label == 5:
                #     cv2.circle(img256, (cx, cy), 5, (255, 255, 0))

            # cv2.rectangle(image256, (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])),
            #               COLORS[labels % 3], 2)
            # cv2.putText(image256, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels], score=scores),
            #             (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)
        for idx2, cxyp in enumerate(cxyl):
            dis = []
            for idx1, stp in enumerate(stl):
                tdis = cv2.pointPolygonTest(np.array(cxyp[-1]), stp, True)
                tdis = float("inf") if tdis < 0 else tdis
                tdis2 = np.linalg.norm(np.array(stp) - np.array(cxyp[0]))
                dis.append(tdis + tdis2)
            idx = np.argmin(dis)
            # if cv2.pointPolygonTest(np.array(cxyp[-1]), stp, True):
            cv2.arrowedLine(image256, stl[idx], cxyp[0], (0, 0, 255))

        # 6. visualize result
        if args.display is True:
            cv2.imshow('result', image)
            cv2.waitKey(0)

        # 7. write result
        if args.save is not None:
            # path, _ = os.path.splitext(image_path)

            img_name = image_path.split("/")[-1]
            cv2.imwrite(os.path.join(test_out_img_path, img_name), image)
            print("save img:", img_name)
示例#18
0
def train():
    args = parse_args()
    if args.config_file is not None:
        cfg_from_file(args.config_file)
    # os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
    train_model()
    parser.add_argument('--input_type', default='rgb', type=str, help='INput tyep default rgb can take flow as well')
    parser.add_argument('--test_scope', dest='test_scope', help='Set phase to test', default=None)
    parser.add_argument('--K', dest='K', help='Length of tubelet', default=2, type=int)
    parser.add_argument('--interval', dest='interval', help='Intra-frame interval', default=1, type=int)
    parser.add_argument('--init_checkpoint', default=None, help="Specify checkpoints to init from")

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = parse_args()
    cfg_from_file(args.cfg)

    # override default config
    if args.dataset is not None:
        cfg.DATASET.DATASET = args.dataset
        cfg.DATASET.DATASET_DIR = args.dataset_dir
        cfg.DATASET.TRAIN_SETS = ['train', args.split]
        cfg.DATASET.TEST_SETS = ['test', args.split]
        cfg.DATASET.INPUT_TYPE = args.input_type

    cfg.PHASE = ['visualize']

    if args.test_scope is not None:
        cfg.TEST.TEST_SCOPE = [int(args.test_scope), int(args.test_scope)]
    
    if args.K is not None:
示例#20
0
import torch

import lib.ssds_train
from lib.utils.config_parse import cfg_from_file

cfg_from_file('./experiments/cfgs/ssd_lite_mobilenetv2_train_coco.yml')
s = lib.ssds_train.Solver()

s.test_model()
示例#21
0
def train():
    cfg_from_file('./experiments/cfgs_new/yolo_v3_small_inceptionv4_v4_8.15.yml')
    train_model()
示例#22
0
def main():
    log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
    args = build_argparser().parse_args()
    # --------------------------- 1. Read IR Generated by ModelOptimizer (.xml and .bin files) ------------
    model_xml = args.model
    model_bin = os.path.splitext(model_xml)[0] + ".bin"
    log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
    net = IENetwork(model=model_xml, weights=model_bin)
    # -----------------------------------------------------------------------------------------------------

    # ------------- 2. Load Plugin for inference engine and extensions library if specified --------------
    log.info("Loading Inference Engine")
    ie = IECore()
    log.info("Device info:")
    versions = ie.get_versions(args.device)
    print("{}{}".format(" "*8, args.device))
    print("{}MKLDNNPlugin version ......... {}.{}".format(" "*8, versions[args.device].major, versions[args.device].minor))
    print("{}Build ........... {}".format(" "*8, versions[args.device].build_number))
    
    if args.cpu_extension and "CPU" in args.device:
        ie.add_extension(args.cpu_extension, "CPU")
        log.info("CPU extension loaded: {}".format(args.cpu_extension))

    if "CPU" in args.device:
        supported_layers = ie.query_network(net, "CPU")
        not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers]
        if len(not_supported_layers) != 0:
            log.error("Following layers are not supported by the plugin for specified device {}:\n {}".
                      format(args.device, ', '.join(not_supported_layers)))
            log.error("Please try to specify cpu extensions library path in sample's command line parameters using -l "
                      "or --cpu_extension command line argument")
            sys.exit(1)
    # -----------------------------------------------------------------------------------------------------

    # --------------------------- 3. Read and preprocess input --------------------------------------------
    input_blob = next(iter(net.inputs))
    n, c, h, w = net.inputs[input_blob].shape
    images = np.ndarray(shape=(n, c, h, w))
    images_hw = []
    images_raw = []
    for i in range(n):
        image = cv2.imread(args.input[i])
        images_raw.append(image)
        ih, iw = image.shape[:-1]
        images_hw.append((ih, iw))
        log.info("File was added: ")
        log.info("        {}".format(args.input[i]))
        if (ih, iw) != (h, w):
            log.warning("Image {} is resized from {} to {}".format(args.input[i], image.shape[:-1], (h, w)))
            image = cv2.resize(image, (w, h))
        image = image.transpose((2, 0, 1))  # Change data layout from HWC to CHW
        images[i] = image
    # -----------------------------------------------------------------------------------------------------

    # --------------------------- 4. Configure input & output ---------------------------------------------
    # --------------------------- Prepare input blobs -----------------------------------------------------
    log.info("Preparing input blobs")
    assert (len(net.inputs.keys()) == 1 or len(net.inputs.keys()) == 2), "Sample supports topologies only with 1 or 2 inputs"
    input_blob = next(iter(net.inputs))
    out_blob = next(iter(net.outputs))
    input_name, input_info_name = "", ""

    for input_key in net.inputs:
        if len(net.inputs[input_key].layout) == 4:
            input_name = input_key
            log.info("Batch size is {}".format(net.batch_size))
            net.inputs[input_key].precision = 'U8'
        elif len(net.inputs[input_key].layout) == 2:
            input_info_name = input_key
            net.inputs[input_key].precision = 'FP32'
            if net.inputs[input_key].shape[1] != 3 and net.inputs[input_key].shape[1] != 6 or net.inputs[input_key].shape[0] != 1:
                log.error('Invalid input info. Should be 3 or 6 values length.')

    # --------------------------- Prepare output blobs ----------------------------------------------------
    log.info('Preparing output blobs')

    output_conf, conf_info = "", net.outputs[next(iter(net.outputs.keys()))]

    for output_key in net.outputs:
        if net.layers[output_key].type == "SoftMax":
            output_conf, conf_info = output_key, net.outputs[output_key]
        if net.layers[output_key].type == "Reshape":
            output_loc, loc_info = output_key, net.outputs[output_key]

    # if output_name == "":
    #     log.error("Can't find a DetectionOutput layer in the topology")

    # conf_dims = conf_info.shape
    # loc_dims = loc_info.shape
    # # if len(conf_dims) != 4:
    # #     log.error("Incorrect output dimensions for SSD model")
    # max_proposal_count, object_size = output_dims[2], output_dims[3]

    # if object_size != 7:
    #     log.error("Output item should have 7 as a last dimension")

    conf_info.precision = "FP32"
    loc_info.precision = "FP32"
    
    # --------------------------Prepare priorbox-------------------------------------
    log.info("Preparing priorboxes")
    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

        
    # --------------------------- Performing inference ----------------------------------------------------
    log.info("Loading model to the device")
    exec_net = ie.load_network(network=net, device_name=args.device)
    log.info("Creating infer request and starting inference")
    res = exec_net.infer(inputs={input_blob: images})
    print(res)
    # -----------------------------------------------------------------------------------------------------

    # --------------------------- Read and postprocess output ---------------------------------------------
    log.info("Processing output blobs")
    conf = res['742']
    loc = res['output']
    _labels, _scores, _coords = object_detector.predict(images_raw[0].shape[0], images_raw[0].shape[1], loc, conf)

    # imageshow = cv2.resize(images_raw[0], (600,600))

    # draw bounding box on the image
    if len(_labels) == 0:
        log.info("Nothing was detected!")
        exit(0)
    else:
        for labels, scores, coords in zip(_labels, _scores, _coords):
            cv2.rectangle(images_raw[0], (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])), COLORS[labels % 3], 2)
            cv2.putText(images_raw[0], '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels], score=scores), (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)
    
    # visualize result
    if args.display is True:
        cv2.imshow('result', images_raw[0])
        cv2.waitKey(0)

    
    #  write result
    if args.save is True:
        path, _ = os.path.splitext(args.input[0])
        cv2.imwrite(path + '_result.bmp', images_raw[0])
        log.info("Image out.bmp created!")
    # -----------------------------------------------------------------------------------------------------

    log.info("Execution successful\n")
    log.info("This sample is an API example, for any performance measurements please use the dedicated benchmark_app tool")
示例#23
0
def parse_args():
    """
    Parse input arguments
    """
    parser = argparse.ArgumentParser(description='Train a ssds.pytorch network')
    parser.add_argument('--cfg', dest='config_file',
            help='optional config file', default=None, type=str)

    parser.add_argument('--onnx', dest='onnx_file',
                    help='optional onnx_file to be exported', default=None, type=str)
    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()
    return args

def test():
    test_model()

if __name__ == '__main__':
    args = parse_args()
    if args.config_file is not None:
        cfg_from_file(args.config_file)

    if args.onnx_file is not None:
        export_onnx_model(args.onnx_file)
    else:
        export_onnx_model("/tmp/bayer_ssd_lite_mbv2.onnx")
        #test_model()
def test():
    args = parse_args()
    if args.config_file is not None:
        cfg_from_file(args.config_file)
    test_model()
示例#25
0
def visualize(image):
    cfg_from_file(config_file)
    s = Solver()
    return s.test_model(image)