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_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_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_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_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_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_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--')