diff --git a/Chapter09/BugNIST2D_overview.py b/Chapter09/BugNIST2D_overview.py new file mode 100644 index 0000000000000000000000000000000000000000..bd57102da1f7a5e1bc396ed8ab45e92658230295 --- /dev/null +++ b/Chapter09/BugNIST2D_overview.py @@ -0,0 +1,43 @@ +#%% + +import os +import numpy as np + +path = '../02506/data/bugNIST2D/' # path to unzipped data directory +train_filenames = sorted(os.listdir(path + 'train')) +train_targets = np.loadtxt(path + 'train_targets.txt', dtype=int) + + +# %% +import matplotlib.pyplot as plt +import PIL.Image + +rg = np.random.default_rng() + +class_names = [ + 'AC: brown cricket', 'BC: black cricket', 'BF: blow fly', + 'BL: buffalo beetle larva', 'BP: blow fly pupa', 'CF: curly-wing fly', + 'GH: grasshopper', 'MA: maggot', 'ML: mealworm', + 'PP: green bottle fly pupa', 'SL: soldier fly larva', 'WO: woodlice' + ] + +for i in range(12): + + fig, ax = plt.subplots(1, 8, figsize=(15, 5)) + + this_class = np.where(train_targets == i)[0] + random_subset = sorted(rg.choice(this_class, 8, replace=False)) + + for j in range(8): + + filename = train_filenames[random_subset[j]] + image = PIL.Image.open(path + 'train/' + filename) + + ax[j].imshow(image) + ax[j].set_title(filename) + + fig.suptitle(f'Class {i} ({class_names[i]})') + plt.show() + + +# %%