diff --git a/simple_xception.keras b/simple_xception.keras new file mode 100644 index 0000000000000000000000000000000000000000..2b195483fcd5c6109c4cf679d228a3af0e6663ca Binary files /dev/null and b/simple_xception.keras differ diff --git a/test.py b/test.py index 4fe02cfb8eff44f8a11f8c449f488d82d246dfa7..1579836d4977f35dab5f0eb94b2b3fa6a39c9a31 100644 --- a/test.py +++ b/test.py @@ -1,8 +1,12 @@ import keras import matplotlib.pyplot as plt import numpy as np +import os +import random -# Load just enough to get class_names +#os.environ["CUDA_VISIBLE_DEVICES"] = "-1" + +# Get class names from directory structure temp_ds = keras.utils.image_dataset_from_directory( "Combined_Dataset", labels="inferred", @@ -11,33 +15,52 @@ temp_ds = keras.utils.image_dataset_from_directory( batch_size=1, shuffle=False ) - -# Load class names class_names = temp_ds.class_names # Load model -model = keras.models.load_model("save_at_6.keras") +model = keras.models.load_model("simple_xception.keras") -# Load and show image -img = keras.utils.load_img( - "Combined_Dataset/Dracaufeu/0f0537c0761b48be754706eb260cf3634f71238a7cb6961dd39b9914857c6283.jpg", - target_size=(256, 256) -) -plt.imshow(img) -plt.axis("off") +# Base path +base_path = "Combined_Dataset" + +# Prepare 2x2 plot +plt.figure(figsize=(10, 10)) + +for i in range(4): + # Pick random class and image + random_class = random.choice(class_names) + class_folder = os.path.join(base_path, random_class) + random_image = random.choice([ + f for f in os.listdir(class_folder) + if f.lower().endswith(('.png', '.jpg', '.jpeg')) + ]) + img_path = os.path.join(class_folder, random_image) + # Load and preprocess + img = keras.utils.load_img(img_path, target_size=(256, 256)) + img_array = keras.utils.img_to_array(img) + img_array = keras.ops.expand_dims(img_array, 0) -# Preprocess image -img_array = keras.utils.img_to_array(img) -img_array = keras.ops.expand_dims(img_array, 0) + # Predict + predictions = model.predict(img_array, verbose=0) + probabilities = keras.ops.softmax(predictions[0]) + predicted_class_index = np.argmax(probabilities) + predicted_label = class_names[predicted_class_index] + confidence = 100 * probabilities[predicted_class_index] -# Predict -predictions = model.predict(img_array) -probabilities = keras.ops.softmax(predictions[0]) -predicted_class_index = np.argmax(probabilities) + # Compare with actual + is_correct = predicted_label == random_class -# Output result -print(f"Predicted Pokémon: {class_names[predicted_class_index]}") -print(f"Confidence: {100 * probabilities[predicted_class_index]:.2f}%") + # Plot + ax = plt.subplot(2, 2, i + 1) + plt.imshow(img) + plt.axis("off") + plt.title( + f"Pred: {predicted_label}\n" + f"True: {random_class}\n" + f"{'Yes' if is_correct else 'No'} | {confidence:.1f}%", + fontsize=10 + ) +plt.tight_layout() plt.show()