-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_cloud_classifier.py
More file actions
265 lines (225 loc) · 8.54 KB
/
run_cloud_classifier.py
File metadata and controls
265 lines (225 loc) · 8.54 KB
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
import cloud_fcns as cloud
from cloud_fcns import writedot
import h5py
import sys
import numpy as np
import time
use_sample_storage = True #off by default - below import may turn on
from cloud_params import *
from nnet_toolkit import nnet
np.random.seed = randomseed1;
inputsize = 7*patchsize**2
offset = (patchsize-1)/2;
d = cloud.load_all_data(data_path)
load_cache = False
if(use_sample_storage):
#d is a tuple containing (A_list,MASK_list,MASKF_list)
print('creating h5py sample file: ' + cloud.get_sample_fname())
cache_file = h5py.File(cloud.get_sample_fname(),'a')
if('final' in cache_file):
load_cache = True
else:
#we may have a file that wasn't finished saving... need to truncate
cache_file.close()
cache_file = h5py.File(cloud.get_sample_fname(),'w')
#40 images to load @ 1000x1000
#cautiously allow a 3x margin of error
estimated_sample_size = int(len(d[0])*1000.0*1000.0*load_percentage*3/num_batches)
sample_list_file = [];
class_list_file = [];
for i in range(num_batches):
sample_list_file.append(cache_file.create_dataset('sample_list_' + str(i),(estimated_sample_size,inputsize),chunks=(chunk_size,inputsize),dtype=np.float32))
class_list_file.append(cache_file.create_dataset('class_list_' + str(i),(estimated_sample_size,3),chunks=(chunk_size,3),dtype=np.uint8))
sample_file_size = list(np.zeros(50,dtype=np.uint32));
if(load_cache):
print('h5py file found. loading...')
sample_list_file = [];
class_list_file = [];
for i in range(num_batches):
sample_list_file.append(cache_file['sample_list_' + str(i)])
class_list_file.append(cache_file['class_list_' + str(i)])
sample_mean = np.array(cache_file['sample_mean'])
sample_std = np.array(cache_file['sample_std'])
sample_list_test = np.array(cache_file['sample_list_test'])
class_list_test = np.array(cache_file['class_list_test'])
else:
sample_list = []
class_list = []
sample_list_test = []
class_list_test = []
for i in range(len(d[0])):
sys.stdout.write('\nsampling image: '+ str(i));
A = d[0][0]
MASK = d[1][0]
MASKF = d[2][0]
fname = d[3][0]
#remove the first element from the list and from memory
#to save memory as we go.
del d[0][0]
del d[1][0]
del d[2][0]
del d[3][0]
#test set comes from p31r43
is_test = False
if(fname.startswith('p31r43')):
sys.stdout.write('test set')
is_test = True
if(not is_test):
[sample_list,class_list] = cloud.sample_img(A,MASK,load_percentage)
else:
[sample_list_test_extend,class_list_test_extend] = cloud.sample_img(A,MASK,load_percentage)
sample_list_test.extend(sample_list_test_extend)
class_list_test.extend(class_list_test_extend)
del sample_list_test_extend
del class_list_test_extend
#save sample_list for this image h5py file
if use_sample_storage:
sample_list = sample_list
class_list = class_list
sample_size = len(sample_list);
#sample_list = np.array(sample_list)
#class_list = np.array(class_list)
#sample_size = sample_list.shape[0];
#append peices of sample_list to all batches
chunk_size = chunk_append_size
while sample_size > 0:
if(chunk_size > sample_size):
chunk_size = sample_size
b = np.random.randint(num_batches)
#if the below line crashes with "zero-length selections are not allowed"
#it means the estimated sample size was too small
sample_list_file[b][sample_file_size[b]:sample_file_size[b] + chunk_size,:] = sample_list[sample_size - chunk_size:sample_size]
class_list_file[b][sample_file_size[b]:sample_file_size[b] + chunk_size,:] = class_list[sample_size - chunk_size:sample_size]
sample_file_size[b] += chunk_size
sample_size -= chunk_size
del sample_list
del class_list
sample_list = []
class_list = []
del A
del MASKF
del MASK
if use_sample_storage:
sys.stdout.write('\nreshaping h5py')
for i, s in enumerate(sample_file_size):
sample_list_file[i].resize((sample_file_size[i],inputsize))
class_list_file[i].resize((sample_file_size[i],3))
writedot()
sys.stdout.write('\ncalculating mean and std from h5py dataset')
sample_mean = np.zeros(inputsize)
sample_std = np.zeros(inputsize)
for i in range(len(sample_file_size)):
sample_list = np.array(sample_list_file[i])
sample_mean += np.mean(sample_list,0)
sample_std += np.std(sample_list,0)
writedot()
sample_mean /= len(sample_file_size)
sample_std /= len(sample_file_size)
sys.stdout.write('\nnormalizing data')
for i in range(len(sample_file_size)):
sample_list_file[i][:] = sample_list_file[i][:] - sample_mean
sample_list_file[i][:] = sample_list_file[i][:]/sample_std
writedot()
sample_list_test = np.array(sample_list_test)
class_list_test = np.array(class_list_test)
sample_list_test = sample_list_test - sample_mean
sample_list_test = sample_list_test/sample_std
sys.stdout.write('\nshuffling data h5py (could be slow)')
#we need to shuffle both class and sample together in unison
#to do this we reset the random number generator state
for i in range(len(sample_file_size)):
sample_list = np.array(sample_list_file[i])
class_list = np.array(class_list_file[i])
rng_state = np.random.get_state()
np.random.shuffle(sample_list)
np.random.set_state(rng_state)
np.random.shuffle(class_list)
sample_list[:] = sample_list
class_list[:] = class_list
writedot()
print('\ncreating test set')
#print('training size: ' + str(sample_list_file.shape[0]))
#print('test size: ' + str(sample_list_test.shape[0]))
#print('validation size: ' + str(sample_list_validation.shape[0]))
#save more stuff to the h5py file
print('saving stuff to h5py')
cache_file['sample_mean'] = sample_mean
cache_file['sample_std'] = sample_std
cache_file['sample_list_test'] = sample_list_test
cache_file['class_list_test'] = class_list_test
cache_file['final'] = np.array([1])
print('getting initial batch')
sample_list = np.array(sample_list_file[0])
class_list = np.array(class_list_file[0])
else:
print('\nreshaping...')
sample_list = np.array(sample_list)
class_list = np.array(class_list)
print('calculating mean and std...')
sample_mean = np.mean(sample_list,0)
sample_std = np.std(sample_list,0)
print('normalizing data...')
sample_list = sample_list - sample_mean
sample_list = sample_list/sample_std
print('shuffling data...')
#we need to shuffle both class and sample together in unison
#to do this we reset the random number generator state
rng_state = np.random.get_state()
np.random.shuffle(sample_list)
np.random.set_state(rng_state)
np.random.shuffle(class_list)
print('total number of samples: ' + str(sample_list.shape[0]))
print('creating testing set')
sample_list_test = sample_list_validation
train_size = sample_list.shape[0]
print('training size: ' + str(sample_list.shape[0]))
print('validation size: ' + str(sample_list_validation.shape[0]))
import pdb; pdb.set_trace()
print('initializing network...')
layers = [nnet.layer(inputsize)]
for i in range(len(hidden_sizes)):
l = hidden_sizes[i]
a = hidden_activations[i]
layers.append(nnet.layer(l,a))
layers.append(nnet.layer(4,'squash'))
net = nnet.net(layers,step_size=step_size,dropout=dropout_percentage)
for i in range(training_epochs):
minibatch_count = int(train_size/minibatch_size)
#loop thru minibatches
training_correct = 0;
sys.stdout.write('.')
sys.stdout.flush();
rng_state = np.random.get_state()
np.random.shuffle(sample_list)
np.random.set_state(rng_state)
np.random.shuffle(class_list)
sys.stdout.write('.')
sys.stdout.flush();
for j in range(minibatch_count+1):
#grab a minibatch
net.input = np.transpose(sample_list[j*minibatch_size:(j+1)*minibatch_size])
classification = class_list[j*minibatch_size:(j+1)*minibatch_size]
#print(str(net.input.shape[0]) + ' ' + str(j*minibatch_size) + ' ' + str((j+1)*minibatch_size))
#feed forward
net.feed_forward();
#calculate error & back propagate
net.error = net.output - np.transpose(classification)
#calculate # of correct classifications
guess = np.argmax(net.output,0)
c = np.argmax(classification,1);
training_correct = training_correct + np.sum(c == guess)
net.back_propagate();
#update weights
net.update_weights();
#calculate test error
net.train = False
net.input = np.transpose(sample_list_test)
net.feed_forward()
guess = np.argmax(net.output,0)
c = np.argmax(class_list_test,1);
test_correct = np.sum(c == guess)
net.train = True
#calculate (and print) test error
print('epoch ' + str(i) + ': training rate : ' + str(float(training_correct)/float(train_size)) + \
' test rate: ' + str(float(test_correct)/float(sample_list_test.shape[0])))
import pdb; pdb.set_trace()