5 Star 34 Fork 9

MindSpore Lab / mindcv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

简介

MindCV是一个基于 MindSpore 开发的,致力于计算机视觉相关技术研发的开源工具箱。它提供大量的计算机视觉领域的经典模型和SoTA模型以及它们的预训练权重和训练策略。同时,还提供了自动增强等SoTA算法来提高模型性能。通过解耦的模块设计,您可以轻松地将MindCV应用到您自己的CV任务中。

主分支代码目前支持 MindSpore 1.8+ 以上的版本,包含 MindSpore 2.0🔥 版本。

主要特性

  • 高易用性 MindCV将视觉任务分解为各种可配置的组件,用户可以轻松地构建自己的数据处理和模型训练流程。

    >>> import mindcv
    # 创建数据集
    >>> dataset = mindcv.create_dataset('cifar10', download=True)
    # 创建模型
    >>> network = mindcv.create_model('resnet50', pretrained=True)

    用户可通过预定义的训练和微调脚本,快速配置并完成训练或迁移学习任务。

    # 配置和启动迁移学习任务
    python train.py --model swin_tiny --pretrained --opt=adamw --lr=0.001 --data_dir=/path/to/dataset
  • 高性能 MindCV集成了大量基于CNN和Transformer的高性能模型, 如SwinTransformer,并提供预训练权重、训练策略和性能报告,帮助用户快速选型并将其应用于视觉模型。

  • 灵活高效 MindCV基于高效的深度学习框架MindSpore开发,具有自动并行和自动微分等特性,支持不同硬件平台上(CPU/GPU/Ascend),同时支持效率优化的静态图模式和调试灵活的动态图模式。

模型支持

基于MindCV进行模型实现和重训练的汇总结果详见模型仓库, 所用到的训练策略和训练后的模型权重均可通过表中链接获取。

各模型讲解和训练说明详见configs目录。

安装

详情请见安装页面。

快速入门

上手教程

在开始上手MindCV前,可以阅读MindCV的快速开始,该教程可以帮助用户快速了解MindCV的各个重要组件以及训练、验证、测试流程。

以下是一些供您快速体验的代码样例。

>>> import mindcv
# 列出满足条件的预训练模型名称
>>> mindcv.list_models("swin*", pretrained=True)
['swin_tiny']
# 创建模型
>>> network = mindcv.create_model('swin_tiny', pretrained=True)
# 验证模型的准确度
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}

图片分类示例

右键点击如下图片,另存为dog.jpg

使用加载了预训练参数的SoTA模型对图片进行推理。

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}

预测结果排名前1的是拉布拉多犬,正是这张图片里的狗狗的品种。

模型训练

通过train.py,用户可以很容易地在标准数据集或自定义数据集上训练模型,用户可以通过外部变量或者yaml配置文件来设置训练策略(如数据增强、学习率策略)。

  • 单卡训练

    # 单卡训练
    python train.py --model resnet50 --dataset cifar10 --dataset_download

    以上代码是在CIFAR10数据集上单卡(CPU/GPU/Ascend)训练ResNet的示例,通过modeldataset参数分别指定需要训练的模型和数据集。

  • 分布式训练

    对于像ImageNet这样的大型数据集,有必要在多个设备上以分布式模式进行训练。基于MindSpore对分布式相关功能的良好支持,用户可以使用mpirun来进行模型的分布式训练。

    # 分布式训练
    # 假设你有4张GPU或者NPU卡
    mpirun --allow-run-as-root -n 4 python train.py --distribute \
        --model densenet121 --dataset imagenet --data_dir ./datasets/imagenet

    完整的参数列表及说明在config.py中定义,可运行python train.py --help快速查看。

    如需恢复训练,请指定--ckpt_path--ckpt_save_dir参数,脚本将加载路径中的模型权重和优化器状态,并恢复中断的训练进程。

  • 超参配置和预训练策略

    您可以编写yaml文件或设置外部参数来指定配置数据、模型、优化器等组件及其超参数。以下是使用预设的训练策略(yaml文件)进行模型训练的示例。

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

    预定义的训练策略 MindCV目前提供了超过20种模型训练策略,在ImageNet取得SoTA性能。 具体的参数配置和详细精度性能汇总请见configs文件夹。 您可以便捷地将这些训练策略用于您的模型训练中以提高性能(复用或修改相应的yaml文件即可)。

  • 在ModelArts/OpenI平台上训练

    ModelArtsOpenI云平台上进行训练,需要执行以下操作:

    1、在云平台上创建新的训练任务。
    2、在网站UI界面添加运行参数`config`,并指定yaml配置文件的路径。
    3、在网站UI界面添加运行参数`enable_modelarts`并设置为True。
    4、在网站上填写其他训练信息并启动训练任务。

