-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
183 lines (147 loc) · 8.07 KB
/
dataset.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
# Author:LiPu
import cv2
import numpy as np
import tensorflow as tf
import utils
class Dataset(object):
def __init__(self, opt, datatype="train"):
self.label_path = utils.read_label_path(opt.data, datatype)
self.image_path = utils.read_image_path(opt.data, datatype)
self.classes = utils.read_class_names(opt.data)
self.input_size = opt.img_size
self.strides = np.array([8, 16, 32])
self.output_sizes = self.input_size // self.strides
self.batch_size = opt.batch_size
self.num_classes = len(self.classes)
self.max_bbox_per_scale = 150
# 读取anchors
self.anchors = np.array(utils.get_anchors(opt.data))
self.anchor_per_scale = 3
self.num_samples = len(self.label_path)
# np.ceil起到向上取整的作用
self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
self.batch_count = 0
# 加载label
def load_label(self, label_path):
f = open(label_path, 'r')
txt = f.readlines()
f.close()
label = np.array([list(map(float, line.strip("\n").split(' '))) for line in txt])
return label
# 加载图片
def load_image(self, image_path):
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return img
def bbox_iou(self, boxes1, boxes2):
boxes1 = np.array(boxes1)
boxes2 = np.array(boxes2)
boxes1_area = boxes1[..., 2] * boxes1[..., 3]
boxes2_area = boxes2[..., 2] * boxes2[..., 3]
boxes1 = np.concatenate([boxes1[..., :2] - boxes1[..., 2:] * 0.5,
boxes1[..., :2] + boxes1[..., 2:] * 0.5], axis=-1)
boxes2 = np.concatenate([boxes2[..., :2] - boxes2[..., 2:] * 0.5,
boxes2[..., :2] + boxes2[..., 2:] * 0.5], axis=-1)
left_up = np.maximum(boxes1[..., :2], boxes2[..., :2])
right_down = np.minimum(boxes1[..., 2:], boxes2[..., 2:])
inter_section = np.maximum(right_down - left_up, 0.0)
inter_area = inter_section[..., 0] * inter_section[..., 1]
union_area = boxes1_area + boxes2_area - inter_area
return inter_area / union_area
def preprocess_true_boxes(self, bboxes):
label = [np.zeros((self.output_sizes[i], self.output_sizes[i], self.anchor_per_scale,
5 + self.num_classes)) for i in range(3)]
bboxes_xywh = [np.zeros((self.max_bbox_per_scale, 4)) for _ in range(3)]
bbox_count = np.zeros((3,))
for bbox in bboxes:
bbox_coor = bbox[1:]
bbox_class_ind = bbox[0]
# 获得独热码
onehot = np.zeros(self.num_classes, dtype=np.float)
onehot[int(bbox_class_ind)] = 1.0
# 向独热码添加均匀分布项
uniform_distribution = np.full(self.num_classes, 1.0 / self.num_classes)
deta = 0.01
smooth_onehot = onehot * (1 - deta) + deta * uniform_distribution
bbox_xywh = np.array(bbox_coor)
# 将bbox分成三个取样空间
bbox_xywh_scaled = 1.0 * bbox_xywh[np.newaxis, :] / self.strides[:, np.newaxis]
iou = []
exist_positive = False
for i in range(3):
anchors_xywh = np.zeros((self.anchor_per_scale, 4))
anchors_xywh[:, 0:2] = np.floor(bbox_xywh_scaled[i, 0:2]).astype(np.int32) + 0.5
anchors_xywh[:, 2:4] = self.anchors[i]
iou_scale = self.bbox_iou(bbox_xywh_scaled[i][np.newaxis, :], anchors_xywh)
iou.append(iou_scale)
iou_mask = iou_scale > 0.3
if np.any(iou_mask):
xind, yind = np.floor(bbox_xywh_scaled[i, 0:2]).astype(np.int32)
label[i][yind, xind, iou_mask, :] = 0
label[i][yind, xind, iou_mask, 0:4] = bbox_xywh
label[i][yind, xind, iou_mask, 4:5] = 1.0
label[i][yind, xind, iou_mask, 5:] = smooth_onehot
bbox_ind = int(bbox_count[i] % self.max_bbox_per_scale)
bboxes_xywh[i][bbox_ind, :4] = bbox_xywh
bbox_count[i] += 1
exist_positive = True
if not exist_positive:
best_anchor_ind = np.argmax(np.array(iou).reshape(-1), axis=-1)
best_detect = int(best_anchor_ind / self.anchor_per_scale)
best_anchor = int(best_anchor_ind % self.anchor_per_scale)
xind, yind = np.floor(bbox_xywh_scaled[best_detect, 0:2]).astype(np.int32)
label[best_detect][yind, xind, best_anchor, :] = 0
label[best_detect][yind, xind, best_anchor, 0:4] = bbox_xywh
label[best_detect][yind, xind, best_anchor, 4:5] = 1.0
label[best_detect][yind, xind, best_anchor, 5:] = smooth_onehot
bbox_ind = int(bbox_count[best_detect] % self.max_bbox_per_scale)
bboxes_xywh[best_detect][bbox_ind, :4] = bbox_xywh
bbox_count[best_detect] += 1
label_sbbox, label_mbbox, label_lbbox = label
sbboxes, mbboxes, lbboxes = bboxes_xywh
return label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes
def __iter__(self):
return self
def __next__(self):
with tf.device('/cpu:0'):
batch_image = np.zeros((self.batch_size, self.input_size, self.input_size, 3), dtype=np.float32)
batch_label_sbbox = np.zeros((self.batch_size, self.output_sizes[0], self.output_sizes[0],
self.anchor_per_scale, 5 + self.num_classes), dtype=np.float32)
batch_label_mbbox = np.zeros((self.batch_size, self.output_sizes[1], self.output_sizes[1],
self.anchor_per_scale, 5 + self.num_classes), dtype=np.float32)
batch_label_lbbox = np.zeros((self.batch_size, self.output_sizes[2], self.output_sizes[2],
self.anchor_per_scale, 5 + self.num_classes), dtype=np.float32)
batch_sbboxes = np.zeros((self.batch_size, self.max_bbox_per_scale, 4), dtype=np.float32)
batch_mbboxes = np.zeros((self.batch_size, self.max_bbox_per_scale, 4), dtype=np.float32)
batch_lbboxes = np.zeros((self.batch_size, self.max_bbox_per_scale, 4), dtype=np.float32)
num = 0
if self.batch_count < self.num_batchs:
while num < self.batch_size:
index = self.batch_count * self.batch_size + num
if index >= self.num_samples:
index -= self.num_samples
bboxes = self.load_label(self.label_path[index])
image = self.load_image(self.image_path[index])
image, bboxes = utils.image_preporcess(np.copy(image),
[self.input_size, self.input_size],
np.copy(bboxes))
label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes = self.preprocess_true_boxes(
bboxes)
batch_image[num, :, :, :] = image
batch_label_sbbox[num, :, :, :, :] = label_sbbox
batch_label_mbbox[num, :, :, :, :] = label_mbbox
batch_label_lbbox[num, :, :, :, :] = label_lbbox
batch_sbboxes[num, :, :] = sbboxes
batch_mbboxes[num, :, :] = mbboxes
batch_lbboxes[num, :, :] = lbboxes
num += 1
self.batch_count += 1
batch_smaller_target = batch_label_sbbox, batch_sbboxes
batch_medium_target = batch_label_mbbox, batch_mbboxes
batch_larger_target = batch_label_lbbox, batch_lbboxes
return batch_image, (batch_smaller_target, batch_medium_target, batch_larger_target)
else:
self.batch_count = 0
raise StopIteration
def __len__(self):
return self.num_batchs