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