-
Notifications
You must be signed in to change notification settings - Fork 2
/
Planing.py
191 lines (149 loc) · 6.76 KB
/
Planing.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
##############################################
# (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 pprint
from collections import Counter
import numpy as np
import pandas # noqa: F401 # pylint: disable=unused-import # Prevent tflearn importing dodgy version
import tflearn
from tflearn.layers.core import dropout, fully_connected, input_data
from tflearn.layers.estimator import regression
from tflearn.layers.merge_ops import merge
import DataPreparation as dp
class FFN():
"""Object for handling TFLearn DNN models with added support for saving / loading different network configurations"""
def __init__(self, name, networkConfig=None, para_num=24, LR=1e-3):
self.name = name
self.networkConfig = networkConfig
self.para_num = para_num
self.LR = LR
self.isLoaded = False
self._model = None
self._network = None
self.run_id = None
def __str__(self):
out = ('Model: ' + self.name + '\n'
+ 'Network type: ' + str(self.networkConfig) + '\n'
+ 'Number of inputs: ' + str(self.para_num))
return(out)
def Network1(self):
network = input_data(shape=[None, self.para_num], name='input')
network = fully_connected(network, 32, activation='leakyrelu')
network = fully_connected(network, 32, activation='leakyrelu')
network = dropout(network, 0.8)
network = fully_connected(network, 32, activation='leakyrelu')
network = dropout(network, 0.8)
network = fully_connected(network, 32, activation='leakyrelu')
network = dropout(network, 0.8)
softmax = fully_connected(network, 2, activation='softmax')
weights = input_data(shape=[None, 2], name='weight')
weighted_softmax = merge([softmax, weights], 'weighted')
self._network = regression(weighted_softmax, optimizer='Adam', learning_rate=self.LR,
loss='categorical_crossentropy', name='targets')
self.networkConfig = 'Network1'
@property
def network(self):
if self._network is not None:
return self._network
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/Planing')
return self._model
def Train(self, training_data, training_truth, validation_data, validation_truth, training_weights, validation_weights, n_epoch=1):
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_weights], training_truth, n_epoch=n_epoch,
validation_set=(
[validation_data, validation_weights], 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.para_num) + '\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.para_num) + '\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()
if len(settings) == 1:
self.networkConfig = settings[0]
elif len(settings) >= 2:
self.networkConfig = settings[0].strip()
self.para_num = int(settings[1].strip())
self.model.load(path)
else:
with open('Models/' + self.name + '.txt', 'r') as file:
settings = file.readlines()
if len(settings) == 1:
self.networkConfig = settings[0]
elif len(settings) >= 2:
self.networkConfig = settings[0].strip()
self.para_num = int(settings[1].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('Number of inputs: ' + str(self.para_num))
print('##############################################')
def Predict(self, X):
return(self.model.predict(X))
def Predict_label(self, X):
return(self.model.predict_label(X))
def apply_mask(self, Sreference):
if self.isLoaded is False:
raise AssertionError(
'Model is neither loaded nor trained, cannot make predictions')
inputs = dp.getinputsFFN(Sreference, input_type=self.para_num)
label = self.model.predict_label(inputs)
lmask = np.array(label)
lmask = lmask[:, 0].reshape(2400, 3000)
prob = self.model.predict(inputs)
pmask = np.array(prob)
pmask = pmask[:, 0].reshape(2400, 3000)
return lmask, pmask
if __name__ == '__main__':
# Pixel Loading
df = dp.PixelLoader('./SatelliteData/SLSTR/Pixels3')
df = df.dp.remove_nan()
data = df['confidence_an'].values
data = data & 4
df.dp.get_weights(data, 1000)
tdata, vdata, ttruth, vtruth, tweights = df.dp.get_ffn_training_data(22, weights=True)
counter = Counter(tweights)
pprint.pprint(counter.most_common(5))
print(np.mean(tweights))
print((np.min(tweights), np.max(tweights)))
print('################################################')
tweights = tweights.reshape(-1, 1)
tweights = np.concatenate((tweights, ttruth[:, 0].reshape(-1, 1)), axis=1)
vweights = np.ones((vdata.shape[0], 1))
vweights = np.concatenate((vweights, vtruth[:, 0].reshape(-1, 1)), axis=1)
model = FFN('Test', 'Network1', 22)
model.Train(tdata, ttruth, vdata, vtruth, tweights, vweights, 10)
model.Save()