import tensorflow as tf import numpy as np import cv2 import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # This imports our pre-trained Keras model. # To troubleshoot model loading error, restart the kernel. Ensure Tensorflow 2.12 is installed. from tensorflow.keras.models import load_model model = load_model("/kaggle/input/muzzlecmpd268/tensorflow2/default/1/MuzzleCMPD268.keras", safe_mode=False) data_generator1 = tf.keras.preprocessing.image.ImageDataGenerator( ) test_ds = data_generator1.flow_from_directory( directory = "/kaggle/input/cmpd268/Split/test", target_size=(224, 224), color_mode="rgb", batch_size=10, class_mode="sparse", shuffle=True ) class_names = list(test_ds.class_indices.keys()) test_accuracy = model.evaluate(test_ds, verbose=1) print(test_accuracy) y_true = [] y_pred = [] confidences = [] test_root_directory = "/kaggle/input/cmpd268/Split/test" output_mismatch_path = f'/kaggle/working/misclassified.txt' output_match_path = f'/kaggle/working/classified.txt' # Ensure the Reports/SavedOutputs directory exists os.makedirs(os.path.dirname(output_mismatch_path), exist_ok=True) # Open both files for writing with open(output_mismatch_path, 'w') as mismatch_file, open(output_match_path, 'w') as match_file: for root, dirs, files in os.walk(test_root_directory): folder_name = os.path.basename(root) for file in files: if file.lower().endswith(('.jpg', '.jpeg', '.png', '.JPG')): # Case insensitive image_path = os.path.join(root, file) # Load and preprocess image img = tf.keras.utils.load_img(image_path) img = tf.image.resize(img, (224, 224)) # Adjust as needed img = tf.expand_dims(img, 0) # Add batch dimension # Get predictions predictions = model.predict(img) score = tf.nn.softmax(predictions[0]) predicted_class_index = np.argmax(score) predicted_class = class_names[predicted_class_index] # Store results y_true.append(folder_name) y_pred.append(predicted_class) confidence = 100 * np.max(score) confidences.append(confidence) log_entry = f"File: {image_path}, Folder: {folder_name}, Predicted: {predicted_class}, Confidence: {confidence:.2f}%\n" # Write to respective files if folder_name != predicted_class: mismatch_file.write(log_entry) print("Mismatch:", log_entry.strip()) else: match_file.write(log_entry) print("Match:", log_entry.strip()) print(f"Mismatched outputs saved to: {output_mismatch_path}") print(f"Matched outputs saved to: {output_match_path}") from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score accuracy = accuracy_score(y_true, y_pred) precision = precision_score(y_true, y_pred, average='weighted') recall = recall_score(y_true, y_pred, average='weighted') f1 = f1_score(y_true, y_pred, average='weighted') print(f"Accuracy: {accuracy}") print(f"Precision: {precision}") print(f"Recall: {recall}") print(f"F1 Score: {f1}")