Training pipeline
import os
os.environ['WANDB_NOTEBOOK_NAME'] = 'pokemon_image_classification.ipynb'
wandb.init(project='pokemon_image_classification')
path = Path('../../data/pokemon')
files = get_image_files(path)
def img2rgb(img):
return img.convert('RGB')
datasets = Datasets(
files,
tfms=[[PILImage.create, img2rgb], [parent_label, Categorize]],
splits=RandomSplitter()(files)
)
dataloaders = datasets.dataloaders(
after_item=[ToTensor, RandomResizedCrop(224)],
after_batch=[IntToFloatTensor],
bs=128,
)
dataloaders.show_batch()
model = nn.Sequential(OrderedDict([
('body', create_body(xresnet50, cut=-4)),
('head', create_head(2048, 151, lin_ftrs=[2048]))
]))
learner = Learner(
dataloaders,
model,
opt_func=Adam,
loss_func=CrossEntropyLossFlat(),
metrics=[accuracy, top_k_accuracy],
# cbs=[SaveModelCallback, WandbCallback],
cbs=[SaveModelCallback, ShowGraphCallback]
)
@patch
def show_activations(self: Learner, img):
img = Path(img)
pipeline = Pipeline([PILImage.create, ToTensor, Resize(224, method='squish'), IntToFloatTensor])
x = pipeline(img).unsqueeze(0)
# y, name = img.parent.name.split('-')
# y = int(y)
y = 0
name = 'Farfetchd'
with HookBwd(learner.model.body) as hookg:
with Hook(learner.model.body) as hook:
output = learner.model.eval()(x)
act = hook.stored
output[0,y].backward()
grad = hookg.stored
w = grad[0].mean(dim=[1,2], keepdim=True)
cam_map = (w * act[0]).sum(0)
_,ax = plt.subplots()
x[0].show(ctx=ax)
ax.title.set_text(name)
ax.imshow(cam_map.detach().cpu(), alpha=0.80, extent=(0,224,224,0),
interpolation='sinc', cmap='magma_r')
learner.show_activations(files[0])
learner.show_results()
interp = ClassificationInterpretation.from_learner(learner)
interp.plot_top_losses(4, figsize=(9,6))