-
Notifications
You must be signed in to change notification settings - Fork 2
/
CNN.py
272 lines (216 loc) · 9.8 KB
/
CNN.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
##############################################
# (c) Copyright 2018-2019 Kenza Tazi and Thomas Zhu
# This software is distributed under the terms of the GNU General Public
# Licence version 3 (GPLv3)
##############################################
import datetime
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import dropout, fully_connected, input_data
from tflearn.layers.estimator import regression
import DataPreparation as dp
class CNN():
"""Object for handling TFLearn DNN models with added support for saving / loading different network configurations"""
def __init__(self, name, networkConfig=None, LR=1e-4, img_length=8, img_width=50):
self.name = name
self.networkConfig = networkConfig
self.LR = LR
self.isLoaded = False
self.img_length = img_length
self.img_width = img_width
self._model = None
self._network = None
self.run_id = None
def __str__(self):
out = ('Model: ' + self.name + '\n'
+ 'Network type: ' + str(self.networkConfig) + '\n'
+ 'Context shape: ' + str(self.img_length) + ',' + str(self.img_width))
return(out)
def NetworkA(self):
convnet = input_data(
shape=[None, self.img_length, self.img_width, 1], name='input')
# Layer 1
convnet = conv_2d(convnet, nb_filter=32,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
# Layer 2
convnet = conv_2d(convnet, nb_filter=64,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
# Layer 3
convnet = conv_2d(convnet, nb_filter=128,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
# Layer 4
convnet = conv_2d(convnet, nb_filter=64,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
# Layer 5
convnet = conv_2d(convnet, nb_filter=32,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
# Layer 6
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, keep_prob=0.8)
# Layer 7
softmax = fully_connected(convnet, 2, activation='softmax')
# gives the paramaters to optimise the network
self._network = regression(softmax, optimizer='Adam', learning_rate=self.LR,
loss='categorical_crossentropy', name='targets')
self.networkConfig = 'NetworkA'
def NetworkB(self):
convnet = input_data(
shape=[None, self.img_length, self.img_width, 1], name='input')
# Layer 1
convnet = conv_2d(convnet, nb_filter=32,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 2
convnet = conv_2d(convnet, nb_filter=64,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 3
convnet = conv_2d(convnet, nb_filter=128,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 4
convnet = conv_2d(convnet, nb_filter=64,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 5
convnet = conv_2d(convnet, nb_filter=32,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 6
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, keep_prob=0.8)
# Layer 7
softmax = fully_connected(convnet, 2, activation='softmax')
# gives the paramaters to optimise the network
self._network = regression(softmax, optimizer='Adam', learning_rate=self.LR,
loss='categorical_crossentropy', name='targets')
self.networkConfig = 'NetworkB'
def NetworkC(self):
convnet = input_data(shape=[None, self.img_length, self.img_width, 1], name='input')
# Layer 1
convnet = conv_2d(convnet, nb_filter=32,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 2
convnet = conv_2d(convnet, nb_filter=64,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 3
convnet = conv_2d(convnet, nb_filter=128,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 4
convnet = conv_2d(convnet, nb_filter=246,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 5
convnet = conv_2d(convnet, nb_filter=128,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 5
convnet = conv_2d(convnet, nb_filter=64,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 5
convnet = conv_2d(convnet, nb_filter=32,
filter_size=5, activation='relu')
convnet = max_pool_2d(convnet, kernel_size=5)
convnet = dropout(convnet, 0.8)
# Layer 6
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, keep_prob=0.8)
# Layer 7
softmax = fully_connected(convnet, 2, activation='softmax')
# gives the paramaters to optimise the network
self._network = regression(softmax, optimizer='Adam', learning_rate=self.LR,
loss='categorical_crossentropy', name='targets')
self.networkConfig = 'NetworkC'
@property
def network(self):
if self._network is not None:
return self._network
if self.networkConfig is None:
print('Using default network configuration, Network0')
self.NetworkA()
else:
# Use network function specified by networkConfig
networkFunc = getattr(self, self.networkConfig)
networkFunc()
return self._network
@property
def model(self):
if self._model:
return self._model
self._model = tflearn.DNN(
self.network, tensorboard_verbose=0, tensorboard_dir='./Temp/tflearn_logs')
return self._model
def Train(self, training_data, training_truth, validation_data, validation_truth, n_epoch=25):
timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
self.run_id = 'Models/' + str(self.name) + '_' + timestamp
self.model.fit(training_data, training_truth, n_epoch=n_epoch,
validation_set=(validation_data, validation_truth),
snapshot_step=10000, show_metric=True, run_id=self.run_id)
self.isLoaded = True
def Save(self, path=None):
if path:
self.model.save(path)
with open(path + '.txt', 'w') as file:
file.write(self.networkConfig + '\n')
file.write(str(self.img_length) + '\n')
file.write(str(self.img_width) + '\n')
file.write(str(self.run_id))
else:
self.model.save("Models/" + self.name)
with open("Models/" + self.name + '.txt', 'w') as file:
file.write(self.networkConfig + '\n')
file.write(str(self.img_length) + '\n')
file.write(str(self.img_width) + '\n')
file.write(str(self.run_id))
def Load(self, path=None, verbose=True):
if self.isLoaded:
raise AssertionError(
'Graph already loaded. Consider loading into new object.')
if path:
with open(path + '.txt', 'r') as file:
settings = file.readlines()
self.networkConfig = settings[0].strip()
self.img_length = int(settings[1].strip())
self.img_width = int(settings[2].strip())
self.model.load(path)
else:
with open('Models/' + self.name + '.txt', 'r') as file:
settings = file.readlines()
self.networkConfig = settings[0].strip()
self.img_length = int(settings[1].strip())
self.img_width = int(settings[2].strip())
self.model.load('Models/' + self.name)
self.isLoaded = True
if verbose:
print('##############################################')
print('Loading successful')
print('Model: ' + self.name)
print('Network type: ' + self.networkConfig)
print('Context shape: ' + str(self.img_length) + ',' + str(self.img_width))
print('##############################################')
if __name__ == '__main__':
df = dp.PixelLoader('./SatelliteData/SLSTR/Pixels5')
tdata, vdata, ttruth, vtruth = df.dp.get_cnn_training_data()
model = CNN('CNN_NetA', 'NetworkA')
model.Train(tdata, ttruth, vdata, vtruth)
model.Save()