I have built a CNN model to classify images on either two categories, “Pneumonia” and “Normal”. The data i fed are from chest X-ray images converted to a hdf5 file then loaded into a numpy array. The original size of images varied quite a lot so i resized them as 1024 x 1024 and its in greyscale. the training set has 460 images per class for training and 230 images per class for testing. The overall accuracy seems to be just better than a random guess. I also added data augmentation to improve the accuracy but it doesn’t seem to work.
I have to submit a model with 85% accuracy atleast for my paper but the model isn’t getting better even with some tweeks i do.
This is my code for the model.
I do have more than 1000 images for training the model but its taking too much time to train one that i’m not sure would even work.
this is my imports, they have a lot of unused libraries.
import os
import h5py
import numpy as np
from skimage.transform import resize
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Input, Dropout
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report, roc_curve, roc_auc_score, precision_recall_curve, average_precision_score
import seaborn as sns
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
from tensorflow.keras.callbacks import EarlyStopping
import Augmentor
from PIL import Image
takes a hdf5 file reads the data and load the entire data into the dataset.
# Load data from HDF5
def load_data_from_hdf5(file_path, dataset_name):
with h5py.File(file_path, 'r') as hdf:
dataset = hdf[dataset_name][:]
return dataset
# Load data
train_normal = load_data_from_hdf5('/Users/shivaram/Documents/Shiva/Personal_Docs/Amity_Docs/NTCC/CNN/chest_xray_hdf5/train_normal.h5', 'images')
train_pneumonia = load_data_from_hdf5('/Users/shivaram/Documents/Shiva/Personal_Docs/Amity_Docs/NTCC/CNN/chest_xray_hdf5/train_pneumonia.h5', 'images')
test_normal = load_data_from_hdf5('/Users/shivaram/Documents/Shiva/Personal_Docs/Amity_Docs/NTCC/CNN/chest_xray_hdf5/test_normal.h5', 'images')
test_pneumonia = load_data_from_hdf5('/Users/shivaram/Documents/Shiva/Personal_Docs/Amity_Docs/NTCC/CNN/chest_xray_hdf5/test_pneumonia.h5', 'images')
# Resize and normalize images
def resize_normalize_images(images, target_size=(1024,1024)):
resized_images = np.array([resize(img, target_size) for img in images])
return resized_images.astype('float32') / 255.0
# take a data set and takes each row at a time and resize it to target shape.
# return a array of the data with each value between0-1.
X_train_normal = resize_normalize_images(train_normal[:460])
X_train_pneumonia = resize_normalize_images(train_pneumonia[:460])
X_test_normal = resize_normalize_images(test_normal[:230])
X_test_pneumonia = resize_normalize_images(test_pneumonia[:230])
X_train = np.concatenate((X_train_normal, X_train_pneumonia), axis=0)
X_test = np.concatenate((X_test_normal, X_test_pneumonia), axis=0)
# concatenates data from bothe the classes.
y_train = np.concatenate((np.zeros(len(X_train_normal)), np.ones(len(X_train_pneumonia))), axis=0)
y_test = np.concatenate((np.zeros(len(X_test_normal)), np.ones(len(X_test_pneumonia))), axis=0)
# Convert labels to categorical
y_train_categorical = to_categorical(y_train)
y_test_categorical = to_categorical(y_test)
# creates a matrix with class labels as columns and rows as specific instance of the dataset
# Create the model
def create_model(input_shape, num_classes=2):
model = Sequential([
Conv2D(16, (3, 3), activation='relu', input_shape=input_shape),
MaxPooling2D((2, 2)),
Conv2D(32, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(num_classes, activation='softmax')
])
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
input_shape = [1024,1024,1]
def augment_image(image):
# Expand dimensions to (height, width, channels) for compatibility with tf.image functions
image = tf.expand_dims(image, axis=-1)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.random_flip_left_right(image)
image = tf.image.random_flip_up_down(image)
image = tf.image.random_brightness(image, max_delta=0.2)
image = tf.image.random_contrast(image, lower=0.8, upper=1.2)
image = tf.squeeze(image, axis=-1)
return image
kf = KFold(n_splits=5, shuffle=True, random_state=42)
early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
tf.config.run_functions_eagerly(True)
tf.data.experimental.enable_debug_mode()
train_accs = []
val_accs = []
# K-fold cross-validation
for train_index, val_index in kf.split(X_train):
X_train_fold, X_val_fold = X_train[train_index], X_train[val_index]
y_train_fold, y_val_fold = y_train_categorical[train_index], y_train_categorical[val_index]
tf.keras.backend.clear_session()
model = create_model(input_shape=(1024, 1024, 1))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
def augment_data(images, labels):
augmented_images = []
for img in images:
augmented_img = augment_image(img)
augmented_images.append(augmented_img)
return np.array(augmented_images), labels # No change to labels
X_train_augmented, y_train_augmented = augment_data(X_train_fold, y_train_fold)
X_train_augmented = X_train_augmented.reshape((-1, 1024, 1024, 1))
X_val_fold = X_val_fold.reshape((-1, 1024, 1024, 1))
batch_size = 128
history = model.fit(X_train_augmented, y_train_augmented,
batch_size=batch_size,
epochs=10,
validation_data=(X_val_fold, y_val_fold),
callbacks=[early_stopping])
train_accs.append(history.history['accuracy'])
val_accs.append(history.history['val_accuracy'])
train_accs_flat = [item for sublist in train_accs for item in sublist]
val_accs_flat = [item for sublist in val_accs for item in sublist]
avg_train_acc = np.mean(train_accs_flat, axis=0)
avg_val_acc = np.mean(val_accs_flat, axis=0)
plt.figure(figsize=(10, 6))
plt.plot(avg_train_acc, label='Training Accuracy')
plt.plot(avg_val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
Most of this code i learnt from books and old research papers from seniors. This is my first ML project so i am very confused. Any help would be greatly appreciated. I am using Jupyter Notebook in a virtual env and using CPU since i don’t know how to use GPU. I also tried to use zero padding the images instead of resizing but it took too much time for that.