Example #1
0
def gt_test():
    import tensorflow_datasets as tfds
    strat = tf.distribute.MirroredStrategy()
    with strat.scope():
        train, info = tfds.load('coco',
                                split='train',
                                shuffle_files=True,
                                with_info=True)
        test, info = tfds.load('coco',
                               split='validation',
                               shuffle_files=False,
                               with_info=True)
        model = build_model(
            model_version="v4", policy="mixed_float16"
        )  #, weights_file= "testing_weights/yolov3-regular.weights")
        model.get_summary()

        loss_fn = model.generate_loss(loss_type="ciou")
        train, test = model.process_datasets(train,
                                             test,
                                             jitter_boxes=None,
                                             jitter_im=0.1,
                                             batch_size=1,
                                             _eval_is_training=False)

    colors = gen_colors(80)
    coco_names = get_coco_names()
    i = 0
    for image, label in train:
        print(label.keys())
        pred = model.predict(image)

        image = tf.image.draw_bounding_boxes(image, pred["bbox"],
                                             [[1.0, 0.0, 0.0]])
        image = tf.image.draw_bounding_boxes(image,
                                             _xcycwh_to_yxyx(label["bbox"]),
                                             [[0.0, 1.0, 0.0]])
        image = image[0].numpy()

        plt.imshow(image)
        plt.show()

        loss, metric_dict = model.apply_loss_fn(label, pred["raw_output"])
        print(f"loss: {loss}")
        if i == 10:
            break
        i += 1
    return
Example #2
0
def video_processor(model, version , vidpath, device="/CPU:0"):
    img_array = []

    i = 0
    t = 0
    start = time.time()
    tick = 0
    e, f, a, b, c, d = 0, 0, 0, 0, 0, 0
    if isinstance(model, str):
        with tf.device(device):
            model = build_model(name=model, model_version=version)
            model.make_predict_function()

    if hasattr(model, "predict"):
        predfunc = model.predict
        print("using pred function")
    else:
        predfunc = model
        print("using call function")

    colors = gen_colors(80)
    label_names = get_coco_names(
        path="yolo/dataloaders/dataset_specs/coco.names")
    print(label_names)

    # output_writer = cv2.VideoWriter('yolo_output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), frame_count, (480, 640))  # change output file name if needed
    pred = None
    cap = cv2.VideoCapture(vidpath)
    assert cap.isOpened()

    width = int(cap.get(3))
    height = int(cap.get(4))
    print('width, height, fps:', width, height, int(cap.get(5)))
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

    while cap.isOpened():
        success, image = cap.read()

        #with tf.device(device):
        e = datetime.datetime.now()
        image = tf.cast(image, dtype=tf.float32)
        image = image / 255
        f = datetime.datetime.now()

        if t % 1 == 0:
            a = datetime.datetime.now()
            #with tf.device(device):
            pimage = tf.expand_dims(image, axis=0)
            pimage = tf.image.resize(pimage, (416, 416))
            pred = predfunc(pimage)
            b = datetime.datetime.now()

        image = image.numpy()
        if pred != None:
            c = datetime.datetime.now()
            boxes, classes = int_scale_boxes(pred["bbox"], pred["classes"],
                                             width, height)
            draw = get_draw_fn(colors, label_names, 'YOLO')
            draw_box(image, boxes[0].numpy(), classes[0].numpy(),
                     pred["confidence"][0], draw)
            d = datetime.datetime.now()

        cv2.imshow('frame', image)
        i += 1
        t += 1

        if time.time() - start - tick >= 1:
            tick += 1
            print_opt((((f - e) + (b - a) + (d - c))), i)
            i = 0
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
    return
    def __init__(self,
                 file_name,
                 model="regular",
                 model_version="v3",
                 preprocess_function=None,
                 process_width=416,
                 process_height=416,
                 disp_h=720,
                 classes=80,
                 labels=None,
                 print_conf=True,
                 max_batch=None,
                 wait_time=None,
                 preprocess_with_gpu=False,
                 scale_que=1,
                 policy="float16",
                 gpu_device="/GPU:0",
                 preprocess_gpu="/GPU:0"):
        self._cap = cv2.VideoCapture(file_name)
        if not self._cap.isOpened():
            raise IOError("video file was not found")

        #support for ANSI cahracters in windows
        support_windows()

        self._file = file_name
        self._fps = 120000000

        self._gpu_device = gpu_device
        if preprocess_with_gpu:
            self._pre_process_device = preprocess_gpu
        else:
            self._pre_process_device = "/CPU:0"

        self._preprocess_function = preprocess_function
        self._height = int(self._cap.get(4)) if disp_h == None else disp_h
        self._og_height = int(self._cap.get(4))
        self._width = int(self._cap.get(3) * (self._height / self._og_height))
        self._classes = classes
        self._p_width = process_width
        self._p_height = process_height
        self._model_version = model_version
        self._policy = policy
        self._model = self._load_model(model)

        # fast but as close to one 2 one as possible
        if max_batch == None:
            if file_name == 0:
                self._batch_size = 5  # 40 fps more conistent frame to frame
            else:
                # faster but more potential for delay from input to output
                if tf.keras.mixed_precision.experimental.global_policy(
                ).name == "mixed_float16" or tf.keras.mixed_precision.experimental.global_policy(
                ).name == "float16":
                    #self._batch_size = 9 # 45 fps faster but less frame to frame consistent, it will remain consistant, but there is opertunity for more frames to be loaded than
                    self._batch_size = 5
                else:
                    self._batch_size = 3

                if process_width > 416 or process_height > 416:
                    self._batch_size = 3
        else:
            self._batch_size = max_batch

        self._colors = gen_colors(self._classes)

        if labels == None:
            self._labels = get_coco_names(
                path="yolo/dataloaders/dataset_specs/coco.names")
        else:
            self._labels = labels

        self._draw_fn = get_draw_fn(self._colors, self._labels, print_conf)

        self._load_que = Queue(self._batch_size * scale_que)
        self._display_que = Queue(1 * scale_que)
        self._running = True
        if self._batch_size != 1:
            self._wait_time = 0.0015 * self._batch_size if wait_time == None else wait_time  # 0.05 default
        else:
            self._wait_time = 0.0001

        self._read_fps = 1
        self._display_fps = 1
        self._latency = -1
        self._batch_proc = 1
        self._frames = 1
        self._obj_detected = -1
        return
