This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
NewerOlder