예제 #1
0
def run_video():
    timer = Timer()
    sum = 0
    cap = cv2.VideoCapture('ne1.avi')  # capture from camera
    predictor = load_model()
    fourcc = cv2.VideoWriter_fourcc(*'H264')
    out = cv2.VideoWriter('output.mp4', fourcc, 30.0, (1920, 1080))
    while True:
        ret, orig_image = cap.read()
        if orig_image is None:
            break
        image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
        timer.start()
        start = time.time()
        boxes, labels, probs = predictor.predict(image, candidate_size / 2,
                                                 threshold)
        # if labels.size(0) == 0:
        # continue
        # else:
        interval = timer.end()
        fps = 1 / (time.time() - start)
        print('FPS: {} Time: {:.6f}s, Detect Objects: {:d}.'.format(
            fps, interval, labels.size(0)))
        for i in range(boxes.size(0)):
            box = boxes[i, :]
            score = f" {probs[i]:.2f}"
            label = labels[i]
            cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]),
                          (0, 255, 0), 4)

            cv2.putText(
                orig_image,
                "{}-{}".format(score, class_names[label]),
                (box[0], box[1] - 10),
                cv2.FONT_HERSHEY_SIMPLEX,
                0.5,  # font scale
                (0, 0, 255),
                2)  # line type
        orig_image = cv2.resize(orig_image, None, None, fx=0.8, fy=0.8)
        sum += boxes.size(0)
        cv2.imshow('annotated', orig_image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        # out.write(orig_image)
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    print("all face num:{}".format(sum))
예제 #2
0
def main(args):
    net_type = args.net_type
    img_folder = args.img_folder
    model_path = args.weights_path
    label_path = args.label_path
    class_names = [name.strip() for name in open(label_path).readlines()]
    out_path = args.out_path
    if not os.path.exists(out_path):
        os.mkdir(out_path)

    num_gpus = torch.cuda.device_count()
    device = 'cuda' if num_gpus else 'cpu'

    if net_type == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    #elif net_type == 'mb3-ssd-lite':
    #    net = create_mobilenetv3_ssd_lite(len(class_names), is_test=True)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)

    net.load(model_path)

    if net_type == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net,
                                             candidate_size=20,
                                             device=device)
    elif net_type == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net,
                                                     candidate_size=20,
                                                     device=device)
    elif net_type == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(net,
                                                          candidate_size=20,
                                                          device=device)
    elif net_type == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(net,
                                                          candidate_size=20,
                                                          device=device)
    elif net_type == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(net,
                                                         candidate_size=20,
                                                         device=device)
    #elif net_type == 'mb3-ssd-lite':
    #    predictor = create_mobilenetv3_ssd_lite_predictor(net, candidate_size=10)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)

    timer = Timer()

    img_names = glob.glob(img_folder + os.sep + '*.jpg')
    #result_csv=os.path.join(out_path,'rest_result.csv')
    if len(img_names) == 0:
        print('No imagesfound in {}'.format(img_folder))
        exit(-1)

    for img_name in img_names:
        image = cv2.imread(img_name)

        timer.start()
        boxes, labels, probs = predictor.predict(image, 10, 0.3)
        interval = timer.end()

        print('Time: {:.2f}s, Detect Objects: {:d}.'.format(
            interval, labels.size(0)))

        label_text = []
        for i in range(boxes.size(0)):
            box = boxes[i, :]
            label = f"{class_names[labels[i]]}: {probs[i]:.2f}"
            label_text.append(label)
            cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]),
                          (255, 255, 0), 4)
            cv2.putText(image, label, (box[0] + 20, box[1] + 40),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 255), 2)

        if args.store_result:
            new_name = '{}/{}'.format(out_path, img_name.split('/')[-1])
            cv2.imwrite(new_name, image)
            if not label_text:
                result_label = 'empty'
            else:
                result_label = label_text[0]
            with open(os.path.join(out_path, 'rest_result.csv'),
                      'a+') as result_writer:
                result_writer.write(
                    img_name.split('/')[-1] + ',' + result_label + '\n')

        cv2.imshow('result', image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cv2.destroyAllWindows()
예제 #3
0
class WagonDetector:
    def __init__(self,
                 net_type,
                 label_path,
                 model_path,
                 top_k=10,
                 prob_threshold=0.4):
        self.class_names = ['BACKGROUND'] + [
            name.strip() for name in open(label_path).readlines()
        ]

        self.net = self._create_network(net_type)
        self.net.load(model_path)

        self.predictor = self._create_predictor(net_type)

        self.top_k = top_k
        self.prob_threshold = prob_threshold

        self.timer = Timer()

    def _create_network(self, net_type):
        if net_type == 'vgg16-ssd':
            return create_vgg_ssd(len(self.class_names), is_test=True)
        elif net_type == 'mb1-ssd':
            return create_mobilenetv1_ssd(len(self.class_names), is_test=True)
        elif net_type == 'mb1-ssd-lite':
            return create_mobilenetv1_ssd_lite(len(self.class_names),
                                               is_test=True)
        elif net_type == 'mb2-ssd-lite':
            return create_mobilenetv2_ssd_lite(len(self.class_names),
                                               is_test=True)
        elif net_type == 'sq-ssd-lite':
            return create_squeezenet_ssd_lite(len(self.class_names),
                                              is_test=True)
        else:
            raise RuntimeError(
                "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
            )

    def _create_predictor(self, net_type):
        if net_type == 'vgg16-ssd':
            return create_vgg_ssd_predictor(self.net, candidate_size=200)
        elif net_type == 'mb1-ssd':
            return create_mobilenetv1_ssd_predictor(self.net,
                                                    candidate_size=200)
        elif net_type == 'mb1-ssd-lite':
            return create_mobilenetv1_ssd_lite_predictor(self.net,
                                                         candidate_size=200)
        elif net_type == 'mb2-ssd-lite':
            return create_mobilenetv2_ssd_lite_predictor(self.net,
                                                         candidate_size=200)
        elif net_type == 'sq-ssd-lite':
            return create_squeezenet_ssd_lite_predictor(self.net,
                                                        candidate_size=200)
        else:
            raise RuntimeError(
                "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
            )

    def __call__(self, image):
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        self.timer.start()
        boxes, labels, probs = self.predictor.predict(image, self.top_k,
                                                      self.prob_threshold)
        interval = self.timer.end()
        print('Time: {:.2f}s, Detect Objects: {:d}.'.format(
            interval, labels.size(0)))

        return boxes, labels, probs
        args.extra_layers_lr
    }, {
        'params':
        itertools.chain(net.regression_headers.parameters(),
                        net.classification_headers.parameters())
    }]
    # log.info("params 2 = "+str(params))

    timer.start("Load Model")
    if args.resume:
        log.info("Resume from the model " + args.resume)
        net.load(args.resume)
    else:
        log.info("Init from pretrained ssd " + args.pretrained_ssd)
        net.init_from_pretrained_ssd(args.pretrained_ssd)
    log.info('Took ' + str(timer.end("Load Model")) +
             ' seconds to load the model.')

    net.to(DEVICE)

    criterion = MultiboxLoss(config.priors,
                             iou_threshold=0.5,
                             neg_pos_ratio=3,
                             center_variance=0.1,
                             size_variance=0.2,
                             device=DEVICE)
    optimizer = torch.optim.SGD(params,
                                lr=args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)
    log.info("Learning rate: " + str(args.lr) +
예제 #5
0
def main(args):
    net_type = args.net_type
    model_path = args.weights_path
    label_path = args.label_path
    class_names = [name.strip() for name in open(label_path).readlines()]
    num_classes = len(class_names)

    if args.live:
        cap = cv2.VideoCapture(0)
        cap.set(3, 640)
        cap.set(4, 480)
    else:
        cap = cv2.VideoCapture(args.video_path)

    Fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
    writer = cv2.VideoWriter('result.mp4',
                             fourcc=Fourcc,
                             fps=15,
                             frameSize=(640, 480))

    num_gpus = torch.cuda.device_count()
    device = 'cuda' if num_gpus else 'cpu'

    if net_type == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    #elif net_type == 'mb3-ssd-lite':
    #    net = create_mobilenetv3_ssd_lite(len(class_names), is_test=True)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)

    net.load(model_path)

    if net_type == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net,
                                             candidate_size=20,
                                             device=device)
    elif net_type == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net,
                                                     candidate_size=20,
                                                     device=device)
    elif net_type == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(net,
                                                          candidate_size=20,
                                                          device=device)
    elif net_type == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(net,
                                                          candidate_size=20,
                                                          device=device)
    elif net_type == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(net,
                                                         candidate_size=20,
                                                         device=device)
    #elif net_type == 'mb3-ssd-lite':
    #    predictor = create_mobilenetv3_ssd_lite_predictor(net, candidate_size=10)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)

    timer = Timer()

    while True:
        _, orig_image = cap.read()
        if orig_image is None:
            print('END')
            break

        image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
        timer.start()
        boxes, labels, probs = predictor.predict(image, 10, 0.4)
        interval = timer.end()
        print('Time: {:.2f}s, Detect Objects: {:d}.'.format(
            interval, labels.size(0)))
        for i in range(boxes.size(0)):
            box = boxes[i, :]
            label = f"{class_names[labels[i]]}: {probs[i]:.2f}"
            cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]),
                          (255, 255, 0), 4)

            cv2.putText(
                orig_image,
                label,
                (box[0] + 20, box[1] + 40),
                cv2.FONT_HERSHEY_SIMPLEX,
                1,  # font scale
                (255, 0, 255),
                2)  # line type
        writer.write(orig_image)
        cv2.imshow('annotated', orig_image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    writer.release()
    cv2.destroyAllWindows()
    if args.out_video:
        shutil.move('result.mp4', args.out_video)
    else:
        os.remove('result.mp4')
예제 #6
0
    else:
        logging.fatal(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        parser.print_help(sys.stderr)
        sys.exit(1)

    LTs = []
    PTs = []
    ITs = []
    results = []
    for i in range(len(dataset)):
        print("process image", i)
        timer.start("Load Image")
        image = dataset.get_image(i)
        LT = timer.end("Load Image")
        LTs.append(LT)
        print("Load Image: {:4f} seconds.".format(LT))
        timer.start("Predict")
        IT, boxes, labels, probs = predictor.predict(image)
        PT = timer.end("Predict")
        ITs.append(IT)
        PTs.append(PT)
        print("Prediction: {:4f} seconds.".format(PT))
        indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i
        results.append(
            torch.cat(
                [
                    indexes.reshape(-1, 1),
                    labels.reshape(-1, 1).float(),
                    probs.reshape(-1, 1),
예제 #7
0
def PersonDetector(orig_image, net_type="mb1-ssd"):

    class_names = [name.strip() for name in open(label_path).readlines()]
    num_classes = len(class_names)

    if net_type == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)
    net.load(model_path)

    if net_type == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net, candidate_size=200)
    elif net_type == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200)
    elif net_type == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(net,
                                                          candidate_size=200)
    elif net_type == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(net,
                                                          candidate_size=200)
    elif net_type == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(net,
                                                         candidate_size=200)
    else:
        print(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        sys.exit(1)

    timer = Timer()
    image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
    timer.start()
    boxes, labels, probs = predictor.predict(image, 10, 0.4)
    interval = timer.end()
    print('Time: {:.2f}s, Detect Objects: {:d}.'.format(
        interval, labels.size(0)))
    max_width = -1
    x, y, w, h = None, None, None, None
    for i in range(boxes.size(0)):
        box = boxes[i, :]
        label = f"{class_names[labels[i]]}: {probs[i]:.2f}"
        if (max_width < box[2] - box[0]):
            x, y = box[0], box[1]
            w, h = box[2] - box[0], box[3] - box[1]
            max_width = w

    if (x is not None and y is not None and w is not None and h is not None):
        cv2.rectangle(orig_image, (x, y), (w + x, h + y), (255, 255, 0), 4)

    return (x, y, w, h)


# img = cv2.imread("Image/img.jpg")
# PersonDetector(img)
예제 #8
0
    predictor = create_squeezenet_ssd_lite_predictor(net, candidate_size=200)
else:
    print(
        "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
    )
    sys.exit(1)

timer = Timer()
while True:
    ret, orig_image = cap.read()
    if orig_image is None:
        continue
    image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
    timer.start()
    boxes, labels, probs = predictor.predict(image, 10, 0.4)
    interval = timer.end()
    print('Time: {:.2f}s, Detect Objects: {:d}.'.format(
        interval, labels.size(0)))
    for i in range(boxes.size(0)):
        box = boxes[i, :].int()
        label = f"{class_names[labels[i]]}: {probs[i]:.2f}"
        cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]),
                      (255, 255, 0), 4)

        cv2.putText(
            orig_image,
            label,
            (box[0] + 20, box[1] + 40),
            cv2.FONT_HERSHEY_SIMPLEX,
            1,  # font scale
            (255, 0, 255),
예제 #9
0
def main():
    if len(sys.argv) < 4:
        print(
            'Usage: python run_ssd_example.py <net type> <model path> <label path> [video file]'
        )
        sys.exit(0)

    net_type = sys.argv[1]
    model_path = sys.argv[2]
    label_path = sys.argv[3]
    if len(sys.argv) >= 5:
        cap = cv2.VideoCapture(sys.argv[4])  # capture from file
    else:
        cap = cv2.VideoCapture(0)  # capture from camera
        cap.set(3, 1920)
        cap.set(4, 1080)

    class_names = [name.strip() for name in open(label_path).readlines()]
    net = net_select(model_type=net_type, class_names=class_names)
    predictor = create_predictor(model=net, model_type=net_type)
    net.load(model_path)

    count = 0
    max_count = 10
    fps = 0
    timer = Timer()
    tm = cv2.TickMeter()
    tm.start()
    while True:
        ret, orig_image = cap.read()
        if orig_image is None:
            continue

        # for fps count
        if count == max_count:
            tm.stop()
            fps = max_count / tm.getTimeSec()
            tm.reset()
            tm.start()
            count = 0

        image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
        timer.start()
        boxes, labels, probs = predictor.predict(image, 10, 0.4)
        interval = timer.end()
        print('Time: {:.3f}s, Detect Objects: {:d}.'.format(
            interval, labels.size(0)))

        # fpsの書き込み
        cv2.putText(orig_image, f'{fps:.2f}', (10, 30),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)

        for i in range(boxes.size(0)):
            # ボックスの書き込み
            box = boxes[i, :]
            label = f"{class_names[labels[i]]}: {probs[i]:.3f}"
            # ラベルと確信度の書き込み
            cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]),
                          (255, 255, 0), 4)
            cv2.putText(
                orig_image,
                label,
                (box[0] + 20, box[1] + 40),
                cv2.FONT_HERSHEY_SIMPLEX,
                1,  # font scale
                (255, 0, 255),
                2)  # line type

        cv2.imshow('Capture Demo', orig_image)
        count += 1

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
예제 #10
0
def main():
    timer = Timer()
    DEVICE = torch.device("cuda:0" if args.use_cuda and torch.cuda.
                          is_available() and args.use_cuda else "cpu")

    print("=== Use ", DEVICE)
    classes = []
    with open("./models/char_obj_EN.names", "r") as f:
        lines = f.readlines()

    for i in lines:
        if "\n" in i:
            classes.append(i.split("\n")[0])
        else:
            classes.append(i)
    class_names = classes
    print("label size is ", len(class_names))
    net = create_mobilenetv2_ssd_lite(len(class_names),
                                      width_mult=args.mb2_width_mult,
                                      is_test=True)

    timer.start("Load Model")
    net.load(args.trained_model)
    net = net.to(DEVICE)
    print(f'{timer.end("Load Model")} sec to load the model.')

    predictor = create_mobilenetv2_ssd_lite_predictor(
        net, nms_method=args.nms_method, device=DEVICE)

    test_list = [
        x for x in os.listdir(args.test_folder) if x.split(".")[1] == "jpg"
    ]

    total_time = 0.0
    for i in test_list:
        # Read Images
        img_path = args.test_folder + "/" + i
        image = cv2.imread(img_path)
        cvt_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        # Predict
        timer.start("Predict")
        boxes, labels, probs = predictor.predict(cvt_image)
        end_time = timer.end("Predict")
        print("Prediction: {:4f} sec.".format(end_time))
        total_time += end_time

        # to numpy
        bb = boxes.numpy()
        lb = labels.numpy()
        pb = probs.numpy()
        save_name = ""

        # resize
        s_factor = 4
        rows, cols = image.shape[:2]
        image = cv2.resize(image, (cols * s_factor, rows * s_factor))

        # score > 0.5
        for b in range(pb.size):
            if pb[b] > args.prob_thresh:
                cv2.rectangle(
                    image,
                    (int(bb[b][0] * s_factor), int(bb[b][1] * s_factor)),
                    (int(bb[b][2] * s_factor), int(bb[b][3] * s_factor)),
                    (0, 255, 0), 2)
                cv2.putText(
                    image, class_names[int(lb[b]) + 1],
                    (int(bb[b][0] * s_factor), int(bb[b][1] * s_factor)),
                    cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 2)

                save_name += class_names[int(lb[b]) + 1]

        cv2.imwrite("./output/Final_Testset/" + save_name + ".jpg", image)

        print("Input Image : {}  ====> Predict : {}".format(i, save_name))

    print("Avg Time is {:4f} sec.".format(total_time / len(test_list)))
