-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloss.py
92 lines (70 loc) · 3.72 KB
/
loss.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
from keras import backend as K
import tensorflow as tf
import numpy as np
LABELS = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
COLORS = [(43,206,72),(255,204,153),(128,128,128),(148,255,181),(143,124,0),(157,204,0),(194,0,136),(0,51,128),(255,164,5),(255,168,187),(66,102,0),(255,0,16),(94,241,242),(0,153,143),(224,255,102),(116,10,255),(153,0,0),(255,255,128),(255,255,0),(255,80,5)]
NORM_H, NORM_W = 416, 416
GRID_H, GRID_W = 13 , 13
BATCH_SIZE = 8
BOX = 5
CLASS = 20
THRESHOLD = 0.2
ANCHORS = '1.08,1.19, 3.42,4.41, 6.63,11.38, 9.42,5.11, 16.62,10.52'
ANCHORS = [float(ANCHORS.strip()) for ANCHORS in ANCHORS.split(',')]
SCALE_NOOB, SCALE_CONF, SCALE_COOR, SCALE_PROB = 0.5, 5.0, 5.0, 1.0
def yolo_loss(y_true, y_pred):
### Adjust prediction
# adjust x and y
pred_box_xy = tf.sigmoid(y_pred[:,:,:,:,:2])
# adjust w and h
pred_box_wh = tf.exp(y_pred[:,:,:,:,2:4]) * np.reshape(ANCHORS, [1,1,1,BOX,2])
pred_box_wh = tf.sqrt(pred_box_wh / np.reshape([float(GRID_W), float(GRID_H)], [1,1,1,1,2]))
# adjust confidence
pred_box_conf = tf.expand_dims(tf.sigmoid(y_pred[:, :, :, :, 4]), -1)
# adjust probability
pred_box_prob = tf.nn.softmax(y_pred[:, :, :, :, 5:])
y_pred = tf.concat([pred_box_xy, pred_box_wh, pred_box_conf, pred_box_prob], 4)
### Adjust ground truth
# adjust x and y
center_xy = .5*(y_true[:,:,:,:,0:2] + y_true[:,:,:,:,2:4])
center_xy = center_xy / np.reshape([(float(NORM_W)/GRID_W), (float(NORM_H)/GRID_H)], [1,1,1,1,2])
true_box_xy = center_xy - tf.floor(center_xy)
# adjust w and h
true_box_wh = (y_true[:,:,:,:,2:4] - y_true[:,:,:,:,0:2])
true_box_wh = tf.sqrt(true_box_wh / np.reshape([float(NORM_W), float(NORM_H)], [1,1,1,1,2]))
# adjust confidence
pred_tem_wh = tf.pow(pred_box_wh, 2) * np.reshape([GRID_W, GRID_H], [1,1,1,1,2])
pred_box_area = pred_tem_wh[:,:,:,:,0] * pred_tem_wh[:,:,:,:,1]
pred_box_ul = pred_box_xy - 0.5 * pred_tem_wh
pred_box_bd = pred_box_xy + 0.5 * pred_tem_wh
true_tem_wh = tf.pow(true_box_wh, 2) * np.reshape([GRID_W, GRID_H], [1,1,1,1,2])
true_box_area = true_tem_wh[:,:,:,:,0] * true_tem_wh[:,:,:,:,1]
true_box_ul = true_box_xy - 0.5 * true_tem_wh
true_box_bd = true_box_xy + 0.5 * true_tem_wh
intersect_ul = tf.maximum(pred_box_ul, true_box_ul)
intersect_br = tf.minimum(pred_box_bd, true_box_bd)
intersect_wh = intersect_br - intersect_ul
intersect_wh = tf.maximum(intersect_wh, 0.0)
intersect_area = intersect_wh[:,:,:,:,0] * intersect_wh[:,:,:,:,1]
iou = tf.truediv(intersect_area, true_box_area + pred_box_area - intersect_area)
best_box = tf.equal(iou, tf.reduce_max(iou, [3], True))
best_box = tf.to_float(best_box)
true_box_conf = tf.expand_dims(best_box * y_true[:,:,:,:,4], -1)
# adjust confidence
true_box_prob = y_true[:,:,:,:,5:]
y_true = tf.concat([true_box_xy, true_box_wh, true_box_conf, true_box_prob], 4)
#y_true = tf.Print(y_true, [true_box_wh], message='DEBUG', summarize=30000)
### Compute the weights
weight_coor = tf.concat(4 * [true_box_conf], 4)
weight_coor = SCALE_COOR * weight_coor
weight_conf = SCALE_NOOB * (1. - true_box_conf) + SCALE_CONF * true_box_conf
weight_prob = tf.concat(CLASS * [true_box_conf], 4)
weight_prob = SCALE_PROB * weight_prob
weight = tf.concat([weight_coor, weight_conf, weight_prob], 4)
### Finalize the loss
loss = tf.pow(y_pred - y_true, 2)
loss = loss * weight
loss = tf.reshape(loss, [-1, GRID_W*GRID_H*BOX*(4 + 1 + CLASS)])
loss = tf.reduce_sum(loss, 1)
loss = .5 * tf.reduce_mean(loss)
return loss