静态图和动态图模式

在默认情况下,模型训练(train.py)在MindSpore上以图模式 运行,该模式对使用静态图编译对性能和并行计算进行了优化。 相比之下,pynative模式的优势在于灵活性和易于调试。为了方便调试,您可以将参数--mode设为1以将运行模式设置为调试模式。

混合模式

基于mindspore.jit的混合模式 是兼顾了MindSpore的效率和灵活的混合模式。用户可通过使用train_with_func.py文件来使用该混合模式进行训练。

python train_with_func.py --model=resnet50 --dataset=cifar10 --dataset_download --epoch_size=10

注:此为试验性质的训练脚本,仍在改进,在MindSpore 1.8.1或更早版本上使用此模式目前并不稳定。

模型验证

使用validate.py可以便捷地验证训练好的模型。

# 验证模型
python validate.py --model=resnet50 --dataset=imagenet --data_dir=/path/to/data --ckpt_path=/path/to/model.ckpt

训练过程中进行验证

当需要在训练过程中,跟踪模型在测试集上精度的变化时,请启用参数--val_while_train,如下

python train.py --model=resnet50 --dataset=cifar10 \
    --val_while_train --val_split=test --val_interval=1

各轮次的训练损失和测试精度将保存在{ckpt_save_dir}/results.log中。

更多训练和验证的示例请见示例

教程

我们提供了系列教程,帮助用户学习如何使用MindCV.

模型列表

目前,MindCV支持以下模型。

支持模型

关于模型性能和预训练权重的信息请查看 configs 文件夹。

我们将持续加入更多SoTA模型及其训练策略,敬请关注。

支持算法

支持算法列表
  • 数据增强
  • 优化器
    • Adam
    • AdamW
    • Lion
    • Adan (experimental)
    • AdaGrad
    • LAMB
    • Momentum
    • RMSProp
    • SGD
    • NAdam
  • 学习率调度器
    • Warmup Cosine Decay
    • Step LR
    • Polynomial Decay
    • Exponential Decay
  • 正则化
    • Weight Decay
    • Label Smoothing
    • Stochastic Depth (depends on networks)
    • Dropout (depends on networks)
  • 损失函数
    • Cross Entropy (w/ class weight and auxiliary logit support)
    • Binary Cross Entropy (w/ class weight and auxiliary logit support)
    • Soft Cross Entropy Loss (automatically enabled if mixup or label smoothing is used)
    • Soft Binary Cross Entropy Loss (automatically enabled if mixup or label smoothing is used)
  • 模型融合
    • Warmup EMA (Exponential Moving Average)

更新

  • 2024/1/17

