Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rs/docs update #1398

Draft
wants to merge 116 commits into
base: main
Choose a base branch
from
Draft

Rs/docs update #1398

wants to merge 116 commits into from

Conversation

robertgshaw2-redhat
Copy link
Contributor

No description provided.

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Recipes encode the instructions and hyperparameters for sparsifying a model using modifiers to the training process.
The modifiers can range from pruning and quantization to learning rate and weight decay.
When appropriately combined, it becomes possible to create highly sparse and accurate models.
Neural Magic has pre-sparsified each version of YOLOv5. These models can be deployed directly or can be fine-tuned onto custom dataset via sparse transfer learning. This
Copy link
Contributor

@mwitiderrick mwitiderrick Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These models can be deployed directly or fine-tuned onto a custom datasets via sparse transfer learning.


### SparseZoo

Neural Magic has pre-sparsified versions of common Torchvision models such as ResNet-50. These models can be deployed directly or can be fine-tuned onto custom dataset via sparse transfer learning. This makes it easy to create a sparse image classification model trained on your dataset.
Copy link
Contributor

@mwitiderrick mwitiderrick Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These models can be deployed directly or fine-tuned onto a custom datasets via sparse transfer learning


Just like the CLI, the Python API uses YAML-based recipes to encode the parameters of the sparsification process, allowing youto add SparseML with just a few lines of code.

The `ScheduleModifierManager` class is responsible for parsing the YAML recipes and overriding the standard PyTorch model and optimizer objects,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ScheduleModifierManager class parses the YAML recipes and overrides the standard PyTorch model and optimizer objects, encoding the logic of the sparsity algorithms from the recipe


### Overview

Sparse Transfer is quite similiar to the typical transfer learing process used to train NLP models, where we fine-tune a pretrained checkpoint onto a smaller downstream dataset. With Sparse Transfer Learning, we simply start the fine-tuning process from a pre-sparsified checkpoint and maintain sparsity while the training process occurs.
Copy link
Contributor

@mwitiderrick mwitiderrick Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sparse Transfer is similar to the typical transfer learning process used to train NLP models, where we fine-tune a pre-trained checkpoint onto a smaller downstream dataset. With Sparse Transfer Learning, we start fine-tuning from a pre-sparsified checkpoint and maintain sparsity during training.


### Kick off Training

We can start Sparse Transfer Learning by passing a starting checkpoint and recipe to the training script. For Sparse Transfer, we will use a recipe that instructs SparseML to maintain sparsity during training and to quantize the model. The starting checkpoitn and transfer recipe are specified by the following SparseZoo stub:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkpoitn -> checkpoint


The key `Modifiers` for sparse transfer learning are the following:
- `ConstantPruningModifier` instructs SparseML to maintain the sparsity structure of the network during the fine-tuning process
- `QuantizationModifier` instructs SparseML to apply quantization aware training to quantize the weights over the final epochs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quantization aware -> quantization-aware


To benchmark the model, run
To transfer learn this sparsified model to other datasets you may have to adjust certain hyperparameters in this recipe and/or training script such as the optimizer type, the number of epochs, and the learning rates.
Copy link
Contributor

@mwitiderrick mwitiderrick Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma before such and other

limitations under the License.
-->

# Sparse Transfer Learning
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sparse Transfer Learning with YOLOv5 and SparseML


## Sparse Transfer Learning with VOC

Let's try a simple example of Sparse Transfer Learning YOLOv5s onto the VOC dataset.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try -> Let's implement

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's implement a simple Sparse Transfer Learning example with YOLOv5s and the VOC dataset.


Run the following to download the dataset from Google Drive:

```python
Copy link
Contributor

@mwitiderrick mwitiderrick Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this code will work for everyone reading this tutorial. Can you upload the data to G Drive, make it public, and use something like gdown or googledrivedownloader to download it?


## Wrapping Up

Checkout the DeepSparse repository for more information on deploying your sparse YOLOv5 models with DeepSparse for GPU class performance on CPUs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out the DeepSparse repository for more information on deploying your sparse YOLOv5 models with DeepSparse for GPU class performance on CPUs.

Can we link to the repository?


## Overview

Sparse Transfer is quite similiar to the typical YOLOv5 training, where we fine-tune a checkpoint pretrained on COCO onto a smaller downstream dataset. However, with Sparse Transfer Learning, we simply start the fine-tuning process from a pre-sparsified YOLOv5 and maintain sparsity while the training process occurs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sparse Transfer is similar to the typical YOLOv5 training, where we fine-tune a checkpoint pre-trained on COCO onto a smaller downstream dataset. However, with Sparse Transfer Learning, we start fine-tuning from a pre-sparsified YOLOv5 and maintain sparsity during training.

- `--weights` specifies the starting checkpoint for the training process. Here, we passed a SparseZoo stub, which
identifies the 75% pruned-quantized YOLOv5s model in the SparseZoo. The script downloads the PyTorch model to begin training. In addition to SparseZoo stubs, you can also pass a local path to a PyTorch checkpoint.

- `--recipe` specifies the transfer learning recipe. Recipes are YAML files that declare the sparsity related algorithms
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sparsity related -> sparsity-related


- `--recipe` specifies the transfer learning recipe. Recipes are YAML files that declare the sparsity related algorithms
that SparseML should apply. For transfer learning, the recipe instructs SparseML to maintain sparsity during training
and to apply quantization over the final epochs. In this case, we passed a SparseZoo stub, which instructs SparseML
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, we passed a SparseZoo stub instructing SparseML to download a premade YOLOv5s transfer learning recipe.


The "Modifiers" encode how SparseML should modify the training process for Sparse Transfer Learning.
- `ConstantPruningModifier` tells SparseML to pin weights at 0 over all epochs, maintaining the sparsity structure of the network
- `QuantizationModifier` tells SparseML to quanitze the weights with quantization aware training over the last 5 epochs
Copy link
Contributor

@mwitiderrick mwitiderrick Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quantization aware -> quantization-aware
quanitze -> quantize


SparseZoo contains mutliple variants of each version of YOLOv5 at various levels of sparsity, which can be fine-tuned onto your dataset.

[Checkout the full list!](https://sparsezoo.neuralmagic.com/?page=1&domain=cv&sub_domain=detection)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkout -> Check out

#### 2. Create Labels

After using a tool like [Roboflow Annotate](https://roboflow.com/annotate?ref=ultralytics) to label your data, export your labels to the YOLO Format, with one
`*.txt` file per image (if not objects are in the image, no `*.txt` file is require).
Copy link
Contributor

@mwitiderrick mwitiderrick Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not objects -> if no objects
is require -> is required

../datasets/coco128/labels/im0.txt # label
```

