This repository was archived by the owner on Jun 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
rwightman/pytorch-image-models integration #70
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa88a89
rwightman/pytorch-image-models integration
587514c
sparsezoo recipes and weights integration
908b841
splitbn qat fusing, rebase to main, address comments
29aecc3
option to override bn subclass during fusing without overriding the p…
14ca60a
addressing comments
60ab884
rebasing on main
75169ff
renaming script to train.py
2ca2ffb
recipe_type=transfer -> transfer_learn
bfineran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| <!-- | ||
| Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
|
|
||
| # SparseML-rwightman/pytorch-image-models integration | ||
| This directory provides a SparseML integrated training script for the popular | ||
| [rwightman/pytorch-image-models](https://github.com/rwightman/pytorch-image-models) | ||
| repository also known as [timm](https://pypi.org/project/timm/). | ||
|
|
||
| Using this integration, you will be able to apply SparseML optimizations | ||
| to the powerful training flows of the pytorch-image-models repository. | ||
|
|
||
| Some of the tasks you can perform using this integration include, but are not limited to: | ||
| * model pruning | ||
| * quantization-aware-training | ||
| * sparse quantization-aware-training | ||
| * sparse transfer learning | ||
|
|
||
| ## Installation | ||
| Both requirements can be installed via `pip` or can be cloned | ||
| and installed from their respective source repositories. | ||
|
|
||
| ```bash | ||
| pip install git+https://github.com/rwightman/pytorch-image-models.git | ||
| pip install sparseml[torchvision] | ||
| ``` | ||
|
|
||
|
|
||
| ## Script | ||
| `integrations/timm/train.py` modifies | ||
| [`train.py`](https://github.com/rwightman/pytorch-image-models/blob/master/train.py) | ||
| from pytorch-image-models to include a `sparseml-recipe` argument | ||
| to run SparseML optimizations with. This can be a file path to a local | ||
| SparseML recipe or a SparseZoo model stub prefixed by `zoo:` such as | ||
| `zoo:cv-classification/resnet_v1-50/pytorch-rwightman/imagenet-augmented/pruned_quant-aggressive`. | ||
|
|
||
| Additionally, to run sparse transfer learning with a SparseZoo model that has | ||
| a transfer learning recipe, add `?recipe_type=transfer_learn` as part of the model stub. | ||
| i.e. `zoo:cv-classification/resnet_v1-50/pytorch-rwightman/imagenet-augmented/pruned_quant-aggressive?recipe_type=transfer_learn`. | ||
| This will run a recipe that holds the optimized sparsity structure the same while allowing | ||
| non-zero weights to be updated during training, so pre-learned optimizations can be applied | ||
| to different datasets. | ||
|
|
||
| To load the base weights for a SparseZoo recipe as the initial checkpoint, set | ||
| `--initial-checkpoint` to `zoo`. To use the weights of a SparseZoo model as the | ||
| initial checkpoint, pass that model's SparseZoo stub prefixed by `zoo:` to the | ||
| `--initial-checkpoint` argument. | ||
|
|
||
| Running the script will | ||
| follow the normal pytorch-image-models training flow with the given | ||
| SparseML optimizations enabled. | ||
|
|
||
| Some considerations: | ||
|
|
||
| * `--sparseml-recipe` is a required parameter | ||
| * `--epochs` will now be overridden by the epochs set in the SparseML recipe | ||
| * Modifiers will log their outputs to the console as well as to a tensorboard file | ||
| * After training is complete, the final model will be exported to ONNX using SparseML | ||
|
|
||
| You can learn how to build or download a recipe using the | ||
| [SparseML](https://github.com/neuralmagic/sparseml) | ||
| or [SparseZoo](https://github.com/neuralmagic/sparsezoo) | ||
| documentation, or export one with [Sparsify](https://github.com/neuralmagic/sparsify). | ||
|
|
||
| Documentation on the original script can be found | ||
| [here](https://rwightman.github.io/pytorch-image-models/scripts/). | ||
| The latest commit hash that `train.py` is based on is included in the docstring. | ||
|
|
||
|
|
||
| #### Example Command | ||
| Training from a local recipe and checkpoint | ||
| ```bash | ||
| python integrations/timm/train.py \ | ||
| /PATH/TO/DATASET/imagenet/ \ | ||
| --sparseml-recipe /PATH/TO/RECIPE/recipe.yaml \ | ||
| --initial-checkpoint PATH/TO/CHECKPOINT/model.pth \ | ||
| --dataset imagenet \ | ||
| --batch-size 64 \ | ||
| --remode pixel --reprob 0.6 --smoothing 0.1 \ | ||
| --output models/optimized \ | ||
| --model resnet50 \ | ||
| --workers 8 \ | ||
| ``` | ||
|
|
||
| Training from a local recipe and SparseZoo checkpoint | ||
| ```bash | ||
| python integrations/timm/train.py \ | ||
| /PATH/TO/DATASET/imagenet/ \ | ||
| --sparseml-recipe /PATH/TO/RECIPE/recipe.yaml \ | ||
| --initial-checkpoint zoo:model/stub/path \ | ||
| --dataset imagenet \ | ||
| --batch-size 64 \ | ||
| --remode pixel --reprob 0.6 --smoothing 0.1 \ | ||
| --output models/optimized \ | ||
| --model resnet50 \ | ||
| --workers 8 \ | ||
| ``` | ||
|
|
||
| Training from a SparseZoo recipe and checkpoint with sparse transfer learning enabled | ||
| ```bash | ||
| python integrations/timm/train.py \ | ||
| /PATH/TO/DATASET/imagenet/ \ | ||
| --sparseml-recipe zoo:model/stub/path?recipe_type=transfer_learn \ | ||
| --initial-checkpoint zoo \ | ||
| --dataset imagenet \ | ||
| --batch-size 64 \ | ||
| --remode pixel --reprob 0.6 --smoothing 0.1 \ | ||
| --output models/optimized \ | ||
| --model resnet50 \ | ||
| --workers 8 \ | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.