-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassify_CNN.py
More file actions
269 lines (206 loc) · 7.84 KB
/
classify_CNN.py
File metadata and controls
269 lines (206 loc) · 7.84 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
#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
# Initialize the CNN model
model = Sequential()
train_and_save = 1 ### se quiseres treinar
if (train_and_save):
# 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.8)) # Dropout for regularization
# Output layer (Sigmoid for binary classification)
model.add(Dense(units=1, activation='sigmoid'))
# Compile the model
learning_rate = 0.001 # Set your desired learning rate
optimizer = Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
history = model.fit(
X_train,
y_train,
#steps_per_epoch=len(X_train),
epochs=8, # Number of epochs (adjust as needed)
validation_data=(X_val , y_val),
#validation_steps=len(X_val)
batch_size = 32
)
# Save the model
model.save('cnn_binary_classifier.h5')
else:
model = load_model('cnn_binary_classifier.h5')
# Evaluate the model on the validation/test set
test_loss, test_acc = model.evaluate(X_val , y_val)
print(f"Test Accuracy: {test_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"Learning rate: {learning_rate} Validation F1 Score: {f1}")
model.save(f'cnn_binary_classifier_{f1:.4f}.h5')