예제 #11
0
def main(args):
    DEVICE = torch.device(
        "cuda:0" if torch.cuda.is_available() and args.use_cuda else "cpu")
    #DEVICE = torch.device("cpu")
    if args.use_cuda and torch.cuda.is_available():
        torch.backends.cudnn.benchmark = True
        logging.info("Use Cuda.")

    timer = Timer()

    logging.info(args)
    if args.net == 'vgg16-ssd':
        create_net = create_vgg_ssd
        config = vgg_ssd_config
    elif args.net == 'mb1-ssd':
        create_net = create_mobilenetv1_ssd
        config = mobilenetv1_ssd_config
    elif args.net == 'mb1-ssd-lite':
        create_net = create_mobilenetv1_ssd_lite
        config = mobilenetv1_ssd_config
    elif args.net == 'sq-ssd-lite':
        create_net = create_squeezenet_ssd_lite
        config = squeezenet_ssd_config
    elif args.net == 'mb2-ssd-lite':
        create_net = lambda num: create_mobilenetv2_ssd_lite(
            num, width_mult=args.mb2_width_mult)
        config = mobilenetv1_ssd_config
    else:
        logging.fatal("The net type is wrong.")
        parser.print_help(sys.stderr)
        sys.exit(1)
    train_transform = TrainAugmentation(config.image_size, config.image_mean,
                                        config.image_std)
    target_transform = MatchPrior(config.priors, config.center_variance,
                                  config.size_variance, 0.5)

    test_transform = TestTransform(config.image_size, config.image_mean,
                                   config.image_std)

    logging.info("Prepare training datasets.")
    datasets = []
    for dataset_path in args.datasets:
        if args.dataset_type == 'voc':
            dataset = VOCDataset(dataset_path,
                                 transform=train_transform,
                                 target_transform=target_transform)
            label_file = os.path.join(args.checkpoint_folder,
                                      "voc-model-labels.txt")
            store_labels(label_file, dataset.class_names)
            num_classes = len(dataset.class_names)
        elif args.dataset_type == 'open_images':
            dataset = OpenImagesDataset(dataset_path,
                                        transform=train_transform,
                                        target_transform=target_transform,
                                        dataset_type="train",
                                        balance_data=args.balance_data)
            label_file = os.path.join(args.checkpoint_folder,
                                      "open-images-model-labels.txt")
            store_labels(label_file, dataset.class_names)
            logging.info(dataset)
            num_classes = len(dataset.class_names)
        elif args.dataset_type == 'coco':
            # root, annFile, transform=None, target_transform=None, transforms=None)
            #  dataset_type="train", balance_data=args.balance_data)
            dataset = CocoDetection(
                "/home/wenyen4desh/datasets/coco/train2017",
                "/home/wenyen4desh/datasets/coco/annotations/instances_train2017.json",
                transform=train_transform,
                target_transform=target_transform)

            label_file = os.path.join(args.checkpoint_folder,
                                      "open-images-model-labels.txt")
            store_labels(label_file, dataset.class_names)
            logging.info(dataset)
            num_classes = len(dataset.class_names)
            # raise ValueError("Dataset type {} yet implement.".format(args.dataset_type))
        else:
            raise ValueError("Dataset type {} is not supported.".format(
                args.dataset_type))
        datasets.append(dataset)
    logging.info("Stored labels into file {}.".format(label_file))
    train_dataset = ConcatDataset(datasets)
    logging.info("Train dataset size: {}".format(len(train_dataset)))
    train_loader = DataLoader(train_dataset,
                              args.batch_size,
                              num_workers=args.num_workers,
                              shuffle=True)
    logging.info("Prepare Validation datasets.")
    if args.dataset_type == "voc":
        val_dataset = VOCDataset(args.validation_dataset,
                                 transform=test_transform,
                                 target_transform=target_transform,
                                 is_test=True)
    elif args.dataset_type == 'open_images':
        val_dataset = OpenImagesDataset(dataset_path,
                                        transform=test_transform,
                                        target_transform=target_transform,
                                        dataset_type="test")
        logging.info(val_dataset)
    elif args.dataset_type == "coco":
        val_dataset = CocoDetection(
            "/home/wenyen4desh/datasets/coco/val2017",
            "/home/wenyen4desh/datasets/coco/annotations/instances_val2017.json",
            transform=test_transform,
            target_transform=target_transform)
        logging.info(val_dataset)
    logging.info("validation dataset size: {}".format(len(val_dataset)))

    val_loader = DataLoader(val_dataset,
                            args.batch_size,
                            num_workers=args.num_workers,
                            shuffle=False)
    logging.info("Build network.")
    net = create_net(num_classes)
    min_loss = -10000.0
    last_epoch = -1

    base_net_lr = args.base_net_lr if args.base_net_lr is not None else args.lr
    extra_layers_lr = args.extra_layers_lr if args.extra_layers_lr is not None else args.lr
    if args.freeze_base_net:
        logging.info("Freeze base net.")
        freeze_net_layers(net.base_net)
        params = itertools.chain(net.source_layer_add_ons.parameters(),
                                 net.extras.parameters(),
                                 net.regression_headers.parameters(),
                                 net.classification_headers.parameters())
        params = [{
            'params':
            itertools.chain(net.source_layer_add_ons.parameters(),
                            net.extras.parameters()),
            'lr':
            extra_layers_lr
        }, {
            'params':
            itertools.chain(net.regression_headers.parameters(),
                            net.classification_headers.parameters())
        }]
    elif args.freeze_net:
        freeze_net_layers(net.base_net)
        freeze_net_layers(net.source_layer_add_ons)
        freeze_net_layers(net.extras)
        params = itertools.chain(net.regression_headers.parameters(),
                                 net.classification_headers.parameters())
        logging.info("Freeze all the layers except prediction heads.")
    else:
        params = [{
            'params': net.base_net.parameters(),
            'lr': base_net_lr
        }, {
            'params':
            itertools.chain(net.source_layer_add_ons.parameters(),
                            net.extras.parameters()),
            'lr':
            extra_layers_lr
        }, {
            'params':
            itertools.chain(net.regression_headers.parameters(),
                            net.classification_headers.parameters())
        }]

    timer.start("Load Model")
    if args.resume:
        logging.info("Resume from the model {}".format(args.resume))
        net.load(args.resume)
    elif args.base_net:
        logging.info("Init from base net {}".format(args.base_net))
        net.init_from_base_net(args.base_net)
    elif args.pretrained_ssd:
        logging.info("Init from pretrained ssd {}".format(args.pretrained_ssd))
        net.init_from_pretrained_ssd(args.pretrained_ssd)
    logging.info('Took {:.2f} seconds to load the model.'.format(
        timer.end("Load Model")))

    net.to(DEVICE)

    criterion = MultiboxLoss(config.priors,
                             iou_threshold=0.5,
                             neg_pos_ratio=3,
                             center_variance=0.1,
                             size_variance=0.2,
                             device=DEVICE)
    optimizer = torch.optim.SGD(params,
                                lr=args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)
    logging.info("Learning rate: {}, Base net learning rate: {}, ".format(
        args.lr, base_net_lr) +
                 "Extra Layers learning rate: {}.".format(extra_layers_lr))

    if args.scheduler == 'multi-step':
        logging.info("Uses MultiStepLR scheduler.")
        milestones = [int(v.strip()) for v in args.milestones.split(",")]
        scheduler = MultiStepLR(optimizer,
                                milestones=milestones,
                                gamma=0.1,
                                last_epoch=last_epoch)
    elif args.scheduler == 'cosine':
        logging.info("Uses CosineAnnealingLR scheduler.")
        scheduler = CosineAnnealingLR(optimizer,
                                      args.t_max,
                                      last_epoch=last_epoch)
    else:
        logging.fatal("Unsupported Scheduler: {}.".format(args.scheduler))
        parser.print_help(sys.stderr)
        sys.exit(1)

    logging.info("Start training from epoch {}.".format(last_epoch + 1))
    for epoch in range(last_epoch + 1, args.num_epochs):
        scheduler.step()
        train(train_loader,
              net,
              criterion,
              optimizer,
              device=DEVICE,
              debug_steps=args.debug_steps,
              epoch=epoch)

        if epoch % args.validation_epochs == 0 or epoch == args.num_epochs - 1:
            val_loss, val_regression_loss, val_classification_loss = test(
                val_loader, net, criterion, DEVICE)
            logging.info("Epoch: {}, ".format(epoch) +
                         "Validation Loss: {:.4f}, ".format(val_loss) +
                         "Validation Regression Loss {:.4f}, ".format(
                             val_regression_loss) +
                         "Validation Classification Loss: {:.4f}".format(
                             val_classification_loss))
            model_path = os.path.join(
                args.checkpoint_folder,
                "{}-Epoch-{}-Loss-{}.pth".format(args.net, epoch, val_loss))
            net.save(model_path)
            logging.info("Saved model {}".format(model_path))
        net = create_mobilenetv2_ssd_lite(len(class_names), width_mult=args.mb2_width_mult, is_test=True)
    elif args.net == 'mb2-ssd-lite-xiaomi':
        net = create_mobilenetv2_ssd_lite_xiaomi(len(class_names), width_mult=args.mb2_width_mult, is_test=True)
    elif args.net == 'fairnas-a-ssd-lite':
        net = create_fairnas_a_ssd_lite(len(class_names), is_test=True)
    elif args.net == 'fairnas-b-ssd-lite':
        net = create_fairnas_b_ssd_lite(len(class_names), is_test=True)
    else:
        logging.fatal("The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite.")
        parser.print_help(sys.stderr)
        sys.exit(1)

    timer.start("Load Model")
    net.load(args.trained_model)
    net = net.to(DEVICE)
    print('It took {} seconds to load the model.'.format(timer.end("Load Model")))
    if args.net == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net, nms_method=args.nms_method, device=DEVICE)
    elif args.net == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net, nms_method=args.nms_method, device=DEVICE)
    elif args.net == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(net, nms_method=args.nms_method, device=DEVICE)
    elif args.net == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(net,nms_method=args.nms_method, device=DEVICE)
    elif args.net == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(net, nms_method=args.nms_method, device=DEVICE)
    elif args.net == 'mb2-ssd-lite-xiaomi':
        predictor = create_mobilenetv2_ssd_lite_predictor_xiaomi(net, nms_method=args.nms_method, device=DEVICE,
                                                                 nms_gpu=args.nms_gpu)
    elif args.net == 'fairnas-a-ssd-lite':
        predictor = create_fairnas_a_ssd_lite_predictor(net, nms_method=args.nms_method, device=DEVICE,
예제 #13
0
    mult_adds = comp_multadds(net, input_size=(3, 320, 320))
    logger.info("Mult-Adds = %.2fMB" % mult_adds)
    net.load(args.model)
    net = net.to(DEVICE)
    predictor = create_ssd_lite_predictor(net,
                                          nms_method=args.nms_method,
                                          device=DEVICE)
    logger.info(
        f'It took {timer.end("Load Model")} seconds to load the model.')

    results = []
    timer.start("Detection")
    for i in range(len(dataset)):
        timer.start("Load Image")
        image = dataset.get_image(i)
        load_t = timer.end("Load Image")
        timer.start("Predict")
        boxes, labels, probs = predictor.predict(image)
        predict_t = timer.end("Predict")
        indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i
        boxes = boxes + 1.0  # matlab's indexes start from 1 for pascal voc
        results.append(
            torch.cat([
                indexes.reshape(-1, 1),
                labels.reshape(-1, 1).float(),
                probs.reshape(-1, 1), boxes
            ],
                      dim=1))
    logger.info(args.model)
    logger.info("     ### Detecting {} imgs with using {:.3f} ###     ".format(
        len(dataset), timer.end("Detection")))
예제 #14
0
    predictor = create_mobilenetv1_ssd_lite_predictor(net, candidate_size=200)
elif net_type == 'mb2-ssd-lite':
    predictor = create_mobilenetv2_ssd_lite_predictor(net, candidate_size=200)
elif net_type == 'sq-ssd-lite':
    predictor = create_squeezenet_ssd_lite_predictor(net, candidate_size=200)
else:
    predictor = create_vgg_ssd_predictor(net, candidate_size=200)

orig_image = cv2.imread(image_path)
image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
t_sum = 0
timer = Timer()
for i in range(100):
    start = timer.start()
    boxes, labels, probs = predictor.predict(image, -1, 0.5)
    end = timer.end()
    t_sum += end
    # break
print("time: ", t_sum / 100)
for i in range(boxes.size(0)):
    box = boxes[i, :]
    cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]),
                  (255, 255, 0), 4)
    #label = f"""{voc_dataset.class_names[labels[i]]}: {probs[i]:.2f}"""
    label = f"{class_names[labels[i]]}: {probs[i]:.2f}"
    cv2.putText(
        orig_image,
        label,
        (box[0] + 20, box[1] + 40),
        cv2.FONT_HERSHEY_SIMPLEX,
        1,  # font scale
                net.regression_headers.parameters(),
                net.classification_headers.parameters()
            )}
        ]

    timer.start("Load Model")
    if args.resume:
        logging.info("Resume from the model {}".format(args.resume))
        net.load(args.resume)
    elif args.base_net:
        logging.info("Init from base net {}".format(args.base_net))
        net.init_from_base_net(args.base_net)
    elif args.pretrained_ssd:
        logging.info("Init from pretrained ssd {}".format(args.pretrained_ssd))
        net.init_from_pretrained_ssd(args.pretrained_ssd)
    logging.info('Took {} seconds to load the model.'.format(timer.end("Load Model")))

    net.to(DEVICE)

    criterion = MultiboxLoss(config.priors, iou_threshold=0.5, neg_pos_ratio=3,
                             center_variance=0.1, size_variance=0.2, device=DEVICE)
    optimizer = torch.optim.SGD(params, lr=args.lr, momentum=args.momentum,
                                weight_decay=args.weight_decay)
    logging.info("Learning rate: {}, Base net learning rate: {}, ".format(args.lr, base_net_lr)
                 + "Extra Layers learning rate: {}.".format(extra_layers_lr))

    if args.scheduler == 'multi-step':
        logging.info("Uses MultiStepLR scheduler.")
        milestones = [int(v.strip()) for v in args.milestones.split(",")]
        scheduler = MultiStepLR(optimizer, milestones=milestones,
                                                     gamma=0.1, last_epoch=last_epoch)
