def plot_val_with_title(idxs, title): imgs = np.stack([data.val_ds[x][0] for x in idxs]) title_probs = [probs[x] for x in idxs] print(title) return plots(data.val_ds.denorm(imgs), rows=1, titles=title_probs)
def plotXY(x, y): plots([x]) plt.gca().add_patch(create_rect(y))
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] print(title) return plots(imgs, rows=1, titles=title_probs, figsize=(16, 8))
plotXY(x, y) # ## Scale # In[14]: xx, yy = Scale(sz=350, tfm_y=TfmType.COORD)(x, y) # In[15]: plotXY(xx, yy) # In[16]: xx, yy = Scale(sz=350, tfm_y=TfmType.PIXEL)(x, x) plots([xx, yy]) # ## RandomScale # In[17]: xx, yy = RandomScale(sz=350, max_zoom=1.1, tfm_y=TfmType.COORD)(x, y) plotXY(xx, yy) print(yy) print(y) # In[18]: xx, yy = RandomScale(sz=350, max_zoom=1.1, tfm_y=TfmType.PIXEL)(x, x) plots([xx, yy])
classes=['dogs', 'cats'], test_name='test', tfms=tfms, num_workers=1, bs=2) x, _ = next(iter(data.aug_dl)) return data.trn_ds.denorm(x)[1] # In[ ]: ims = np.stack([get_augs() for i in range(6)]) # In[ ]: plots(ims, rows=2) # Let's create a new `data` object that includes this augmentation in the transforms. # In[ ]: data = ImageClassifierData.from_names_and_array(path=PATH, fnames=fnames, y=labels, classes=['dogs', 'cats'], test_name='test', tfms=tfms) learn = ConvLearner.pretrained(arch, data, precompute=True, tmp_name=TMP_PATH,
def plot_aug(tfms): ims = np.stack([get_augs(tfms) for i in range(6)]) plots(ims, rows=2)
#%% def plots(ims, figsize=(12, 6), rows=2, titles=None): f = plt.figure(figsize=figsize) cols = len(ims) // rows for i in range(len(ims)): sp = f.add_subplot(rows, cols, i + 1) sp.axis('Off') if titles is not None: sp.set_title(titles[i], fontsize=16) plt.imshow(ims[i], cmap='gray') #%% plots(imgs[:8], titles=[ class_names[test_preds[0]], class_names[test_preds[1]], class_names[test_preds[2]], class_names[test_preds[3]], class_names[test_preds[4]], class_names[test_preds[5]], class_names[test_preds[6]], str(data.classes[preds]) ]) #%% #Predict on test set This step is under construction... acc = 0 test_log_preds = np.zeros([7, 60, 7]) preds_test = np.zeros([7, 60, 7]) for insects in data.classes: data = ImageClassifierData.from_paths(PATH, bs=bsz, tfms=tfms_from_model(arch, sz), test_name='test/' + insects) learn = ConvLearner.pretrained(arch, data) test_log_preds[[data.classes.index(insects)]] = learn.predict(is_test=True)
# We can look at part of an image: # In[19]: x_imgs[0, 10:15, 10:15] # In[20]: show(x_imgs[0, 10:15, 10:15]) # In[21]: plots(x_imgs[:8], titles=[ class_names[y_valid[0]], class_names[y_valid[1]], class_names[y_valid[2]], class_names[y_valid[3]], class_names[y_valid[4]], class_names[y_valid[5]], class_names[y_valid[6]], class_names[y_valid[7]] ]) # In[14]: from fastai.metrics import * from fastai.model import * from fastai.dataset import * from fastai.plots import * import torch.nn as nn # We will begin with the highest level abstraction: using a neural net defined by PyTorch's Sequential class. #%%