def build_model(version): if version == "v4": config = exp_cfg.YoloTask(model=exp_cfg.Yolo( base="v4", min_level=3, norm_activation=exp_cfg.common.NormActivation(activation="mish"), #_boxes = ['(10, 14)', '(23, 27)', '(37, 58)', '(81, 82)', '(135, 169)', '(344, 319)'], _boxes=[ "(12, 16)", "(19, 36)", "(40, 28)", "(36, 75)", "(76, 55)", "(72, 146)", "(142, 110)", "(192, 243)", "(459, 401)" ], )) elif "tiny" in version: config = exp_cfg.YoloTask(model=exp_cfg.Yolo( base=version, min_level=4, norm_activation=exp_cfg.common.NormActivation(activation="leaky"), _boxes=[ "(10, 14)", "(23, 27)", "(37, 58)", "(81, 82)", "(135, 169)", "(344, 319)" ], #_boxes = ['(12, 16)', '(19, 36)', '(40, 28)', '(36, 75)','(76, 55)', '(72, 146)', '(142, 110)', '(192, 243)','(459, 401)'], )) else: config = exp_cfg.YoloTask(model=exp_cfg.Yolo( base=version, min_level=3, norm_activation=exp_cfg.common.NormActivation(activation="leaky"), #_boxes = ['(10, 14)', '(23, 27)', '(37, 58)', '(81, 82)', '(135, 169)', '(344, 319)'], _boxes=[ "(10, 13)", "(16, 30)", "(33, 23)", "(30, 61)", "(62, 45)", "(59, 119)", "(116, 90)", "(156, 198)", "(373, 326)" ], )) task = YoloTask(config) model = task.build_model() task.initialize(model) pfn = ms.preprocess_fn pofn = utils.DrawBoxes(classes=80, labels=coco.get_coco_names(), display_names=True, thickness=2) server_t = ms.ModelServer(model=model, preprocess_fn=pfn, postprocess_fn=pofn, wait_time=0.00001, max_batch=5) return server_t
def uniary_convert(): with tf.device('gpu:0'): model = None input_size = [416, 416, 3] # config = exp_cfg.YoloTask( # model=exp_cfg.Yolo(_input_size = [416, 416, 3], # base='v4tiny', # min_level=4, # norm_activation = exp_cfg.common.NormActivation(activation="leaky"), # _boxes = ['(10, 14)', '(23, 27)', '(37, 58)', '(81, 82)', '(135, 169)', '(344, 319)'], # #_boxes = ['(20, 28)', '(46, 54)', '(74, 116)', '(81, 82)', '(135, 169)', '(344, 319)'], # #_boxes = ["(10, 13)", "(16, 30)", "(33, 23)","(30, 61)", "(62, 45)", "(59, 119)","(116, 90)", "(156, 198)", "(373, 326)"], # #_boxes = ['(12, 16)', '(19, 36)', '(40, 28)', '(36, 75)','(76, 55)', '(72, 146)', '(142, 110)', '(192, 243)','(459, 401)'], # filter = exp_cfg.YoloLossLayer(use_nms=False) # )) config = exp_cfg.YoloTask(model=exp_cfg.Yolo( _input_size=input_size, base='v3', min_level=3, norm_activation=exp_cfg.common.NormActivation(activation='leaky'), _boxes=[ '(10, 13)', '(16, 30)', '(33, 23)', '(30, 61)', '(62, 45)', '(59, 119)', '(116, 90)', '(156, 198)', '(373, 326)' ], filter=exp_cfg.YoloLossLayer(use_nms=False))) task = YoloTask(config) model = task.build_model() task.initialize(model) #model.build((1, 416, 416, 3)) model(tf.ones((1, 416, 416, 3), dtype=tf.float32), training=False) image = url_to_image( 'https://raw.githubusercontent.com/zhreshold/mxnet-ssd/master/data/demo/dog.jpg' ) image = cv2.resize(image, (416, 416)) image = tf.expand_dims(image, axis=0) func = conversion(model) model.summary() # Convert the model converter = tf.lite.TFLiteConverter.from_concrete_functions( [func.get_concrete_function(image)]) converter.optimizations = [tf.lite.Optimize.DEFAULT] #converter.representative_dataset = get_rep_data() try: tflite_model = converter.convert() except BaseException: print('here') # st.print_exc() import sys sys.exit() with open('detect.tflite', 'wb') as f: f.write(tflite_model)
def get_model(model, input_size=[416, 416, 3], training=True): with tf.device("gpu:0"): if model == "v4tiny": config = exp_cfg.YoloTask(model=exp_cfg.Yolo( _input_size=input_size, base="v4tiny", min_level=4, norm_activation=exp_cfg.common.NormActivation( activation="leaky"), _boxes=[ "(10, 14)", "(23, 27)", "(37, 58)", "(81, 82)", "(135, 169)", "(344, 319)" ], #_boxes = ['(20, 28)', '(46, 54)', '(74, 116)', '(81, 82)', '(135, 169)', '(344, 319)'], #_boxes = ["(10, 13)", "(16, 30)", "(33, 23)","(30, 61)", "(62, 45)", "(59, 119)","(116, 90)", "(156, 198)", "(373, 326)"], #_boxes = ['(12, 16)', '(19, 36)', '(40, 28)', '(36, 75)','(76, 55)', '(72, 146)', '(142, 110)', '(192, 243)','(459, 401)'], filter=exp_cfg.YoloLossLayer(use_nms=False))) name = f"detect-{input_size[0]}.tflite" else: config = exp_cfg.YoloTask(model=exp_cfg.Yolo( _input_size=input_size, base="v3", min_level=3, norm_activation=exp_cfg.common.NormActivation( activation="leaky"), _boxes=[ "(10, 13)", "(16, 30)", "(33, 23)", "(30, 61)", "(62, 45)", "(59, 119)", "(116, 90)", "(156, 198)", "(373, 326)" ], filter=exp_cfg.YoloLossLayer(use_nms=False))) name = f"detect-large-{input_size[0]}.tflite" task = YoloTask(config) model = task.build_model() task.initialize(model) model(tf.ones((1, *input_size), dtype=tf.float32), training=False) return model, name
def test_yolo_input_task(): with tf.device('/CPU:0'): config = yolocfg.YoloTask(model=yolocfg.Yolo( base='v4', min_level=3, norm_activation=yolocfg.common.NormActivation(activation='mish'), #norm_activation = yolocfg.common.NormActivation(activation="leaky"), #_boxes = ['(10, 14)', '(23, 27)', '(37, 58)', '(81, 82)', '(135, 169)', '(344, 319)'], #_boxes = ["(10, 13)", "(16, 30)", "(33, 23)","(30, 61)", "(62, 45)", "(59, 119)","(116, 90)", "(156, 198)", "(373, 326)"], _boxes=[ '(12, 16)', '(19, 36)', '(40, 28)', '(36, 75)', '(76, 55)', '(72, 146)', '(142, 110)', '(192, 243)', '(459, 401)' ], filter=yolocfg.YoloLossLayer(use_nms=False))) task = yolo.YoloTask(config) # loading both causes issues, but oen at a time is not issue, why? train_data = task.build_inputs(config.train_data) test_data = task.build_inputs(config.validation_data) return train_data, test_data
ckpt = tf.train.Checkpoint(backbone=model.backbone) status = ckpt.restore(ckpt_dir_or_file) status.expect_partial().assert_existing_objects_matched() else: assert "Only 'all' or 'backbone' can be used to initialize the model." logging.info('Finished loading pretrained checkpoint from %s', ckpt_dir_or_file) if __name__ == '__main__': import matplotlib.pyplot as plt from yolo.utils.run_utils import prep_gpu prep_gpu() config = exp_cfg.YoloTask(model=exp_cfg.Yolo(base='v3')) task = YoloTask(config) model = task.build_model() model.summary() task.initialize(model) train_data = task.build_inputs(config.train_data) # test_data = task.build_inputs(config.task.validation_data) for l, (i, j) in enumerate(train_data): preds = model(i, training=False) boxes = xcycwh_to_yxyx(j['bbox']) i = tf.image.draw_bounding_boxes(i, boxes, [[1.0, 0.0, 0.0]]) i = tf.image.draw_bounding_boxes(i, preds['bbox'], [[0.0, 1.0, 0.0]])
from yolo.configs import yolo as exp_cfg from yolo.tasks.yolo import YoloTask import yolo.utils.export.tensor_rt as trt prep_gpu() from tensorflow.keras.mixed_precision import experimental as mixed_precision mixed_precision.set_policy('mixed_float16') # mixed_precision.set_policy("float32") config = exp_cfg.YoloTask( model=exp_cfg.Yolo( base='v4', min_level=3, norm_activation=exp_cfg.common.NormActivation(activation='mish'), #norm_activation = exp_cfg.common.NormActivation(activation="leaky"), #_boxes = ['(10, 14)', '(23, 27)', '(37, 58)', '(81, 82)', '(135, 169)', '(344, 319)'], #_boxes = ["(10, 13)", "(16, 30)", "(33, 23)","(30, 61)", "(62, 45)", "(59, 119)","(116, 90)", "(156, 198)", "(373, 326)"], _boxes=[ '(12, 16)', '(19, 36)', '(40, 28)', '(36, 75)', '(76, 55)', '(72, 146)', '(142, 110)', '(192, 243)', '(459, 401)' ], filter=exp_cfg.YoloLossLayer(use_nms=False))) # config = exp_cfg.YoloTask(model=exp_cfg.Yolo(base='v3', # min_level=3, # #norm_activation = exp_cfg.common.NormActivation(activation="mish"), # norm_activation = exp_cfg.common.NormActivation(activation="leaky"), # #_boxes = ['(10, 14)', '(23, 27)', '(37, 58)', '(81, 82)', '(135, 169)', '(344, 319)'], # #_boxes = ["(10, 13)", "(16, 30)", "(33, 23)","(30, 61)", "(62, 45)", "(59, 119)","(116, 90)", "(156, 198)", "(373, 326)"], # _boxes = ['(12, 16)', '(19, 36)', '(40, 28)', '(36, 75)','(76, 55)', '(72, 146)', '(142, 110)', '(192, 243)','(459, 401)'], # filter = exp_cfg.YoloLossLayer(use_nms=False)
from yolo.tasks.yolo import YoloTask if __name__ == "__main__": # initialize YOLOv4 model prep_gpu() from tensorflow.keras.mixed_precision import experimental as mixed_precision mixed_precision.set_policy("float32") config = exp_cfg.YoloTask( model=exp_cfg.Yolo( _input_size=[608, 608, 3], base="v4", min_level=3, norm_activation=exp_cfg.common.NormActivation( activation="mish", use_sync_bn=False), _boxes=[ "(12, 16)", "(19, 36)", "(40, 28)", "(36, 75)", "(76, 55)", "(72, 146)", "(142, 110)", "(192, 243)", "(459, 401)" ], dilate=False, filter=exp_cfg.YoloLossLayer(use_nms=False))) task = YoloTask(config) model = task.build_model() task.initialize(model) # pass in a all white image white_image = tf.fill([1, 608, 608, 3], 1.0) # layers = list(model.backbone.layers) output = model(white_image)
from tensorflow.keras.mixed_precision import experimental as mixed_precision mixed_precision.set_policy('float16') # init a fake webcam # ls /dev/video* # sudo modprobe -r v4l2loopback # sudo modprobe v4l2loopback devices=1 video_nr=20 card_label="v4l2loopback" exclusive_caps=1 # name = "saved_models/v4/regular" # new_name = f"{name}_tensorrt" # model = trt.TensorRT(saved_model=new_name, save_new_path=new_name, max_workspace_size_bytes=4000000000, max_batch_size=5)#, precision_mode="INT8", use_calibration=True) # model.compile() # model.summary() # model.set_postprocessor_fn(func) config = exp_cfg.YoloTask() task = YoloTask(config) model = task.build_model() task.initialize(model) model.summary() cap = FastVideo( # 0, #"testing_files/test.mp4", model=model, process_width=416, process_height=416, preprocess_with_gpu=True, print_conf=True, max_batch=3, disp_h=720, scale_que=1,
import datetime from yolo.utils.run_utils import prep_gpu prep_gpu() from yolo.configs import yolo as exp_cfg from yolo.utils.training import batch_lr_schedule as bls from yolo.utils.training import schedule from yolo.utils.training import history_full_callback as hfc from yolo.tasks.yolo import YoloTask config = exp_cfg.YoloTask( model=exp_cfg.Yolo( base="v4", min_level=3, norm_activation=exp_cfg.common.NormActivation(activation="mish"), #norm_activation = exp_cfg.common.NormActivation(activation="leaky"), #_boxes = ['(10, 14)', '(23, 27)', '(37, 58)', '(81, 82)', '(135, 169)', '(344, 319)'], _boxes=[ "(12, 16)", "(19, 36)", "(40, 28)", "(36, 75)", "(76, 55)", "(72, 146)", "(142, 110)", "(192, 243)", "(459, 401)" ], filter=exp_cfg.YoloLossLayer(use_nms=True)), darknet_load_decoder=False) task = YoloTask(config) trainer = Trainer(task) train, test = trainer.build_datasets() size = tf.data.experimental.cardinality(train).numpy() tsize = tf.data.experimental.cardinality(test).numpy() EPOCHS = 1 step_changes = [int(size * EPOCHS * 0.8), int(size * EPOCHS * 0.9)]
def build_losses(self, outputs, labels, aux_losses=None): return def build_metrics(self, training=True): return def train_step(self, inputs, model, optimizer, metrics=None): return def validation_step(self, inputs, model, metrics=None): return def aggregate_logs(self, state=None, step_outputs=None): return def reduce_aggregated_logs(self, aggregated_logs): return if __name__ == "__main__": cfg = exp_cfg.YoloTask() print(cfg.as_dict()) # task = YoloTask(exp_cfg.YoloTask()) # model = task.build_model() # model.summary()
if __name__ == "__main__": from yolo.utils.run_utils import prep_gpu from yolo.configs import yolo as exp_cfg from yolo.tasks.yolo import YoloTask import tensorflow_datasets as tfds import yolo.utils.export.tensor_rt as trt import matplotlib.pyplot as plt prep_gpu() from tensorflow.keras.mixed_precision import experimental as mixed_precision mixed_precision.set_policy("mixed_float16") config = exp_cfg.YoloTask(model=exp_cfg.Yolo(base="v4tiny", min_level=4)) task = YoloTask(config) model = task.build_model() task.initialize(model) model.summary() model.predict(tf.ones((1, 416, 416, 3), dtype=tf.float16)) # #name = "saved_models/v4/tflite-regualr-no-nms" # #name = "saved_models/v4/tflite-tiny-no-nms" # name = "saved_models/v4/tiny" # new_name = f"{name}_tensorrt" # model = trt.TensorRT( # saved_model=new_name, # save_new_path=new_name, # max_workspace_size_bytes=4000000000, # max_batch_size=5) # , precision_mode="INT8", use_calibration=True)