-
Notifications
You must be signed in to change notification settings - Fork 0
/
yolov3.py
167 lines (122 loc) · 5.58 KB
/
yolov3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
Written by: Rahmad Sadli
Website : https://machinelearningspace.com
I finally made this program simple and readable
Hopefully, this program will help some beginners like me to understand better object detection.
If you want to redistribute it, just keep the author's name.
In oder to execute this program, you need to install TensorFlow 2.0 and opencv 4.x
For more details about how this program works. I explained well about it, just click the link below:
https://machinelearningspace.com/the-beginners-guide-to-implementing-yolo-v3-in-tensorflow-2-0-part-1/
Credit to:
Ayoosh Kathuria who shared his great work using pytorch, really appreaciated it.
https://blog.paperspace.com/how-to-implement-a-yolo-object-detector-in-pytorch/
"""
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import BatchNormalization, Conv2D, \
Input, ZeroPadding2D, LeakyReLU, UpSampling2D
physical_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
def parse_cfg(cfgfile):
with open(cfgfile, 'r') as file:
lines = [line.rstrip('\n') for line in file if line != '\n' and line[0] != '#']
holder = {}
blocks = []
for line in lines:
if line[0] == '[':
line = 'type=' + line[1:-1].rstrip()
if len(holder) != 0:
blocks.append(holder)
holder = {}
key, value = line.split("=")
holder[key.rstrip()] = value.lstrip()
blocks.append(holder)
return blocks
def YOLOv3Net(cfgfile, model_size, num_classes):
blocks = parse_cfg(cfgfile)
outputs = {}
output_filters = []
filters = []
out_pred = []
scale = 0
inputs = input_image = Input(shape=model_size)
inputs = inputs / 255.0
for i, block in enumerate(blocks[1:]):
# If it is a convolutional layer
if (block["type"] == "convolutional"):
activation = block["activation"]
filters = int(block["filters"])
kernel_size = int(block["size"])
strides = int(block["stride"])
if strides > 1:
inputs = ZeroPadding2D(((1, 0), (1, 0)))(inputs)
inputs = Conv2D(filters,
kernel_size,
strides=strides,
padding='valid' if strides > 1 else 'same',
name='conv_' + str(i),
use_bias=False if ("batch_normalize" in block) else True)(inputs)
if "batch_normalize" in block:
inputs = BatchNormalization(name='bnorm_' + str(i))(inputs)
if activation == "leaky":
inputs = LeakyReLU(alpha=0.1, name='leaky_' + str(i))(inputs)
elif (block["type"] == "upsample"):
stride = int(block["stride"])
inputs = UpSampling2D(stride)(inputs)
# If it is a route layer
elif (block["type"] == "route"):
block["layers"] = block["layers"].split(',')
start = int(block["layers"][0])
if len(block["layers"]) > 1:
end = int(block["layers"][1]) - i
filters = output_filters[i + start] + output_filters[end] # Index negatif :end - index
inputs = tf.concat([outputs[i + start], outputs[i + end]], axis=-1)
else:
filters = output_filters[i + start]
inputs = outputs[i + start]
elif block["type"] == "shortcut":
from_ = int(block["from"])
inputs = outputs[i - 1] + outputs[i + from_]
# Yolo detection layer
elif block["type"] == "yolo":
mask = block["mask"].split(",")
mask = [int(x) for x in mask]
anchors = block["anchors"].split(",")
anchors = [int(a) for a in anchors]
anchors = [(anchors[i], anchors[i + 1]) for i in range(0, len(anchors), 2)]
anchors = [anchors[i] for i in mask]
n_anchors = len(anchors)
out_shape = inputs.get_shape().as_list()
inputs = tf.reshape(inputs, [-1, n_anchors * out_shape[1] * out_shape[2], \
5 + num_classes])
box_centers = inputs[:, :, 0:2]
box_shapes = inputs[:, :, 2:4]
confidence = inputs[:, :, 4:5]
classes = inputs[:, :, 5:num_classes + 5]
box_centers = tf.sigmoid(box_centers)
confidence = tf.sigmoid(confidence)
classes = tf.sigmoid(classes)
anchors = tf.tile(anchors, [out_shape[1] * out_shape[2], 1])
box_shapes = tf.exp(box_shapes) * tf.cast(anchors, dtype=tf.float32)
x = tf.range(out_shape[1], dtype=tf.float32)
y = tf.range(out_shape[2], dtype=tf.float32)
cx, cy = tf.meshgrid(x, y)
cx = tf.reshape(cx, (-1, 1))
cy = tf.reshape(cy, (-1, 1))
cxy = tf.concat([cx, cy], axis=-1)
cxy = tf.tile(cxy, [1, n_anchors])
cxy = tf.reshape(cxy, [1, -1, 2])
strides = (input_image.shape[1] // out_shape[1], \
input_image.shape[2] // out_shape[2])
box_centers = (box_centers + cxy) * strides
prediction = tf.concat([box_centers, box_shapes, confidence, classes], axis=-1)
if scale:
out_pred = tf.concat([out_pred, prediction], axis=1)
else:
out_pred = prediction
scale = 1
outputs[i] = inputs
output_filters.append(filters)
model = Model(input_image, out_pred)
model.summary()
return model