For more details, checkout the [custom dataset set tutorial](https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data) in the Ultralytics repository.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkout -> check out

optional argument (as pruning can be applied without model distillation as well). Here, we passed a SparseZoo stub identifying the standard dense YOLOv5s model in SparseZoo. Alternatively, you can pass a path to a YOLOv5 PyTorch model.

- `--data coco.yaml` specifies dataset configuration file to use during the sparsification process. Here, we pass `coco.yaml`, which SparseML instructs SparseML to
automatically download the COCO dataset. Alternatively, you can pass a config file for your local dataset. Checkout the [Ultralytics Custom Data Tutorial Repo]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which SparseML instructs SparseML to automatically download the COCO dataset.


- `--data coco.yaml` specifies dataset configuration file to use during the sparsification process. Here, we pass `coco.yaml`, which SparseML instructs SparseML to
automatically download the COCO dataset. Alternatively, you can pass a config file for your local dataset. Checkout the [Ultralytics Custom Data Tutorial Repo]
(https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data) for more details on how to structure your datasets. SparseML conforms to the Ultralytics
Copy link
Contributor

@mwitiderrick mwitiderrick Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out the Ultralytics Custom Data Tutorial Repo for more details on how to structure your datasets

Remove space after ] for proper linking

```bash
!sparseml.yolov5.export_onnx \
--weights yolov5_runs/train/exp/weights/last.pt \
--dynamic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's import to explain what dynamic means.


## Next Steps

Checkout the DeepSparse repository for more information on deploying your sparse YOLOv5 models with DeepSparse for GPU class performance on CPUs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkout -> Check out

## Overview

Sparsifying a model involves removing redundant information from a
trained model using algorithms such as pruning and quantization. The sparse models can then be deployed with DeepSparse, which implements many optimizations to increase performance via sparsity, for GPU-class performance on CPUs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sparsity for GPU-class performance on CPUs.

In this tutorial, we will demonstrate how to use recipes to create
sparse versions of YOLOv5.

**Pro Tip**: For YOLOv5, there are pre-sparsified checkpoints of each version available in [SparseZoo](https://sparsezoo.neuralmagic.com/?domain=cv&sub_domain=detection&page=1).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pro Tip: For YOLOv5, each version has pre-sparsified checkpoints available in SparseZoo.

freeze_bn_stats_epoch: eval(num_epochs - quantization_epochs + 1)
```

There is is a lot here, but the important items are the `pruning_modifiers`, `distillation_modifiers`, and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is is a lot here,

`quantization_modifiers`.

The `pruning_modifiers` instruct SparseML to apply the Gradual Magnitude Pruning algorithm to various layers of
the network. As you can see, the recipe specifies a target level of sparsity for each layer of the network.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see, the recipe specifies a target sparsity level for each network layer


The `pruning_modifiers` instruct SparseML to apply the Gradual Magnitude Pruning algorithm to various layers of
the network. As you can see, the recipe specifies a target level of sparsity for each layer of the network.
At the end of every epoch, the GMP algorithm iteratively removes the lowest magnitude weights gradually inducing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the end of every epoch, the GMP algorithm iteratively removes the lowest magnitude weights, gradually inducing sparsity into the network.

sparsitty -> sparsity

optional argument (as pruning can be applied without model distillation as well). Here, we passed a SparseZoo stub identifying the standard dense YOLOv5s model in SparseZoo. Alternatively, you can pass a path to a YOLOv5 PyTorch model.

- `--data coco.yaml` specifies dataset configuration file to use during the sparsification process. Here, we pass `coco.yaml`, which SparseML instructs SparseML to
automatically download the COCO dataset. Alternatively, you can pass a config file for your local dataset. Checkout the [Ultralytics Custom Data Tutorial Repo]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkout -> Check out


## Other YOLOv5 Models

Here are some sample transfer learning commands for other versions of YOLOv5. Checkout the [SparseZoo](https://sparsezoo.neuralmagic.com/?page=1&domain=cv&sub_domain=detection) for the full repository of pre-sparsified checkpoints.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkout -> Check out

Once exported to ONNX, you can deploy your models with DeepSparse. Checkout the DeepSparse repo for examples.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkout -> Check out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants