Your PyTorch AI Factory
Installation • Flash in 3 Steps • Docs • Contribute • Community • Website • License
In a nutshell, Flash is the production grade research framework you always dreamed of but didn't have time to build.
- Sept 30: Lightning Flash now supports Meta-Learning
- Sept 9: Lightning Flash 0.5
- Jul 12: Flash Task-a-thon community sprint with 25+ community members
- Jul 1: Lightning Flash 0.4
- Jun 22: Ushering in the New Age of Video Understanding with PyTorch
- May 24: Lightning Flash 0.3
- May 20: Video Understanding with PyTorch
- Feb 2: Read our launch blogpost
From PyPI:
pip install lightning-flash
See our installation guide for more options.
All data loading in Flash is performed via a from_*
classmethod on a DataModule
.
Which DataModule
to use and which from_*
methods are available depends on the task you want to perform.
For example, for image segmentation where your data is stored in folders, you would use the from_folders
method of the SemanticSegmentationData
class:
from flash.image import SemanticSegmentationData
dm = SemanticSegmentationData.from_folders(
train_folder="data/CameraRGB",
train_target_folder="data/CameraSeg",
val_split=0.1,
image_size=(256, 256),
num_classes=21,
)
Our tasks come loaded with pre-trained backbones and (where applicable) heads.
You can view the available backbones to use with your task using available_backbones
.
Once you've chosen, create the model:
from flash.image import SemanticSegmentation
print(SemanticSegmentation.available_heads())
# ['deeplabv3', 'deeplabv3plus', 'fpn', ..., 'unetplusplus']
print(SemanticSegmentation.available_backbones('fpn'))
# ['densenet121', ..., 'xception'] # + 113 models
print(SemanticSegmentation.available_pretrained_weights('efficientnet-b0'))
# ['imagenet', 'advprop']
model = SemanticSegmentation(
head="fpn", backbone='efficientnet-b0', pretrained="advprop", num_classes=dm.num_classes)
from flash import Trainer
trainer = Trainer(max_epochs=3)
trainer.finetune(model, datamodule=datamodule, strategy="freeze")
trainer.save_checkpoint("semantic_segmentation_model.pt")
Serve in just 2 lines.
from flash.image import SemanticSegmentation
model = SemanticSegmentation.load_from_checkpoint("semantic_segmentation_model.pt")
model.serve()
or make predictions from raw data directly.
predictions = model.predict(["data/CameraRGB/F61-1.png", "data/CameraRGB/F62-1.png"])
or make predictions with 2 GPUs.
trainer = Trainer(accelerator='ddp', gpus=2)
dm = SemanticSegmentationData.from_folders(predict_folder="data/CameraRGB")
predictions = trainer.predict(model, dm)
Training strategies are PyTorch SOTA Training Recipes which can be utilized with a given task.
Check out this example where the ImageClassifier
supports 4 Meta Learning Algorithms from Learn2Learn.
This is particularly useful if you use this model in production and want to make sure the model adapts quickly to its new environment with minimal labelled data.
model = ImageClassifier(
backbone="resnet18",
optimizer=torch.optim.Adam,
optimizer_kwargs={"lr": 0.001},
training_strategy="prototypicalnetworks",
training_strategy_kwargs={
"epoch_length": 10 * 16,
"meta_batch_size": 4,
"num_tasks": 200,
"test_num_tasks": 2000,
"ways": datamodule.num_classes,
"shots": 1,
"test_ways": 5,
"test_shots": 1,
"test_queries": 15,
},
)
In detail, the following methods are currently implemented:
- prototypicalnetworks : from Snell et al. 2017, Prototypical Networks for Few-shot Learning
- maml : from Finn et al. 2017, Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks
- metaoptnet : from Lee et al. 2019, Meta-Learning with Differentiable Convex Optimization
- anil : from Raghu et al. 2020, Rapid Learning or Feature Reuse? Towards Understanding the Effectiveness of MAML
Flash includes some simple augmentations for each task by default, however, you will often want to override these and control your own augmentation recipe.
To this end, Flash supports custom transformations backed by our powerful data pipeline.
The transform requires to be passed as a dictionary of transforms where the keys are the hook's name.
This enable transforms to be applied per sample or per batch either on or off device.
It is important to note that data are being processed as a dictionary for all tasks (typically containing input
, target
, and metadata
),
Therefore, you can use ApplyToKeys
utility to apply the transform to a specific key.
Complex transforms (like MixUp) can then be implemented with ease.
The example also uses our merge_transforms
utility to merge our custom augmentations with the default transforms for images (which handle resizing and converting to a tensor).
import torch
from typing import Any
import numpy as np
import albumentations
from torchvision import transforms as T
from flash.core.data.transforms import ApplyToKeys, merge_transforms
from flash.image import ImageClassificationData
from flash.image.classification.transforms import default_transforms, AlbumentationsAdapter
def mixup(batch, alpha=1.0):
images = batch["input"]
targets = batch["target"].float().unsqueeze(1)
lam = np.random.beta(alpha, alpha)
perm = torch.randperm(images.size(0))
batch["input"] = images * lam + images[perm] * (1 - lam)
batch["target"] = targets * lam + targets[perm] * (1 - lam)
return batch
train_transform = {
# applied only on images as ApplyToKeys is used with `input`
"post_tensor_transform": ApplyToKeys(
"input", AlbumentationsAdapter(albumentations.HorizontalFlip(p=0.5))),
# applied to the entire dictionary as `ApplyToKeys` isn't used.
# this would be applied on GPUS !
"per_batch_transform_on_device": mixup,
# this would be applied on CPUS within the DataLoader workers !
# "per_batch_transform": mixup
}
# merge the default transform for this task with new one.
train_transform = merge_transforms(default_transforms((256, 256)), train_transform)
datamodule = ImageClassificationData.from_folders(
train_folder = "data/train",
train_transform=train_transform,
)
Flash Zero is a zero-code machine learning platform built
directly into lightning-flash
using the Lightning CLI
.
To get started and view the available tasks, run:
flash --help
For example, to train an image classifier for 10 epochs with a resnet50
backbone on 2 GPUs using your own data, you can do:
flash image_classification --trainer.max_epochs 10 --trainer.gpus 2 --model.backbone resnet50 from_folders --train_folder {PATH_TO_DATA}
The lightning + Flash team is hard at work building more tasks for common deep-learning use cases. But we're looking for incredible contributors like you to submit new tasks!
Join our Slack and/or read our CONTRIBUTING guidelines to get help becoming a contributor!
Note: Flash is currently being tested on real-world use cases and is in active development. Please open an issue if you find anything that isn't working as expected.
Flash is maintained by our core contributors.
For help or questions, join our huge community on Slack!
We’re excited to continue the strong legacy of opensource software and have been inspired over the years by Caffe, Theano, Keras, PyTorch, torchbearer, and fast.ai. When/if additional papers are written about this, we’ll be happy to cite these frameworks and the corresponding authors.
Flash leverages models from many different frameworks in order to cover such a wide range of domains and tasks. The full list of providers can be found in our documentation.
Please observe the Apache 2.0 license that is listed in this repository.