Inverse plot_top_losses()

Hey all! Is there a convenient way to flip around the plot_top_losses() function and instead show the lowest losses? I’m trying to understand where my model performs the best and it would be great to get a similar visual representation of the best predictions, just like you get with plot_top_losses() for the worst predictions as shown in lesson 2.

In case anyone else is looking for an answer, here’s a non-fast.ai hack to do this when you’ve created dataloaders from a dataframe (not quite as pretty though):

# Get the validation dataframe
df_valid = dls.valid.items.copy()

# Add columns for the probabilities and extract the predicted class from it
# In my case I have 2 classes
df_valid['prob 0'] = preds[0][:,0]
df_valid['prob 1'] = preds[0][:,1]
df_valid['pred'] = df_valid[['prob 0','prob 1']].values.argmax(axis=1)

# Get the probability of the predicted class
df_valid['prob max'] = df_valid[['prob 0','prob 1']].max(axis=1)

# Create separate dataframes for the best and the worst predictions (>0.7 and right / wrong)
best = df_valid.loc[(df_valid['prob max']>0.7) & (df_valid['pred']==0) & (df_valid['pred']==df_valid['label'])]
worst = df_valid.loc[(df_valid['prob max']>0.7) & (df_valid['pred']==0) & (df_valid['pred']!=df_valid['label'])]

# create figure to plot the images
fig = plt.figure(figsize=(10, 10))
columns = 4
rows = 4

# reading images
imgs = []
for i in range(columns * rows):
  # Open a random file from best/worst dataframe and add in subplots
  fn = np.random.choice(best['fn']) 
  im = PIL.Image.open(fn)
  fig.add_subplot(rows, columns, i+1)
  plt.imshow(im)

Hey,
plot_top_losses takes the parameter largest which sorts by largest or smallest values.
interp.plot_top_losses(k=6, largest=False) should do what you want :wink:

1 Like