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 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 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
_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) # )) task = YoloTask(config) model = task.build_model() task.initialize(model) # model(tf.ones((1, 416, 416, 3), dtype = tf.float32)) # name = "saved_models/v3/tflite-tiny-no-nms" # model.save(name, overwrite=True) # new_name = f"{name}_tensorrt" # model = trt.TensorRT(saved_model=name, save_new_path=new_name, max_workspace_size_bytes=4000000000, max_batch_size=5)#, precision_mode="INT8", use_calibration=True) # model.convertModel() # model.compile() # model.summary() # model.set_postprocessor_fn(func) # #name = "saved_models/v4/tflite-regualr-no-nms" # name = "saved_models/v4/tflite-tiny-no-nms"
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)] schedule = schedule.Schedule(1000, 0.001, total_steps=size * EPOCHS, type="STEPWISE", step_changes=step_changes, decay_factor=0.1) lr_callback = bls.LearningRateScheduler(schedule) history = hfc.HistoryFull()