Commit
·
f532851
1
Parent(s):
e78c496
Init
Browse files- .gitignore +1 -0
- src/fit_model.py +56 -0
- src/main.py +18 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
my_models
|
src/fit_model.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
from tensorflow import keras
|
3 |
+
|
4 |
+
# dataset utilizzato https://keras.io/api/datasets/fashion_mnist/#load_data-function
|
5 |
+
|
6 |
+
mnist = keras.datasets.fashion_mnist.load_data()
|
7 |
+
#vettori per trainare l'ia e per testarla
|
8 |
+
(x_train,y_train),(x_test,y_test) = mnist
|
9 |
+
|
10 |
+
import numpy as np
|
11 |
+
|
12 |
+
#min = 0 e max = 255
|
13 |
+
np.min(x_train), np.min(x_train)
|
14 |
+
|
15 |
+
#categorie vestiti
|
16 |
+
class_names = ['top', 'trouser', 'pullover', 'dress', ' coat', 'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
|
17 |
+
|
18 |
+
#x_train e' una matrice e dobbiamo normalizzarlo (dividendolo per il max value)
|
19 |
+
|
20 |
+
X_train = x_train/255.0
|
21 |
+
X_test = x_test/255.0
|
22 |
+
|
23 |
+
from keras import Sequential as Sequ
|
24 |
+
from tensorflow.keras.layers import Flatten, Dense
|
25 |
+
|
26 |
+
model = Sequ()
|
27 |
+
#aggiungo il layer Flatten con la forma 28 x 28 pixel
|
28 |
+
model.add(Flatten(input_shape=(28,28)))
|
29 |
+
#aggiungo 128 neuroni
|
30 |
+
model.add(Dense(128, activation='relu'))
|
31 |
+
model.add(Dense(256, activation='relu'))
|
32 |
+
#qui aggiungo 10 neuroni perche' abbiamo 10 categorie
|
33 |
+
#con softmax activation -> perche' facciamo un multiclass classification (piu' di 2 classi)
|
34 |
+
model.add(Dense(10, activation='softmax'))
|
35 |
+
|
36 |
+
print(model.summary())
|
37 |
+
|
38 |
+
#model compilation
|
39 |
+
# loss function, optimazer, metrics
|
40 |
+
|
41 |
+
#utilizziamo sparse_categorical_crossentropy perche'
|
42 |
+
#il nostro output e' 0,1,2,3,4,5 -> sparse_categorical_crossentropy
|
43 |
+
#il nostro output e' one hot encoded -> categorical_entropy
|
44 |
+
# 0,1 -> binary_cross_entropy
|
45 |
+
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
46 |
+
|
47 |
+
#Passiamo i dati per il training e li dividiamo in 10 'epoche' passando 32 dati ogni epoca
|
48 |
+
model_checkpoint_best_callback = keras.callbacks.ModelCheckpoint(
|
49 |
+
filepath = '../my_models/fashion_model.keras',
|
50 |
+
monitor = 'val_loss',
|
51 |
+
verbose = 1,
|
52 |
+
save_best_only = False,
|
53 |
+
save_weights_only = False
|
54 |
+
)
|
55 |
+
model.fit(X_train, y_train, epochs=10, batch_size=32, callbacks=[model_checkpoint_best_callback])
|
56 |
+
|
src/main.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
(x_train,y_train),(x_test,y_test) = tf.keras.datasets.fashion_mnist.load_data()
|
5 |
+
|
6 |
+
X_test = x_test/255.0
|
7 |
+
|
8 |
+
model = tf.keras.models.load_model('my_models_h5/fashion_model.keras')
|
9 |
+
|
10 |
+
model.evaluate(X_test, y_test)
|
11 |
+
|
12 |
+
y_pred = model.predict(X_test)
|
13 |
+
y_pred = np.argmax(y_pred, axis=1)
|
14 |
+
|
15 |
+
from sklearn.metrics import classification_report
|
16 |
+
#report per predire la accuracy
|
17 |
+
print(classification_report(y_test, y_pred))
|
18 |
+
|