예제 #16
0
def main(args):
    DEVICE = torch.device("cuda:0" if torch.cuda.is_available()
                          and args['flow_control']['use_cuda'] else "cpu")

    # eval_path = pathlib.Path(args.eval_dir)
    # eval_path.mkdir(exist_ok=True)
    if not os.path.exists(args['flow_control']['eval_dir']):
        os.mkdir(args['flow_control']['eval_dir'])
    timer = Timer()
    class_names = [
        name.strip()
        for name in open(args['flow_control']['label_file']).readlines()
    ]

    _net = args['flow_control']['net']
    _dataset_type = args['flow_control']['dataset_type']

    if _dataset_type == "voc":
        raise NotImplementedError("Not implement error")
        dataset = VOCDataset(args['flow_control']['dataset'], is_test=True)
    elif _dataset_type == 'open_images':
        raise NotImplementedError("Not implement error")
        dataset = OpenImagesDataset(args['flow_control']['dataset'],
                                    dataset_type="test")
    elif _dataset_type == "coco":
        # dataset = CocoDetection("/home/wenyen4desh/datasets/coco/test2017","/home/wenyen4desh/datasets/annotations/image_info_test2017.json")
        #dataset = CocoDetection("../../dataset/datasets/coco/val2017","../../dataset/datasets/coco/annotations/instances_val2017.json")
        # dataset = CocoDetection("/home/wenyen4desh/datasets/coco/train2017","/home/wenyen4desh/datasets/coco/annotations/instances_train2017.json")
        dataset = CocoDetection(args['Datasets']['coco']['val_image_path'],
                                args['Datasets']['coco']['val_anno_path'])
    elif _dataset_type == "ecp":
        dataset = EuroCity_Dataset(args['Datasets']['ecp']['val_image_path'],
                                   args['Datasets']['ecp']['val_anno_path'])
    true_case_stat, all_gb_boxes, all_difficult_cases = group_annotation_by_class(
        dataset)
    if _net == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif _net == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif _net == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif _net == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    elif _net == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(
            len(class_names),
            width_mult=args['flow_control']['mb2_width_mult'],
            is_test=True)
    else:
        logging.fatal(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        parser.print_help(sys.stderr)
        sys.exit(1)

    #train_transform = MatchPrior(config.priors, config.center_variance,
    #                              config.size_variance, 0.5)

    #test_transform = TestTransform(config.image_size, config.image_mean, config.image_std)
    import pdb
    pdb.set_trace()
    ############################## automatically validation ############################################
    timer.start("Load Model")
    net.load(args['flow_control']['trained_model'])
    net = net.to(DEVICE)
    print('It took {} seconds to load the model.'.format(
        timer.end("Load Model")))
    _nms_method = args['flow_control']['nms_method']
    if _net == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net,
                                             nms_method=_nms_method,
                                             device=DEVICE)
    elif _net == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net,
                                                     nms_method=_nms_method,
                                                     device=DEVICE)
    elif _net == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(
            net, nms_method=_nms_method, device=DEVICE)
    elif _net == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(
            net, nms_method=_nms_method, device=DEVICE)
    elif _net == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(
            net, nms_method=_nms_method, device=DEVICE)
    else:
        logging.fatal(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        parser.print_help(sys.stderr)
        sys.exit(1)

    results = []
    # Predict Bounding Box
    for i in range(len(dataset)):
        print("process image {}", i)
        timer.start("Load Image")
        image = dataset.get_image(i)
        print("Load Image: {:4f} seconds.".format(timer.end("Load Image")))
        timer.start("Predict")
        boxes, labels, probs = predictor.predict(image)
        print("Prediction: {:4f} seconds.".format(timer.end("Predict")))
        indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i
        results.append(
            torch.cat(
                [
                    indexes.reshape(-1, 1),
                    labels.reshape(-1, 1).float(),
                    probs.reshape(-1, 1),
                    boxes + 1.0  # matlab's indexes start from 1
                ],
                dim=1))
    results = torch.cat(results)

    # Write the result to file
    for class_index, class_name in enumerate(class_names):
        if class_index == 0: continue  # ignore background
        file_name = "det_test_{}.txt".format(class_name)
        prediction_path = os.path.join(args['flow_control']['eval_dir'],
                                       file_name)
        with open(prediction_path, "w") as f:
            sub = results[results[:, 1] == class_index, :]
            for i in range(sub.size(0)):
                prob_box = sub[i, 2:].numpy()
                image_id, _ = dataset.get_annotation(int(sub[i, 0]))
                f.write(
                    str(image_id) + " " + " ".join([str(v)
                                                    for v in prob_box]) + "\n")
                # image_id = dataset.ids[int(sub[i, 0])]
                # print(str(image_id) + " " + " ".join([str(v) for v in prob_box]), file=f)

    aps = []
    prcs = []
    recalls = []
    print("\n\nAverage Precision Per-class:")
    for class_index, class_name in enumerate(class_names):
        if class_index == 0:
            continue
        file_name = "det_test_{}.txt".format(class_name)
        prediction_path = os.path.join(args['flow_control']['eval_dir'],
                                       file_name)
        # [email protected] evaluation method
        ap, precision, recall = compute_average_precision_per_class(
            args, true_case_stat[class_index], all_gb_boxes[class_index],
            all_difficult_cases[class_index], prediction_path,
            args['flow_control']['iou_threshold'],
            args['flow_control']['use_2007_metric'])

        # # COCO eval

        # ap, precision, recall = coco_ap_per_class(
        #     true_case_stat[class_index],
        #     all_gb_boxes[class_index],
        #     all_difficult_cases[class_index],
        #     prediction_path,
        #     args.use_2007_metric
        # )

        aps.append(ap)
        prcs.append(precision)
        recalls.append(recall)
        print("{}: {}".format(class_name, ap))

    print("\nAverage Precision Across All Classes:{}".format(
        sum(aps[0:5]) / len(aps[0:5])))
    print("\nAverage Precision :{}".format(sum(prcs[0:5]) / len(prcs[0:5])))
    print("\nAverage Recall :{}".format(sum(recalls[0:5]) / len(recalls[0:5])))
예제 #17
0
def get_map(trained_model, label_file):
    eval_dir = "eval_results"
    eval_path = pathlib.Path(eval_dir)
    eval_path.mkdir(exist_ok=True)
    timer = Timer()
    class_names = [name.strip() for name in open(label_file).readlines()]

    dataset_path = "/home/qindanfeng//work/YOLOv3/datasets/VOC/VOCtest_06-Nov-2007/VOCdevkit/VOC2007"
    dataset = VOCDataset(dataset_path, is_test=True)
    true_case_stat, all_gb_boxes, all_difficult_cases = group_annotation_by_class(
        dataset)

    net = create_mobilenetv2_ssd_lite(len(class_names),
                                      width_mult=1.0,
                                      is_test=True)
    timer.start("Load Model")
    net.load(trained_model)
    net = net.to(DEVICE)
    predictor = create_mobilenetv2_ssd_lite_predictor(net,
                                                      nms_method="hard",
                                                      device=DEVICE)

    results = []
    for i in range(len(dataset)):
        print("process image", i)
        timer.start("Load Image")
        image = dataset.get_image(i)
        print("Load Image: {:4f} seconds.".format(timer.end("Load Image")))
        timer.start("Predict")
        boxes, labels, probs = predictor.predict(image)
        print("Prediction: {:4f} seconds.".format(timer.end("Predict")))
        indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i
        results.append(
            torch.cat(
                [
                    indexes.reshape(-1, 1),
                    labels.reshape(-1, 1).float(),
                    probs.reshape(-1, 1),
                    boxes + 1.0  # matlab's indexes start from 1
                ],
                dim=1))
    results = torch.cat(results)
    for class_index, class_name in enumerate(class_names):
        if class_index == 0: continue  # ignore background
        prediction_path = eval_path / f"det_test_{class_name}.txt"
        with open(prediction_path, "w") as f:
            sub = results[results[:, 1] == class_index, :]
            for i in range(sub.size(0)):
                prob_box = sub[i, 2:].numpy()
                image_id = dataset.ids[int(sub[i, 0])]
                print(image_id + " " + " ".join([str(v) for v in prob_box]),
                      file=f)
    aps = []
    print("\n\nAverage Precision Per-class:")
    for class_index, class_name in enumerate(class_names):
        if class_index == 0:
            continue
        prediction_path = eval_path / f"det_test_{class_name}.txt"
        ap = compute_average_precision_per_class(
            true_case_stat[class_index], all_gb_boxes[class_index],
            all_difficult_cases[class_index], prediction_path, 0.5, True)
        aps.append(ap)
        print(f"{class_name}: {ap}")

    print(f"\nAverage Precision Across All Classes:{sum(aps) / len(aps)}")
    return sum(aps) / len(aps)
예제 #18
0
            itertools.chain(net.regression_headers.parameters(),
                            net.classification_headers.parameters())
        }]

    timer.start("Load Model")
    if args.resume:
        logging.info("Resume from the model {}".format(args.resume))
        net.load(args.resume)
    elif args.base_net:
        logging.info("Init from base net {}".format(args.base_net))
        net.init_from_base_net(args.base_net)
    elif args.pretrained_ssd:
        logging.info("Init from pretrained ssd {}".format(args.pretrained_ssd))
        net.init_from_pretrained_ssd(args.pretrained_ssd)
    logging.info('Took {:.2f} seconds to load the model.'.format(
        timer.end("Load Model")))

    net = get_vgg16_ssd(pretrained=True, is_test=False)

    net.to(DEVICE)

    criterion = MultiboxLoss(config.priors,
                             iou_threshold=0.5,
                             neg_pos_ratio=3,
                             center_variance=0.1,
                             size_variance=0.2,
                             device=DEVICE)
    optimizer = torch.optim.SGD(params,
                                lr=args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)
