Neural Network - Testers

This commit is contained in:
s473609 2023-06-01 23:51:27 +02:00
parent 6ec28f4052
commit c006fc128e
2 changed files with 119 additions and 0 deletions

62
Network/TesterRandom.py Normal file
View File

@ -0,0 +1,62 @@
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing import image
import numpy as np
import os
# Load the trained model
model = keras.models.load_model('trained_model.h5')
# Load the class names
class_names = ['Empty', 'Food','People']
# Path to the folder containing test images
test_images_folder = 'Testing/'
# Iterate over the test images
i = 0
errorcount = 0
for folder_name in os.listdir(test_images_folder):
folder_path = os.path.join(test_images_folder, folder_name)
if os.path.isdir(folder_path):
print('Testing images in folder:', folder_name)
# True class based on folder name
if folder_name == 'Empty':
true_class = 'Empty'
elif folder_name == 'Food':
true_class = 'Food'
elif folder_name == 'People':
true_class = 'People'
true_class = folder_name
# Iterate over the files in the subfolder
for filename in os.listdir(folder_path):
if filename.endswith('.jpg') or filename.endswith('.jpeg'):
i+=1
# Load and preprocess the test image
image_path = os.path.join(folder_path, filename)
test_image = image.load_img(image_path, target_size=(100, 100))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
test_image = test_image / 255.0 # Normalize the image
# Reshape the image array to (1, height, width, channels)
test_image = np.reshape(test_image, (1,100, 100, 3))
# Make predictions
predictions = model.predict(test_image)
predicted_class_index = np.argmax(predictions[0])
predicted_class = class_names[predicted_class_index]
direct = 'Results/'
filename = str(i) + predicted_class + '.jpeg'
test_image = np.reshape(test_image, (100, 100, 3))
tf.keras.preprocessing.image.save_img(direct+filename, test_image)
if predicted_class != true_class:
errorcount += 1
print('Image:', filename)
print('True class:', true_class)
print('Predicted class:', predicted_class)
print()
print('Error count: ', errorcount)

57
Network/testerVal.py Normal file
View File

@ -0,0 +1,57 @@
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
# Load the trained model
model = keras.models.load_model('trained_model.h5')
# Load the class names
class_names = ['Empty', 'Food','People']
# Load and preprocess the validation dataset
data_dir = "Training/"
image_size = (100, 100)
batch_size = 32
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=image_size,
batch_size=batch_size,
)
# Select 20 random images from the validation set
val_images = []
val_labels = []
for images, labels in val_ds.unbatch().shuffle(1000).take(60):
val_images.append(images)
val_labels.append(labels)
# Make predictions on the random images
errorcount = 0
for i in range(60):
test_image = val_images[i]
test_label = val_labels[i]
test_image = np.expand_dims(test_image, axis=0)
test_image = test_image / 255.0 # Normalize the image
# Make predictions
predictions = model.predict(test_image)
predicted_class_index = np.argmax(predictions[0])
predicted_class = class_names[predicted_class_index]
true_class = class_names[test_label]
direct = 'Results/'
filename = predicted_class + str(i) + '.jpeg'
tf.keras.preprocessing.image.save_img(direct+filename, test_image)
if predicted_class != true_class:
errorcount += 1
print('Image', i+1)
print('True class:', true_class)
print('Predicted class:', predicted_class)
print()
print('Error count: ', errorcount)