-
Notifications
You must be signed in to change notification settings - Fork 16
/
model.py
385 lines (373 loc) · 16.6 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import os
import string
import numpy as np
from itertools import islice
import random
import csv
import tensorflow as tf
import tensorflow.contrib.slim as slim
from time import time
import json
import pandas as pd
'''
readcsv: Read feature tensors from csv data packet
args:
target: the directory that stores the csv files
fealen: the length of feature tensor, related to to discarded DCT coefficients
returns: (1) numpy array of feature tensors with shape: N x H x W x C
(2) numpy array of labels with shape: N x 1
'''
def readcsv_(target, fealen=32):
#read label
path = target + '/label.csv'
label = np.genfromtxt(path, delimiter=',')
#read feature
feature = []
for dirname, dirnames, filenames in os.walk(target):
for i in range(0, len(filenames)-1):
if i==0:
file = '/dc.csv'
path = target + file
featemp = np.genfromtxt(path, delimiter=',')
feature.append(featemp)
else:
file = '/ac'+str(i)+'.csv'
path = target + file
featemp = np.genfromtxt(path, delimiter=',')
feature.append(featemp)
return np.rollaxis(np.asarray(feature), 0, 3)[:,:,0:fealen], label
def readcsv(target, fealen=32):
#read label
path = target + '/label.csv'
label = np.genfromtxt(path, delimiter=',')
#read feature
feature = []
for dirname, dirnames, filenames in os.walk(target):
for i in range(0, len(filenames)-1):
if i==0:
file = '/dc.csv'
path = target + file
featemp = pd.read_csv(path, header=None).as_matrix()
feature.append(featemp)
else:
file = '/ac'+str(i)+'.csv'
path = target + file
featemp = pd.read_csv(path, header=None).as_matrix()
feature.append(featemp)
return np.rollaxis(np.asarray(feature), 0, 3)[:,:,0:fealen], label
'''
processlabel: adjust ground truth for biased learning
args:
label: numpy array contains labels
cato : number of classes in the task
delta1: bias for class 1
delta2: bias for class 2
return: softmax label with bias
'''
def processlabel(label, cato=2, delta1 = 0, delta2=0):
softmaxlabel=np.zeros(len(label)*cato, dtype=np.float32).reshape(len(label), cato)
for i in range(0, len(label)):
if int(label[i])==0:
softmaxlabel[i,0]=1-delta1
softmaxlabel[i,1]=delta1
if int(label[i])==1:
softmaxlabel[i,0]=delta2
softmaxlabel[i,1]=1-delta2
return softmaxlabel
'''
loss_to_bias: calculate the bias term for batch biased learning
args:
loss: the average loss of current batch with respect to the label without bias
threshold: start biased learning when loss is below the threshold
return: the bias value to calculate the gradient
'''
def loss_to_bias(loss, alpha, threshold=0.3):
if loss >= threshold:
bias = 0
else:
bias = 1.0/(1+np.exp(alpha*loss))
return bias
'''
forward: define the neural network architecute
args:
input: feature tensor batch with size B x H x W x C
is_training: whether the forward process is training, affect dropout layer
reuse: undetermined
scope: undetermined
return: prediction socre(s) of input batch
'''
def forward(input, is_training=True, reuse=False, scope='model', flip=False):
if flip == True:
input = tf.map_fn(lambda img: tf.image.random_flip_left_right(img), input)
input = tf.map_fn(lambda img: tf.image.random_flip_up_down(img), input)
with tf.variable_scope(scope, reuse=reuse):
with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu, stride=1, padding='SAME',
weights_initializer=tf.contrib.layers.xavier_initializer(uniform=False),
biases_initializer=tf.constant_initializer(0.0)):
net = slim.conv2d(input, 16, [3, 3], scope='conv1_1')
net = slim.conv2d(net, 16, [3, 3], scope='conv1_2')
net = slim.max_pool2d(net, [2, 2], stride=2, padding='SAME', scope='pool1')
net = slim.conv2d(net, 32, [3, 3], scope='conv2_1')
net = slim.conv2d(net, 32, [3, 3], scope='conv2_2')
net = slim.max_pool2d(net, [2, 2], stride=2, padding='SAME', scope='pool2')
net = slim.flatten(net)
w_init = tf.contrib.layers.xavier_initializer(uniform=False)
net = slim.fully_connected(net, 250, activation_fn=tf.nn.relu, scope='fc1')
net = slim.dropout(net, 0.5, is_training=is_training, scope='dropout')
predict = slim.fully_connected(net, 2, activation_fn=None, scope='fc2')
return predict
'''
data: a class to handle the training and testing data, implement minibatch fetch
args:
fea: feature tensor of whole data set
lab: labels of whole data set
ptr: a pointer for the current location of minibatch
maxlen: length of entire dataset
preload: in current version, to reduce the indexing overhead of SGD, we load all the data into memeory at initialization.
methods:
nextinstance(): returns a single instance and its label from the training set, used for SGD
nextbatch(): returns a batch of instances and their labels from the training set, used for MGD
args:
batch: minibatch number
channel: the channel length of feature tersor, lenth > channel will be discarded
delta1, delta2: see process_label
sgd_batch(): returns a batch of instances and their labels from the trainin set randomly, number of hs and nhs are equal.
args:
batch: minibatch number
channel: the channel length of feature tersor, lenth > channel will be discarded
delta1, delta2: see process_label
'''
class data:
def __init__(self, fea, lab, preload=False):
self.ptr_n=0
self.ptr_h=0
self.ptr=0
self.dat=fea
self.label=lab
with open(lab) as f:
self.maxlen=sum(1 for _ in f)
if preload:
print("loading data into the main memory...")
self.ft_buffer, self.label_buffer=readcsv(self.dat)
def nextinstance(self):
temp_fea=[]
label=None
idx=random.randint(0,self.maxlen)
for dirname, dirnames, filenames in os.walk(self.dat):
for i in range(0, len(filenames)-1):
if i==0:
file='/dc.csv'
path=self.dat+file
with open(path) as f:
r=csv.reader(f)
fea=[[int(s) for s in row] for j,row in enumerate(r) if j==idx]
temp_fea.append(np.asarray(fea))
else:
file='/ac'+str(i)+'.csv'
path=self.dat+file
with open(path) as f:
r=csv.reader(f)
fea=[[int(s) for s in row] for j,row in enumerate(r) if j==idx]
temp_fea.append(np.asarray(fea))
with open(self.label) as l:
temp_label=np.asarray(list(l)[idx]).astype(int)
if temp_label==0:
label=[1,0]
else:
label=[0,1]
return np.rollaxis(np.array(temp_fea),0,3),np.array([label])
def sgd(self, channel=None, delta1=0, delta2=0):
with open(self.label) as l:
labelist=np.asarray(list(l)).astype(int)
length=labelist.size
idx=random.randint(0, length-1)
temp_label=labelist[idx]
if temp_label==0:
label=[1,0]
else:
label=[0,1]
ft= self.ft_buffer[idx]
return ft, np.array(label)
def sgd_batch_2(self, batch, channel=None, delta1=0, delta2=0):
with open(self.label) as l:
labelist=np.asarray(list(l)).astype(int)
labexn = np.where(labelist==0)[0]
labexh = np.where(labelist==1)[0]
n_length = labexn.size
h_length = labexh.size
if not batch % 2 == 0:
print('ERROR:Batch size must be even')
print('Abort.')
quit()
else:
num = batch // 2
idxn = labexn[(np.random.rand(num)*n_length).astype(int)]
idxh = labexh[(np.random.rand(num)*h_length).astype(int)]
label = np.concatenate((np.zeros(num), np.ones(num)))
label = processlabel(label,2, 0,0 )
ft_batch = np.concatenate((self.ft_buffer[idxn], self.ft_buffer[idxh]))
ft_batch_nhs = self.ft_buffer[idxn]
label_nhs = np.zeros(num)
return ft_batch, label
def sgd_batch(self, batch, channel=None, delta1=0, delta2=0):
with open(self.label) as l:
labelist=np.asarray(list(l)).astype(int)
labexn = np.where(labelist==0)[0]
labexh = np.where(labelist==1)[0]
n_length = labexn.size
h_length = labexh.size
if not batch % 2 == 0:
print('ERROR:Batch size must be even')
print('Abort.')
quit()
else:
num = batch // 2
idxn = labexn[(np.random.rand(num)*n_length).astype(int)]
idxh = labexh[(np.random.rand(num)*h_length).astype(int)]
label = np.concatenate((np.zeros(num), np.ones(num)))
#label = processlabel(label,2, delta1, delta2)
ft_batch = np.concatenate((self.ft_buffer[idxn], self.ft_buffer[idxh]))
ft_batch_nhs = self.ft_buffer[idxn]
label_nhs = np.zeros(num)
return ft_batch, label, ft_batch_nhs, label_nhs
'''
nextbatch_beta: returns the balalced batch, used for training only
'''
def nextbatch_beta(self, batch, channel=None, delta1=0, delta2=0):
def update_ptr(ptr, batch, length):
if ptr+batch<length:
ptr+=batch
if ptr+batch>=length:
ptr=ptr+batch-length
return ptr
with open(self.label) as l:
labelist=np.asarray(list(l)).astype(int)
labexn = np.where(labelist==0)[0]
labexh = np.where(labelist==1)[0]
n_length = labexn.size
h_length = labexh.size
if not batch % 2 == 0:
print('ERROR:Batch size must be even')
print('Abort.')
quit()
else:
num = batch//2
if num>=n_length or num>=h_length:
print('ERROR:Batch size exceeds data size')
print('Abort.')
quit()
else:
if self.ptr_n+num <n_length:
idxn = labexn[self.ptr_n:self.ptr_n+num]
elif self.ptr_n+num >=n_length:
idxn = np.concatenate((labexn[self.ptr_n:n_length], labexn[0:self.ptr_n+num-n_length]))
self.ptr_n = update_ptr(self.ptr_n, num, n_length)
if self.ptr_h+num <h_length:
idxh = labexh[self.ptr_h:self.ptr_h+num]
elif self.ptr_h+num >=h_length:
idxh = np.concatenate((labexh[self.ptr_h:h_length], labexh[0:self.ptr_h+num-h_length]))
self.ptr_h = update_ptr(self.ptr_h, num, h_length)
#print self.ptr_n, self.ptr_h
label = np.concatenate((np.zeros(num), np.ones(num)))
#label = processlabel(label,2, delta1, delta2)
ft_batch = np.concatenate((self.ft_buffer[idxn], self.ft_buffer[idxh]))
ft_batch_nhs = self.ft_buffer[idxn]
label_nhs = np.zeros(num)
return ft_batch, label, ft_batch_nhs, label_nhs
'''
nextbatch_without_balance: returns the normal batch. Suggest to use for training and validation
'''
def nextbatch_without_balance_alpha(self, batch, channel=None, delta1=0, delta2=0):
def update_ptr(ptr, batch, length):
if ptr+batch<length:
ptr+=batch
if ptr+batch>=length:
ptr=ptr+batch-length
return ptr
if self.ptr + batch < self.maxlen:
label = self.label_buffer[self.ptr:self.ptr+batch]
ft_batch = self.ft_buffer[self.ptr:self.ptr+batch]
else:
label = np.concatenate((self.label_buffer[self.ptr:self.maxlen], self.label_buffer[0:self.ptr+batch-self.maxlen]))
ft_batch = np.concatenate((self.ft_buffer[self.ptr:self.maxlen], self.ft_buffer[0:self.ptr+batch-self.maxlen]))
self.ptr = update_ptr(self.ptr, batch, self.maxlen)
return ft_batch, label
def nextbatch(self, batch, channel=None, delta1=0, delta2=0):
#print('recommed to use nextbatch_beta() instead')
databat=None
temp_fea=[]
label=None
if batch>self.maxlen:
print('ERROR:Batch size exceeds data size')
print('Abort.')
quit()
if self.ptr+batch < self.maxlen:
#processing labels
with open(self.label) as l:
temp_label=np.asarray(list(l)[self.ptr:self.ptr+batch])
label=processlabel(temp_label, 2, delta1, delta2)
for dirname, dirnames, filenames in os.walk(self.dat):
for i in range(0, len(filenames)-1):
if i==0:
file='/dc.csv'
path=self.dat+file
with open(path) as f:
temp_fea.append(np.genfromtxt(islice(f, self.ptr, self.ptr+batch),delimiter=','))
else:
file='/ac'+str(i)+'.csv'
path=self.dat+file
with open(path) as f:
temp_fea.append(np.genfromtxt(islice(f, self.ptr, self.ptr+batch),delimiter=','))
self.ptr=self.ptr+batch
elif (self.ptr+batch) >= self.maxlen:
#processing labels
with open(self.label) as l:
a=np.genfromtxt(islice(l, self.ptr, self.maxlen),delimiter=',')
with open(self.label) as l:
b=np.genfromtxt(islice(l, 0, self.ptr+batch-self.maxlen),delimiter=',')
#processing data
if self.ptr==self.maxlen-1 or self.ptr==self.maxlen:
temp_label=b
elif self.ptr+batch-self.maxlen==1 or self.ptr+batch-self.maxlen==0:
temp_label=a
else:
temp_label=np.concatenate((a,b))
label=processlabel(temp_label,2, delta1, delta2)
#print label.shape
for dirname, dirnames, filenames in os.walk(self.dat):
for i in range(0, len(filenames)-1):
if i==0:
file='/dc.csv'
path=self.dat+file
with open(path) as f:
a=np.genfromtxt(islice(f, self.ptr, self.maxlen),delimiter=',')
with open(path) as f:
b=np.genfromtxt(islice(f, None, self.ptr+batch-self.maxlen),delimiter=',')
if self.ptr==self.maxlen-1 or self.ptr==self.maxlen:
temp_fea.append(b)
elif self.ptr+batch-self.maxlen==1 or self.ptr+batch-self.maxlen==0:
temp_fea.append(a)
else:
try:
temp_fea.append(np.concatenate((a,b)))
except:
print (a.shape, b.shape, self.ptr)
else:
file='/ac'+str(i)+'.csv'
path=self.dat+file
with open(path) as f:
a=np.genfromtxt(islice(f, self.ptr, self.maxlen),delimiter=',')
with open(path) as f:
b=np.genfromtxt(islice(f, 0, self.ptr+batch-self.maxlen),delimiter=',')
if self.ptr==self.maxlen-1 or self.ptr==self.maxlen:
temp_fea.append(b)
elif self.ptr+batch-self.maxlen==1 or self.ptr+batch-self.maxlen==0:
temp_fea.append(a)
else:
try:
temp_fea.append(np.concatenate((a,b)))
except:
print (a.shape, b.shape, self.ptr)
self.ptr=self.ptr+batch-self.maxlen
#print np.asarray(temp_fea).shape
return np.rollaxis(np.asarray(temp_fea), 0, 3)[:,:,0:channel], label