예제 #19
0
def main(args):
    DEVICE = torch.device(
        "cuda:0" if torch.cuda.is_available() and args.use_cuda else "cpu")
    if not os.path.exists(args.eval_dir):
        os.mkdir(args.eval_dir)
    timer = Timer()
    class_names = [name.strip() for name in open(args.label_file).readlines()]

    # dataset = Folder_image_set()

    # true_case_stat, all_gb_boxes, all_difficult_cases = group_annotation_by_class(dataset)
    if args.net == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif args.net == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif args.net == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif args.net == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    elif args.net == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(len(class_names),
                                          width_mult=args.mb2_width_mult,
                                          is_test=True)
    else:
        logging.fatal(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        parser.print_help(sys.stderr)
        sys.exit(1)

    #train_transform = MatchPrior(config.priors, config.center_variance,
    #                              config.size_variance, 0.5)

    #test_transform = TestTransform(config.image_size, config.image_mean, config.image_std)
    # test_transform = TestTransform(config.image_size, config.image_mean, config.image_std)
    # dataset = FolderDataset("/media/wy_disk/wy_file/Detection/dataset/datasets/ECP_Golden_pattern", transform = test_transform)
    # dataset = FolderDataset("/media/wy_disk/wy_file/Detection/dataset/datasets/ECP_Golden_pattern")
    dataset = FolderDataset("/media/wy_disk/ChenYen/VIRAT/dataset_orgnize/val")

    timer.start("Load Model")
    net.load(args.trained_model)
    net = net.to(DEVICE)
    print('It took {} seconds to load the model.'.format(
        timer.end("Load Model")))

    if args.net == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net,
                                             nms_method=args.nms_method,
                                             device=DEVICE)
    elif args.net == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(
            net, nms_method=args.nms_method, device=DEVICE)
    elif args.net == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(
            net, nms_method=args.nms_method, device=DEVICE)
    elif args.net == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(
            net, nms_method=args.nms_method, device=DEVICE)
    elif args.net == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(
            net, nms_method=args.nms_method, device=DEVICE)
    else:
        logging.fatal(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        parser.print_help(sys.stderr)
        sys.exit(1)

    results = []
    eval_path = "eval_results"
    # eval_whole_image(dataset,5, predictor)
    eval_subblock_image(dataset, 5, predictor)
    import pdb
    pdb.set_trace()
    for i in range(len(dataset)):
        print("process image", i)
        timer.start("Load Image")
        import pdb
        pdb.set_trace()
        image = dataset.get_image(i)
        print("Load Image: {:4f} seconds.".format(timer.end("Load Image")))
        timer.start("Predict")
        boxes, labels, probs = predictor.predict(image, 10, 0.5)
        print("Prediction: {:4f} seconds.".format(timer.end("Predict")))
        # indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i
        # print("index:p\t{}".format(sum(probs>0.5)))
        # import pdb;pdb.set_trace()
        boxes, labels, probs = boxes.data.numpy(), labels.data.numpy(
        ), probs.data.numpy()
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        for box, _label, _prob in zip(boxes, labels, probs):
            if _prob < 0.7: continue
            print(box)
            box = box.astype(int)
            # import pdb;pdb.set_trace()
            print(box)
            cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]),
                          (255, 255, 0), 4)
            # str(str.split(class_names[_label]," ")[1])
            cv2.putText(
                image,
                dataset.class_names[_label],
                (box[0] + 20, box[1] + 40),
                cv2.FONT_HERSHEY_SIMPLEX,
                1,  # font scale
                (255, 0, 255),
                2)  # line type
        print(boxes.shape[0])
        cv2.imshow('annotated', image)
        # key = cv2.waitKey(0)
        if cv2.waitKey(0) & 0xFF == ord('q'):
            break
