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)
Example #2
0
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
Example #3
0
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
  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)
  #                     ))
  task = YoloTask(config)
  model = task.build_model()
  task.initialize(model)

  # model(tf.ones((1, 416, 416, 3), dtype = tf.float32))
Example #5
0
    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)]
    schedule = schedule.Schedule(1000,
                                 0.001,
                                 total_steps=size * EPOCHS,
                                 type="STEPWISE",
                                 step_changes=step_changes,