# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Import des fonctions nécessaires :

# +
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
# -

# # Load the data, the model and prepare samples to evaluate the model

# 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)

# # Predict all images from X_test !
# (using argmax because softmax retunrs des probabilites)

y_prob = model.predict(X_test, batch_size=64)
y_pred = np.argmax(y_prob, axis = 1)

# # Display the results

# +
## 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))
# -

# # Plot few images with their labels

# +
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]]))

StarcoderdataJupyterScriptsDedupFiltered