<jupyter_start><jupyter_text>Convolutional NN su database FASHION MNISTConvolutional neural network sviluppata in TensorFlow 1.x, su database FASHION MNIST.In questo notebook lo scopo è testare il modello con un altro database; per spiegazioni più dettagliate, guardare la versione corrispondente al modello su database MNIST.<jupyter_code>%tensorflow_version 1.x
import tensorflow as tf
print(tf.__version__)
from tensorflow import keras as ks
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.metrics import confusion_matrix
#Per data visualization
import seaborn as sns

import time

print("Done")<jupyter_output>Done<jupyter_text>Definizione metodi<jupyter_code>class TimeHistory(ks.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.times = []

    def on_epoch_begin(self, epoch, logs={}):
        self.epoch_time_start = time.time()

    def on_epoch_end(self, epoch, logs={}):
        self.times.append(time.time() - self.epoch_time_start)

print("Done")<jupyter_output>Done<jupyter_text>Import datasetImport del dataset, e stampa della sua struttura.<jupyter_code>#Import del database MNIST
fashion_mnist = tf.keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

#Struttura dati nel database
print("Numero elementi e dimensione immagini in train set: " + str(train_images.shape))
print("Numero elementi e dimensione immagini in test set: " + str(test_images.shape))
print("Numero elementi label in train set: " + str(len(train_labels)))
print("Numero elementi label in test set: " + str(len(test_labels)))
print("Tipo di struttura del train set: " + str(type(train_images)) + ", composto da " + str(type(train_images[0])))
print("Tipo di struttura del label set: " + str(type(train_labels)) + ", composto da " + str(type(train_labels[0])))
print("Le etichette sono interi; valore più piccolo: " + str(np.amin(train_labels)) + ", valore più grande: " + str(np.amax(train_labels)))
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
               
print("Done")<jupyter_output>Done<jupyter_text>Preprocessing dei datiOltre a scalare le immagini, le trasformo in matrici.<jupyter_code>#Preprocessing dei dati
#Scalo i valori nel range tra 0 e 1
train_images = train_images / 255.0

test_images = test_images / 255.0
train_images = train_images.reshape((60000, 28, 28, 1))
test_images = test_images.reshape((10000, 28, 28, 1))

print("Training dataset shape: {}".format(train_images.shape))<jupyter_output>Training dataset shape: (60000, 28, 28, 1)<jupyter_text>Costruzione modelloCostruisco il modello convoluzionale.Piccola spiegazione del pooling e della costruzione di modelli convoluzionali: [pooling](https://machinelearningmastery.com/pooling-layers-for-convolutional-neural-networks/). Build<jupyter_code>cnn_model = ks.models.Sequential()
cnn_model.add(ks.layers.Conv2D(64, (5, 5), activation = 'relu',
                               input_shape=(28, 28, 1), name='Conv2D_layer1'))
cnn_model.add(ks.layers.Conv2D(32, (3, 3), activation = 'relu',
                               input_shape=(28, 28, 1), name='Conv2D_layer2'))
cnn_model.add(ks.layers.MaxPooling2D((2, 2), name = 'Maxpooling_2D'))
cnn_model.add(ks.layers.Flatten(name = 'Flatten'))
cnn_model.add(ks.layers.Dense(64, activation = 'relu', name = 'Hidden_layer'))
#L'ultimo layer avrà sempre numero nodi pari alle categorie da identificare
cnn_model.add(ks.layers.Dense(10, activation = 'softmax', name = 'Output_layer'))

cnn_model.summary()<jupyter_output>WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
Conv2D_layer1 (Conv2D)       (None, 24, 24, 64)        1664      
_________________________________________________________________
Conv2D_layer2 (Conv2D)       (None, 22, 22, 32)        18464     
_________________________________________________________________
Maxpooling_2D (MaxPooling2D) (None, 11, 11, 32)        0         
_________________________________________________________________
Flatten (Flatten)       [...]<jupyter_text>Compile<jupyter_code>cnn_model.compile(optimizer = 'adam', 
                  loss = 'sparse_categorical_crossentropy',
                  metrics = ['accuracy'])

print("Done")<jupyter_output>Done<jupyter_text>Training<jupyter_code>#Training
time_callback = TimeHistory()

hist = cnn_model.fit(train_images, train_labels, validation_data=(test_images, test_labels), 
                     epochs = 3, callbacks=[time_callback])

print("Done")<jupyter_output>Train on 60000 samples, validate on 10000 samples
Epoch 1/3
60000/60000 [==============================] - 14s 228us/sample - loss: 0.4157 - acc: 0.8506 - val_loss: 0.3440 - val_acc: 0.8730
Epoch 2/3
60000/60000 [==============================] - 8s 125us/sample - loss: 0.2736 - acc: 0.9000 - val_loss: 0.2996 - val_acc: 0.8917
Epoch 3/3
60000/60000 [==============================] - 7s 123us/sample - loss: 0.2292 - acc: 0.9159 - val_loss: 0.2620 - val_acc: 0.9066
Done<jupyter_text>Risultati<jupyter_code>#Misuro accuratezza
test_accuracy = cnn_model.evaluate(test_images, test_labels)

print("Accuratezza: " + str(test_accuracy))

print("Time per epoch: " + str(time_callback.times))
total_time = time_callback.times
total_time_str = sum(total_time)
print("Total time: " + str(total_time_str))

print("Done")<jupyter_output>10000/10000 [==============================] - 1s 59us/sample - loss: 0.2620 - acc: 0.9066
Accuratezza: [0.26200328268408773, 0.9066]
Time per epoch: [13.706544637680054, 7.520665645599365, 7.365785598754883]
Total time: 28.5929958820343
Done<jupyter_text>Visualizzo l'accuratezza con un grafo (dove si nota overfitting).<jupyter_code>#history_dict = hist.history
#print(hist.keys())
#Visualize the models accuracy
plt.plot(hist.history['acc'])
plt.plot(hist.history['val_acc'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>Matrice di confusioneGenero la matrice di confusione.<jupyter_code>#Necessito di un array con tutte le labels, ho già class_names

test_predictions = cnn_model.predict_classes(test_images)

conf_matr = confusion_matrix(y_true = test_labels, y_pred = test_predictions)
print(conf_matr)

con_mat_norm = np.around(conf_matr.astype('float') / conf_matr.sum(axis=1)[:, np.newaxis], decimals=2)

con_mat_df = pd.DataFrame(con_mat_norm,
                          index = class_names, 
                          columns = class_names)
figure = plt.figure(figsize=(10, 10))
sns.heatmap(con_mat_df, annot=True,cmap=plt.cm.Blues)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()<jupyter_output><empty_output><jupyter_text>Predizioni<jupyter_code>#Show predictions as probabilites for the first 4 images in the test set
predictions = cnn_model.predict(test_images[:4])
predictions
#Print our predictions as number labels for the first 4 images
print( np.argmax(predictions, axis=1))
#Print the actual labels
print(test_labels[:4])<jupyter_output>[9 2 1 1]
[9 2 1 1]<jupyter_text>Ricorda che      Label    Class        0    T-shirt/top        1    Trouser          2    Pullover          3    Dress          4    Coat          5    Sandal          6    Shirt          7    Sneaker          8    Bag          9    Ankle boot<jupyter_code>#Show the first 4 images as pictures
for i in range(0,4):
  image = test_images[i]
  image = np.array(image, dtype='float')
  pixels = image.reshape((28,28))
  plt.imshow(pixels, cmap='gray')
  plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1338544211040547, 'fraction_numerical': 0.04689789936492428, 'mean_word_length': 4.870250896057348, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '574207', 'license': 'MIT', 'n_tokens_mistral': 3020, 'n_tokens_neox': 2722, 'n_words': 622, 'path': 'TensorFlow 1.x on MNIST/FASHION_MNIST_CNN.ipynb', 'repo_name': 'PaoloDarienzo/BinaryClassificationRNN', 'size': '8188'}