Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yolov4-tiny-3l issue resolved #325

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ python detect.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --

python detect.py --weights ./checkpoints/yolov4-tiny-416 --size 416 --model yolov4 --image ./data/kite.jpg --tiny

# yolov4-tiny-3l
python save_model.py --weights ./data/yolov4-tiny-3l-608_5000.weights --output ./checkpoints/yolov4-tiny-3l-608 --input_size 608 --model yolov4-tiny-3l --tiny

# Run yolov4-tiny-3l tensorflow model
python detect.py --weights ./checkpoints/yolov4-tiny-3l-608 --size 608 --model yolov4 --images ./data/images/kite.jpg --tiny
```
If you want to run yolov3 or yolov3-tiny change ``--model yolov3`` in command

Expand All @@ -48,6 +53,15 @@ python save_model.py --weights ./data/yolov4.weights --output ./checkpoints/yolo
# yolov4
python convert_tflite.py --weights ./checkpoints/yolov4-416 --output ./checkpoints/yolov4-416.tflite

# yolov4-tiny
python save_model.py --weights ./data/yolov4-tiny-3l-608_5000.weights --output ./checkpoints/yolov4-tiny-3l-608 --input_size 608 --model yolov4-tiny-3l --tiny -framework tflite

# convert custom yolov4-tiny-3l tflite model
python convert_tflite.py --weights ./checkpoints/yolov4-tiny-3l-608 --output ./checkpoints/yolov4-tiny-3l-608.tflite

# yolov4-tiny-3l quantize float16
python convert_tflite.py --weights ./checkpoints/yolov4-tiny-3l-608 --output ./checkpoints/yolov4-tiny-3l-fp16.tflite --quantize_mode float16

# yolov4 quantize float16
python convert_tflite.py --weights ./checkpoints/yolov4-416 --output ./checkpoints/yolov4-416-fp16.tflite --quantize_mode float16

Expand Down
1 change: 1 addition & 0 deletions convert_tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def save_tflite():
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
converter.allow_custom_ops = True
elif FLAGS.quantize_mode == 'int8':
converter.experimental_new_converter = False
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
Expand Down
43 changes: 43 additions & 0 deletions core/backbone.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,49 @@ def cspdarknet53_tiny(input_data):

return route_1, input_data

def cspdarknet53_tiny_3l(input_data):
input_data = common.convolutional(input_data, (3, 3, 3, 32), downsample=True)
input_data = common.convolutional(input_data, (3, 3, 32, 64), downsample=True)
input_data = common.convolutional(input_data, (3, 3, 64, 64))

route = input_data
input_data = common.route_group(input_data, 2, 1)
input_data = common.convolutional(input_data, (3, 3, 32, 32))
route_1 = input_data
input_data = common.convolutional(input_data, (3, 3, 32, 32))
input_data = tf.concat([input_data, route_1], axis=-1)
input_data = common.convolutional(input_data, (1, 1, 64, 64))
input_data = tf.concat([route, input_data], axis=-1)
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)

input_data = common.convolutional(input_data, (3, 3, 128, 128))
route = input_data
input_data = common.route_group(input_data, 2, 1)
input_data = common.convolutional(input_data, (3, 3, 64, 64))
route_1 = input_data
input_data = common.convolutional(input_data, (3, 3, 64, 64))
input_data = tf.concat([input_data, route_1], axis=-1)
input_data = common.convolutional(input_data, (1, 1, 128, 128))
route_2 = input_data
input_data = tf.concat([route, input_data], axis=-1)
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)

input_data = common.convolutional(input_data, (3, 3, 256, 256))
route = input_data
input_data = common.route_group(input_data, 2, 1)
input_data = common.convolutional(input_data, (3, 3, 128, 128))
route_1 = input_data
input_data = common.convolutional(input_data, (3, 3, 128, 128))
input_data = tf.concat([input_data, route_1], axis=-1)
input_data = common.convolutional(input_data, (1, 1, 256, 256))
route_1 = input_data
input_data = tf.concat([route, input_data], axis=-1)
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)

input_data = common.convolutional(input_data, (3, 3, 512, 512))

return route_1, route_2, input_data

def darknet53_tiny(input_data):
input_data = common.convolutional(input_data, (3, 3, 3, 16))
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)
Expand Down
8 changes: 6 additions & 2 deletions core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
# YOLO options
__C.YOLO = edict()

__C.YOLO.CLASSES = "./data/classes/coco.names"
__C.YOLO.CLASSES = "./data/classes/obj.names"
__C.YOLO.ANCHORS = [12,16, 19,36, 40,28, 36,75, 76,55, 72,146, 142,110, 192,243, 459,401]
__C.YOLO.ANCHORS_V3 = [10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326]
__C.YOLO.ANCHORS_TINY = [23,27, 37,58, 81,82, 81,82, 135,169, 344,319]
# __C.YOLO.ANCHORS_TINY = [23,27, 37,58, 81,82, 81,82, 135,169, 344,319]
__C.YOLO.ANCHORS_TINY = [10,14, 23,27, 37,58, 81,82, 135,169, 344,319]
__C.YOLO.ANCHORS_TINY_3l = [12,16, 19,36, 40,28, 36,75, 76,55, 72,146, 142,110, 192,243, 459,401]
__C.YOLO.STRIDES = [8, 16, 32]
__C.YOLO.STRIDES_TINY = [16, 32]
__C.YOLO.STRIDES_TINY_3l = [8, 16, 32]
__C.YOLO.XYSCALE = [1.2, 1.1, 1.05]
__C.YOLO.XYSCALE_TINY = [1.05, 1.05]
__C.YOLO.XYSCALE_TINY_3l = [1.05, 1.05, 1.05]
__C.YOLO.ANCHOR_PER_SCALE = 3
__C.YOLO.IOU_LOSS_THRESH = 0.5

Expand Down
25 changes: 18 additions & 7 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def load_weights(model, weights_file, model_name='yolov4', is_tiny=False):
if model_name == 'yolov3':
layer_size = 13
output_pos = [9, 12]
elif model_name == 'yolov4-tiny-3l':
layer_size = 24
output_pos = [17, 20, 23]
else:
layer_size = 21
output_pos = [17, 20]
Expand Down Expand Up @@ -81,24 +84,32 @@ def read_class_names(class_file_name):

def load_config(FLAGS):
if FLAGS.tiny:
STRIDES = np.array(cfg.YOLO.STRIDES_TINY)
ANCHORS = get_anchors(cfg.YOLO.ANCHORS_TINY, FLAGS.tiny)
XYSCALE = cfg.YOLO.XYSCALE_TINY if FLAGS.model == 'yolov4' else [1, 1]
if FLAGS.model == 'yolov4-tiny-3l':
STRIDES = np.array(cfg.YOLO.STRIDES_TINY_3l)
ANCHORS = get_anchors(cfg.YOLO.ANCHORS_TINY_3l, FLAGS.tiny, FLAGS.model)
XYSCALE = cfg.YOLO.XYSCALE_TINY_3l
else:
STRIDES = np.array(cfg.YOLO.STRIDES_TINY)
ANCHORS = get_anchors(cfg.YOLO.ANCHORS_TINY, FLAGS.tiny, FLAGS.model)
XYSCALE = cfg.YOLO.XYSCALE_TINY if FLAGS.model == 'yolov4' else [1, 1]
else:
STRIDES = np.array(cfg.YOLO.STRIDES)
if FLAGS.model == 'yolov4':
ANCHORS = get_anchors(cfg.YOLO.ANCHORS, FLAGS.tiny)
ANCHORS = get_anchors(cfg.YOLO.ANCHORS, FLAGS.tiny, FLAGS.model)
elif FLAGS.model == 'yolov3':
ANCHORS = get_anchors(cfg.YOLO.ANCHORS_V3, FLAGS.tiny)
ANCHORS = get_anchors(cfg.YOLO.ANCHORS_V3, FLAGS.tiny, FLAGS.model)
XYSCALE = cfg.YOLO.XYSCALE if FLAGS.model == 'yolov4' else [1, 1, 1]
NUM_CLASS = len(read_class_names(cfg.YOLO.CLASSES))

return STRIDES, ANCHORS, NUM_CLASS, XYSCALE

def get_anchors(anchors_path, tiny=False):
def get_anchors(anchors_path, tiny=False, model='yolov4'):
anchors = np.array(anchors_path)
if tiny:
return anchors.reshape(2, 3, 2)
if model == 'yolov4-tiny-3l':
return anchors.reshape(3, 3, 2)
else:
return anchors.reshape(2, 3, 2)
else:
return anchors.reshape(3, 3, 2)

Expand Down
30 changes: 30 additions & 0 deletions core/yolov4.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def YOLO(input_layer, NUM_CLASS, model='yolov4', is_tiny=False):
if is_tiny:
if model == 'yolov4':
return YOLOv4_tiny(input_layer, NUM_CLASS)
elif model == 'yolov4-tiny-3l':
print("#"*100)
print("\n"*5)
print("loading tiny-3l")
print("\n"*5)
print("#"*100)
return YOLOv4_tiny_3l(input_layer, NUM_CLASS)
elif model == 'yolov3':
return YOLOv3_tiny(input_layer, NUM_CLASS)
else:
Expand Down Expand Up @@ -143,6 +150,29 @@ def YOLOv4_tiny(input_layer, NUM_CLASS):

return [conv_mbbox, conv_lbbox]

def YOLOv4_tiny_3l(input_layer, NUM_CLASS):
route_1, route_2, conv = backbone.cspdarknet53_tiny_3l(input_layer)

conv = common.convolutional(conv, (1, 1, 512, 256))

conv_lobj_branch = common.convolutional(conv, (3, 3, 256, 512))
conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 512, 3 * (NUM_CLASS + 5)), activate=False, bn=False)

conv = common.convolutional(conv, (1, 1, 256, 128))
conv = common.upsample(conv)
conv = tf.concat([conv, route_1], axis=-1)

conv = common.convolutional(conv, (3, 3, 384, 256))
conv_mbbox = common.convolutional(conv, (1, 1, 256, 3 * (NUM_CLASS + 5)), activate=False, bn=False)

conv = common.convolutional(conv, (1, 1, 256, 64))
conv = common.upsample(conv)
conv = tf.concat([conv, route_2], axis=-1)

conv_sobj_branch = common.convolutional(conv, (3, 3, 192, 128))
conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 128, 3 * (NUM_CLASS + 5)), activate=False, bn=False)
return [conv_sbbox, conv_mbbox, conv_lbbox]

def YOLOv3_tiny(input_layer, NUM_CLASS):
route_1, conv = backbone.darknet53_tiny(input_layer)

Expand Down
1 change: 1 addition & 0 deletions data/classes/obj.names
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
license_plate
26 changes: 19 additions & 7 deletions save_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ def save_tf():
bbox_tensors = []
prob_tensors = []
if FLAGS.tiny:
for i, fm in enumerate(feature_maps):
if i == 0:
output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
else:
output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
bbox_tensors.append(output_tensors[0])
prob_tensors.append(output_tensors[1])
if FLAGS.model == 'yolov4-tiny-3l':
for i, fm in enumerate(feature_maps):
# import pdb; pdb.set_trace()
if i == 0:
output_tensors = decode(fm, FLAGS.input_size // 8, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
elif i == 1:
output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
else:
output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
bbox_tensors.append(output_tensors[0])
prob_tensors.append(output_tensors[1])
else:
for i, fm in enumerate(feature_maps):
if i == 0:
output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
else:
output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
bbox_tensors.append(output_tensors[0])
prob_tensors.append(output_tensors[1])
else:
for i, fm in enumerate(feature_maps):
if i == 0:
Expand Down