MindCV is an open-source toolbox for computer vision research and development based on MindSpore. It collects a series of classic and SoTA vision models, such as ResNet and SwinTransformer, along with their pre-trained weights and training strategies. SoTA methods such as auto augmentation are also provided for performance improvement. With the decoupled module design, it is easy to apply or adapt MindCV to your own CV tasks.
The following is the corresponding mindcv
versions and supported mindspore
versions.
mindcv | mindspore |
---|---|
main | master |
0.5 | 2.5.0 |
0.4 | 2.3.0/2.3.1 |
0.3 | 2.2.10 |
0.2 | 2.0 |
0.1 | 1.8 |
Easy-to-Use. MindCV decomposes the vision framework into various configurable components. It is easy to customize your data pipeline, models, and learning pipeline with MindCV:
>>> import mindcv
# create a dataset
>>> dataset = mindcv.create_dataset('cifar10', download=True)
# create a model
>>> network = mindcv.create_model('resnet50', pretrained=True)
Users can customize and launch their transfer learning or training task in one command line.
# transfer learning in one command line
python train.py --model=swin_tiny --pretrained --opt=adamw --lr=0.001 --data_dir=/path/to/data
State-of-The-Art. MindCV provides various CNN-based and Transformer-based vision models including SwinTransformer. Their pretrained weights and performance reports are provided to help users select and reuse the right model:
Flexibility and efficiency. MindCV is built on MindSpore which is an efficient DL framework that can be run on different hardware platforms (GPU/CPU/Ascend). It supports both graph mode for high efficiency and pynative mode for flexibility.
The performance of the models trained with MindCV is summarized in here, where the training recipes and weights are both available.
Model introduction and training details can be viewed in each sub-folder under configs.
See Installation for details.
To get started with MindCV, please see the Quick Start, which will give you a quick tour on each key component and the train/validate/predict pipelines.
Below are a few code snippets for your taste.
>>> import mindcv
# List and find a pretrained vision model
>>> mindcv.list_models("swin*", pretrained=True)
['swin_tiny']
# Create the model object
>>> network = mindcv.create_model('swin_tiny', pretrained=True)
# Validate its accuracy
python validate.py --model=swin_tiny --pretrained --dataset=imagenet --val_split=validation
# {'Top_1_Accuracy': 0.80824, 'Top_5_Accuracy': 0.94802, 'loss': 1.7331367141008378}
Image classification demo
Right click on the image below and save as dog.jpg
.
Classify the downloaded image with a pretrained SoTA model:
python infer.py --model=swin_tiny --image_path='./dog.jpg'
# {'Labrador retriever': 0.5700152, 'golden retriever': 0.034551315, 'kelpie': 0.010108651, 'Chesapeake Bay retriever': 0.008229004, 'Walker hound, Walker foxhound': 0.007791956}
The top-1 prediction result is labrador retriever, which is the breed of this cut dog.
It is easy to train your model on a standard or customized dataset using train.py
, where the training strategy (e.g., augmentation, LR scheduling) can be configured with external arguments or a yaml config file.
Standalone Training
# standalone training
python train.py --model=resnet50 --dataset=cifar10 --dataset_download
Above is an example for training ResNet50 on CIFAR10 dataset on a CPU/GPU/Ascend device
Distributed Training
For large datasets like ImageNet, it is necessary to do training in distributed mode on multiple devices. This can be achieved with msrun
and parallel features supported by MindSpore.
# distributed training
# assume you have 4 NPUs
msrun --bind_core=True --worker_num 4 python train.py --distribute \
--model=densenet121 --dataset=imagenet --data_dir=/path/to/imagenet
Notice that if you are using msrun startup with 2 devices, please add --bind_core=True
to improve performance. For example:
msrun --bind_core=True --worker_num=2--local_worker_num=2 --master_port=8118 \
--log_dir=msrun_log --join=True --cluster_time_out=300 \
python train.py --distribute --model=densenet121 --dataset=imagenet --data_dir=/path/to/imagenet
For more information, please refer to https://www.mindspore.cn/docs/en/r2.5.0/model_train/parallel/startup_method.html
Detailed parameter definitions can be seen in config.py
and checked by running `python train.py --help'.
To resume training, please set the --ckpt_path
and --ckpt_save_dir
arguments. The optimizer state including the learning rate of the last stopped epoch will also be recovered.
Config and Training Strategy
You can configure your model and other components either by specifying external parameters or by writing a yaml config file. Here is an example of training using a preset yaml file.
msrun --bind_core=True --worker_num 4 python train.py -c configs/squeezenet/squeezenet_1.0_ascend.yaml
Pre-defined Training Strategies:
We provide more than 20 training recipes that achieve SoTA results on ImageNet currently.
Please look into the configs
folder for details.
Please feel free to adapt these training strategies to your own model for performance improvement, which can be easily done by modifying the yaml file.
Train on ModelArts/OpenI Platform
To run training on the ModelArts or OpenI cloud platform:
1. Create a new training task on the cloud platform.
2. Add run parameter `config` and specify the path to the yaml config file on the website UI interface.
3. Add run parameter `enable_modelarts` and set True on the website UI interface.
4. Fill in other blanks on the website and launch the training task.
Graph Mode and PyNative Mode:
By default, the training pipeline train.py
is run in graph mode on MindSpore, which is optimized for efficiency and parallel computing with a compiled static graph.
In contrast, pynative mode is optimized for flexibility and easy debugging. You may alter the parameter --mode
to switch to pure pynative mode for debugging purpose.
Mixed Mode:
PyNative mode with mindspore.jit is a mixed mode for comprising flexibility and efficiency in MindSpore. To apply pynative mode with mindspore.jit for training, please run train_with_func.py
, e.g.,
python train_with_func.py --model=resnet50 --dataset=cifar10 --dataset_download --epoch_size=10
Note: this is an experimental function under improvement. It is not stable on MindSpore 1.8.1 or earlier versions.
To evaluate the model performance, please run validate.py
# validate a trained checkpoint
python validate.py --model=resnet50 --dataset=imagenet --data_dir=/path/to/data --ckpt_path=/path/to/model.ckpt
Validation while Training
You can also track the validation accuracy during training by enabling the --val_while_train
option.
python train.py --model=resnet50 --dataset=cifar10 \
--val_while_train --val_split=test --val_interval=1
The training loss and validation accuracy for each epoch will be saved in {ckpt_save_dir}/results.log
.
More examples about training and validation can be seen in examples.
We provide the following jupyter notebook tutorials to help users learn to use MindCV.
Currently, MindCV supports the model families listed below. More models with pre-trained weights are under development and will be released soon.
Please see configs for the details about model performance and pretrained weights.
Release 0.3.0
is published. We will drop MindSpore 1.x in the future release.
num_classes
when creating a pre-trained model.filter_bias_and_bn
will be removed and renamed as weight_decay_filter
,
due to a prolonged misunderstanding of the MindSpore optimizer.
We will migrate the existing training recipes, but the signature change of function create_optimizer
will be incompatible
and the old version training recipes will also be incompatible. See PR/752 for details.See RELEASE for detailed history.
We appreciate all kinds of contributions including issues and PRs to make MindCV better.
Please refer to CONTRIBUTING.md for the contributing guideline.
Please follow the Model Template and Guideline for contributing a model that fits the overall interface
This project follows the Apache License 2.0 open-source license.
MindCV is an open-source project jointly developed by the MindSpore team, Xidian University, and Xi'an Jiaotong University. Sincere thanks to all participating researchers and developers for their hard work on this project. We also acknowledge the computing resources provided by OpenI.
If you find this project useful in your research, please consider citing:
@misc{MindSpore Computer Vision 2022,
title={{MindSpore Computer Vision}:MindSpore Computer Vision Toolbox and Benchmark},
author={MindSpore Vision Contributors},
howpublished = {\url{https://github.com/mindspore-lab/mindcv/}},
year={2022}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. Open source ecosystem
2. Collaboration, People, Software
3. Evaluation model