-
Notifications
You must be signed in to change notification settings - Fork 1
/
modelo2_kaggle_2ds.py
351 lines (250 loc) · 9.94 KB
/
modelo2_kaggle_2ds.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
# -*- coding: utf-8 -*-
"""modelo2_kaggle_2ds.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/13XA5UBezophPqWRd2xLEYyz9vxHY9WpB
"""
import numpy as np
import pandas as pd
import os
import cv2 as cv
import matplotlib.pyplot as plt
import seaborn as sns
all_0 = "./C-NMC_Leukemia/training_data/fold_0/all"
all_1 = "./C-NMC_Leukemia/training_data/fold_1/all"
all_2 = "./C-NMC_Leukemia/training_data/fold_2/all"
hem_0 = "./C-NMC_Leukemia/training_data/fold_0/hem"
hem_1 = "./C-NMC_Leukemia/training_data/fold_1/hem"
hem_2 = "./C-NMC_Leukemia/training_data/fold_2/hem"
def get_path_image(folder):
image_paths = []
image_fnames = os.listdir(folder)
for img_id in range(len(image_fnames)):
img = os.path.join(folder,image_fnames[img_id])
image_paths.append(img)
return image_paths
img_data = []
for i in [all_0,all_1,all_2,hem_0,hem_1,hem_2]:
paths = get_path_image(i)
img_data.extend(paths)
print(len(img_data))
data = {"img_data":img_data,
"labels":[np.nan for x in range(len(img_data))]}
data = pd.DataFrame(data)
data["labels"][0:7272] = 1 # ALL
data["labels"][7272:10661] = 0 # HEM
data["labels"] = data["labels"].astype("int64")
image = cv.imread(data["img_data"][1000])
plt.imshow(image)
plt.title("Sample image before cropping")
plt.show()
img_list = []
for i in range(len(img_data)):
image = cv.imread(data["img_data"][i])
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1]
result = cv.bitwise_and(image, image, mask=thresh)
result[thresh==0] = [255,255,255]
(x, y, z_) = np.where(result > 0)
mnx = (np.min(x))
mxx = (np.max(x))
mny = (np.min(y))
mxy = (np.max(y))
crop_img = image[mnx:mxx,mny:mxy,:]
crop_img_r = cv.resize(crop_img, (224,224))
img_list.append(crop_img_r)
plt.imshow(img_list[1000])
plt.title("Sample image after cropping")
plt.show()
from tensorflow.keras.applications import ResNet50, ResNet101
from keras.applications.vgg19 import VGG19
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.applications.resnet50 import preprocess_input
"""Modelos"""
def feature_extract(model):
if model == "VGG19": model = VGG19(weights='imagenet',include_top=False, pooling="avg")
elif model == "ResNet50": model = ResNet50(weights='imagenet',include_top=False,pooling="avg")
elif model == "ResNet101": model = ResNet101(weights='imagenet',include_top=False,pooling="avg")
return model
model = feature_extract("ResNet50") # or "VGG19", "ResNet101"
features_list = []
for i in range(len(img_list)):
image = img_list[i].reshape(-1, 224, 224, 3)
image = preprocess_input(image)
"""
# Reshaping when VGG19 model is selected
features = model.predict(image).reshape(512,)
"""
#Reshaping when ResNet50 or ResNet101 model is selected
features = model.predict(image).reshape(2048,)
features_list.append(features)
features_df = pd.DataFrame(features_list)
features_df["labels"] = data["labels"]
x = features_df.drop(['labels'], axis = 1)
y = features_df.loc[:,"labels"].values
x
print(f"Number of features before feature selection: {x.shape[1]}")
y
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(x)
x_ = scaler.transform(x)
x_ = pd.DataFrame(x_)
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
def anova_fs():
selector = SelectKBest(f_classif, k=500) # k is number of features
selector.fit(x_, y)
cols = selector.get_support(indices=True)
anova_x = x_[cols]
return anova_x
from sklearn.feature_selection import RFE
from sklearn.ensemble import RandomForestClassifier
def RFE_fs():
rfe_selector = RFE(estimator=RandomForestClassifier())
rfe_selector.fit(x_, y)
rfe_support = rfe_selector.get_support()
rfe_feature = x_.loc[:,rfe_support].columns.tolist()
rfe_x = x_[rfe_feature]
return rfe_x
from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import RandomForestClassifier
def rf_fs():
embeded_rf_selector = SelectFromModel(RandomForestClassifier(n_estimators=200, random_state=5), threshold='1.25*median')
embeded_rf_selector.fit(x, y)
embeded_rf_support = embeded_rf_selector.get_support()
embeded_rf_feature = x.loc[:,embeded_rf_support].columns.tolist()
rf_x = x[embeded_rf_feature]
return rf_x
fs_x = rf_fs() # feature selection methods "anova_fs", "RFE_fs"
print(f"Number of features after feature selection: {fs_x.shape[1]}")
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(fs_x, y, test_size = 0.2, random_state = 42)
from sklearn.model_selection import cross_val_score,cross_val_predict
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score,precision_score,recall_score,accuracy_score
from sklearn.model_selection import GridSearchCV
neig = np.arange(1, 25)
train_accuracy = []
test_accuracy = []
for i, k in enumerate(neig):
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(x_train,y_train)
prediction_ = knn.predict(x_test)
train_accuracy.append(knn.score(x_train, y_train))
test_accuracy.append(knn.score(x_test, y_test))
print("Best accuracy is {} with K = {}".format(np.max(test_accuracy),1+test_accuracy.index(np.max(test_accuracy))))
knn = KNeighborsClassifier(n_neighbors=17)
knn.fit(x_train,y_train)
predicted = knn.predict(x_test)
score = knn.score(x_test, y_test)
knn_score_ = np.mean(score)
print('Accuracy : %.3f' % (knn_score_))
p=precision_score(y_test, predicted)
print('Precision : %.3f' % (p))
r=recall_score(y_test, predicted)
print('Recall : %.3f' % (r))
f1=f1_score(y_test, predicted)
print('F1-score: %.3f' % (f1))
f1_w=f1_score(y_test, predicted, average='weighted')
print('Weighted f1-score: %.3f' % (f1_w))
cf_matrix = confusion_matrix(y_test, predicted)
sns.heatmap(cf_matrix, cmap="PuBu", annot=True, fmt='.0f')
plt.show()
param_grid_svm = {'C': [0.1, 1, 10, 100, 1000],
'gamma': [1, 0.1, 0.01, 0.001, 0.0001],
'kernel': ['rbf', 'poly']}
SVM_grid = GridSearchCV(svm.SVC(), param_grid_svm, cv=5)
SVM_grid.fit(x_train, y_train)
print(SVM_grid.best_params_)
print(SVM_grid.best_estimator_)
svm_clf = svm.SVC(C=100, gamma=0.01, kernel='rbf')
svm_clf.fit(x_train,y_train)
predicted = svm_clf.predict(x_test)
score = svm_clf.score(x_test, y_test)
svm_score_ = np.mean(score)
print('Accuracy : %.3f' % (svm_score_))
p=precision_score(y_test, predicted)
print('precision : %.3f' % (p))
r=recall_score(y_test, predicted)
print('recall : %.3f' % (r))
f1=f1_score(y_test, predicted)
print('f1-score: %.3f' % (f1))
f1_w=f1_score(y_test, predicted, average='weighted')
print('weighted f1-score: %.3f' % (f1_w))
cf_matrix = confusion_matrix(y_test, predicted)
sns.heatmap(cf_matrix, cmap="PuBu", annot=True, fmt='.0f')
plt.show()
param_grid_rf = {
'n_estimators': [200, 500],
'max_depth' : [4,5,6,7,8]}
RF_grid = GridSearchCV(estimator=RandomForestClassifier(), param_grid=param_grid_rf, cv= 5)
RF_grid.fit(x_train, y_train)
print(RF_grid.best_params_)
r_forest = RandomForestClassifier(500,max_depth=8, random_state=5)
r_forest.fit(x_train,y_train)
predicted = r_forest.predict(x_test)
score = r_forest.score(x_test, y_test)
rf_score_ = np.mean(score)
print('Accuracy : %.3f' % (rf_score_))
p=precision_score(y_test, predicted)
print('precision : %.3f' % (p))
r=recall_score(y_test, predicted)
print('recall : %.3f' % (r))
f1=f1_score(y_test, predicted)
print('f1-score: %.3f' % (f1))
f1_w=f1_score(y_test, predicted, average='weighted')
print('weighted f1-score: %.3f' % (f1_w))
cf_matrix = confusion_matrix(y_test, predicted)
sns.heatmap(cf_matrix, cmap="PuBu", annot=True, fmt='.0f')
plt.show()
nb_model = GaussianNB()
nb_model.fit(x_train,y_train)
predicted = nb_model.predict(x_test)
score = nb_model.score(x_test, y_test)
nb_score_ = np.mean(score)
print('Accuracy : %.3f' % (nb_score_))
p=precision_score(y_test, predicted)
print('precision : %.3f' % (p))
r=recall_score(y_test, predicted)
print('recall : %.3f' % (r))
f1=f1_score(y_test, predicted)
print('f1-score: %.3f' % (f1))
f1_w=f1_score(y_test, predicted, average='weighted')
print('weighted f1-score: %.3f' % (f1_w))
cf_matrix = confusion_matrix(y_test, predicted)
sns.heatmap(cf_matrix, cmap="PuBu", annot=True, fmt='.0f')
plt.show()
import joblib
# Entrena y selecciona el mejor modelo SVM (SVM_grid.best_estimator_)
svm_clf = svm.SVC(C=100, gamma=0.01, kernel='rbf')
svm_clf.fit(x_train, y_train)
# Especifica el nombre del archivo donde deseas guardar el modelo SVM
modelo_svm_filename = 'modelo_svm.h5'
# Guarda el modelo en el archivo h5
joblib.dump(svm_clf, modelo_svm_filename)
print(f'Modelo SVM guardado en {modelo_svm_filename}')
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
# Cargar el modelo desde un archivo .h5 (asegúrate de tener el archivo)
modelo_cargado = tf.keras.models.load_model('./modelo_svm.h5')
# Ruta de la imagen que deseas clasificar
ruta_imagen_a_clasificar = '33.bpm' # Cambia la ruta a la ubicación de tu imagen
# Cargar y preprocesar la imagen
imagen = image.load_img(ruta_imagen_a_clasificar, target_size=(224, 224))
imagen_array = image.img_to_array(imagen)
imagen_array = np.expand_dims(imagen_array, axis=0) # Añadir una dimensión para crear un lote
# Realizar la clasificación utilizando el modelo
predicciones = modelo_cargado.predict(imagen_array)
# El resultado será un arreglo de probabilidades, puedes interpretarlo
# según tus etiquetas (por ejemplo, si la etiqueta 0 es HEM y la etiqueta 1 es ALL)
if predicciones[0][0] > predicciones[0][1]:
print('La imagen se clasifica como HEM (0)')
else:
print('La imagen se clasifica como ALL (1)')