In [3]:
import cv2
from matplotlib import pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array
import numpy as np
from tensorflow.keras.callbacks import ReduceLROnPlateau
import tensorflow as tf
import os, shutil
from sklearn.model_selection import train_test_split

from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications import MobileNetV2

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout, GlobalAveragePooling2D

from sklearn.metrics import classification_report, confusion_matrix, accuracy_score,precision_score,recall_score,f1_score
import seaborn as sns
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint

import random
from tensorflow.keras.models import load_model
2025-07-23 20:07:00.572461: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:467] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1753290420.624245  154260 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1753290420.642531  154260 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
W0000 00:00:1753290420.731307  154260 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1753290420.731364  154260 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1753290420.731383  154260 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
W0000 00:00:1753290420.731388  154260 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
2025-07-23 20:07:00.749692: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
/home/asir_l_aroof/anaconda3/envs/tf_gpu/lib/python3.12/site-packages/requests/__init__.py:86: RequestsDependencyWarning: Unable to find acceptable character detection dependency (chardet or charset_normalizer).
  warnings.warn(
In [2]:
def evaluate_model_new(model_name, model, generator, y_true, y_pred_classes):
    labels = list(generator.class_indices.keys())
    
    # Evaluation scores
    accuracy = accuracy_score(y_true, y_pred_classes)
    precision = precision_score(y_true, y_pred_classes, average='weighted')
    recall = recall_score(y_true, y_pred_classes, average='weighted')
    f1 = f1_score(y_true, y_pred_classes, average='weighted')
    confusion = confusion_matrix(y_true, y_pred_classes)

    print(f"Model: {model_name}")
    print("Accuracy:", accuracy)
    print("Precision:", precision)
    print("Recall:", recall)
    print("F1 Score:", f1)
    print("Classification Report:")
    print(classification_report(y_true, y_pred_classes, target_names=labels))

    # Confusion Matrix Plot
    plt.figure(figsize=(6, 5))
    sns.heatmap(confusion, annot=True, fmt='d', cmap='Blues', xticklabels=labels, yticklabels=labels)
    plt.xlabel('Predicted')
    plt.ylabel('True')
    plt.title('Confusion Matrix')
    plt.show()

    print("\nShowing ALL misclassified images with one correct from predicted class:")

    for idx, (true_label, pred_label) in enumerate(zip(y_true, y_pred_classes)):
        if true_label != pred_label:
            true_class_name = labels[true_label]
            pred_class_name = labels[pred_label]

            # Misclassified image path
            misclassified_img_path = generator.filepaths[idx]

            # Get random image from predicted class
            pred_class_paths = [p for i, p in enumerate(generator.filepaths) if y_true[i] == pred_label]
            random_pred_img_path = random.choice(pred_class_paths) if pred_class_paths else None

            # Plot
            fig, axs = plt.subplots(1, 2, figsize=(4, 2))  # small size

            # Misclassified image
            axs[0].imshow(plt.imread(misclassified_img_path))
            axs[0].set_title(f"Was: {true_class_name}\nPred: {pred_class_name}", fontsize=8)
            axs[0].axis('off')

            # Random image from predicted class
            if random_pred_img_path:
                axs[1].imshow(plt.imread(random_pred_img_path))
                axs[1].set_title(f"Example of {pred_class_name}", fontsize=8)
            else:
                axs[1].text(0.5, 0.5, "No image", ha='center', va='center')
                axs[1].set_title(f"No {pred_class_name}", fontsize=8)
            axs[1].axis('off')

            plt.tight_layout()
            plt.show()
In [8]:
from tensorflow.keras.applications.resnet50 import preprocess_input as resnet_preprocess
from tensorflow.keras.applications.inception_v3 import preprocess_input as inception_preprocess
from tensorflow.keras.applications.efficientnet import preprocess_input as efficientnet_preprocess


val_test_datagen = ImageDataGenerator(preprocessing_function=efficientnet_preprocess)
test_generator = val_test_datagen.flow_from_directory(
        "dataset_cropped/test",
        target_size=(224, 224),
        batch_size=16,
        class_mode='categorical',
        shuffle=False,
    )

val_test_datagen = ImageDataGenerator(preprocessing_function=resnet_preprocess)
test_generator2 = val_test_datagen.flow_from_directory(
        "dataset/test",
        target_size=(224, 224),
        batch_size=16,
        class_mode='categorical',
        shuffle=False,
    )

val_test_datagen = ImageDataGenerator(preprocessing_function=efficientnet_preprocess)
test_generator3 = val_test_datagen.flow_from_directory(
        "dataset_cropped_2/test",
        target_size=(224, 224),
        batch_size=16,
        class_mode='categorical',
        shuffle=False,
    )
Found 2753 images belonging to 32 classes.
Found 2867 images belonging to 32 classes.
Found 2753 images belonging to 32 classes.
In [10]:
#first atemt resnet50, unfroze 50 layers
from tensorflow.keras.models import load_model

new_resnet50_model = load_model('best_model.h5')

y_pred = new_resnet50_model.predict(test_generator2)
y_true = test_generator2.classes
y_pred_classes = np.argmax(y_pred, axis=1)

evaluate_model_new("EfficientNetB0", new_resnet50_model, test_generator2, y_true, y_pred_classes)
WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.
180/180 ━━━━━━━━━━━━━━━━━━━━ 36s 166ms/step
Model: EfficientNetB0
Accuracy: 0.8974537844436693
Precision: 0.8999788239166187
Recall: 0.8974537844436693
F1 Score: 0.8978338469210082
Classification Report:
              precision    recall  f1-score   support

         ain       0.97      0.97      0.97        90
          al       0.94      0.93      0.94        91
       aleff       0.96      0.91      0.94        90
          bb       0.91      0.91      0.91        90
         dal       0.93      0.84      0.88        81
         dha       0.95      0.98      0.96        91
        dhad       0.95      0.94      0.95        88
          fa       0.79      0.75      0.77        91
        gaaf       0.70      0.79      0.74        90
       ghain       0.99      0.93      0.96        91
          ha       0.93      0.85      0.89        91
         haa       0.85      0.89      0.87        90
        jeem       0.86      0.90      0.88        90
        kaaf       0.88      0.93      0.90        90
        khaa       0.89      0.93      0.91        91
          la       0.86      0.86      0.86        91
        laam       0.81      0.90      0.85        89
        meem       0.91      0.78      0.84        91
         nun       0.86      0.94      0.90        85
          ra       0.93      0.87      0.90        86
        saad       0.98      0.97      0.97        91
        seen       0.96      0.90      0.93        91
       sheen       0.97      0.95      0.96        91
          ta       0.88      0.84      0.86        91
         taa       0.86      0.91      0.89        90
        thaa       0.85      0.87      0.86        91
        thal       0.85      0.93      0.89        91
        toot       0.91      0.91      0.91        91
         waw       0.99      0.92      0.95        83
          ya       0.94      0.92      0.93        90
         yaa       0.91      0.93      0.92        91
         zay       0.83      0.87      0.85        89

    accuracy                           0.90      2867
   macro avg       0.90      0.90      0.90      2867
weighted avg       0.90      0.90      0.90      2867

No description has been provided for this image
Showing ALL misclassified images with one correct from predicted class:
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image