File size: 6,178 Bytes
3fde58f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# -------------------------
# 1. Import Libraries
# -------------------------
import os
import glob
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
import itertools
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model, load_model
from keras.layers import Dense, Dropout
from keras.applications import ResNet50
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing import image
import warnings
warnings.filterwarnings('ignore')
# -------------------------
# 2. Load Dataset
# -------------------------
file_path = 'dataset'
# List all classes
name_class = os.listdir(file_path)
print("Classes:", name_class)
# Get all filepaths
filepaths = list(glob.glob(file_path + '/**/*.*'))
print(f"Total images found: {len(filepaths)}")
# Extract labels
labels = list(map(lambda x: os.path.split(os.path.split(x)[0])[1], filepaths))
# Create DataFrame
filepath_series = pd.Series(filepaths, name='Filepath').astype(str)
labels_series = pd.Series(labels, name='Label')
data = pd.concat([filepath_series, labels_series], axis=1)
data = data.sample(frac=1).reset_index(drop=True) # shuffle
print(data.head())
# -------------------------
# 3. EDA (Exploratory Data Analysis)
# -------------------------
# Class distribution
plt.figure(figsize=(8,5))
sns.countplot(x='Label', data=data, order=data['Label'].value_counts().index)
plt.title('Number of Images per Class')
plt.xticks(rotation=45)
plt.show()
# Check image dimensions
sample_img = plt.imread(data['Filepath'][0])
print(f"Sample image shape: {sample_img.shape}")
# Visualize few images
fig, axes = plt.subplots(2, 4, figsize=(12,6))
for ax, (img_path, label) in zip(axes.flatten(), zip(data['Filepath'][:8], data['Label'][:8])):
img = plt.imread(img_path)
ax.imshow(img)
ax.set_title(label)
ax.axis('off')
plt.tight_layout()
plt.show()
# -------------------------
# 4. Train-Test Split
# -------------------------
train, test = train_test_split(data, test_size=0.2, random_state=42)
print(f"Training samples: {len(train)}, Testing samples: {len(test)}")
# -------------------------
# 5. Data Preprocessing and Augmentation
# -------------------------
train_datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
rotation_range=20,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
horizontal_flip=True
)
test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
train_gen = train_datagen.flow_from_dataframe(
dataframe=train,
x_col='Filepath',
y_col='Label',
target_size=(256, 256),
class_mode='categorical',
batch_size=32,
shuffle=True,
seed=42
)
valid_gen = test_datagen.flow_from_dataframe(
dataframe=test,
x_col='Filepath',
y_col='Label',
target_size=(256, 256),
class_mode='categorical',
batch_size=32,
shuffle=False
)
# -------------------------
# 6. Model Building (Transfer Learning with ResNet50)
# -------------------------
pretrained_model = ResNet50(
input_shape=(256, 256, 3),
include_top=False,
weights='imagenet',
pooling='avg'
)
pretrained_model.trainable = False
x = Dense(128, activation="relu")(pretrained_model.output)
x = Dropout(0.5)(x)
x = Dense(128, activation="relu")(x)
outputs = Dense(8, activation='softmax')(x)
model = Model(inputs=pretrained_model.input, outputs=outputs)
model.compile(optimizer="adam", loss='categorical_crossentropy', metrics=['accuracy'])
# -------------------------
# 7. Model Training
# -------------------------
history = model.fit(
train_gen,
validation_data=valid_gen,
epochs=20,
)
# -------------------------
# 8. Training Curves
# -------------------------
pd.DataFrame(history.history)[['accuracy', 'val_accuracy']].plot()
plt.title('Training vs Validation Accuracy')
plt.show()
pd.DataFrame(history.history)[['loss', 'val_loss']].plot()
plt.title('Training vs Validation Loss')
plt.show()
# -------------------------
# 9. Evaluation and Confusion Matrix
# -------------------------
# Evaluate
results = model.evaluate(valid_gen, verbose=0)
print(f"Test Loss: {results[0]:.5f}")
print(f"Test Accuracy: {results[1]*100:.2f}%")
# Predictions
predictions = model.predict(valid_gen)
y_pred = np.argmax(predictions, axis=1)
# True labels
y_true = valid_gen.classes
# Labels Mapping
labels_map = train_gen.class_indices
labels_map = dict((v,k) for k,v in labels_map.items())
# Classification report
print(classification_report(y_true, y_pred, target_names=list(labels_map.values())))
# Confusion Matrix
cm = confusion_matrix(y_true, y_pred)
plt.figure(figsize=(8,6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=list(labels_map.values()),
yticklabels=list(labels_map.values()))
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
plt.show()
# -------------------------
# 10. Save the Model
# -------------------------
model.save("model_blood_group_detection_resnet.h5")
print("Model saved successfully!")
# -------------------------
# 11. Single Image Prediction Example
# -------------------------
# Load model
model = load_model('model_blood_group_detection_resnet.h5')
# Single image prediction
img_path = 'C:\Users\ADMIN\Documents\SEM-6\DL PROJECT\dataset\AB+\augmented_cluster_4_4.BMP' # Update this path as needed
img = image.load_img(img_path, target_size=(256, 256))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
predicted_class = np.argmax(preds)
confidence = preds[0][predicted_class] * 100
# Get label
predicted_label = labels_map[predicted_class]
# Show image
plt.imshow(image.array_to_img(img))
plt.axis('off')
plt.title(f"Prediction: {predicted_label} ({confidence:.2f}%)")
plt.show()
|