3 In 1 Intro To Weights & Biases CV, NLP And RL
A 3-in-1 Intro to Weights & Biases: Computer Vision, Natural Language Processing and Reinforcement Learning
Weights & Biases is a developer toolkit for machine learning experiment tracking, dataset and model versioning, and collaboration
In this mega 3-in-1 notebook you'll see how Weights and Biases seamlessly integrates into ML code across modalities -- one example each from Computer Vision, NLP and Reinforcement Learning -- as well as across frameworks like PyTorch, Keras, and more.
For the full range of supported integrations, plus more examples, see our docs.
0. ✍️ The wandb Library
When working directly with the wandb library,
the functions you'll use most often are:
wandb.login- Login to W&B at the start of your sessionwandb.init- Initialise a new W&B, returns a "run" objectwandb.log- Add information to the logs for your run
Most of the code in this notebook is for setting up and executing our ML experiments and is not specific to experiment tracking with W&B. To see where W&B is added in the code below you can search for these functions in the notebook or look for the ✍️ emoji.
When a W&B run begins, a link labeled Run Page will be printed to the standard out. This link will take you to a W&B dashboard where you can view live, interactive charts and information about your experiment.
Data & Privacy
We take security seriously, and our cloud-hosted dashboard uses industry best practices for encryption. If you're working with datasets that cannot leave your enterprise cluster, we have on-prem installations available. It's also easy to download all your data and export it to other tools, for example, for custom analysis in a Jupyter notebook. Here's more on our API.
Creating a W&B Account, Importing Libraries, and Logging in
Create an account, then run the following code cell to install wandb and log in.
✍️ Login to wandb
1. 👓 Computer Vision - Fine-tune MobileNet with W&B and PyTorch
First, we'll see an example of image classification
on a super-mini version of the
iNaturalist 2021 dataset
using a mobilenet-V3 model
from the
PyTorch IMage Models, or "timm" library.
Credit: This code based on the official PyTorch tutorial here.
Download Data from W&B Artifacts
We use W&B Artifacts to download the super-mini iNaturalist 2021 dataset, a subset of the iNaturalist 2021 dataset with only 50 classes with 50 examples each.
W&B Artifacts is a versioning system for models, datasets, and other large files. Read more in our docs here.
Setup Data and DataLoaders
The following cells are boilerplate that defines our dataset and how it should be loaded off disk. No Weights & Biases-specific code here -- you can just execute and move to the next section.
Train the Model
The cell below defines our entire training setup.
First, we define and download the model, then set up its optimizer.
Finally, we define the model training logic --
and that's where we integrate the wandb code
for logging metrics and more during training.
✍️ Start a wandb Run
Here, we inititalize a wandb run,
log the hyperparameters.
Start Training
Training may take several minutes, but you can watch your metrics stream in live at the Run Page link produced by the cell above.
Bonus: Object Detection with YOLOv5 and W&B
- Interested in object detection? You're in luck! Check out our YOLOv5 integration here.
2. 📜 Natural Language Processing - Fine-tune a BERT Model with W&B and Hugging Face
In this example, we will use Hugging Face to fine-tune a distilled version of the BERT model for topic classification using the Yahoo! Answers dataset.
To learn more about the HuggingFace W&B integration see our docs.
Setup wandb
✍️ wandb Environment Variables
The easiest way to configure the W&B integration with Hugging Face is by setting the values of certain environment variables.
Here we will define the project to which we'll log our results and upload our model. See the docs for more on how to configure the Hugging Face integration.
Setup Data and DataLoaders
The following cell is boilerplate that defines our dataset and how it should be loaded off disk. No Weights & Biases-specific code here -- you can just execute and move to the next section.
Train the Model
In the following cell,
we use the Hugging Face
transformers
and datasets
libraries to set up our training and logging
and to download the pretrained model.
✍️ Start a wandb.Run
Start Training
Training may take several minutes, but you can watch your metrics stream in live at the 🚀 link produced by the cell above once training starts.
Test the Predictions
3. 🤖 Reinforcement Learning - Train a Simple Agent with W&B, Keras, and OpenAI Gym
In this section, we train a Keras-RL2 model to solve Cartpole, a classic introductory RL problem, using OpenAI Gym.
Setup Data and DataLoaders
Tools for Rendering OpenAI Gym Videos in Colab
✍️ Integrating W&B with Keras-RL - WandB Test Logger
WandB Test Logger
Setup Gym Environment
Setting Hyperparameters
Setup Model
Construct an Agent
Train
Define your train function
Train your agent
✍️ W&B Best Practices
- Projects: Log multiple runs to a project to compare them.
wandb.init(project="project-name") - Groups: For multiple processes or cross validation folds, log each process as a runs and group them together.
wandb.init(group="experiment-1") - Tags: Add tags to track your current baseline or production model.
- Notes: Type notes in the table to track the changes between runs.
- Reports: Take quick notes on progress to share with colleagues and make dashboards and snapshots of your ML projects.
✍️ W&B Advanced Setup
- Environment variables: Set API keys in environment variables so you can run training on a managed cluster.
- Offline mode: Use
dryrunmode to train offline and sync results later. - On-prem: Install W&B in a private cloud or air-gapped servers in your own infrastructure. We have local installations for everyone from academics to enterprise teams.
- Sweeps: Set up hyperparameter search quickly with our lightweight tool for tuning.
- Artifacts: Track and version models and datasets in a streamlined way that automatically picks up your pipeline steps as you train models.