Skip to content

Commit 13e6adc

Browse files
committed
yolov4-tiny-3l issue resolved
1 parent 9f16748 commit 13e6adc

File tree

7 files changed

+118
-16
lines changed

7 files changed

+118
-16
lines changed

convert_tflite.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def save_tflite():
3636
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
3737
converter.allow_custom_ops = True
3838
elif FLAGS.quantize_mode == 'int8':
39+
converter.experimental_new_converter = False
3940
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
4041
converter.optimizations = [tf.lite.Optimize.DEFAULT]
4142
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]

core/backbone.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,49 @@ def cspdarknet53_tiny(input_data):
146146

147147
return route_1, input_data
148148

149+
def cspdarknet53_tiny_3l(input_data):
150+
input_data = common.convolutional(input_data, (3, 3, 3, 32), downsample=True)
151+
input_data = common.convolutional(input_data, (3, 3, 32, 64), downsample=True)
152+
input_data = common.convolutional(input_data, (3, 3, 64, 64))
153+
154+
route = input_data
155+
input_data = common.route_group(input_data, 2, 1)
156+
input_data = common.convolutional(input_data, (3, 3, 32, 32))
157+
route_1 = input_data
158+
input_data = common.convolutional(input_data, (3, 3, 32, 32))
159+
input_data = tf.concat([input_data, route_1], axis=-1)
160+
input_data = common.convolutional(input_data, (1, 1, 64, 64))
161+
input_data = tf.concat([route, input_data], axis=-1)
162+
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)
163+
164+
input_data = common.convolutional(input_data, (3, 3, 128, 128))
165+
route = input_data
166+
input_data = common.route_group(input_data, 2, 1)
167+
input_data = common.convolutional(input_data, (3, 3, 64, 64))
168+
route_1 = input_data
169+
input_data = common.convolutional(input_data, (3, 3, 64, 64))
170+
input_data = tf.concat([input_data, route_1], axis=-1)
171+
input_data = common.convolutional(input_data, (1, 1, 128, 128))
172+
route_2 = input_data
173+
input_data = tf.concat([route, input_data], axis=-1)
174+
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)
175+
176+
input_data = common.convolutional(input_data, (3, 3, 256, 256))
177+
route = input_data
178+
input_data = common.route_group(input_data, 2, 1)
179+
input_data = common.convolutional(input_data, (3, 3, 128, 128))
180+
route_1 = input_data
181+
input_data = common.convolutional(input_data, (3, 3, 128, 128))
182+
input_data = tf.concat([input_data, route_1], axis=-1)
183+
input_data = common.convolutional(input_data, (1, 1, 256, 256))
184+
route_1 = input_data
185+
input_data = tf.concat([route, input_data], axis=-1)
186+
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)
187+
188+
input_data = common.convolutional(input_data, (3, 3, 512, 512))
189+
190+
return route_1, route_2, input_data
191+
149192
def darknet53_tiny(input_data):
150193
input_data = common.convolutional(input_data, (3, 3, 3, 16))
151194
input_data = tf.keras.layers.MaxPool2D(2, 2, 'same')(input_data)

core/config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
# YOLO options
1212
__C.YOLO = edict()
1313

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

core/utils.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def load_weights(model, weights_file, model_name='yolov4', is_tiny=False):
2323
if model_name == 'yolov3':
2424
layer_size = 13
2525
output_pos = [9, 12]
26+
elif model_name == 'yolov4-tiny-3l':
27+
layer_size = 24
28+
output_pos = [17, 20, 23]
2629
else:
2730
layer_size = 21
2831
output_pos = [17, 20]
@@ -81,24 +84,32 @@ def read_class_names(class_file_name):
8184

8285
def load_config(FLAGS):
8386
if FLAGS.tiny:
84-
STRIDES = np.array(cfg.YOLO.STRIDES_TINY)
85-
ANCHORS = get_anchors(cfg.YOLO.ANCHORS_TINY, FLAGS.tiny)
86-
XYSCALE = cfg.YOLO.XYSCALE_TINY if FLAGS.model == 'yolov4' else [1, 1]
87+
if FLAGS.model == 'yolov4-tiny-3l':
88+
STRIDES = np.array(cfg.YOLO.STRIDES_TINY_3l)
89+
ANCHORS = get_anchors(cfg.YOLO.ANCHORS_TINY_3l, FLAGS.tiny, FLAGS.model)
90+
XYSCALE = cfg.YOLO.XYSCALE_TINY_3l
91+
else:
92+
STRIDES = np.array(cfg.YOLO.STRIDES_TINY)
93+
ANCHORS = get_anchors(cfg.YOLO.ANCHORS_TINY, FLAGS.tiny, FLAGS.model)
94+
XYSCALE = cfg.YOLO.XYSCALE_TINY if FLAGS.model == 'yolov4' else [1, 1]
8795
else:
8896
STRIDES = np.array(cfg.YOLO.STRIDES)
8997
if FLAGS.model == 'yolov4':
90-
ANCHORS = get_anchors(cfg.YOLO.ANCHORS, FLAGS.tiny)
98+
ANCHORS = get_anchors(cfg.YOLO.ANCHORS, FLAGS.tiny, FLAGS.model)
9199
elif FLAGS.model == 'yolov3':
92-
ANCHORS = get_anchors(cfg.YOLO.ANCHORS_V3, FLAGS.tiny)
100+
ANCHORS = get_anchors(cfg.YOLO.ANCHORS_V3, FLAGS.tiny, FLAGS.model)
93101
XYSCALE = cfg.YOLO.XYSCALE if FLAGS.model == 'yolov4' else [1, 1, 1]
94102
NUM_CLASS = len(read_class_names(cfg.YOLO.CLASSES))
95103

