Integration with tensorboard

First thing first, you need to install tensorboard with

pip install tensorboard

Then launch tensorboard with

tensorboard --logdir=runs

in your terminal. You can change the logdir as long as it matches the log_dir you pass to TensorBoardCallback (default is runs in the working directory).

Tensorboard Embedding Projector support

Tensorboard Embedding Projector is currently only supported for image classification

Export Embeddings during Training

Tensorboard Embedding Projector is supported in TensorBoardCallback (set parameter projector=True) during training. The validation set embeddings will be written after each epoch.

cbs = [TensorBoardCallback(projector=True)]
learn = cnn_learner(dls, resnet18, metrics=accuracy, cbs=cbs)

Export Embeddings for a custom dataset

To write the embeddings for a custom dataset (e. g. after loading a learner) use TensorBoardProjectorCallback. Add the callback manually to the learner.

learn = load_learner('path/to/export.pkl')
learn.add_cb(TensorBoardProjectorCallback())
dl = learn.dls.test_dl(files, with_labels=True)
_ = learn.get_preds(dl=dl)

If using a custom model (non fastai-resnet) pass the layer where the embeddings should be extracted as a callback-parameter.

layer = learn.model[1][1]
learn.add_cb(TensorBoardProjectorCallback(layer=layer))

class TensorBoardBaseCallback[source]

TensorBoardBaseCallback() :: Callback

Basic class handling tweaks of the training loop by changing a Learner in various events

class TensorBoardCallback[source]

TensorBoardCallback(log_dir=None, trace_model=True, log_preds=True, n_preds=9, projector=False, layer=None) :: TensorBoardBaseCallback

Saves model topology, losses & metrics

class TensorBoardProjectorCallback[source]

TensorBoardProjectorCallback(log_dir=None, layer=None) :: TensorBoardBaseCallback

Saves Embeddings for Tensorboard Projector

Test

from fastai.vision.all import Resize, RandomSubsetSplitter, aug_transforms, cnn_learner, resnet18

TensorBoardCallback

path = untar_data(URLs.PETS)

db = DataBlock(blocks=(ImageBlock, CategoryBlock), 
                  get_items=get_image_files, 
                  item_tfms=Resize(128),
                  splitter=RandomSubsetSplitter(train_sz=0.1, valid_sz=0.01),
                  batch_tfms=aug_transforms(size=64),
                  get_y=using_attr(RegexLabeller(r'(.+)_\d+.*$'), 'name'))

dls = db.dataloaders(path/'images')
learn = cnn_learner(dls, resnet18, metrics=accuracy)
learn.unfreeze()
learn.fit_one_cycle(3, cbs=TensorBoardCallback(Path.home()/'tmp'/'runs', trace_model=True))
epoch train_loss valid_loss accuracy time
0 5.085115 5.673248 0.123288 00:14
1 4.300926 4.160697 0.164384 00:14
2 3.805664 3.333386 0.164384 00:16

Projector

Projector in TensorBoardCallback

path = untar_data(URLs.PETS)
db = DataBlock(blocks=(ImageBlock, CategoryBlock), 
                  get_items=get_image_files, 
                  item_tfms=Resize(128),
                  splitter=RandomSubsetSplitter(train_sz=0.05, valid_sz=0.01),
                  batch_tfms=aug_transforms(size=64),
                  get_y=using_attr(RegexLabeller(r'(.+)_\d+.*$'), 'name'))

dls = db.dataloaders(path/'images')
cbs = [TensorBoardCallback(log_dir=Path.home()/'tmp'/'runs', projector=True)]
learn = cnn_learner(dls, resnet18, metrics=accuracy, cbs=cbs)
learn.unfreeze()
learn.fit_one_cycle(3)
epoch train_loss valid_loss accuracy time
0 4.940275 6.841114 0.041096 00:08
1 4.543565 5.886593 0.054795 00:08
2 4.128808 4.730549 0.082192 00:08

TensorBoardProjectorCallback

path = untar_data(URLs.PETS)
db = DataBlock(blocks=(ImageBlock, CategoryBlock), 
                  get_items=get_image_files, 
                  item_tfms=Resize(128),
                  splitter=RandomSubsetSplitter(train_sz=0.1, valid_sz=0.01),
                  batch_tfms=aug_transforms(size=64),
                  get_y=using_attr(RegexLabeller(r'(.+)_\d+.*$'), 'name'))

dls = db.dataloaders(path/'images')
files = get_image_files(path/'images')
files = files[:256]
learn = cnn_learner(dls, resnet18, metrics=accuracy)
learn.add_cb(TensorBoardProjectorCallback(log_dir=Path.home()/'tmp'/'runs'))
<fastai.learner.Learner at 0x7f833bfbae80>
dl = learn.dls.test_dl(files, with_labels=True)
_ = learn.get_preds(dl=dl)

Validate results in tensorboard

Run the following command in the command line to check if the projector embeddings have been correctly wirtten:

tensorboard --logdir=~/tmp/runs

Open http://localhost:6006 in browser (TensorBoard Projector doesn't work correctly in Safari!)