新版本0.3.0发布。我们将在未来的发布中丢弃对MindSpore1.x版本的支持

  1. 新模型:
  2. 特性:
    • 损失函数AsymmetricLoss及JSDCrossEntropy
    • 数据增强分离(Augmentations Split)
    • 自定义混合精度策略
  3. 错误修复:
    • 由于分类器参数未完全弹出,您在新建预训练模型时传入参数num_classes可能会导致错误。
  4. 重构:
    • 许多模型的名字发生了变更,以便更好的理解。
    • VisionTransformer的模型定义脚本
    • 混合模式(PyNative+jit)的训练脚本
  5. 文档:
    • 如何提取多尺度特征的教程指引。
    • 如何在自定义数据集上微调预训练模型的教程指引。
  6. BREAKING CHANGES:
    • 我们将在此小版本的未来发布中丢弃对MindSpore1.x的支持。
    • 配置项filter_bias_and_bn将被移除并更名为weight_decay_filter。 我们会对已有训练策略进行迁移,但函数create_optimizer的签名变更将是不兼容的,且未迁移旧版本的训练策略也将变得不兼容。详见PR/752
  • 2023/6/16
  1. 新版本 0.2.2 发布啦!我们将MindSpore升级到了2.0版本,同时保持了对1.8版本的兼容
  2. 新模型:
  3. 新特性:
    • 梯度累加
    • 自定义TrainStep支持了动态损失缩放
    • OneCycleLRCyclicLR学习率调度器
    • 更好的日志打印与记录
    • 金字塔特征抽取
  4. 错误修复:
    • Serving部署教程(mobilenet_v3在昇腾后端的MindSpore1.8版本上不支持)
    • 文档网站上的损坏链接
  • 2023/6/2
  1. 新版本:0.2.1 发布
  2. 文档上线
  • 2023/5/30
  1. 新模型:
  2. 新特性:
    • 3-Augment, Augmix, TrivialAugmentWide
  3. 错误修复:
    • ViT 池化模式
  • 2023/04/28
  1. 增添了一些新模型,列出如下:
  2. 错误修正:
    • 分布式训练时,需对每个进程设置相同的随机数种子
    • 检查YAML配置文件中的选项是否存在于命令行解析器
    • 修正了优化器Adan中标志变量不为Tensor的错误
  • 2023/03/25
  1. 更新ResNet网络预训练权重,现在预训练权重有更高Top1精度
    • ResNet18精度从70.09提升到70.31
    • ResNet34精度从73.69提升到74.15
    • ResNet50精度从76.64提升到76.69
    • ResNet101精度从77.63提升到78.24
    • ResNet152精度从78.63提升到78.72
  2. 按照规则(model_scale-sha256sum.ckpt)更新预训练权重名字和相应下载URL链接
  • 2023/03/05
  1. 增加Lion (EvoLved Sign Momentum)优化器,论文 https://arxiv.org/abs/2302.06675
    • Lion所使用的学习率一般比Adamw小3到10倍,而权重衰减(weigt_decay)要大3到10倍
  2. 增加6个模型及其训练策略、预训练权重:
  3. 支持梯度裁剪
  • 2023/01/10
  1. MindCV v0.1发布! 支持通过PyPI安装 (pip install mindcv)
  2. 新增4个模型的预训练权重及其策略: googlenet, inception_v3, inception_v4, xception
  • 2022/12/09
  1. 支持在所有学习率策略中添加学习率预热操作,除cosine decay策略外
  2. 支持Repeated Augmenation操作,可以通过--aug_repeats对其进行设置,设置值应大于1(通常为3或4)
  3. 支持EMA
  4. 通过支持mixup和cutmix操作进一步优化BCE损失函数
  • 2022/11/21
  1. 支持模型损失和正确率的可视化
  2. 支持轮次维度的cosine decay策略的学习率预热操作(之前仅支持步维度)
  • 2022/11/09
  1. 支持2个ViT预训练模型
  2. 支持RandAugment augmentation操作
  3. 提高了CutMix操作的可用性,CutMix和Mixup目前可以一起使用
  4. 解决了学习率画图的bug
  • 2022/10/12
  1. BCE和CE损失函数目前都支持class-weight config操作、label smoothing操作、auxilary logit input操作(适用于类似Inception模型)
  • 2022/09/13
  1. 支持Adan优化器(试用版)

贡献方式

欢迎开发者用户提issue或提交代码PR,或贡献更多的算法和模型,一起让MindCV变得更好。

有关贡献指南,请参阅CONTRIBUTING.md。 请遵循模型编写指南所规定的规则来贡献模型接口:)

许可证

本项目遵循Apache License 2.0开源协议。

致谢

MindCV是由MindSpore团队、西安电子科技大学、西安交通大学联合开发的开源项目。 衷心感谢所有参与的研究人员和开发人员为这个项目所付出的努力。 十分感谢 OpenI 平台所提供的算力资源。

引用

如果你觉得MindCV对你的项目有帮助,请考虑引用:

@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}
}
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] 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.

简介

MindSpore Computer Vision is an open source computer vision research toolbox based on MindSpore in computer vision direction. 展开 收起
Python 等 2 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/mindspore-lab/mindcv.git
git@gitee.com:mindspore-lab/mindcv.git
mindspore-lab
mindcv
mindcv
main

搜索帮助