Spaces:
Runtime error
Runtime error
scontess
commited on
Commit
Β·
9e3e3ef
1
Parent(s):
b1b981c
dopopull
Browse files- src/streamlit_app.py +7 -13
src/streamlit_app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import streamlit as st
|
2 |
-
import tensorflow_datasets as tfds
|
3 |
import tensorflow as tf
|
4 |
import numpy as np
|
5 |
import time
|
@@ -51,15 +50,17 @@ X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_st
|
|
51 |
st.write(f"π **Training:** {X_train.shape[0]} immagini")
|
52 |
st.write(f"π **Validation:** {X_val.shape[0]} immagini")
|
53 |
|
54 |
-
# π
|
|
|
|
|
|
|
55 |
history = None # π Inizializza history
|
56 |
|
57 |
-
if os.path.exists("Silva.h5"):
|
58 |
model = load_model("Silva.h5")
|
59 |
st.write("β
Modello `Silva.h5` caricato, nessun nuovo training necessario!")
|
60 |
else:
|
61 |
-
st.write("π Training in corso
|
62 |
-
|
63 |
base_model = VGG16(weights="imagenet", include_top=False, input_shape=(64, 64, 3))
|
64 |
for layer in base_model.layers:
|
65 |
layer.trainable = False
|
@@ -72,12 +73,7 @@ else:
|
|
72 |
model = Model(inputs=base_model.input, outputs=output)
|
73 |
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
|
74 |
|
75 |
-
# π Training con monitoraggio validazione
|
76 |
history = model.fit(X_train, y_train, epochs=10, validation_data=(X_val, y_val))
|
77 |
-
|
78 |
-
st.write("β
Addestramento completato!")
|
79 |
-
|
80 |
-
# π Salvare il modello
|
81 |
model.save("Silva.h5")
|
82 |
st.write("β
Modello salvato come `Silva.h5`!")
|
83 |
|
@@ -106,7 +102,7 @@ if st.button("π Genera matrice di confusione per validazione"):
|
|
106 |
st.write("β
Matrice di confusione generata!")
|
107 |
|
108 |
# π Grafico per Loss e Accuracy con validazione
|
109 |
-
if history:
|
110 |
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
|
111 |
ax[0].plot(history.history["loss"], label="Training Loss")
|
112 |
ax[0].plot(history.history["val_loss"], label="Validation Loss")
|
@@ -143,5 +139,3 @@ def upload_model():
|
|
143 |
st.write("π₯ Carica il modello Silva su Hugging Face")
|
144 |
if st.button("π Carica Silva su Model Store"):
|
145 |
upload_model()
|
146 |
-
|
147 |
-
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
import time
|
|
|
50 |
st.write(f"π **Training:** {X_train.shape[0]} immagini")
|
51 |
st.write(f"π **Validation:** {X_val.shape[0]} immagini")
|
52 |
|
53 |
+
# π Checkbox per decidere se rifare il training
|
54 |
+
force_training = st.checkbox("π Rifai il training anche se Silva.h5 esiste")
|
55 |
+
|
56 |
+
# π Caricamento o training del modello
|
57 |
history = None # π Inizializza history
|
58 |
|
59 |
+
if os.path.exists("Silva.h5") and not force_training:
|
60 |
model = load_model("Silva.h5")
|
61 |
st.write("β
Modello `Silva.h5` caricato, nessun nuovo training necessario!")
|
62 |
else:
|
63 |
+
st.write("π Training in corso...")
|
|
|
64 |
base_model = VGG16(weights="imagenet", include_top=False, input_shape=(64, 64, 3))
|
65 |
for layer in base_model.layers:
|
66 |
layer.trainable = False
|
|
|
73 |
model = Model(inputs=base_model.input, outputs=output)
|
74 |
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
|
75 |
|
|
|
76 |
history = model.fit(X_train, y_train, epochs=10, validation_data=(X_val, y_val))
|
|
|
|
|
|
|
|
|
77 |
model.save("Silva.h5")
|
78 |
st.write("β
Modello salvato come `Silva.h5`!")
|
79 |
|
|
|
102 |
st.write("β
Matrice di confusione generata!")
|
103 |
|
104 |
# π Grafico per Loss e Accuracy con validazione
|
105 |
+
if history is not None:
|
106 |
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
|
107 |
ax[0].plot(history.history["loss"], label="Training Loss")
|
108 |
ax[0].plot(history.history["val_loss"], label="Validation Loss")
|
|
|
139 |
st.write("π₯ Carica il modello Silva su Hugging Face")
|
140 |
if st.button("π Carica Silva su Model Store"):
|
141 |
upload_model()
|
|
|
|