-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_gen.py
101 lines (62 loc) · 2.72 KB
/
batch_gen.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
import numpy as np
import os
import xml.etree.ElementTree as et
import cv2
import sklearn
class Batch_Generator:
def __init__(self):
self.labels = []
self.x = []
self.image_ids = []
def parse_xml(self, annot_dirs, img_dirs, image_sets, classes):
with open(image_sets) as f:
image_id = [line.strip() for line in f]
self.image_ids += image_id
for image_id in self.image_ids:
filename = image_id + '.jpg'
img = cv2.imread(os.path.join(img_dirs, filename))
height, width, _ = np.array(img).shape
self.x.append(cv2.resize(img, (300, 300)))
tree = et.parse(os.path.join(annot_dirs, image_id + '.xml'))
objects = tree.findall('object')
boxes = []
for obj in objects:
box = []
class_name = obj.findtext('name')
if (not class_name in classes) : continue
class_id = classes.index(class_name)
xmin = (float (obj.find('bndbox').findtext('xmin')) * 300) // width
ymin = (float (obj.find('bndbox').findtext('ymin')) * 300) // height
xmax = (float (obj.find('bndbox').findtext('xmax')) * 300) // width
ymax = (float (obj.find('bndbox').findtext('ymax')) * 300) // height
items = {
'class_id' : class_id,
'xmin' : xmin,
'ymin' : ymin,
'xmax' : xmax,
'ymax' : ymax
}
for item in items.keys():
box.append(items[item])
boxes.append(box)
self.labels.append(boxes)
def total_samples(self):
return len(self.x)
def generate(self, batch_size = 32, ssd_box_encoder = None, train = False):
current = 0
while True :
batch_x, batch_y = [], []
if (current + batch_size >= len(self.x)):
current = 0
self.x, self.labels, self.image_ids = sklearn.utils.shuffle(self.x, self.labels, self.image_ids)
batch_y = np.array(self.labels[current : current + batch_size])
batch_x = self.x[current : current + batch_size]
batch_x = np.array(batch_x)
batch_x = batch_x / 255.0
current += batch_size
ret = []
ret.append(batch_x)
if train:
batch_y_true = ssd_box_encoder.encode_y(batch_y)
ret.append(batch_y_true)
yield ret