Skip to content

Instantly share code, notes, and snippets.

View therealnaveenkamal's full-sized avatar
🎯
Focusing

Naveenraj Kamalakannan therealnaveenkamal

🎯
Focusing
View GitHub Profile
@therealnaveenkamal
therealnaveenkamal / cxr_auc.py
Created July 22, 2021 06:48
This code is the gives the AUC score of each and every class of the Chest X-Ray
auc_vals = []
for i in range(len(labels)):
try:
gt = np.array(testgenerator.labels[:, i])
pred = predicted_vals[:,i]
gt = gt.astype('int64')
auc_vals.append(roc_auc_score(gt, pred))
fpr_rf, tpr_rf, _ = roc_curve(gt, pred)
plt.figure(1, figsize=(10, 10))
plt.plot([0, 1], [0, 1], 'k--')
@therealnaveenkamal
therealnaveenkamal / cxr_building_nn_model.py
Created July 22, 2021 06:13
This code is for building up the CXR Disease classification model using the Dense121 layer
model = DenseNet121(weights='densenet.hdf5', include_top=False)
model = Model(inputs=model.input, outputs=Dense(len(labels), activation="sigmoid")(GlobalAveragePooling2D()(model.output)))
model.compile(optimizer='adam', loss=calcloss(negative_freqs, positive_freqs))
fitter = model.fit(traingenerator, validation_data=valgenerator, steps_per_epoch = 1000, epochs = 50)
model.save_weights("cxr_naveen.h5")
plt.plot(fitter.history['loss'])
plt.ylabel("loss")
plt.xlabel("epoch")
plt.title("Training Loss Curve")
@therealnaveenkamal
therealnaveenkamal / cxr_customized_loss_function.py
Created July 22, 2021 05:54
This is a customized loss function for Chest X-Ray Multi-class classification problem addressing the class imbalance issue
def calcloss(positivewt, negativewt, al=1e-7):
def weighted_loss(y_true, y_pred):
loss = 0.0
for i in range(len(positivewt)):
loss += -((positivewt[i] * K.transpose(y_true)[i] * K.log(K.transpose(y_pred)[i] + al))+(negativewt[i]*(1 - K.transpose(y_true)[i])*K.log(1 - K.transpose(y_pred)[i] + al)))
return K.mean(loss)
return weighted_loss
@therealnaveenkamal
therealnaveenkamal / cxr_removing_class_imbalance.py
Created July 22, 2021 05:50
This code provides an effective solution to remove class imbalance problem
data = {
'Class': labels,
'Positive': positive_freqs*negative_freqs,
'Negative':negative_freqs*positive_freqs
}
X_axis = np.arange(len(labels))
fig, ax = plt.subplots(figsize=(10, 5))
ax.bar(X_axis-0.2, data['Positive'], width=0.4, color='b', label = "Positive")
ax.bar(X_axis+0.2, data['Negative'], width=0.4, color='r', label = 'Negative')
@therealnaveenkamal
therealnaveenkamal / cxr_class_imbalance.py
Created July 22, 2021 05:40
This code is to visualize the balance between the positive and the negative values of each class
positive_freqs = np.mean(traingenerator.labels, axis = 0)
negative_freqs = 1 - positive_freqs
data = {
'Class': labels,
'Positive': positive_freqs,
'Negative':negative_freqs
}
X_axis = np.arange(len(labels))
fig, ax = plt.subplots(figsize=(10, 5))
@therealnaveenkamal
therealnaveenkamal / cxr_imagedatagen.py
Created July 20, 2021 07:16
This code standardizes and normalizes the images using ImageDataGenerator Class of Keras to make the intensity mean 0 and intensity standard deviation 1
from keras.preprocessing.image import ImageDataGenerator
#To standardize the images, we use samplewise_center = True and samplewize_std_normalization = True
traingen = ImageDataGenerator(samplewise_center=True, samplewise_std_normalization= True)
traingenerator = traingen.flow_from_dataframe(
dataframe=trainset,
directory="images",
x_col="Image",
y_col= labels,
class_mode="raw",
@therealnaveenkamal
therealnaveenkamal / cxr_sample_image_analysis.py
Created July 20, 2021 06:40
This code takes in a random sample image, plots a histplot and analyzes the pixel intensities
num = np.random.randint(trainset.shape[0])
sample = plt.imread(os.path.join(img_dir,trainset.iloc[[num]]["Image"].values[0]))
plt.figure(figsize=(15, 15))
plt.title(dataframe[dataframe["Image Index"] == trainset.iloc[[num]]["Image"].values[0]].values[0][1])
plt.imshow(sample, cmap = 'gray')
plt.colorbar()
trainset.iloc[[num]]
print("Maximum Pixel Value: ", sample.max())
print("Minimum Pixel Value: ", sample.min())
@therealnaveenkamal
therealnaveenkamal / cxr_multiclassclassification_overlap.py
Created July 20, 2021 06:08
This code checks for the overlap of patients between train, validation and test dataset and removes the respective patients from their datasets.
def isOverlap(s1, s2):
total = set(s1).intersection(set(s2))
return [len(total), total]
def overlapcheck(trainset, valset, testset):
patid_train = []
patid_val = []
patid_test = []
for name in trainset['Image'].values:
patid_train.append(int(name.split("_")[0]))
@therealnaveenkamal
therealnaveenkamal / cxr_multiclassclassification_datasetsplit.py
Created July 20, 2021 05:13
This code manually splits up the dataset into train, validation and test with 20:4:1 as their respective ratios.
dataframe = pd.read_csv("Data_Entry_2017_v2020.csv")
#Enumerating all column names
columns = ["Image"]
for i in dataframe["Finding Labels"].values:
for j in i.split("|"):
if j not in columns:
columns.append(j)
labels = columns.copy()
labels.remove("Image")
@therealnaveenkamal
therealnaveenkamal / cxr_multiclassclassification.py
Last active July 22, 2021 05:37
Packages required for Chest X-Ray based Multi-Class Disease Classification using DenseNet121 - Transfer Learning Approach
import os
import cv2
import random
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
import matplotlib.pyplot as plt
from keras import backend as K