예제 #20
0
    elif args.net == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(
            net, nms_method=args.nms_method, device=DEVICE)
    else:
        logging.fatal(
            "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
        )
        parser.print_help(sys.stderr)
        sys.exit(1)

    results = []
    for i in range(len(dataset)):
        print("process image", i)
        timer.start("Load Image")
        image = dataset.get_image(i)
        print("Load Image: {:4f} seconds.".format(timer.end("Load Image")))
        timer.start("Predict")
        boxes, labels, probs = predictor.predict(image)
        print("Prediction: {:4f} seconds.".format(timer.end("Predict")))
        indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i
        results.append(
            torch.cat(
                [
                    indexes.reshape(-1, 1),
                    labels.reshape(-1, 1).float(),
                    probs.reshape(-1, 1),
                    boxes + 1.0  # matlab's indexes start from 1
                ],
                dim=1))
    results = torch.cat(results)
    for class_index, class_name in enumerate(class_names):
예제 #21
0
                predictor = create_mobilenetv2_ssd_lite_predictor(
                    net, nms_method=args.nms_method, device=DEVICE)
            else:
                logging.fatal(
                    "The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite."
                )
                parser.print_help(sys.stderr)
                sys.exit(1)

            results = []
            for i in range(len(dataset)):
                print("process image", i)
                timer.start("Load Image")
                image = dataset.get_image(i)
                print("Load Image: {:4f} seconds.".format(
                    timer.end("Load Image")))
                timer.start("Predict")
                boxes, labels, probs = predictor.predict(image)
                print("Prediction: {:4f} seconds.".format(
                    timer.end("Predict")))
                indexes = torch.ones(labels.size(0), 1,
                                     dtype=torch.float32) * i
                if len(boxes) == 0:
                    continue
                results.append(
                    torch.cat(
                        [
                            indexes.reshape(-1, 1),
                            labels.reshape(-1, 1).float(),
                            probs.reshape(-1, 1),
                            boxes + 1.0  # matlab's indexes start from 1