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
Ejemplo n.º 2
0
def main2():
    import contextlib
    import yolo.utils.tensor_rt as trt
    prep_gpu()

    def func(inputs):
        boxes = inputs["bbox"]
        classifs = tf.one_hot(inputs["classes"],
                              axis=-1,
                              dtype=tf.float32,
                              depth=80)
        nms = tf.image.combined_non_max_suppression(
            tf.expand_dims(boxes, axis=2), classifs, 200, 200, 0.5, 0.5)
        return {
            "bbox": nms.nmsed_boxes,
            "classes": nms.nmsed_classes,
            "confidence": nms.nmsed_scores,
        }

    name = "testing_weights/yolov4/full_models/v4_32"
    new_name = f"{name}_tensorrt"
    model = trt.TensorRT(saved_model=new_name,
                         save_new_path=new_name,
                         max_workspace_size_bytes=4000000000)
    model.compile()
    model.summary()
    model.set_postprocessor_fn(func)

    support_windows()
    video_processor(model, version=None, vidpath="testing_files/test2.mp4")

    return 0
Ejemplo n.º 3
0
def new_video_proc_rt(og_model_dir, rt_model_save_path, video_path):
    import yolo.demos.tensor_rt as trt
    video = frame_iter(video_path, rt_preprocess_fn)
    prep_gpu()
    trt.get_rt_model(og_model_dir, rt_model_save_path, video.get_frame_rt)
    # model = trt.get_func_from_saved_model(rt_model_save_path)
    # for frame in video.get_frame():
    #     pred = model(frame)
    #     frame = video.get_og_frame()
    #     cv2.imshow('frame', frame)
    #     if cv2.waitKey(1) & 0xFF == ord('q'):
    #         break
    return
Ejemplo n.º 4
0
import cv2

import tensorflow as tf
import tensorflow.keras as ks
import tensorflow.keras.backend as K

import tensorflow_datasets as tfds
from yolo.modeling.functions.iou import box_iou

import matplotlib.pyplot as plt
import numpy as np

from yolo.utils.testing_utils import prep_gpu, build_model, filter_partial, draw_box, int_scale_boxes, gen_colors, get_coco_names, load_loss

prep_gpu()
#from yolo.modeling.functions.voc_test import load_dataset
from yolo.dataloaders.preprocessing_functions import preprocessing


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)
Ejemplo n.º 5
0
def main(args, argv=None):
    prep_gpu()
    _train_eager()
    return
Ejemplo n.º 6
0
            load_weights_backbone(self._backbone, encoder)
            self._backbone.trainable = False

        if dn2tf_head:
            load_weights_backbone(self._neck, neck)
            self._neck.trainable = False
            load_weights_v4head(self._head, decoder)
            self._head.trainable = False
        return


if __name__ == "__main__":
    import tensorflow_datasets as tfds
    from yolo.utils.testing_utils import prep_gpu
    from yolo.training.call_backs.PrintingCallBack import Printer
    prep_gpu()  # must be called before loading a dataset
    # train, info = tfds.load('coco/2017',
    #                         split='train',
    #                         shuffle_files=True,
    #                         with_info=True)
    # test, info = tfds.load('coco/2017',
    #                        split='validation',
    #                        shuffle_files=False,
    #                        with_info=True)

    model = Yolov4(model="regular", policy="float32", use_tie_breaker=True)
    model.build(model._input_shape)
    model.get_summary()
    # model.load_weights_from_dn(dn2tf_head=True)

    # train, test = model.process_datasets(train, test, batch_size=1, jitter_im = 0.1, jitter_boxes = 0.005, _eval_is_training = False)
def loss_test_fast(model_name="regular", batch_size=5, epochs=3):
    #very large probelm, pre processing fails when you start batching
    prep_gpu()
    from yolo.dataloaders.preprocessing_functions import preprocessing
    strat = tf.distribute.MirroredStrategy()
    with strat.scope():
        model, loss_fn, anchors, masks = build_model_partial(name=model_name,
                                                             ltype="giou",
                                                             use_mixed=False,
                                                             split="train",
                                                             load_head=False,
                                                             fixed_size=True)

        setname = "coco"
        dataset, Info = tfds.load(setname,
                                  split="train",
                                  with_info=True,
                                  shuffle_files=True,
                                  download=True)
        val, InfoVal = tfds.load(setname,
                                 split="validation",
                                 with_info=True,
                                 shuffle_files=True,
                                 download=True)
        dataset.concatenate(val)

        size = int(Info.splits["train"].num_examples)
        valsize = int(Info.splits["validation"].num_examples)

        dataset = preprocessing(dataset,
                                100,
                                "detection",
                                size + valsize,
                                batch_size,
                                80,
                                anchors=anchors,
                                masks=masks,
                                fixed=False,
                                jitter=True)

        train = dataset.take(size // batch_size)
        test = dataset.skip(size // batch_size)

        map_50 = YoloMAP_recall(name="recall")
        Detection_50 = YoloMAP(name="Det")

    optimizer = ks.optimizers.SGD(lr=1e-3, momentum=0.99)
    callbacks = [
        ks.callbacks.LearningRateScheduler(lr_schedule2)
    ]  #, tf.keras.callbacks.TensorBoard(log_dir="./logs", update_freq = 10)]
    model.compile(optimizer=optimizer, loss=loss_fn,
                  metrics=[map_50])  #, Detection_50])
    try:
        model.summary()
        print(size // batch_size, epochs)
        model.fit(train,
                  validation_data=test,
                  shuffle=True,
                  callbacks=callbacks,
                  epochs=epochs)
        model.save_weights("weights/train_test_desk_fast_1")
    except KeyboardInterrupt:
        model.save_weights("weights/train_test_desk_fast_exit_early_1")
    return