-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassify_CNN_plot.py
More file actions
335 lines (260 loc) · 9.97 KB
/
classify_CNN_plot.py
File metadata and controls
335 lines (260 loc) · 9.97 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
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
#17.10.2024
#Machine Learning lab
#The goal is to identify craters in pictures of mars
#############################################
import json
import math
import numpy as np
from sklearn.metrics import f1_score
##################################################
import random
import matplotlib.pyplot as plt
random.seed(42)
X_train = np.load("Xtrain1.npy")
y_train = np.load("Ytrain1.npy")
X_test = np.load("Xtest1.npy")
####################################################################
### define the validation and training sets #######################
####################################################################
percent_val = 30
n_val = int(percent_val * len(X_train) / 100)
print("Use " + str(n_val) + " images for validation")
X_val = X_train[0:n_val]
y_val = y_train[0:n_val]
X_train = X_train[n_val:]
y_train = y_train[n_val:]
######## Counting labels ##########
train_total = y_train.shape[0]
n_craters = 0
n_plain = 0
for element in y_train:
if element== 0:
n_plain += 1
else: n_craters += 1
print()
print("DESCRIPTION OF DATASET")
print()
print("Number of training images = " + str(train_total))
print("Number of validation images = " +str(X_val.shape[0]) + str(" ( ") + str(percent_val) + " %)")
print("Number of craters = " + str(n_craters))
print("Number of plain= " + str(n_plain))
print("Percentage of craters is " + str(100*n_craters/train_total) + " %")
print("Percentage of plain is " + str(100*n_plain/train_total) + " %")
####### Function to rotate image ###########
def rotate_image_Sofia(image):
for i in range(int(len(image)/2)):
ii = len(image) -1 - i
aux = image[i]
image[i] = image[ii]
image[ii] = aux
return image
####### Function for brightness variations image ###########
def bright_image(image):
fator_brilho = 1.5
image_bright = np.clip(image * fator_brilho, 0, 255)#.astype(np.uint8)
#cv2.imshow('Imagem Original', image)
#cv2.imshow('Imagem com Mais Brilho', image_bright)
return image_bright
####### Function for transposing the image ###########
def transpose_image(image):
matrix = np.array(image).reshape(48, 48)
transpose = np.transpose(matrix)
return np.array(transpose).reshape(48**2)
####### Function for negative ############
def negative_image(image):
negative = [255-pixel for pixel in image ]
return np.array(negative)
######## Even number of craters and plains ##############
def equalize_crat_and_plain(X, y):
craters = list(X[y==1] )
plains = list(X[y==0] )
while(1):
if len(craters) == len(plains) :
break
#add random plain
aux = plains[random.randint(0, len(plains)-1)]
aux = rotate_image_Sofia(aux)
plains.append(aux)
if len(craters) == len(plains) :
break
#add random plain
aux = plains[random.randint(0, len(plains)-1)]
aux = bright_image(aux)
plains.append(aux)
#add labels to the data
for i in range(len(craters)):
craters[i] = np.concatenate( ([1] , craters[i]) )
plains[i] = np.concatenate( ([0] , plains[i] ) )
X_final = craters + plains
random.shuffle(X_final)
y_final = []
for i in range( len(X_final) ):
y_final.append( X_final[i][0] ) #put the label in y
X_final[i]=X_final[i][1:] #remove the label from X
X_final = np.array(X_final)
y_final = np.array(y_final)
return X_final, y_final
def add_transpose_images(X,y):
more_X = []
for image in X:
more_X.append(transpose_image(image))
more_X = np.array(more_X)
X_final = np.concatenate( (X,more_X) )
y_final = np.concatenate( (y,y) )
return X_final , y_final
def add_bright_images(X,y):
more_X = []
for image in X:
more_X.append(bright_image(image))
more_X = np.array(more_X)
X_final = np.concatenate( (X,more_X) )
y_final = np.concatenate( (y,y) )
return X_final , y_final
def add_negative_images(X,y):
more_X = []
for image in X:
more_X.append(bright_image(image))
more_X = np.array(more_X)
X_final = np.concatenate( (X,more_X) )
y_final = np.concatenate( (y,y) )
return X_final , y_final
print("Equalize number of images")
X_train, y_train = equalize_crat_and_plain(X_train,y_train)
X_train, y_train = add_transpose_images(X_train,y_train)
X_train, y_train = add_bright_images(X_train, y_train)
X_train , y_train = add_negative_images(X_train, y_train)
################ Recount ##################
train_total = y_train.shape[0]
n_craters = 0
n_plain = 0
for element in y_train:
if element== 0:
n_plain += 1
else: n_craters += 1
print("-------------------------------------------------------------")
print("DESCRIPTION OF BALANCED DATASET")
print()
print("Number of test images = " +str(X_test.shape[0]))
print("Number of validation images = " +str(X_val.shape[0]))
print("Number of training images = " + str(train_total))
print("Number of craters = " + str(n_craters))
print("Number of plain= " + str(n_plain))
print("Percentage of craters is " + str(100*n_craters/train_total) + " %")
print("Percentage of plain is " + str(100*n_plain/train_total) + " %")
########## Prepare shapes for CNN ##########
X_train = np.array(X_train).reshape(X_train.shape[0],48 , 48)
X_val = np.array(X_val ).reshape(X_val.shape[0] ,48 , 48)
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], X_train.shape[2], 1))
X_val = X_val.reshape((X_val.shape[0], X_val.shape[1], X_val.shape[2], 1))
################################################################################
############### CNN
################################################################################
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
from keras.models import load_model
from keras.optimizers import Adam
import tensorflow as tf
def f1_score_tf(y_true, y_pred):
y_true = tf.cast(y_true, tf.float32)
y_pred = tf.round(y_pred) # Round predictions to 0 or 1
tp = tf.reduce_sum(tf.round(tf.clip_by_value(y_true * y_pred, 0, 1))) # True positives
predicted_positives = tf.reduce_sum(tf.round(tf.clip_by_value(y_pred, 0, 1))) # Predicted positives
possible_positives = tf.reduce_sum(tf.round(tf.clip_by_value(y_true, 0, 1))) # Actual positives
precision = tp / (predicted_positives + tf.keras.backend.epsilon()) # Precision calculation
recall = tp / (possible_positives + tf.keras.backend.epsilon()) # Recall calculation
f1_val = 2 * ((precision * recall) / (precision + recall + tf.keras.backend.epsilon())) # F1 calculation
return f1_val
# Initialize the CNN model
model = Sequential()
f1_scores_train = []
f1_scores_val = []
accur_scores_train = []
accur_scores_val = []
# 1st Convolutional Layer + Pooling
model.add(Conv2D(32, (3, 3), input_shape=(48, 48, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# 2nd Convolutional Layer + Pooling
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# 3rd Convolutional Layer + Pooling
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Flatten the layers
model.add(Flatten())
# Fully connected layer (Dense layer)
model.add(Dense(units=128, activation='relu'))
model.add(Dropout(0.7)) # Dropout for regularization
# Output layer (Sigmoid for binary classification)
model.add(Dense(units=1, activation='sigmoid'))
learning_rate = 0.001
max_epoch = 34
our_batch_size = 64
# Compile the model
optimizer = Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
all_train_loss =[]
all_val_loss = []
for n_epochs in range(max_epoch):
# Train the model
history = model.fit(
X_train,
y_train,
#steps_per_epoch=len(X_train),
epochs=1, # Number of epochs (adjust as needed)
validation_data=(X_val , y_val),
#validation_steps=len(X_val)
batch_size = our_batch_size
)
# Evaluate the model on the training set
train_loss, train_acc = model.evaluate(X_train , y_train)
print(f"Train Accuracy: {train_acc}")
all_train_loss.append(train_loss)
accur_scores_train.append(train_acc)
# Make predictions
y_train_pred = model.predict(X_train)
# Convert predictions to binary values (0 or 1)
y_train_pred = (y_train_pred > 0.5).astype(int)
# Calculate F1 score
f1 = f1_score(y_train, y_train_pred)
print(f"Train F1 Score: {f1}")
f1_scores_train.append(f1)
print(f"Learning Rate:{learning_rate}")
# Evaluate the model on the validation set
val_loss, val_acc = model.evaluate(X_val , y_val)
print(f"Validation Accuracy: {val_acc}")
all_val_loss.append(val_loss)
accur_scores_val.append(val_acc)
# Make predictions
y_val_pred = model.predict(X_val)
# Convert predictions to binary values (0 or 1)
y_val_pred = (y_val_pred > 0.5).astype(int)
# Calculate F1 score
f1 = f1_score(y_val, y_val_pred)
print(f"Validation F1 Score: {f1}")
f1_scores_val.append(f1)
# Save the model
model.save('cnn_binary_classifier.h5')
## plotting error with gradient steps ##
plt.plot(accur_scores_train, color='blue',label = "Training")
plt.plot(accur_scores_val , color= 'red',label = "Validation")
plt.xlabel('Number of epochs')
plt.ylabel('Accuracy score')
plt.title('Accuracy score evolution with training epochs')
plt.legend()
plt.figure()
plt.plot(all_train_loss, color='blue',label = "Training")
plt.plot(all_val_loss , color= 'red',label = "Validation")
plt.xlabel('Number of epochs')
plt.ylabel('Loss score')
plt.title('Loss score evolution with training epochs')
plt.legend()
plt.figure()
plt.plot(f1_scores_train, color='blue',label = "Training")
plt.plot(f1_scores_val , color= 'red',label = "Validation")
plt.xlabel('Number of epochs')
plt.ylabel('F1 score')
plt.title('F1 score evolution with training epochs')
plt.legend()
plt.show()