96104
return STRIDES, ANCHORS, NUM_CLASS, XYSCALE
97105

98-
def get_anchors(anchors_path, tiny=False):
106+
def get_anchors(anchors_path, tiny=False, model='yolov4'):
99107
anchors = np.array(anchors_path)
100108
if tiny:
101-
return anchors.reshape(2, 3, 2)
109+
if model == 'yolov4-tiny-3l':
110+
return anchors.reshape(3, 3, 2)
111+
else:
112+
return anchors.reshape(2, 3, 2)
102113
else:
103114
return anchors.reshape(3, 3, 2)
104115

core/yolov4.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ def YOLO(input_layer, NUM_CLASS, model='yolov4', is_tiny=False):
1818
if is_tiny:
1919
if model == 'yolov4':
2020
return YOLOv4_tiny(input_layer, NUM_CLASS)
21+
elif model == 'yolov4-tiny-3l':
22+
print("#"*100)
23+
print("\n"*5)
24+
print("loading tiny-3l")
25+
print("\n"*5)
26+
print("#"*100)
27+
return YOLOv4_tiny_3l(input_layer, NUM_CLASS)
2128
elif model == 'yolov3':
2229
return YOLOv3_tiny(input_layer, NUM_CLASS)
2330
else:
@@ -143,6 +150,29 @@ def YOLOv4_tiny(input_layer, NUM_CLASS):
143150

144151
return [conv_mbbox, conv_lbbox]
145152

153+
def YOLOv4_tiny_3l(input_layer, NUM_CLASS):
154+
route_1, route_2, conv = backbone.cspdarknet53_tiny_3l(input_layer)
155+
156+
conv = common.convolutional(conv, (1, 1, 512, 256))
157+
158+
conv_lobj_branch = common.convolutional(conv, (3, 3, 256, 512))
159+
conv_lbbox = common.convolutional(conv_lobj_branch, (1, 1, 512, 3 * (NUM_CLASS + 5)), activate=False, bn=False)
160+
161+
conv = common.convolutional(conv, (1, 1, 256, 128))
162+
conv = common.upsample(conv)
163+
conv = tf.concat([conv, route_1], axis=-1)
164+
165+
conv = common.convolutional(conv, (3, 3, 384, 256))
166+
conv_mbbox = common.convolutional(conv, (1, 1, 256, 3 * (NUM_CLASS + 5)), activate=False, bn=False)
167+
168+
conv = common.convolutional(conv, (1, 1, 256, 64))
169+
conv = common.upsample(conv)
170+
conv = tf.concat([conv, route_2], axis=-1)
171+
172+
conv_sobj_branch = common.convolutional(conv, (3, 3, 192, 128))
173+
conv_sbbox = common.convolutional(conv_sobj_branch, (1, 1, 128, 3 * (NUM_CLASS + 5)), activate=False, bn=False)
174+
return [conv_sbbox, conv_mbbox, conv_lbbox]
175+
146176
def YOLOv3_tiny(input_layer, NUM_CLASS):
147177
route_1, conv = backbone.darknet53_tiny(input_layer)
148178

data/classes/obj.names

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
license_plate

save_model.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,25 @@ def save_tf():
2121
bbox_tensors = []
2222
prob_tensors = []
2323
if FLAGS.tiny:
24-
for i, fm in enumerate(feature_maps):
25-
if i == 0:
26-
output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
27-
else:
28-
output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
29-
bbox_tensors.append(output_tensors[0])
30-
prob_tensors.append(output_tensors[1])
24+
if FLAGS.model == 'yolov4-tiny-3l':
25+
for i, fm in enumerate(feature_maps):
26+
# import pdb; pdb.set_trace()
27+
if i == 0:
28+
output_tensors = decode(fm, FLAGS.input_size // 8, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
29+
elif i == 1:
30+
output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
31+
else:
32+
output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
33+
bbox_tensors.append(output_tensors[0])
34+
prob_tensors.append(output_tensors[1])
35+
else:
36+
for i, fm in enumerate(feature_maps):
37+
if i == 0:
38+
output_tensors = decode(fm, FLAGS.input_size // 16, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
39+
else:
40+
output_tensors = decode(fm, FLAGS.input_size // 32, NUM_CLASS, STRIDES, ANCHORS, i, XYSCALE, FLAGS.framework)
41+
bbox_tensors.append(output_tensors[0])
42+
prob_tensors.append(output_tensors[1])
3143
else:
3244
for i, fm in enumerate(feature_maps):
3345
if i == 0:

0 commit comments

Comments
 (0)