-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnebulae_nn.py
374 lines (308 loc) · 13.1 KB
/
nebulae_nn.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
# Copyright (c) 2022 Enrique Mondragon Estrada
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Nebulae-NN
from ast import arg
import numpy as np
import argparse
import os
import sys
from PIL import Image
import pandas as pd
import matplotlib.pyplot as plt
import csv
import re
from nn_math_utils import *
def read_data(data_path, labels_file):
'''
reads and preprocess the image dataset
'''
print('\nloading data...\n ')
i = 0
data_names =[]
if os.path.isdir(data_path):
images = sorted(os.listdir(data_path))
if labels_file != None:
images = pd.read_csv(labels_file).values[:,0]
for image in images:
image = image.lower()
if image.endswith(('.png', '.jpg', '.jpeg')) != True:
print ('\t WARNING: ignoring file ',image, ' format not supported')
else:
im=Image.open(data_path + image)
imarr = np.array(im)
print('\timage:\t', i+1, '\t', image, '\tsample shape is', imarr.shape)
data_names.append(image)
if imarr.ndim == 3:
sample = np.reshape(imarr, (imarr.shape[0]*imarr.shape[1]*imarr.shape[2],1))
if i==0:
firstshape = imarr.shape
dataset = sample
else:
if firstshape == imarr.shape:
dataset = np.append(dataset, sample, axis=1)
else:
print('\n\tError: shape of image', image, 'does not match\t\n')
i -= 1
sys.exit
i += 1
elif imarr.ndim == 2:
sample = np.reshape(imarr, (imarr.shape[0]*imarr.shape[1],1))
if i==0:
firstshape = imarr.shape
dataset = sample
else:
if firstshape == imarr.shape:
dataset = np.append(dataset, sample, axis=1)
else:
print('\n\tError: shape of image', image, 'mismatch\t\n')
i -= 1
sys.exit
i += 1
else:
print('\tError: Check dimensions of the data')
elif os.path.isfile(data_path):
if data_path.endswith(('.png', '.jpg', '.jpeg')) != True:
print ('\t WARNING: ignoring file ',data_path, ' format not supported')
else:
im = Image.open(data_path)
imarr = np.array(im)
print('\timage:\t', str(1), '\t', data_path, '\tsample shape is', imarr.shape)
data_names.append(data_path)
if imarr.ndim == 3:
sample = np.reshape(imarr, (imarr.shape[0]*imarr.shape[1]*imarr.shape[2],1))
else:
print('\n\tError: shape of image', data_path, 'does not match\t\n')
dataset = sample
print('\nSize of dataset: ', dataset.shape[1], '\nshape of dataset',dataset.shape, '\n')
dataset = dataset/255
return dataset, data_names
def read_labels(labels_file):
'''
Extracting label values
'''
labels = pd.read_csv(labels_file).values
Y = np.array(labels[:,1])
return Y
def define_layers_prop(dataset, layers_dim, activation):
'''
Create the layers of the networks
initialize the weights using Glorot initialization and bias with zeros
Define the activation function for earch layer
'''
dim_layers = []
dim_layers.append(dataset.shape[0])
for i in layers_dim:
dim_layers.append(i)
params = {}
activations = []
np.random.seed(10)
print('\n\n\n\tBuilding network of',len(layers_dim), 'layers\n')
print('\n\tInitializing weights for each layer...\n')
for l in range(1, len(dim_layers)):
glorot_min = -1 / np.sqrt(dim_layers[l-1]),
glorot_max = 1 / np.sqrt(dim_layers[l-1])
#params['W' + str(l)] = glorot_min + np.random.randn(dim_layers[l], dim_layers[l-1]) * (glorot_max - glorot_min) # langsam
#params['W' + str(l)] = np.random.randn(dim_layers[l], dim_layers[l-1]) * glorot_max * 0.5 # langsam
params['W' + str(l)] = np.random.randn(dim_layers[l], dim_layers[l-1]) * 0.1
params['b' + str(l)] = np.zeros((dim_layers[l], 1))
print('\tDimensions for layer', l)
print('\tweight matrix: ', params['W' + str(l)].shape)
print('\tbias vector: ', params['b' + str(l)].shape, '\n')
activations.append(activation)
activations[-1]='sigmoid'
print('\n\tactivations for each layer:\n')
print('\t',activations, '\n')
return dim_layers, params, activations
def forward_prop(X, params, dim_layers, activation):
'''
Computes forward propagation
'''
A = X
cache = {}
cache['A' + str(0)] = A
for l in range(1, len(dim_layers)):
A_prev = A
Z = linear_forward(A_prev,params['W' + str(l)], params['b' + str(l)])
cache['Z' + str(l)] = Z
if activation[l-1]=='sigmoid':
A = sigmoid(Z)
elif activation[l-1]=='relu':
A = relu(Z)
elif activation[l-1]=='tanh':
A = tanh(Z)
cache['A' + str(l)] = A
return A, cache
def cost(A,Y):
'''
Compute the cost of the objective function
'''
m = Y.shape[0]
C = np.squeeze(binary_cross_entropy(m,A,Y)).astype(np.float64)
return C
def backward_prop(A,Y, cache, params, activation):
'''
Computes the Jacobian's elements of the loss function
'''
grads = {}
L = round((len(cache)-1)/2)
dCdA = d_binary_cross_entropy(cache['A'+str(L)],Y)
dA_da = np.ones_like(dCdA)
for l in reversed(range(1,L+1)):
if activation[l-1]=='sigmoid':
dAdZ = d_sigmoid(cache['Z'+str(l)])
elif activation[l-1]=='relu':
dAdZ = d_relu(cache['Z'+str(l)],dA_da)
elif activation[l-1]=='tanh':
dAdZ = d_tanh(cache['Z'+str(l)])
dCdZ = dCdA * dA_da * dAdZ
grads["dA" + str(l-1)], grads["dW" + str(l)], grads["db" + str(l)] = linear_backward(dCdZ,cache['A'+str(l-1)], params['W' + str(l)], params['b' + str(l)])
dA_da = grads["dA" + str(l-1)]
return grads
def update_params(params, grads, alpha):
'''
update parameters
'''
L = len(params) // 2
for l in range(1,L+1):
params["W" + str(l)] = params["W" + str(l)] - alpha * grads["dW" + str(l)]
params["b" + str(l)] = params["b" + str(l)] - alpha * grads["db" + str(l)]
return params
class Nebulae_NN:
def __init__(self, X, params, dim_layers, activations):
self.X = X
self.params = params
self.dim_layers = dim_layers
self.activations = activations
def train(self, Y, alpha, epochs):
'''
perform forward and backward propagation
'''
history = []
best_cost = 1000 # arbitrary large
best_epoch = 0
model_best = self.params
for i in range(epochs):
A, cache = forward_prop(self.X, self.params, self.dim_layers, self.activations)
C = cost(A, Y)
history.append(C)
grads = backward_prop(A, Y, cache, self.params, self.activations)
self.params = update_params(self.params, grads, alpha)
if C < best_cost:
model_best = self.params
best_cost = C
best_epoch = i
print("epoch : ", i, "\tcost:", C, '\t - Current best model')
else:
print("epoch : ", i, "\tcost:", C)
print('\nBest model summary:\n Epoch: ',best_epoch,'Cost: ',best_cost)
return model_best, history
def predict(self):
'''
makes a prediction for the input data with the trained model
'''
return forward_prop(self.X, self.params, self.dim_layers, self.activations)
def save_model(model, epochs, history, alpha):
'''
Saves model to a npy file
and plots the training
'''
dir = r"./models"
if not os.path.exists(dir):
print("\ncreating output directory...")
os.mkdir(dir)
model_path = os.path.join(dir, 'model' + '.npy')
history_path = os.path.join(dir, 'history' + '.csv')
print("\nWriting model...")
np.save(model_path,model)
np.savetxt(history_path, history)
print("model saved!")
plt.style.use('ggplot')
plt.plot(range(0,epochs), history)
plt.xlabel('Epochs')
plt.ylabel('Cost')
plt.title('Nebulae-NN: Training\n'+'\u03B1 = '+str(alpha))
plt.grid(True)
plot_path = os.path.join(dir, 'training' + '.png')
plt.savefig(plot_path)
plt.show()
def load_model(model_path):
'''
Load model from a npy file
'''
trained_model = np.load(model_path,allow_pickle=True)
L = int(len(trained_model[()].keys()) / 2)
params = {}
layers_dim = []
for l in range(1,L+1):
params['W' + str(l)] = trained_model[()]['W'+str(l)]
params['b' + str(l)] = trained_model[()]['b'+str(l)]
layers_dim.append(trained_model[()]['W'+str(l)].shape[0])
return params, layers_dim
def retrieve_model(dataset, layers_dim, activation, params):
'''
Retrieve pretrained model
'''
dim_layers = []
dim_layers.append(dataset.shape[0])
for i in layers_dim:
dim_layers.append(i)
activations = []
np.random.seed(10)
print('\n\n\n\tRetrieving model of',len(layers_dim), 'layers\n')
for l in range(1, len(dim_layers)):
print('\tDimensions for layer', l)
print('\tweight matrix: ', params['W' + str(l)].shape)
print('\tbias vector: ', params['b' + str(l)].shape, '\n')
activations.append(activation)
activations[-1]='sigmoid'
print('\n\tactivations for each layer:\n')
print('\t',activations, '\n')
return dim_layers, activations
def main():
parser = argparse.ArgumentParser(description=' ################ Nebulae-NN ################', usage='%(prog)s')
parser.add_argument('-in', '--input', type=str, required=True, help='dataset path', dest='data_path')
parser.add_argument('-lb', '--labels', type=str, required=False, help='labels csv file', dest='labels_file')
parser.add_argument('-dim', '--dim_layers', action='store', nargs='+', default=[16,8,1], type=int, help='dim of layers separated with spaces', dest='dim_layers')
parser.add_argument('-act', '--activation', type=str, choices=['sigmoid', 'relu', 'tanh'], default='relu', dest='activation', help='select activation function for inner layers [sigmoid, relu, tanh]')
parser.add_argument('-a', '--alpha', type=float, action='store', default=0.001, dest='alpha', help='value at which the parameters will be updated (learning rate)')
parser.add_argument('-e', '--epochs', type=int, action='store', default=2500, dest='epochs', help='number of complete forward - backward propagation cycles')
parser.add_argument('-m', '--model', type=str, required=False, help='model path for making predicitions with', dest='model_path')
args = parser.parse_args()
print('\n\t',parser.description)
dataset, data_names = read_data(args.data_path, args.labels_file)
X = dataset # split in future
if args.model_path:
# load model and predict
params, layers_dim = load_model(args.model_path)
assert len(args.activation)-1==len(layers_dim), 'Check that the number of activations mathches the number of hidden layers'
dim_layers, activations = retrieve_model(dataset, args.dim_layers, args.activation, params)
model = Nebulae_NN(X, params, dim_layers, activations)
A,_ = model.predict()
for image in range(len(data_names)):
print('Image: ', data_names[image], '\tPrediction: ',A[0,image])
else:
# train
Y = read_labels(args.labels_file)
print(Y)
assert len(Y)==dataset.shape[1], 'Number of samples does not match with number of labels'
dim_layers, params, activations = define_layers_prop(dataset, args.dim_layers, args.activation)
print(params.keys(),'\n')
model = Nebulae_NN(X, params, dim_layers, activations)
model, history = model.train(Y, args.alpha, args.epochs)
save_model(model, args.epochs, history, args.alpha)
if __name__ == "__main__":
main()