How to Baseline Deep Learning Tasks in a Flash
Tags: deeplearning, ai, pytorch, pytorchlightning
Published: 2021-03-24 15:35:51

---
title: How to Baseline Deep Learning Tasks in a Flash
published: true
date: 2021-02-25 13:02:02 UTC
tags: deeplearning,ai,pytorch,pytorchlightning
canonical_url: https://towardsdatascience.com/how-to-baseline-deep-learning-tasks-in-a-flash-b9b7ff4ef482
---

#### This tutorial introduces how to get started building baselines for Deep Learning using [PyTorch Lightning Flash](https://github.com/PyTorchLightning/lightning-flash)

![](https://cdn-images-1.medium.com/max/1024/0*QsFKMjmZNxHDIbJC)<figcaption>Photo by Dmitry Zvolskiy from <a href="https://www.pexels.com/photo/purple-lightning-at-night-1576369/">Pexels</a></figcaption>

### What is PyTorch Lightning Flash?

PyTorch Lightning Flash is a new library from the creators of PyTorch Lightning to enable quick baselining of state-of-the-art Deep Learning tasks on new datasets in a matter of minutes.

[PyTorchLightning/lightning-flash](https://github.com/PyTorchLightning/lightning-flash)

Consistent with PyTorch Lightning’s goal of getting rid of the boilerplate, Flash aims to make it easy to train, inference, and fine-tune deep learning models.

Flash is built on top of PyTorch Lightning to abstract away the unnecessary boilerplate for common Deep Learning Tasks ideal for:

- Data science
- Kaggle Competitions
- Industrial AI
- Applied research

As such, Flash provides seamless support for distributed training and inference of Deep Learning models.

Since Flash is built on top of PyTorch Lightning, as you learn more, you can override your Task code seamlessly with both Lightning and PyTorch to find the [right level of abstraction for your scenario](https://towardsdatascience.com/setting-a-strong-deep-learning-baseline-in-minutes-with-pytorch-c0dfe41f7d7).

![](https://cdn-images-1.medium.com/max/838/0*LuRUEzOLqmDGdwAp.png)<figcaption>Motorcycle Photo by Nikolai Ulltang from <a href="https://www.pexels.com/photo/person-riding-red-sports-bike-529782/">Pexels</a></figcaption>

For the remainder of this post, I will walk you through the 5 steps of building Deep Learning applications with an inline Flash Task code example.

### Creating your first Deep Learning Baseline with Flash

![](https://cdn-images-1.medium.com/max/1024/0*ILR3O2aPPyHTJu5j)<figcaption>Photo by Lalesh Aldarwish from <a href="https://www.pexels.com/photo/timelapse-photography-of-road-with-white-and-red-lights-169976/">Pexels</a></figcaption>

All code for the following tutorial can be found in the Flash Repo [under Notebooks](https://github.com/PyTorchLightning/lightning-flash/tree/master/flash_notebooks).

I will present five repeatable steps that you will be able to apply to any Flash Task on your own data.

1. Choose a Deep Learning Task
2. Load Data
3. Pick a State of the Art Model
4. Fine-tune the Task
5. Predict

Now let’s Get Started!!!

### Step 1: Choose A Deep Learning Task

![](https://cdn-images-1.medium.com/max/1024/0*de2mNzBjRrvBW6Zz.png)<figcaption>Photo by <a href="https://www.pexels.com/@pixabay?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pixabay</a> from <a href="https://www.pexels.com/photo/yellow-cube-on-brown-pavement-208147/">Pexels</a></figcaption>

The first step of the applied Deep Learning Process is to choose the task we want to solve. Out of the box, Flash provides support for common deep learning tasks such as [Image](https://github.com/PyTorchLightning/lightning-flash/blob/master/flash_notebooks/finetuning/image_classification.ipynb), [Text](https://github.com/PyTorchLightning/lightning-flash/blob/master/flash_notebooks/finetuning/text_classification.ipynb), Tabular Classification, and more complex scenarios such as Image Embedding, Object Detection, Document Summarization and Text Translation. New tasks are being added all the time.

In this tutorial, we’ll build a Text Classification model using Flash for sentiment analysis on movie reviews. The model will be able to tell us that a review such as “_This is the worst movie in the history of cinema.”_ is negative and that a review such as “_This director has done a great job with this movie!_” is positive.

To get started, let’s first install Flash and import the required Python libraries for the Text Classification task.

{% gist https://gist.github.com/aribornstein/bf2359ff9ba168b00dd8afa0e460acea %}

{% gist https://gist.github.com/aribornstein/e60582a63236e35385bfc699ce3cf4b8 %}

### Step 2: Load Data

Now that we have installed flash and loaded our dependencies let’s talk about data. To build our first model, we will be using the IMDB Movie Review dataset stored in a CSV file format. Check out some sample reviews from the dataset.

```
review, sentiment"I saw this film for the very first time several years ago - and was hooked up in an instant. It is great and much better than J. F. K. cause you always have to think 'Can it happen to me? Can I become a murderer?' You cannot turn of the TV or your VCR without thinking about the plot and the end, which you should'nt miss under any circumstances.", positive"Winchester 73 gets credit from many critics for bringing back the western after WWII. Director Anthony Mann must get a lot of credit for his excellent direction. Jimmy Stewart does an excellent job, but I think Stephen McNalley and John McIntire steal the movie with their portrayal of two bad guys involved in a high stakes poker game with the treasured Winchester 73 going to the winner. This is a good script with several stories going on at the same time. Look for the first appearance of Rock Hudson as Young Bull. Thank God, with in a few years, we would begin to let Indians play themselves in western films. The film is in black and white and was shot in Tucson Arizona. I would not put Winchester 73 in the category of Stagecoach, High Noon or Shane, but it gets an above average recommendation from me.<br /><br />.", positive
```

The first thing we need to do is download the dataset using the following code.

{% gist https://gist.github.com/aribornstein/235a6865675b6d467a7954ca2f574104 %}

Once we have downloaded the IMDB dataset, Flash provides a convenient TextClassificationData module that handles the complexity of loading Text Classification data stored in CSV format and converting it into a representation that Deep Learning models need train.

{% gist https://gist.github.com/aribornstein/8e69eeb2433f81658632fd80bee37a33 %}

### 3. Pick a State of the Art Model for Our Task

![](https://cdn-images-1.medium.com/max/950/0*ndmfU8riul_f4BqU.jpg)<figcaption>In the past few years in Natural Language Processing, many of the State of the Art Models are named after Sesame Street Characters. Photo used under CC BY-NC-ND 4.0 license from <a href="https://pixy.org/4508354/">pixy</a></figcaption>

Once we have loaded our dataset, we need to pick a model to train. Each Flash Task comes preloaded with support for State of the Art model backbones for you to experiment with instantly.

By default, the TextClassifier task uses the [tiny-bert](https://huggingface.co/prajjwal1/bert-tiny) model, enabling strong performance on most text classification tasks. Still, you can use any model from Hugging Face [transformers — Text Classification](https://huggingface.co/models?filter=text-classification,pytorch) model repository or even bring your own. In Flash, all you need is just one line of code to load a backbone.

{% gist https://gist.github.com/aribornstein/33f0e41bd0279d17150419f658524fee %}

### 4. Fine-tune the Task

![](https://cdn-images-1.medium.com/max/950/0*OqIoBwggKwKjOymK.jpg)<figcaption>Photo by Shae Calkins from <a href="https://pixy.org/4795948/">Pixy</a></figcaption>

Now that we have chosen the model and loaded our data, it’s time to train the model on our classification task using the following two lines of code:

{% gist https://gist.github.com/aribornstein/9b885430b31b50e481c1be5aaf5da866 %}

Because the Flash Trainer is built on top of PyTorch Lightning, it is seamless to distribute training to multiple GPUs, Cluster Nodes, and even TPUs.

{% gist https://gist.github.com/aribornstein/c99feee37b06e211acdb509eae12bf59 %}

Additionally, you get tons of otherwise difficult to implement features, such as automated model checkpointing and logging integrations with platforms such as Tensorboard, Neptune.ai, and MLFlow built-in without any hassle.

![](https://cdn-images-1.medium.com/max/390/0*1Tse2pFzWXdO9ILU.png)

![](https://cdn-images-1.medium.com/max/1024/0*zmre7gqX2sqkFTdN.png)<figcaption>Photo generated by the author.</figcaption>

Tasks come with native support for all standard task metrics. In our case, Flash Trainer will automatically benchmark your model for you on all the standard task metrics for classification, such as Precision, Recall, F1 and [more](https://pytorch-lightning.readthedocs.io/en/stable/extensions/metrics.html?highlight=metrics) with just one line of code.

{% gist https://gist.github.com/aribornstein/e7673e54d425ce5258f38ff7e7bc92e0 %}

Task models can be checkpointed and shared seamlessly using the Flash Trainer as follows.

{% gist https://gist.github.com/aribornstein/bc48c1509def24470f70ea1b20a8d731 %}

### 5. Predict

![](https://cdn-images-1.medium.com/max/1024/0*Oy5Q4Oi-3ZM8AfiY)<figcaption>Photo by <a href="https://www.pexels.com/@blitzboy?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Sindre Strøm</a> from <a href="https://www.pexels.com/photo/crystal-ball-on-person-s-hand-879718/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption>

Once we’ve finished training our model, we can use it to predict on our data. with just one line of code:

{% gist https://gist.github.com/aribornstein/7eb468caeda94fa366c4568bc0a7914b %}

Additionally, we can use the Flash Trainer to scale and distribute model inference for production.

For scaling for inference on 32 GPUs, it is as simple as one line of code.

{% gist https://gist.github.com/aribornstein/42512f04a9a1443c6dc9ee20a2f74c9d %}

You can even export models to Onnx or Torch Script for edge device inference.

{% gist https://gist.github.com/aribornstein/da2f66e313507eb070f59667c05e5014 %}

### Putting it All Together

How fun was that! The 5 steps above are condensed into the simple code snippet below and can be applied to any Flash deep learning task.

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Dpython%26ds%3Dtrue%26dsyoff%3D22px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520flash%25250Afrom%252520flash.core.data%252520import%252520download_data%25250Afrom%252520flash.text%252520import%252520TextClassificationData%25252C%252520TextClassifier%25250A%252523%2525201.%252520Download%252520the%252520data%25250Adownload_data%2528%252522https%25253A%25252F%25252Fpl-flash-data.s3.amazonaws.com%25252Fimdb.zip%252522%25252C%252520%252522data%25252F%252522%2529%25250A%252523%2525202.%252520Load%252520the%252520data%25250Adatamodule%252520%25253D%252520TextClassificationData.from_files%2528%25250A%252520%252520%252520%252520train_file%25253D%252522data%25252Fimdb%25252Ftrain.csv%252522%25252C%25250A%252520%252520%252520%252520valid_file%25253D%252522data%25252Fimdb%25252Fvalid.csv%252522%25252C%25250A%252520%252520%252520%252520test_file%25253D%252522data%25252Fimdb%25252Ftest.csv%252522%25252C%25250A%252520%252520%252520%252520input%25253D%252522review%252522%25252C%25250A%252520%252520%252520%252520target%25253D%252522sentiment%252522%25252C%25250A%252520%252520%252520%252520batch_size%25253D512%25250A%2529%25250A%252523%2525203.%252520Build%252520the%252520model%25250Amodel%252520%25253D%252520TextClassifier%2528num_classes%25253Ddatamodule.num_classes%2529%25250A%252523%2525204.%252520Create%252520the%252520trainer.%252520Run%252520once%252520on%252520data%25250Atrainer%252520%25253D%252520flash.Trainer%2528max_epochs%25253D1%2529%25250A%252523%2525205.%252520Fine-tune%252520the%252520model%25250Atrainer.finetune%2528model%25252C%252520datamodule%25253Ddatamodule%25252C%252520strategy%25253D%252522freeze%252522%2529%25250A%252523%2525206.%252520Test%252520model%25250Atrainer.test%2528%2529%25250A%252523%2525207.%252520Predict%252520on%252520new%252520data%25250Apredictions%252520%25253D%252520model.predict%2528%25255B%25250A%252520%252520%252520%252520%252522Turgid%252520dialogue%25252C%252520feeble%252520characterization%252520-%252520Harvey%252520Keitel%252520a%252520judge%25253F.%252522%25252C%25250A%252520%252520%252520%252520%252522I%252520come%252520from%252520Bulgaria%252520where%252520it%252520%2527s%252520almost%252520impossible%252520to%252520have%252520a%252520tornado.%252522%25250A%252520%252520%252520%252520%252522This%252520guy%252520has%252520done%252520a%252520great%252520job%252520with%252520this%252520movie%2521%252522%25252C%25250A%25255D%2529%25250Aprint%2528predictions%2529%25250A%252523%2525208.%252520Save%252520the%252520model%2521%25250Atrainer.save_checkpoint%2528%252522text_classification_model.pt%252522%2529%25250A&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Dpython%26ds%3Dtrue%26dsyoff%3D22px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520flash%2525250Afrom%25252520flash.core.data%25252520import%25252520download_data%2525250Afrom%25252520flash.text%25252520import%25252520TextClassificationData%2525252C%25252520TextClassifier%2525250A%25252523%252525201.%25252520Download%25252520the%25252520data%2525250Adownload_data%252528%25252522https%2525253A%2525252F%2525252Fpl-flash-data.s3.amazonaws.com%2525252Fimdb.zip%25252522%2525252C%25252520%25252522data%2525252F%25252522%252529%2525250A%25252523%252525202.%25252520Load%25252520the%25252520data%2525250Adatamodule%25252520%2525253D%25252520TextClassificationData.from_files%252528%2525250A%25252520%25252520%25252520%25252520train_file%2525253D%25252522data%2525252Fimdb%2525252Ftrain.csv%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520valid_file%2525253D%25252522data%2525252Fimdb%2525252Fvalid.csv%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520test_file%2525253D%25252522data%2525252Fimdb%2525252Ftest.csv%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520input%2525253D%25252522review%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520target%2525253D%25252522sentiment%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520batch_size%2525253D512%2525250A%252529%2525250A%25252523%252525203.%25252520Build%25252520the%25252520model%2525250Amodel%25252520%2525253D%25252520TextClassifier%252528num_classes%2525253Ddatamodule.num_classes%252529%2525250A%25252523%252525204.%25252520Create%25252520the%25252520trainer.%25252520Run%25252520once%25252520on%25252520data%2525250Atrainer%25252520%2525253D%25252520flash.Trainer%252528max_epochs%2525253D1%252529%2525250A%25252523%252525205.%25252520Fine-tune%25252520the%25252520model%2525250Atrainer.finetune%252528model%2525252C%25252520datamodule%2525253Ddatamodule%2525252C%25252520strategy%2525253D%25252522freeze%25252522%252529%2525250A%25252523%252525206.%25252520Test%25252520model%2525250Atrainer.test%252528%252529%2525250A%25252523%252525207.%25252520Predict%25252520on%25252520new%25252520data%2525250Apredictions%25252520%2525253D%25252520model.predict%252528%2525255B%2525250A%25252520%25252520%25252520%25252520%25252522Turgid%25252520dialogue%2525252C%25252520feeble%25252520characterization%25252520-%25252520Harvey%25252520Keitel%25252520a%25252520judge%2525253F.%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522I%25252520come%25252520from%25252520Bulgaria%25252520where%25252520it%25252520%252527s%25252520almost%25252520impossible%25252520to%25252520have%25252520a%25252520tornado.%25252522%2525250A%25252520%25252520%25252520%25252520%25252522This%25252520guy%25252520has%25252520done%25252520a%25252520great%25252520job%25252520with%25252520this%25252520movie%252521%25252522%2525252C%2525250A%2525255D%252529%2525250Aprint%252528predictions%252529%2525250A%25252523%252525208.%25252520Save%25252520the%25252520model%252521%2525250Atrainer.save_checkpoint%252528%25252522text_classification_model.pt%25252522%252529%2525250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5f1d203391db937738ea9e4d8e0ef6b/href">https://medium.com/media/b5f1d203391db937738ea9e4d8e0ef6b/href</a></iframe>
### Next Steps

Now that you have the tools to get started with building quick Deep Learning Baselines, I can’t wait for you to show us what you can build. If you liked this tutorial, feel free to Clap Below and give us a Star on GitHub.

We are working tirelessly on adding more Flash tasks, so if you have any must-have tasks, comment below or reach out to us on Twitter [@pytorchlightnin](https://twitter.com/PyTorchLightnin) or in our [Slack channel](https://pytorch-lightning.slack.com/).

### About the Author

[**Aaron (Ari) Bornstein**](https://www.linkedin.com/in/aaron-ari-bornstein-22aa7a77/) is an AI researcher with a passion for history, engaging with new technologies and computational medicine. As Head of Developer Advocacy at Grid.ai, he collaborates with the Machine Learning Community to solve real-world problems with game-changing technologies that are then documented, open-sourced, and shared with the rest of the world.

* * *