Ejemplo n.º 1
0
def evaluate_pascal_voc(root_dir, download, batch_size, conf_thres, log_every,
                        limit, plot, save, images_results_dir):
    cfg_file = os.path.join(HERE, '../cfg/yolov3.cfg')
    weight_file = os.path.join(HERE, '../cfg/yolov3.weights')
    namesfile = os.path.join(HERE, '../cfg/coco.names')
    class_names = load_class_names(namesfile)

    model = Yolo(cfg_file=cfg_file,
                 class_names=class_names,
                 batch_size=batch_size)
    model.load_weights(weight_file)

    image_and_target_transform = Compose([SquashResize(416), ToTensor()])

    dataset = YaYoloVocDataset(root_dir=root_dir,
                               batch_size=batch_size,
                               transforms=image_and_target_transform,
                               image_set='val',
                               download=download,
                               class_names=class_names)

    summary_writer = SummaryWriter(comment=f' evaluate={batch_size}')
    evaluate(model,
             dataset,
             summary_writer,
             images_results_dir,
             iou_thres=0.5,
             conf_thres=conf_thres,
             nms_thres=0.5,
             log_every=log_every,
             limit=limit,
             plot=plot,
             save=save)

    summary_writer.close()
def run_detect_cars(in_dir, out_dir, batch_size, limit, conf_thres, nms_thres, debug):
    assert os.path.isdir(out_dir), "directory {} does not exist".format(out_dir)

    now = datetime.datetime.now()
    now_str = now.strftime("%Y-%m-%dT%H-%M-%S")
    detected_dataset_dir = os.path.join(out_dir, now_str)
    os.mkdir(detected_dataset_dir)

    feed_file = os.path.join(detected_dataset_dir, "feed.json")
    detected_dataset_images_dir = os.path.join(detected_dataset_dir, "images")
    os.mkdir(detected_dataset_images_dir)

    cfg_file = os.path.join(HERE, '../cfg/yolov3.cfg')
    weight_file = os.path.join(HERE, '../cfg/yolov3.weights')
    namesfile = os.path.join(HERE, '../cfg/coco.names')
    class_names = load_class_names(namesfile)
    with torch.no_grad():
        model = Yolo(cfg_file=cfg_file, class_names=class_names, batch_size=batch_size)
        model.load_weights(weight_file)
        model.to(DEVICE)
        model.eval()

        image_and_target_transform = Compose([
            SquashResize(416),
            ToTensor()
        ])

        dataset = SimpleCarDataset(
            root_dir=in_dir,
            transforms=image_and_target_transform,
            batch_size=batch_size)

        with FileWriter(file_path=feed_file) as file_writer:
            car_dataset_writer = DetectedCarDatasetWriter(file_writer)

            detected_dataset_helper = DetectedCarDatasetHelper(car_dataset_writer=car_dataset_writer,
                                                               class_names=model.class_names,
                                                               conf_thres=conf_thres,
                                                               nms_thres=nms_thres,
                                                               batch_size=batch_size,
                                                               debug=debug)
            cnt = detect_and_process(model=model,
                                     dataset=dataset,
                                     processor=detected_dataset_helper.process_detections,
                                     limit=limit)

            logger.info("Ran detection of {} images".format(cnt))
Ejemplo n.º 3
0
def detect_image(img_path):

    model = YOLOv3Net(cfgfile, model_size, number_classes)
    model.load_weights(weightfile)

    class_names = load_class_names(class_name)

    # read image
    # image = cv2.imread(img_path)
    # convert image to ndarray image
    image = np.array(img_path)
    """
     This operation is useful if you want to add a batch dimension to a single element. 
     For example, if you have a single image of shape [height, width, channels], you can make it a 
     batch of 1 image with expand_dims(image, 0), which will make the shape [1, height, width, channels].
    """
    image = tf.expand_dims(image, 0)
    resize_frame = resize_image(image, (model_size[0], model_size[1]))
    pred = model.predict(resize_frame)

    boxes, scores, classes, nums = output_boxes(
        pred,
        model_size,
        max_output_size=max_output_size,
        max_output_size_per_class=max_output_size_per_class,
        iou_threshold=iou_threshold,
        confidence_threshold=confidence_threshold)

    # Convert back to ndarray image. The input array, but with all or a subset of the dimensions of length 1 removed.
    image = np.squeeze(image)

    img, obj_name = draw_outputs(image, boxes, scores, classes, nums,
                                 class_names)
    labels = [
        "person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train",
        "truck", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant",
        "bear", "zebra", "giraffe", "chair", "refrigerator"
    ]
    object_with_label = {}
    for key, val in obj_name.items():
        if key in labels:
            object_with_label[key] = val
            print('{} {:.4f}'.format(key, val))
    return object_with_label