Example #4
0
    def __init__(self,
                 file_name,
                 model="regular",
                 preprocess_function=None,
                 process_width=416,
                 process_height=416,
                 classes=80,
                 labels=None,
                 preprocess_with_gpu=False,
                 gpu_device="/GPU:0",
                 preprocess_gpu="/GPU:0"):
        self._cap = cv2.VideoCapture(file_name)
        if not self._cap.isOpened():
            raise IOError("video file was not found")

        #support for ANSI cahracters in windows
        support_windows()

        self._file = file_name
        self._fps = 120

        # fast but as close to one 2 one as possible
        if file_name == 0:
            self._batch_size = 5  # 40 fps more conistent frame to frame
        else:
            # faster but more potential for delay from input to output
            self._batch_size = 9  # 45 fps faster but less frame to frame consistent, it will remain consistant, but there is opertunity for more frames to be loaded than

            if process_width > 416 or process_height > 416:
                self._batch_size = 3

        self._gpu_device = gpu_device
        if preprocess_with_gpu:
            self._pre_process_device = preprocess_gpu
        else:
            self._pre_process_device = "/CPU:0"

        self._preprocess_function = preprocess_function
        self._width = int(self._cap.get(3))
        self._height = int(self._cap.get(4))
        self._classes = classes
        self._model = model
        self._p_width = process_width
        self._p_height = process_height

        self._colors = gen_colors(self._classes)

        if labels == None:
            self._labels = get_coco_names(
                path="yolo/dataloaders/dataset_specs/coco.names")
        else:
            self._labels = labels

        self._load_que = Queue(self._batch_size * 5)
        self._display_que = Queue(self._batch_size)
        self._running = True
        self._wait_time = 0.01

        self._read_fps = 1
        self._display_fps = 1
        self._latency = None
        self._batch_proc = 1
        self._frames = 0
        self._obj_detected = 0
        return