File size: 6,289 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 |
import os
import glob
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from keras_preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten
from keras.applications import ResNet50
from keras.applications.resnet50 import preprocess_input
from sklearn.metrics import classification_report
import zipfile
import os
# Define the file name
zip_file = 'dataset.zip'
# Unzip it to a folder (you can choose your own target directory)
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
zip_ref.extractall('blood_group_dataset') # Extract to this folder
# Walk through the directory
for root, dirs, files in os.walk('blood_group_dataset'):
print(root)
for file in dirs:
print(' ', file)
import os
import glob
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Walk through the directory and collect file paths and labels
filepaths = []
labels = []
for root, dirs, files in os.walk('blood_group_dataset'):
for dir in dirs: # Iterate through subdirectories (blood group types)
for file in glob.glob(os.path.join(root, dir, '*')): # Get all files in the subdirectory
filepaths.append(file)
labels.append(dir) # Use the subdirectory name as the label
# Create a DataFrame with file paths and labels
filepath = pd.Series(filepaths, name='Filepath').astype(str)
Labels = pd.Series(labels, name='Label')
data = pd.concat([filepath, Labels], axis=1)
data = data.sample(frac=1).reset_index(drop=True)
# Filter out the 'dataset' label
filtered_data = data[data['Label'] != 'dataset'] # Remove rows with 'dataset' label
# Visualize class distribution using sns.barplot
counts = filtered_data.Label.value_counts()
sns.barplot(x=counts.index, y=counts)
plt.xlabel('Blood Group Type') # Changed x-axis label
plt.ylabel('Number of Images') # Added y-axis label
plt.xticks(rotation=90)
plt.title('Class Distribution in Blood Group Dataset') # Added title
plt.show()
# Split data into training and testing sets
train, test = train_test_split(data, test_size=0.20, random_state=42)
# Visualize some images from the dataset
fig, axes = plt.subplots(nrows=5, ncols=3, figsize=(10, 8), subplot_kw={'xticks': [], 'yticks': []})
for i, ax in enumerate(axes.flat):
ax.imshow(plt.imread(data.Filepath[i]))
ax.set_title(data.Label[i])
plt.tight_layout()
plt.show()
# Set up ImageDataGenerator for training and validation data
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
train_gen = train_datagen.flow_from_dataframe(
dataframe=train,
x_col='Filepath',
y_col='Label',
target_size=(224, 224), # Adjusted to match ResNet50 input size
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=(224, 224), # Adjusted to match ResNet50 input size
class_mode='categorical',
batch_size=32,
shuffle=False,
seed=42
)
# Define the LeNet model
model = Sequential([
Conv2D(6, kernel_size=(5, 5), activation='relu', input_shape=(224, 224, 3)),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(16, kernel_size=(5, 5), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(120, activation='relu'),
Dense(84, activation='relu'),
Dense(8, activation='softmax')
])
model.compile(
optimizer="adam",
loss='categorical_crossentropy',
metrics=['accuracy']
)
# Train the model
history = model.fit(
train_gen,
validation_data=valid_gen,
epochs=20
)
# Plot training history: accuracy
pd.DataFrame(history.history)[['accuracy', 'val_accuracy']].plot()
plt.title("Accuracy")
plt.show()
# Plot training history: loss
pd.DataFrame(history.history)[['loss', 'val_loss']].plot()
plt.title("Loss")
plt.show()
# Evaluate the model on test data
results = model.evaluate(valid_gen, verbose=0)
print(f"Test Loss: {results[0]:.5f}")
print(f"Test Accuracy: {results[1]*100:.2f}%")
# Predict labels for test data
pred = model.predict(valid_gen)
pred = np.argmax(pred, axis=1)
# Map predicted labels
labels = train_gen.class_indices
labels = dict((v, k) for k, v in labels.items())
pred = [labels[k] for k in pred]
# Compare predicted labels with true labels and print classification report
# Get the true labels from the test DataFrame, ensuring they match the predictions in length
y_test = list(test.Label)
# Adjust y_test to match pred length
y_test = y_test[:len(pred)] # Truncate y_test to match pred length
print(classification_report(y_test, pred))
model.save("model_blood_group_detection_lenet.keras")
import numpy as np
import matplotlib.pyplot as plt
from keras.models import load_model
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
# Load the pre-t rained model
model = load_model('model_blood_group_detection_lenet.keras')
# Define the class labels
labels = {'A+': 0, 'A-': 1, 'AB+': 2, 'AB-': 3, 'B+': 4, 'B-': 5, 'O+': 6, 'O-': 7}
labels = dict((v, k) for k, v in labels.items())
# Example of loading a single image and making a prediction
img_path = 'augmented_cluster_4_3505.BMP'
# Preprocess the image accordingly (check the model's expected input dimensions)
img = image.load_img(img_path, target_size=(224, 224)) # Example target size for AlexNet (224x224)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x) # Ensure this matches the model's preprocessing function
# Make prediction
result = model.predict(x)
predicted_class = np.argmax(result) # Get the predicted class index
# Map the predicted class to the label
predicted_label = labels[predicted_class]
confidence = result[0][predicted_class] * 100 # Confidence level
# Display the image
plt.imshow(image.array_to_img(image.img_to_array(img) / 255.0))
plt.axis('off') # Hide axes
# Display the prediction and confidence below the image
plt.title(f"Prediction: {predicted_label} with confidence {confidence:.2f}%")
plt.show()
|