-
Notifications
You must be signed in to change notification settings - Fork 1
/
model2.py
177 lines (133 loc) · 5.71 KB
/
model2.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
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")
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)
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
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
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(x)
x_ = scaler.transform(x)
x_ = pd.DataFrame(x_)
# testeo de una imagen individual con svm
import cv2 as cv
import numpy as np
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import RandomForestClassifier
import joblib
# Carga el modelo SVM previamente entrenado
loaded_svm_model = joblib.load('svm_model.pkl')
# Carga el modelo ResNet50 para la extracción de características
resnet_model = ResNet50(weights='imagenet', include_top=False, pooling="avg")
# Define una función para preprocesar la imagen de entrada
def preprocess_image(image_path):
image = cv.imread(image_path)
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))
return crop_img_r
# Define una función que extrae características de la imagen
def extract_features(image, model, feature_selector):
image = preprocess_input(image)
features = model.predict(image.reshape(1, 224, 224, 3))
selected_features = feature_selector.transform(features.reshape(1, -1))
return selected_features
# Define una función que clasifica la imagen
def classify_image(image_features, model):
prediction = model.predict(image_features)
if prediction[0] == 1:
return "Clasificación: ALL (Leucemia)"
else:
return "Clasificación: HEM (No Leucemia)"
# Función para la selección de características basada en Random Forest
def rf_fs(x, y):
embeded_rf_selector = SelectFromModel(RandomForestClassifier(n_estimators=619, 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
# Ruta de ejemplo para la imagen
user_image_path = "C-NMC_Leukemia/training_data/fold0/hem/UID_H6_8_1_hem.bmp"
ruta = "./C-NMC_Leukemia/training_data/fold_1/hem/UID_H10_3_3_hem.bmp"
# Preprocesa la imagen
user_image = preprocess_image(user_image_path)
# Extrae características de la imagen
image_features = extract_features(user_image, resnet_model, rf_fs(features_df.drop(['labels'], axis=1), features_df['labels']))
# Clasifica la imagen con el modelo SVM previamente entrenado
result = classify_image(image_features, loaded_svm_model)
print(result)