<jupyter_start><jupyter_text>Import des fonctions nécessaires :<jupyter_code>import pandas as pd
import numpy as np
from tqdm import tqdm
import itertools # Pour créer des iterateurs
import time

import matplotlib.pyplot as plt  # Pour l'affichage d'images
from matplotlib import cm # Pour importer de nouvelles cartes de couleur

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score, confusion_matrix, classification_report
from sklearn import metrics

import tensorflow as tf
from tensorflow.keras.models import Model, Sequential, load_model

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Load the data, the model and prepare samples to evaluate the model<jupyter_code># Define the data filepath and the model filepath
csv_5fam = "/Users/Adrien/DataScientist/projet_Mushroom/reduced_dataset_5_families.csv"
model_path = "/Users/Adrien/DataScientist/projet_Mushroom/models_GPU_20210620/model_vgg16_GPU_20210617.h5"
# Load the file
df = pd.read_csv(csv_5fam)
# keep only interesting datas
df2 = df.drop(['image_id', 'image_url'], axis=1)
# Split the data (like they were splitted during the model training)
data_train, data_test = train_test_split(df2, test_size=0.2, random_state=123)
# load the model
print("loading model...")
model = load_model(model_path)
print("Model loaded !")
print("Preparing test images...")
time.sleep(0.1)
# Prepare images for the model evaluation
X_test = []
for filepath in tqdm(data_test['filepath']):
    # Lecture du fichier
    im = tf.io.read_file(filepath)
    # On décode le fichier
    im = tf.image.decode_jpeg(im, channels=3)
    # Redimensionnement
    im = tf.image.resize(im, size=(256, 256))
    X_test.append([im])
X_test = tf.concat(X_test, axis=0)
# prepare the labels !
y_test = data_test['label'].astype(int)<jupyter_output>loading model...<jupyter_text>Predict all images from X_test !(using argmax because softmax retunrs des probabilites)<jupyter_code>y_prob = model.predict(X_test, batch_size=64)
y_pred = np.argmax(y_prob, axis = 1)<jupyter_output><empty_output><jupyter_text>Display the results<jupyter_code>## Première étape, la matrice de confusion
cnf_matrix = confusion_matrix(y_test, y_pred)

### Display a nice confusion matrix
##
#
classes = range(0,(df['family'].nunique()))
print("Matrice de confusion issue du modèle VGG16:")
plt.figure()

plt.imshow(cnf_matrix, interpolation='nearest',cmap='Blues')
plt.title("Matrice de confusion")
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes)
plt.yticks(tick_marks, classes)

for i, j in itertools.product(range(cnf_matrix.shape[0]), range(cnf_matrix.shape[1])):
    plt.text(j, i, cnf_matrix[i, j],
             horizontalalignment = "center",
             color = "white" if cnf_matrix[i, j] > ( cnf_matrix.max() / 2) else "black")

plt.ylabel('Vrais labels')
plt.xlabel('Labels prédits')
plt.show()
#
##
###



## Seconde étape le classification report

print("\n\nClassification report du modèle VGG16:\n\n", classification_report(y_pred, y_test))<jupyter_output>Matrice de confusion issue du modèle VGG16:<jupyter_text>Plot few images with their labels<jupyter_code>indices_random = tf.random.uniform([3], 0, len(X_test), dtype=tf.int32)

plt.figure(figsize=(15,7))
for i, idx in enumerate(indices_random):
    plt.subplot(1,3,i+1)
    plt.imshow(tf.cast(X_test[idx], tf.int32))
    plt.xticks([])
    plt.yticks([])
    plt.title('Pred class : {} \n Real class : {}'.format(df.family.unique()[y_pred[idx]], df.family.unique()[y_test.values[idx]]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12218202059560256, 'fraction_numerical': 0.018369050932368493, 'mean_word_length': 5.8982725527831095, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '67810', 'license': 'MIT', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1262, 'n_words': 298, 'path': 'models/iteration_1/20210707_evaluate_model_VGG16-datagenerator_GPU_mac.ipynb', 'repo_name': 'amorea04/MushPy', 'size': '3593'}