ki-Praktikum-MS2/TestMitValid.py
2023-06-06 17:38:22 +02:00

53 lines
No EOL
1.3 KiB
Python

#Import libaries and datasets
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import pandas as pd
import seaborn as sns
import pickle
import random
import os
import numpy as np
from keras.utils import np_utils
with open(os.path.join("dataset", "valid.p"), mode='rb') as validation_data:
valid = pickle.load(validation_data)
X_valid, y_valid = valid['features'], valid['labels']
from sklearn.utils import shuffle
X_valid, y_valid = shuffle(X_valid, y_valid)
# Normalize image to [0, 1]
X_valid_norm = X_valid / 255
#Wähle Klassen 0-19
mask = np.isin(y_valid, range(20))
X_valid_subset = X_valid_norm[mask]
y_valid_subset = y_valid[mask]
num_classes = 20 # Anzahl der Straßenschilder-Klassen
y_valid_subset = np_utils.to_categorical(y_valid_subset, num_classes)
#ownModel
#convolutionalNeuralNetwork
#fullyConnectedNeuralNetwork
model = tf.keras.models.load_model('saved_model/fullyConnectedNeuralNetwork.h5')
score = model.evaluate(X_valid_subset, y_valid_subset)
print('Test Accuracy: {}'.format(score[1]))
from sklearn.metrics import confusion_matrix
predicted_classes = np.argmax(model.predict(X_valid_subset), axis=-1)
y_true = np.argmax(y_valid_subset, axis=-1)
cm = confusion_matrix(y_true, predicted_classes)
plt.figure(figsize = (25, 25))
sns.heatmap(cm, annot = True)
plt.show()