Ejemplo n.º 4
0
def train_coco(coco_images_dir, coco_annotations_file, batch_size, lr,
               conf_thres, gradient_accumulations, clip_gradients, epochs,
               limit, log_every, save_every, model_dir, parameters):
    cfg_file = os.path.join(HERE, '../cfg/yolov3.cfg')
    weight_file = os.path.join(HERE, '../cfg/yolov3.weights')
    namesfile = os.path.join(HERE, '../cfg/coco.names')
    class_names = load_class_names(namesfile)
    model = Yolo(cfg_file=cfg_file,
                 class_names=class_names,
                 batch_size=batch_size)
    model.load_weights(weight_file)
    model.freeze_parameters_of_all_old_layers()

    if parameters is not None:
        logger.info(f"loading model parameters from {parameters}")
        model.load_state_dict(torch.load(parameters, map_location=DEVICE))

    image_and_target_transform = Compose([SquashResize(416), ToTensor()])

    dataset = YaYoloCocoDataset(images_dir=coco_images_dir,
                                annotations_file=coco_annotations_file,
                                transforms=image_and_target_transform,
                                batch_size=batch_size)

    summary_writer = SummaryWriter(comment=f' evaluate={batch_size}')
    train(model=model,
          dataset=dataset,
          model_dir=model_dir,
          summary_writer=summary_writer,
          epochs=epochs,
          lr=lr,
          conf_thres=conf_thres,
          nms_thres=0.5,
          iou_thres=0.5,
          lambda_coord=5,
          lambda_no_obj=0.5,
          gradient_accumulations=gradient_accumulations,
          clip_gradients=clip_gradients,
          limit=limit,
          debug=False,
          print_every=log_every,
          save_every=save_every)
    summary_writer.close()
Ejemplo n.º 5
0
def main():

    model = YOLOv3Net(cfgfile, model_size, number_classes)
    model.load_weights(weightfile)
    class_names = load_class_names(class_name)

    win_name = 'Yolov3 detection'
    cv2.namedWindow(win_name)

    #specify the video input

    cap = cv2.VideoCapture(
        'rtsp://*****:*****@218.151.33.75:554/Streaming/channels/301')
    #cap = cv2.VideoCapture('rtsp://*****:*****@sel312.iptime.org:20004/MOBILE/media.smp')
    frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH),
                  cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    try:
        while True:
            start = time.time()
            ret, frame = cap.read()

            if not ret:
                break

            resized_frame = tf.expand_dims(frame, 0)
            resized_frame = resize_image(resized_frame,
                                         (model_size[0], model_size[1]))

            pred = model.predict(resized_frame)

            boxes, scores, classes, nums = output_boxes(
                pred,
                model_size,
                max_output_size=max_output_size,
                max_output_size_per_class=max_output_size_per_class,
                iou_threshold=iou_threshold,
                confidence_threshold=confidence_threshold)

            img, obj_name = draw_outputs(frame, boxes, scores, classes, nums,
                                         class_names)

            labels = [
                "person", "bicycle", "car", "motorbike", "aeroplane", "bus",
                "train", "truck", "bird", "cat", "dog", "horse", "sheep",
                "cow", "elephant", "bear", "zebra", "giraffe"
            ]
            for key, val in obj_name.items():
                if key in labels:
                    print('{} {:.4f}'.format(key, val))

            cv2.imshow(win_name, img)

            stop = time.time()

            seconds = stop - start

            # calculate frame per second
            fpt = 1 / seconds
            print("Estimated frames per second : {0}".format(fpt))

            key = cv2.waitKey(30) & 0xFF

            if key == ord('q'):
                break
    finally:
        cv2.destroyAllWindows()
        cap.release()
        print('Detections have been performed successfully.')