Skip to content

cs304-2022/mindcv

 
 

Repository files navigation

MindSpore Computer Vision

Introduction

MindCV is an open source toolbox for computer vision research and development based on MindSpore. It collects a large number of classic and SoTA vision models such ResNet and SwinTransformer with pretrained weights, and algorithms like MixUp, AutoAugment for boosting performance. With the decoupled module design, it is easy to apply or adapt MindCV to your own deep learning tasks.

Major Features

  • Easy-to-use: Friendly modular design for the overal DL workflow, including constructing dataloader, models, optimizer, loss for training and testing. It is easy to customize thedata processing and learning pipeline.
  • State-of-art models: MindCV provides various SoTA CNN-based and Transformer-based models with pretrained weights including SwinTransformer and EfficientNet (See model list)
  • High efficiency, extensibility and compatibility for different hardware platform (GPU/CPU/Ascend)

Results

Coming soon.

Installation

Dependency

  • mindspore >= 1.8.1
  • numpy >= 1.17.0
  • pyyaml >= 5.3
  • tqdm
  • openmpi 4.0.3 (for distributed mode)

To install the dependency, please run

pip install -r requirements.txt

MindSpore can be easily installed by following the official instruction where you can select your hardware platform for the best fit. To run in distributed mode, openmpi is required to install.

The following instructions assume the desired dependency is fulfilled.

Install with pip

MindCV can be installed with pip.

pip install https://github.com/mindlab-ai/mindcv/releases/download/v0.0.1-beta/mindcv-0.0.1b0-py3-none-any.whl

Install from source

To install MindCV from source, please run,

pip install git+https://github.com/mindlab-ai/mindcv.git

Get Started

Quick Start Demo

Please see the Quick Start Demo to help you get started with MindCV and learn about the basic usage quickly.

You can also see the finetune tutorial to learn how to apply a pretrained SoTA model to your own classification task.

Below is how to find and create a deep vision model quickly.

>>> import mindcv 
# Search a wanted pretrained model 
>>> mindcv.list_models("densenet*", pretrained=True)
['densenet201', 'densenet161', 'densenet169', 'densenet121']
# Create the model object
>>> network = mindcv.create_model('densenet121', pretrained=True)

Training and Validation Scripts

It is easy to train your model on standard datasets or your own dataset with MindCV. Model training, transfer learning, or evaluaiton can be done using one or a few line of code with flexible configuration.

  • Standalone Training

train.py is the main script for model training, where you can set the dataset, data transformation, model, loss, LR scheduler, and optimizier easily. Here is the example for finetuning a pretrained DenseNet on CIFAR10 dataset using Adam optimizer.

python train.py --model=densenet121 --pretrained --opt=adam --lr=0.001 \
		--dataset=cifar10 --num_classes=10 --dataset_download    

Detailed adjustable parameters and their default value can be seen in config.py

  • Distributed Training

For large datasets like ImageNet, it is necessary to do training in distributed mode on multiple devices, which is well supported in MindCV. The following script is an example for training DenseNet121 on ImageNet with 4 GPUs.

export CUDA_VISIBLE_DEVICES=0,1,2,3  # suppose there are 4 GPUs
mpirun --allow-run-as-root -n 4 python train.py --distribute \
	--model=densenet121 --dataset=imagenet --data_dir=./datasets/imagenet   
  • Train with Yaml Config

The yaml config files that yield competitive results on ImageNet for different models are listed in the configs folder. To trigger training using preset yaml config,

mpirun --allow-run-as-root -n 4 python train.py -c configs/squeezenet/squeezenet_1.0_gpu.yaml    
  • Validation

To validate a trained/pretrained model, you can use validate.py.

# validate a trained checkpoint
python validate.py --model=resnet50 --dataset=imagenet --val_split=validation \
		           --ckpt_path='./ckpt/densenet121-best.ckpt' 

# validate a pretrained SwinTransformer model 
python validate.py --model=swin_tiny --dataset=imagenet --val_split=validation \
		           --pretrained
>>> {'Top_1_Accuracy': 0.808343989769821, 'Top_5_Accuracy': 0.9527253836317136, 'loss': 0.8474242982580839}
  • Validate during Training

Validation during training can be enabled by setting the --val_while_train argument, e.g.,

python train.py --model=resnet18 --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_NAME/metric_log.txt.

You can use mindcv.list_models() to find out all supported models. It is easy to apply any of them to your tasks with these scripts. For more examples, see examples/scripts.

Tutorials

We provide jupyter notebook tutorials for

Notes

What is New

  • 2022/10/12
  1. Both BCE and CE loss now support class-weight config, label smoothing, and auxilary logit input (for networks like inception).
  • 2022/09/13
  1. Add Adan optimizer (experimental)

License

This project is released under the Apache License 2.0.

Feedbacks and Contact

The dynamic version is still under development, if you find any issue or have an idea on new features, please don't hesitate to contact us via issue.

Acknowledgement

MindCV is an open source project that welcome any contribution and feedback. We wish that the toolbox and benchmark could serve the growing research community by providing a flexible as well as standardized toolkit to reimplement existing methods and develop their own new computer vision methods.

Contributing

We appreciate all contributions to improve MindCV. Please refer to CONTRIBUTING.md for the contributing guideline.

Citation

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/mindlab-ai/mindcv/}},
    year={2022}
}

About

A toolbox of vision models and algorithms based on MindSpore

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 65.6%
  • Python 34.4%