-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
225 lines (170 loc) · 6.49 KB
/
model.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# yOLO-MODEL
import cv2
import numpy as np
import matplotlib.image as mpimg
# Preprocessing
def crop_and_resize(image):
cropped = image#[300:650,500:,:] # skip for now
return cv2.resize(cropped, (448,448))
def normalize(image):
normalized = 2.0*image/255.0 - 1
return normalized
def preprocess(image):
cropped = crop_and_resize(image)
normalized = normalize(cropped)
# The model works on (channel, height, width) ordering of dimensions
transposed = np.transpose(normalized, (2,0,1))
return transposed
from typing import List
import keras
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.core import Flatten, Dense
# Pre trained weights require this ordering
keras.backend.set_image_data_format('channels_first')
def get_model():
model = Sequential()
# Layer 1
model.add(Convolution2D(16, (3, 3),input_shape=(3,448,448),padding='same',strides=(1,1)))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Layer 2
model.add(Convolution2D(32,(3,3) ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='valid'))
# Layer 3
model.add(Convolution2D(64,(3,3) ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='valid'))
# Layer 4
model.add(Convolution2D(128,(3,3) ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='valid'))
# Layer 5
model.add(Convolution2D(256,(3,3) ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='valid'))
# Layer 6
model.add(Convolution2D(512,(3,3) ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='valid'))
# Layer 7
model.add(Convolution2D(1024,(3,3) ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
# Layer 8
model.add(Convolution2D(1024,(3,3) ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
# Layer 9
model.add(Convolution2D(1024,(3,3) ,padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(Flatten())
# Layer 10
model.add(Dense(256))
# Layer 11
model.add(Dense(4096))
model.add(LeakyReLU(alpha=0.1))
# Layer 12
model.add(Dense(1470))
return model
def load_weights(model, yolo_weight_file):
data = np.fromfile(yolo_weight_file, np.float32)
data = data[4:]
index = 0
for layer in model.layers:
shape = [w.shape for w in layer.get_weights()]
if shape != []:
kshape, bshape = shape
bia = data[index:index + np.prod(bshape)].reshape(bshape)
index += np.prod(bshape)
ker = data[index:index + np.prod(kshape)].reshape(kshape)
index += np.prod(kshape)
layer.set_weights([ker, bia])
class Box:
def __init__(self):
self.x, self.y = float(), float()
self.w, self.h = float(), float()
self.c = float()
self.prob = float()
def overlap(x1, w1, x2, w2):
l1 = x1 - w1 / 2.
l2 = x2 - w2 / 2.
left = max(l1, l2)
r1 = x1 + w1 / 2.
r2 = x2 + w2 / 2.
right = min(r1, r2)
return right - left
def box_intersection(a: Box, b: Box) -> float:
"""Intersection area of the 2 boxes"""
w = overlap(a.x, a.w, b.x, b.w)
h = overlap(a.y, a.h, b.y, b.h)
if w < 0 or h < 0:
return 0
area = w * h
return area
def box_union(a: Box, b: Box) -> float:
"""Area under the union of the 2 boxes"""
i = box_intersection(a, b)
u = a.w * a.h + b.w * b.h - i
return u
def box_iou(a: Box, b: Box) -> float:
"""Intersection over union, which is ratio of intersection area to union area of the 2 boxes"""
return box_intersection(a, b) / box_union(a, b)
def model_output_to_boxes(yolo_output, threshold=0.2, sqrt=1.8, C=20, B=2, S=7) -> List[Box]:
"""yolo_output_to_car_boxes"""
# Position for class 'car' in the VOC dataset classes
car_class_number = 6
boxes = []
SS = S*S # number of grid cells
prob_size = SS*C # class probabilities
conf_size = SS*B # confidences for each grid cell
probabilities = yolo_output[0:prob_size]
confidence_scores = yolo_output[prob_size: (prob_size + conf_size)]
cords = yolo_output[(prob_size + conf_size):]
# Reshape the arrays so that its easier to loop over them
probabilities = probabilities.reshape((SS, C))
confs = confidence_scores.reshape((SS, B))
cords = cords.reshape((SS, B, 4))
for grid in range(SS):
for b in range(B):
bx = Box()
bx.c = confs[grid, b]
# bounding box xand y coordinates are offsets of a particular grid cell location,
# so they are also bounded between 0 and 1.
# convert them absolute locations relative to the image size
bx.x = (cords[grid, b, 0] + grid % S) / S
bx.y = (cords[grid, b, 1] + grid // S) / S
bx.w = cords[grid, b, 2] ** sqrt
bx.h = cords[grid, b, 3] ** sqrt
# multiply confidence scores with class probabilities to get class sepcific confidence scores
p = probabilities[grid, :] * bx.c
# Check if the confidence score for class 'car' is greater than the threshold
if p[car_class_number] >= threshold:
bx.prob = p[car_class_number]
boxes.append(bx)
# combine boxes that are overlap
# sort the boxes by confidence score, in the descending order
boxes.sort(key=lambda b: b.prob, reverse=True)
for i in range(len(boxes)):
boxi = boxes[i]
if boxi.prob == 0:
continue
for j in range(i + 1, len(boxes)):
boxj = boxes[j]
# If boxes have more than 40% overlap then retain the box with the highest confidence score
if box_iou(boxi, boxj) >= 0.4:
boxes[j].prob = 0
boxes = [b for b in boxes if b.prob > 0]
return boxes
# Usage:
"""
>>> model = get_model()
>>> load_weights(model, 'model.weights')
>>> img = mpimg.imread('testimg.jpg')
>>> pre_precessed = preprocess(img)
>>> batch = np.expand_dims(pre_precessed, axis=0)
>>> batch_output = model.predict(batch)
>>> boxes = model_output_to_boxes(batch_output[0], threshold=0.25)
"""
# Attribution:
"https://github.com/subodh-malgonde/vehicle-detection"