Jovian
⭐️
Sign In

Slack notifications for FastAI

Get slack notifications for FastAI model training. fastai_slack provides a simple callback to receive Slack notifcations while training FastAI models, with just one extra line of code.

fastai_slack sends notifications to your chosen Slack workspace & channel for the following events:

  • Start of training
  • Losses and metrics at the end of every epoch (or every few epochs)
  • End of training
  • Exceptions that occur during training (with stack trace)

Installation and Setup

  1. Install the fastai_slack library using pip:
pip install fastai_slack
  1. Generate a Slack incoming webhook URL, which will allow you to send notifications to a Slack channel of your choice. More details here: https://api.slack.com/incoming-webhooks

The webhook URL should be kept secret, so fastai_slack provides a helpful read_webhook_url method to safely input webhook URL within Jupyter.

In [1]:
from fastai_slack import read_webhook_url
In [2]:
webhook_url = read_webhook_url()
Enter webhook URL: ········

Usage

fastai_slack provides a callback called SlackCallback which you can include within the fit function call.

As an example, let's train a ResNet18 network on the MINST dataset.

In [3]:
from fastai.vision import *

We begin by downloading the data and creating an ImageDataBunch:

In [4]:
path = untar_data(URLs.MNIST)
data = ImageDataBunch.from_folder(path,train='training', valid='testing')
data.show_batch(rows=3, figsize=(5,5))
/mnt/terabyte/miniconda3/envs/fastai-v1/lib/python3.7/site-packages/fastai/datasets.py:163: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. with open(fpath, 'r') as yaml_file: return yaml.load(yaml_file)
Notebook Image

Next, we create the FastAI Learner object, and call the fit method to train the model, passing in a SlackCallback configured with:

  • name: project/job name which will be included in every notification
  • webhook_url: The Slack incoming webhook URL (read from user input earlier)
  • frequency: How often to send a notification (defaults to 1 i.e. every epoch)
In [5]:
from fastai_slack import SlackCallback
In [6]:
# Create a learner
learn = cnn_learner(data, 
                    models.resnet18, 
                    metrics=accuracy)
# Create a callback
slack_cb = SlackCallback('mnist', webhook_url, frequency=2)

# fit with callback
learn.fit(8, callbacks=[slack_cb])
Slack notification tag: [mnist 36qw8d]

This callback will send notifications every 2 epochs, and also notfiy you if there's an exception. Each fit call is identified by a unique tag e.g. [mnist-resnet18 xtfl18] that appears in every Slack message.

Here's what the notifications look like:

Instead of passing the webhook URL manually each time, you can also set a system wide environment variable with the name FASTAI_SLACK_WEBHOOK contaning the URL, and it will be read automatically by SlackCallback. For instance, on Mac/Linux, you might need to add the following like to your ~/.bashrc:

FASTAI_SLACK_WEBHOOK=https://hooks.slack.com/services/T00000/B0000/XXXXXXXXXXX
# Replace the URL above with your Slack incoming webhook URL

Here's an example where the webhook isn't specified manually:

In [7]:
learn.fit(4, callbacks=[SlackCallback('mnist')])
You must provide a "Slack Incoming Webhook" URL for sending messages. You can generate a webhook URL for your workspace by following this guide: https://api.slack.com/incoming-webhooks Enter webhook URL: ········ Slack notification tag: [mnist oxvqc2]

Source Code & Contributing

fastai_slack is open source, and you can view the source code here: https://github.com/swiftace-ai/fastai_slack

Please use Github to report bugs, request/propose feature. Pull requests with bug fixes and PRs are most welcome!

In [8]:
import jovian
In [ ]:
jovian.commit()
[jovian] Saving notebook..
In [ ]: