CNN: Image Classifier: Python Error

I am using the image classifier provided in the first session of Part 1 to identify various flowers types.

The data set was downloaded from

I randomly chose 5 images from each flower type for validation and testing.

I was able to train the model but I get error while printing the incorrect flower type.

The error looks like a python array initialization error but I cant locate where exactly is the problem.

The data set I use has 5 types of flowers (I just reused the code used for cats and dogs training)

Below is the section I use for creating the chart

preds = np.argmax(log_preds, axis=1)
probs = np.exp(log_preds[:,1])
def rand_by_mask(mask): return np.random.choice(np.where(mask)[0], min(len(preds), 4), replace=False)
def rand_by_correct(is_correct): return rand_by_mask((preds == data.val_y)==is_correct)

def plots(ims, figsize=(12,6), rows=1, titles=None):
f = plt.figure(figsize=figsize)
for i in range(len(ims)):
sp = f.add_subplot(rows, len(ims)//rows, i+1)
if titles is not None: sp.set_title(titles[i], fontsize=16)

def load_img_id(ds, idx): return np.array([idx]))

def plot_val_with_title(idxs, title):
imgs = [load_img_id(data.val_ds,x) for x in idxs]
title_probs = [probs[x] for x in idxs]
return plots(imgs, rows=1, titles=title_probs, figsize=(16,8)) if len(imgs)>0 else print(‘Not Found.’)

def most_by_mask(mask, mult):
idxs = np.where(mask)[0]
return idxs[np.argsort(mult * probs[idxs])[:5]]

def most_by_correct(y, is_correct):
mult = -1 if (y==1)==is_correct else 1
return most_by_mask(((preds == data.val_y)==is_correct) & (data.val_y == y), mult)


When I execute the following command, it errors out

plot_val_with_title(most_by_correct(1, False), “Most incorrect”)


IndexError Traceback (most recent call last)
----> 1 plot_val_with_title(most_by_correct(1, False), “Most incorrect”)

in most_by_correct(y, is_correct)
27 def most_by_correct(y, is_correct):
28 mult = -1 if (y==1)==is_correct else 1
—> 29 return most_by_mask(((preds == data.val_y)==is_correct) & (data.val_y == y), mult)
30 #

in most_by_mask(mask, mult)
23 def most_by_mask(mask, mult):
24 idxs = np.where(mask)[0]
—> 25 return idxs[np.argsort(mult * probs[idxs])[:5]]
26 #
27 def most_by_correct(y, is_correct):

IndexError: index 5 is out of bounds for axis 0 with size 5