def _load_model(self, model):
        if (type(model) == type(None)):
            prep_gpu()
            with tf.device(self._gpu_device):
                model = build_model(name="regular",
                                    w=self._p_width,
                                    h=self._p_height,
                                    policy=self._policy)
            return model
        default_set = {"regular", "tiny", "spp"}
        if (type(model) == str and model in default_set):
            print(model)
            prep_gpu()
            with tf.device(self._gpu_device):
                model = build_model(name=model,
                                    model_version=self._model_version,
                                    w=self._p_width,
                                    h=self._p_height,
                                    saved=False,
                                    policy=self._policy)
            return model
        elif (type(model) == str):
            raise Exception("unsupported default model")

        # a model object passed in
        return model
def gt_test():
    model, loss_fn, dataset, anchors, masks = build_model_partial(
        name="regular", use_mixed=False, split="validation", batch_size=10)
    partial_model = filter_partial()
    pred_model = build_model()

    i = 0

    for image, label in dataset:
        box, classif = partial_model(label)
        boxes, classes, conf = pred_model(image)
        pred = model(image)

        image = tf.image.draw_bounding_boxes(image, box, [[0.0, 1.0, 0.0]])
        image = tf.image.draw_bounding_boxes(image, boxes, [[1.0, 0.0, 0.0]])
        image = image[0].numpy()

        loss = 0
        sum_lab = 0
        for key in pred.keys():
            loss += loss_fn[key](label[key], pred[key])
        tf.print("loss: ", loss)
        print()
        if loss > 100:
            plt.imshow(image)
            plt.show()
        if i == 1000:
            break
        i += 1

    return
Exemplo n.º 3
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
Exemplo n.º 4
0
def accuracy(model="regular", metrics=None):
    with tf.device("/GPU:0"):
        model = build_model(name=model, w=None, h=None)
        partial_filter = filter_partial()

    dataset, Info = tfds.load('coco',
                              split='validation',
                              with_info=True,
                              shuffle_files=True,
                              download=False)
    Size = int(Info.splits['test'].num_examples)
    dataset = preprocessing(dataset, 100, "detection", Size, 1, 80, False)
    #dataset = load_dataset(skip = 0, batch_size = 1, multiplier = 10000)
    optimizer = ks.optimizers.SGD()
    loss = load_loss()
    model.compile(optimizer=optimizer, loss=loss)
    #model.evaluate(dataset)
    counter = 0
    total_recall = 0
    total_accuracy = 0
    total_iou = 0

    for image, label in dataset:
        with tf.device("/GPU:0"):
            y_pred = model.predict(image)
            y_true = partial_filter(label)

        recall, accuracy, iou = test_iou_match(y_true, y_pred)

        total_recall += recall
        total_accuracy += accuracy
        total_iou += iou

        counter += 1
        print(
            "recall: %0.3f, accuracy: %0.3f, bbox iou: %0.3f, number of samples: %0.3f"
            % (total_recall / counter, total_accuracy / counter,
               total_iou / counter, counter),
            end="\r")
    return
Exemplo n.º 5
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