automatyczny_kelner/Network/testerVal.py

59 lines
1.6 KiB
Python
Raw Normal View History

2023-06-02 12:03:31 +02:00
from pathlib import Path
2023-06-01 23:51:27 +02:00
import numpy as np
import tensorflow as tf
from tensorflow import keras
# Load the trained model
2023-06-02 00:14:50 +02:00
model = keras.models.load_model('Network/trained_model.h5')
2023-06-01 23:51:27 +02:00
# Load the class names
2023-06-02 12:03:31 +02:00
class_names = ['table', 'done', 'order']
2023-06-01 23:51:27 +02:00
# Load and preprocess the validation dataset
2023-06-02 00:14:50 +02:00
data_dir = "Network/Training/"
2023-06-01 23:51:27 +02:00
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]
2023-06-02 00:14:50 +02:00
direct = 'Network/Results/'
2023-06-01 23:51:27 +02:00
filename = predicted_class + str(i) + '.jpeg'
2023-06-02 12:03:31 +02:00
Path(direct).mkdir(parents=True, exist_ok=True)
2023-06-02 00:14:50 +02:00
tf.keras.preprocessing.image.save_img(direct+filename, val_images[i])
2023-06-01 23:51:27 +02:00
if predicted_class != true_class:
errorcount += 1
print('Image', i+1)
print('True class:', true_class)
print('Predicted class:', predicted_class)
print()
2023-06-02 12:03:31 +02:00
print('Error count: ', errorcount)