Learning How To Classify Images Using Keras Part 3

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 174, 206, 10) 280
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 87, 103, 10) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 85, 101, 20) 1820
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 42, 50, 20) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 40, 48, 20) 3620
_________________________________________________________________
flatten (Flatten) (None, 38400) 0
_________________________________________________________________
dense (Dense) (None, 64) 2457664
_________________________________________________________________
dense_1 (Dense) (None, 16) 1040
_________________________________________________________________
dense_2 (Dense) (None, 4) 68
=================================================================
Total params: 2,464,492
Trainable params: 2,464,492
Non-trainable params: 0
_______________________________________________________________
explainer = lime_image.LimeImageExplainer(random_state=42)ncols = 5
nrows = 4
a, b = test_generator[0] # a is now equal to an array of all the images in the test generator
incorrects = np.nonzero(y_pred != test_generator.classes)[0] # list of all indecies our model got wrong
indecies = np.random.choice(incorrects, ncols) # Random selecting of wrongly predicted images. ncols is the how many images
class_name = ['Mild', 'Moderate', 'Normal', 'Very Mild']
fig, ax = plt.subplots(nrows, ncols, sharex='col', sharey='row')
fig.set_figwidth(20)
fig.set_figheight(14)
for j in range(nrows):
explanation = explainer.explain_instance(a[indecies][j],
model.predict,
top_labels=4, hide_color=0, num_samples=200,
random_seed=42)
ax[j,0].imshow(a[indecies][j])
ax[j,0].set_title(class_name[test_generator.classes[indecies][j]])
for i in range(ncols - 1):
temp, mask = explanation.get_image_and_mask(i, positive_only=True,
num_features=3, hide_rest=False)
ax[j,i+1].imshow(mark_boundaries(temp / 2 + 0.5, mask))
ax[j,i+1].set_title('p({}) = {:.4f}'.format(class_name[i], Y_pred[indecies[j]][i]))
plt.savefig(f'../report/figures/Lime_wrong_preds', dpi = 100)
cols = 5
nrows = 4
corrects = np.nonzero(y_pred == test_generator.classes)[0] # list of all indecies our model got wrong
correct_ind = corrects[[1,136,200,800]] # Selected images from each class. ncols is the how many images
fig, ax = plt.subplots(nrows, ncols, sharex='col', sharey='row')
fig.set_figwidth(20)
fig.set_figheight(14)
for j in range(nrows):
explanation = explainer.explain_instance(a[correct_ind][j],
model.predict,
top_labels=4, hide_color=0, num_samples=200,
random_seed=42)
ax[j,0].imshow(a[correct_ind][j])
ax[j,0].set_title(class_name[test_generator.classes[correct_ind][j]])
for i in range(ncols - 1):
temp, mask = explanation.get_image_and_mask(i, positive_only=True,
num_features=3, hide_rest=False)
ax[j,i+1].imshow(mark_boundaries(temp / 2 + 0.5, mask))
ax[j,i+1].set_title('p({}) = {:.4f}'.format(class_name[i], Y_pred[correct_ind[j]][i]))
plt.savefig(f'../report/figures/Lime_correct_preds', dpi = 100)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store