-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_manager.py
203 lines (124 loc) · 4.53 KB
/
data_manager.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
import os
import numpy as np
from numpy.random import random
import cv2
import copy
import glob
import pickle
import IPython
class data_manager(object):
def __init__(self,classes,image_size,compute_features = None, compute_label = None):
#Batch Size for training
self.batch_size = 40
#Batch size for test, more samples to increase accuracy
self.val_batch_size = 400
self.classes = classes
self.num_class = len(self.classes)
self.image_size = image_size
self.class_to_ind = dict(zip(self.classes, range(len(self.classes))))
# To keep track of where in the data you are in the training data
self.cursor = 0
# Same as above but for validation data
self.t_cursor = 0
self.epoch = 1
self.recent_batch = []
if compute_features == None:
self.compute_feature = self.compute_features_baseline
else:
self.compute_feature = compute_features
if compute_label == None:
self.compute_label = self.compute_label_baseline
else:
self.compute_label = compute_label
self.load_train_set()
self.load_validation_set()
def get_train_batch(self):
'''
Compute a training batch for the neural network
The batch size should be size 40
'''
images = []
labels = []
for i in range(self.batch_size):
images.append(self.train_data[self.cursor]['features'])
labels.append(self.train_data[self.cursor]['label'])
self.cursor += 1
if self.cursor == len(self.train_data):
np.random.shuffle(self.train_data)
self.cursor = 0
images = np.stack(images)
labels = np.stack(labels)
return images, labels
def get_empty_state(self):
images = np.zeros((self.batch_size, self.image_size,self.image_size,3))
return images
def get_empty_label(self):
labels = np.zeros((self.batch_size, self.num_class))
return labels
def get_empty_state_val(self):
images = np.zeros((self.val_batch_size, self.image_size,self.image_size,3))
return images
def get_empty_label_val(self):
labels = np.zeros((self.val_batch_size, self.num_class))
return labels
def get_validation_batch(self):
'''
Compute a training batch for the neural network
The batch size should be size 400
'''
images = []
labels = []
for i in range(self.val_batch_size):
images.append(self.val_data[self.t_cursor]['features'])
labels.append(self.val_data[self.t_cursor]['label'])
self.t_cursor += 1
if self.t_cursor == len(self.val_data):
np.random.shuffle(self.val_data)
self.t_cursor = 0
images = np.stack(images)
labels = np.stack(labels)
return images, labels
def compute_features_baseline(self, image):
'''
computes the featurized on the images. In this case this corresponds
to rescaling and standardizing.
'''
image = cv2.resize(image, (self.image_size, self.image_size))
image = (image / 255.0) * 2.0 - 1.0
return image
def compute_label_baseline(self,label):
'''
Compute one-hot labels given the class size
'''
one_hot = np.zeros(self.num_class)
idx = self.classes.index(label)
one_hot[idx] = 1.0
return one_hot
def load_set(self,set_name):
'''
Given a string which is either 'val' or 'train', the function should load all the
data into an
'''
data = []
data_paths = glob.glob(set_name+'/*.png')
count = 0
for datum_path in data_paths:
label_idx = datum_path.find('_')
label = datum_path[len(set_name)+1:label_idx]
if self.classes.count(label) > 0:
img = cv2.imread(datum_path)
label_vec = self.compute_label(label)
features = self.compute_feature(img)
data.append({'c_img': img, 'label': label_vec, 'features': features})
np.random.shuffle(data)
return data
def load_train_set(self):
'''
Loads the train set
'''
self.train_data = self.load_set('train')
def load_validation_set(self):
'''
Loads the validation set
'''
self.val_data = self.load_set('val')