Spaces:
Running
Running
Update pages/Entorno de Ejecución.py
Browse files- pages/Entorno de Ejecución.py +96 -80
pages/Entorno de Ejecución.py
CHANGED
@@ -12,91 +12,107 @@ st.set_page_config(
|
|
12 |
}
|
13 |
)
|
14 |
|
15 |
-
|
|
|
16 |
|
17 |
-
with
|
18 |
-
st.title("Entorno de ejecución")
|
19 |
-
st.caption("Los modelos no están en orden de eficacia, sino en orden de creación.")
|
20 |
-
|
21 |
-
# Get the absolute path to the current directory
|
22 |
-
current_dir = os.path.abspath(os.path.dirname(__file__))
|
23 |
-
|
24 |
-
# Get the absolute path to the parent directory of the current directory
|
25 |
-
root_dir = os.path.abspath(os.path.join(current_dir, os.pardir))
|
26 |
-
|
27 |
-
# Join the path to the models folder
|
28 |
-
DIR = os.path.join(root_dir, "models")
|
29 |
-
|
30 |
-
threshold = .75
|
31 |
-
|
32 |
-
models = os.listdir(DIR)
|
33 |
-
|
34 |
-
model_dict = dict()
|
35 |
-
for model in models:
|
36 |
-
model_name = model.split(DIR)
|
37 |
-
model_name = str(model.split('.h5')[0])
|
38 |
-
model_dir = os.path.join(DIR, model)
|
39 |
-
model_dict[model_name] = model_dir
|
40 |
|
41 |
-
|
42 |
-
ultra_button = st.checkbox('Ultra-Patacotrón (mejores resultados)')
|
43 |
-
ultra_flag = False
|
44 |
-
if ultra_button:
|
45 |
-
ultra_flag = True
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
@tf.function
|
61 |
-
def predict(model_list, img):
|
62 |
-
y_gorrito = 0
|
63 |
-
for model in model_list:
|
64 |
-
y_gorrito += tf.cast(model(tf.expand_dims(img/255., 0)), dtype=tf.float32)
|
65 |
-
return y_gorrito / len(model_list)
|
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 |
-
selected_models = [load_model(model_dict[model]) for model in model_choice]
|
92 |
-
y_gorrito = predict(selected_models, img)
|
93 |
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
else:
|
97 |
-
st.
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
}
|
13 |
)
|
14 |
|
15 |
+
st.title("Entorno de ejecución")
|
16 |
+
cnn, autoencoder, svm, iforest, gan, docc = st.tabs(["Redes neuronales convolucionales clásicas", "Autoencoders", "Máquina de vectores de soporte", 'Bosques de aislamiento', 'Redes generativas adversariales', 'Clasificación de una clase profunda (DOCC)'])
|
17 |
|
18 |
+
with cnn:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
col_a, col_b, = st.columns(2)
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
with col_a:
|
23 |
+
st.title("Redes neuronales convolucionales clásicas")
|
24 |
+
st.caption("Los modelos no están en orden de eficacia, sino en orden de creación.")
|
25 |
+
|
26 |
+
# Get the absolute path to the current directory
|
27 |
+
current_dir = os.path.abspath(os.path.dirname(__file__))
|
28 |
+
|
29 |
+
# Get the absolute path to the parent directory of the current directory
|
30 |
+
root_dir = os.path.abspath(os.path.join(current_dir, os.pardir))
|
31 |
+
|
32 |
+
# Join the path to the models folder
|
33 |
+
DIR = os.path.join(root_dir, "models")
|
34 |
+
|
35 |
+
threshold = .75
|
36 |
+
|
37 |
+
models = os.listdir(DIR)
|
38 |
+
|
39 |
+
model_dict = dict()
|
40 |
+
for model in models:
|
41 |
+
model_name = model.split(DIR)
|
42 |
+
model_name = str(model.split('.h5')[0])
|
43 |
+
model_dir = os.path.join(DIR, model)
|
44 |
+
model_dict[model_name] = model_dir
|
45 |
|
46 |
+
ultraversions = ['ptctrn_v1.4', 'ptctrn_v1.5', 'ptctrn_v1.6', 'ptctrn_v1.12']
|
47 |
+
ultra_button = st.checkbox('Ultra-Patacotrón (mejores resultados)')
|
48 |
+
ultra_flag = False
|
49 |
+
if ultra_button:
|
50 |
+
ultra_flag = True
|
51 |
+
|
52 |
+
# Create a dropdown menu to select the model
|
53 |
+
model_choice = st.multiselect("Seleccione uno o varios modelos de clasificación", model_dict.keys())
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
selected_models = []
|
56 |
+
|
57 |
+
def ensemble_model(model_list, img):
|
58 |
+
y_gorrito = np.zeros((1, 1))
|
59 |
+
for model in model_list:
|
60 |
+
instance_model = load_model(model_dict[model])
|
61 |
+
y_gorrito += float(instance_model.predict(np.expand_dims(img/255., 0)))
|
62 |
+
#clear_session()
|
63 |
+
return y_gorrito/len(model_list)
|
64 |
|
65 |
+
@tf.function
|
66 |
+
def predict(model_list, img):
|
67 |
+
y_gorrito = 0
|
68 |
+
for model in model_list:
|
69 |
+
y_gorrito += tf.cast(model(tf.expand_dims(img/255., 0)), dtype=tf.float32)
|
70 |
+
return y_gorrito / len(model_list)
|
71 |
+
|
72 |
+
# Set the image dimensions
|
73 |
+
IMAGE_WIDTH = IMAGE_HEIGHT = 224
|
74 |
+
|
75 |
+
uploaded_file = st.file_uploader(label = '',type= ['jpg','png', 'jpeg', 'jfif', 'webp', 'heic'])
|
76 |
+
executed = False
|
77 |
+
|
78 |
+
with col_b:
|
79 |
+
if st.button('¿Hay un patacón en la imagen?'):
|
80 |
+
if len(selected_models) > 0 and ultra_flag:
|
81 |
+
st.write('Debe elegir un solo método: Ultra-Patacotrón o selección múltiple.')
|
82 |
+
elif uploaded_file is not None:
|
|
|
|
|
83 |
|
84 |
+
raw_img = tf.image.decode_image(uploaded_file.read(), channels=3)
|
85 |
+
img = tf.image.resize(raw_img,(IMAGE_WIDTH, IMAGE_HEIGHT))
|
86 |
+
|
87 |
+
# Pass the image to the model and get the prediction
|
88 |
+
if ultra_flag:
|
89 |
+
with st.spinner('Cargando ultra-predicción...'):
|
90 |
+
if not executed:
|
91 |
+
ultraptctrn = [load_model(model_dict[model]) for model in ultraversions]
|
92 |
+
executed = True
|
93 |
+
y_gorrito = predict(ultraptctrn, img)
|
94 |
+
else:
|
95 |
+
with st.spinner('Cargando predicción...'):
|
96 |
+
selected_models = [load_model(model_dict[model]) for model in model_choice]
|
97 |
+
y_gorrito = predict(selected_models, img)
|
98 |
+
|
99 |
+
if y_gorrito >= threshold:
|
100 |
+
st.success("¡Patacón Detectado!")
|
101 |
+
else:
|
102 |
+
st.error("No se encontró rastro de patacón.")
|
103 |
+
st.caption(f'La probabilidad de que la imagen tenga un patacón es del: {round(float(y_gorrito), 2)*100}%')
|
104 |
+
st.image(raw_img.numpy())
|
105 |
+
st.caption('Si los resultados no fueron los esperados, por favor, [haz click aquí](https://docs.google.com/forms/d/e/1FAIpQLScH0ZxAV8aSqs7TPYi86u0nkxvQG3iuHCStWNB-BoQnSW2V0g/viewform?usp=sf_link)')
|
106 |
else:
|
107 |
+
st.write('Revisa haber seleccionado los modelos y la imagen correctamente.')
|
108 |
+
|
109 |
+
with autoencoder:
|
110 |
+
st.write('Próximamente')
|
111 |
+
with svm:
|
112 |
+
st.write('Próximamente')
|
113 |
+
with iforest:
|
114 |
+
st.write('Próximamente')
|
115 |
+
with gan:
|
116 |
+
st.write('Próximamente')
|
117 |
+
with docc:
|
118 |
+
st.write('Próximamente')
|