“75aca6d4f1602aa21a70378c17889aa537caba04”上不存在“tools/git@gitcode.net:paddlepaddle/Paddle.git”
提交 6bc03855 编写于 作者: L leiyuning

initial version

上级
# Built html files
api/build_en
api/build_zh_cn
docs/build_en
docs/build_zh_cn
tutorials/build_en
tutorials/build_zh_cn
# Workspace
.idea/
.vscode/
# Contributing Documents
You are welcome to contribute MindSpore documents. Documents that meet requirements will be displayed on the [MindSpore official website](https://www.mindspore.cn).
<!-- TOC -->
- [Contributing Documents](#contributing-documents)
- [Creating or Updating Documents](#creating-or-updating-documents)
- [Submitting Modification](#submitting-modification)
- [Document Writing Specifications](#document-writing-specifications)
<!-- /TOC -->
## Creating or Updating Documents
This project supports contribution documents in MarkDown and reStructuredText formats. You can create the ```.md``` or ```.rst``` files or modify existing documents.
## Submitting Modification
The procedure for submitting the modification is the same as that for submitting the code. For details, see [Code Contribution Guide](https://gitee.com/mindspore/mindspore/blob/master/CONTRIBUTING.md).
## Document Writing Specifications
- The title supports only the ATX style. The title and context must be separated by a blank line.
```
# Heading 1
## Heading 2
### Heading 3
```
- If the list title and content need to be displayed in different lines, add a blank line between the title and content. Otherwise, the line breaks may not be implemented.
```
- Title
Content
```
- Anchors (hyperlinks) in the table of content can contain only Chinese characters, lowercase letters, and hyphens (-). Spaces or other special characters are not allowed. Otherwise, the link is invalid.
- Precautions are marked with a right angle bracket (>).
```
> Precautions
```
- References should be listed at the end of the document and marked in the document.
```
Add a [number] after the referenced text or image description.
## References
[1] Author. [Document Name](http://xxx).
[2] Author. Document Name.
```
- Comments in the sample code must comply with the following requirements:
- Comments are written in English.
- Use ```"""``` to comment out Python functions, methods, and classes.
- Use ```#``` to comment out other Python code.
- Use ```//``` to comment out C++ code.
```
"""
Comments on Python functions, methods, and classes
"""
# Python code comments
// C++ code comments
```
- A blank line must be added before and after an image and an image title. Otherwise, the typesetting will be abnormal.
```
Example:
![](./xxx.png)
Figure 1: xxx
The following content.
```
\ No newline at end of file
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.
Attribution 4.0 International
=======================================================================
Creative Commons Corporation ("Creative Commons") is not a law firm and
does not provide legal services or legal advice. Distribution of
Creative Commons public licenses does not create a lawyer-client or
other relationship. Creative Commons makes its licenses and related
information available on an "as-is" basis. Creative Commons gives no
warranties regarding its licenses, any material licensed under their
terms and conditions, or any related information. Creative Commons
disclaims all liability for damages resulting from their use to the
fullest extent possible.
Using Creative Commons Public Licenses
Creative Commons public licenses provide a standard set of terms and
conditions that creators and other rights holders may use to share
original works of authorship and other material subject to copyright
and certain other rights specified in the public license below. The
following considerations are for informational purposes only, are not
exhaustive, and do not form part of our licenses.
Considerations for licensors: Our public licenses are
intended for use by those authorized to give the public
permission to use material in ways otherwise restricted by
copyright and certain other rights. Our licenses are
irrevocable. Licensors should read and understand the terms
and conditions of the license they choose before applying it.
Licensors should also secure all rights necessary before
applying our licenses so that the public can reuse the
material as expected. Licensors should clearly mark any
material not subject to the license. This includes other CC-
licensed material, or material used under an exception or
limitation to copyright. More considerations for licensors:
wiki.creativecommons.org/Considerations_for_licensors
Considerations for the public: By using one of our public
licenses, a licensor grants the public permission to use the
licensed material under specified terms and conditions. If
the licensor's permission is not necessary for any reason--for
example, because of any applicable exception or limitation to
copyright--then that use is not regulated by the license. Our
licenses grant only permissions under copyright and certain
other rights that a licensor has authority to grant. Use of
the licensed material may still be restricted for other
reasons, including because others have copyright or other
rights in the material. A licensor may make special requests,
such as asking that all changes be marked or described.
Although not required by our licenses, you are encouraged to
respect those requests where reasonable. More_considerations
for the public:
wiki.creativecommons.org/Considerations_for_licensees
=======================================================================
Creative Commons Attribution 4.0 International Public License
By exercising the Licensed Rights (defined below), You accept and agree
to be bound by the terms and conditions of this Creative Commons
Attribution 4.0 International Public License ("Public License"). To the
extent this Public License may be interpreted as a contract, You are
granted the Licensed Rights in consideration of Your acceptance of
these terms and conditions, and the Licensor grants You such rights in
consideration of benefits the Licensor receives from making the
Licensed Material available under these terms and conditions.
Section 1 -- Definitions.
a. Adapted Material means material subject to Copyright and Similar
Rights that is derived from or based upon the Licensed Material
and in which the Licensed Material is translated, altered,
arranged, transformed, or otherwise modified in a manner requiring
permission under the Copyright and Similar Rights held by the
Licensor. For purposes of this Public License, where the Licensed
Material is a musical work, performance, or sound recording,
Adapted Material is always produced where the Licensed Material is
synched in timed relation with a moving image.
b. Adapter's License means the license You apply to Your Copyright
and Similar Rights in Your contributions to Adapted Material in
accordance with the terms and conditions of this Public License.
c. Copyright and Similar Rights means copyright and/or similar rights
closely related to copyright including, without limitation,
performance, broadcast, sound recording, and Sui Generis Database
Rights, without regard to how the rights are labeled or
categorized. For purposes of this Public License, the rights
specified in Section 2(b)(1)-(2) are not Copyright and Similar
Rights.
d. Effective Technological Measures means those measures that, in the
absence of proper authority, may not be circumvented under laws
fulfilling obligations under Article 11 of the WIPO Copyright
Treaty adopted on December 20, 1996, and/or similar international
agreements.
e. Exceptions and Limitations means fair use, fair dealing, and/or
any other exception or limitation to Copyright and Similar Rights
that applies to Your use of the Licensed Material.
f. Licensed Material means the artistic or literary work, database,
or other material to which the Licensor applied this Public
License.
g. Licensed Rights means the rights granted to You subject to the
terms and conditions of this Public License, which are limited to
all Copyright and Similar Rights that apply to Your use of the
Licensed Material and that the Licensor has authority to license.
h. Licensor means the individual(s) or entity(ies) granting rights
under this Public License.
i. Share means to provide material to the public by any means or
process that requires permission under the Licensed Rights, such
as reproduction, public display, public performance, distribution,
dissemination, communication, or importation, and to make material
available to the public including in ways that members of the
public may access the material from a place and at a time
individually chosen by them.
j. Sui Generis Database Rights means rights other than copyright
resulting from Directive 96/9/EC of the European Parliament and of
the Council of 11 March 1996 on the legal protection of databases,
as amended and/or succeeded, as well as other essentially
equivalent rights anywhere in the world.
k. You means the individual or entity exercising the Licensed Rights
under this Public License. Your has a corresponding meaning.
Section 2 -- Scope.
a. License grant.
1. Subject to the terms and conditions of this Public License,
the Licensor hereby grants You a worldwide, royalty-free,
non-sublicensable, non-exclusive, irrevocable license to
exercise the Licensed Rights in the Licensed Material to:
a. reproduce and Share the Licensed Material, in whole or
in part; and
b. produce, reproduce, and Share Adapted Material.
2. Exceptions and Limitations. For the avoidance of doubt, where
Exceptions and Limitations apply to Your use, this Public
License does not apply, and You do not need to comply with
its terms and conditions.
3. Term. The term of this Public License is specified in Section
6(a).
4. Media and formats; technical modifications allowed. The
Licensor authorizes You to exercise the Licensed Rights in
all media and formats whether now known or hereafter created,
and to make technical modifications necessary to do so. The
Licensor waives and/or agrees not to assert any right or
authority to forbid You from making technical modifications
necessary to exercise the Licensed Rights, including
technical modifications necessary to circumvent Effective
Technological Measures. For purposes of this Public License,
simply making modifications authorized by this Section 2(a)
(4) never produces Adapted Material.
5. Downstream recipients.
a. Offer from the Licensor -- Licensed Material. Every
recipient of the Licensed Material automatically
receives an offer from the Licensor to exercise the
Licensed Rights under the terms and conditions of this
Public License.
b. No downstream restrictions. You may not offer or impose
any additional or different terms or conditions on, or
apply any Effective Technological Measures to, the
Licensed Material if doing so restricts exercise of the
Licensed Rights by any recipient of the Licensed
Material.
6. No endorsement. Nothing in this Public License constitutes or
may be construed as permission to assert or imply that You
are, or that Your use of the Licensed Material is, connected
with, or sponsored, endorsed, or granted official status by,
the Licensor or others designated to receive attribution as
provided in Section 3(a)(1)(A)(i).
b. Other rights.
1. Moral rights, such as the right of integrity, are not
licensed under this Public License, nor are publicity,
privacy, and/or other similar personality rights; however, to
the extent possible, the Licensor waives and/or agrees not to
assert any such rights held by the Licensor to the limited
extent necessary to allow You to exercise the Licensed
Rights, but not otherwise.
2. Patent and trademark rights are not licensed under this
Public License.
3. To the extent possible, the Licensor waives any right to
collect royalties from You for the exercise of the Licensed
Rights, whether directly or through a collecting society
under any voluntary or waivable statutory or compulsory
licensing scheme. In all other cases the Licensor expressly
reserves any right to collect such royalties.
Section 3 -- License Conditions.
Your exercise of the Licensed Rights is expressly made subject to the
following conditions.
a. Attribution.
1. If You Share the Licensed Material (including in modified
form), You must:
a. retain the following if it is supplied by the Licensor
with the Licensed Material:
i. identification of the creator(s) of the Licensed
Material and any others designated to receive
attribution, in any reasonable manner requested by
the Licensor (including by pseudonym if
designated);
ii. a copyright notice;
iii. a notice that refers to this Public License;
iv. a notice that refers to the disclaimer of
warranties;
v. a URI or hyperlink to the Licensed Material to the
extent reasonably practicable;
b. indicate if You modified the Licensed Material and
retain an indication of any previous modifications; and
c. indicate the Licensed Material is licensed under this
Public License, and include the text of, or the URI or
hyperlink to, this Public License.
2. You may satisfy the conditions in Section 3(a)(1) in any
reasonable manner based on the medium, means, and context in
which You Share the Licensed Material. For example, it may be
reasonable to satisfy the conditions by providing a URI or
hyperlink to a resource that includes the required
information.
3. If requested by the Licensor, You must remove any of the
information required by Section 3(a)(1)(A) to the extent
reasonably practicable.
4. If You Share Adapted Material You produce, the Adapter's
License You apply must not prevent recipients of the Adapted
Material from complying with this Public License.
Section 4 -- Sui Generis Database Rights.
Where the Licensed Rights include Sui Generis Database Rights that
apply to Your use of the Licensed Material:
a. for the avoidance of doubt, Section 2(a)(1) grants You the right
to extract, reuse, reproduce, and Share all or a substantial
portion of the contents of the database;
b. if You include all or a substantial portion of the database
contents in a database in which You have Sui Generis Database
Rights, then the database in which You have Sui Generis Database
Rights (but not its individual contents) is Adapted Material; and
c. You must comply with the conditions in Section 3(a) if You Share
all or a substantial portion of the contents of the database.
For the avoidance of doubt, this Section 4 supplements and does not
replace Your obligations under this Public License where the Licensed
Rights include other Copyright and Similar Rights.
Section 5 -- Disclaimer of Warranties and Limitation of Liability.
a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
c. The disclaimer of warranties and limitation of liability provided
above shall be interpreted in a manner that, to the extent
possible, most closely approximates an absolute disclaimer and
waiver of all liability.
Section 6 -- Term and Termination.
a. This Public License applies for the term of the Copyright and
Similar Rights licensed here. However, if You fail to comply with
this Public License, then Your rights under this Public License
terminate automatically.
b. Where Your right to use the Licensed Material has terminated under
Section 6(a), it reinstates:
1. automatically as of the date the violation is cured, provided
it is cured within 30 days of Your discovery of the
violation; or
2. upon express reinstatement by the Licensor.
For the avoidance of doubt, this Section 6(b) does not affect any
right the Licensor may have to seek remedies for Your violations
of this Public License.
c. For the avoidance of doubt, the Licensor may also offer the
Licensed Material under separate terms or conditions or stop
distributing the Licensed Material at any time; however, doing so
will not terminate this Public License.
d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
License.
Section 7 -- Other Terms and Conditions.
a. The Licensor shall not be bound by any additional or different
terms or conditions communicated by You unless expressly agreed.
b. Any arrangements, understandings, or agreements regarding the
Licensed Material not stated herein are separate from and
independent of the terms and conditions of this Public License.
Section 8 -- Interpretation.
a. For the avoidance of doubt, this Public License does not, and
shall not be interpreted to, reduce, limit, restrict, or impose
conditions on any use of the Licensed Material that could lawfully
be made without permission under this Public License.
b. To the extent possible, if any provision of this Public License is
deemed unenforceable, it shall be automatically reformed to the
minimum extent necessary to make it enforceable. If the provision
cannot be reformed, it shall be severed from this Public License
without affecting the enforceability of the remaining terms and
conditions.
c. No term or condition of this Public License will be waived and no
failure to comply consented to unless expressly agreed to by the
Licensor.
d. Nothing in this Public License constitutes or may be interpreted
as a limitation upon, or waiver of, any privileges and immunities
that apply to the Licensor or You, including from the legal
processes of any jurisdiction or authority.
=======================================================================
Creative Commons is not a party to its public
licenses. Notwithstanding, Creative Commons may elect to apply one of
its public licenses to material it publishes and in those instances
will be considered the “Licensor.” The text of the Creative Commons
public licenses is dedicated to the public domain under the CC0 Public
Domain Dedication. Except for the limited purpose of indicating that
material is shared under a Creative Commons public license or as
otherwise permitted by the Creative Commons policies published at
creativecommons.org/policies, Creative Commons does not authorize the
use of the trademark "Creative Commons" or any other trademark or logo
of Creative Commons without its prior written consent including,
without limitation, in connection with any unauthorized modifications
to any of its public licenses or any other arrangements,
understandings, or agreements concerning use of licensed material. For
the avoidance of doubt, this paragraph does not form part of the
public licenses.
Creative Commons may be contacted at creativecommons.org.
MindSpore Document
Copyright 2019-2020 Huawei Technologies Co., Ltd
# MindSpore Documents
![MindSpore Logo](resource/MindSpore-logo.png)
## Overview
This project provides the source files of the installation guide, tutorials, and other documents, as well as API configurations on the MindSpore official website <https://www.mindspore.cn>.
## Contributions
You are welcome to contribute documents. If you want to contribute documents, read the [CONTRIBUTING_DOC.md](./CONTRIBUTING_DOC.md). Please comply with the document writing specifications, and submit documents according to the process rules. After the documents are approved, the changes will be displayed in the document project and on the official website.
If you have any comments or suggestions on the documents, submit them in Issues.
## Directory Structure Description
```
docs
├───api // Configuration files for API generation.
├───docs // Introduction to documents.
├───install // Installation guide.
├───resource // Resource-related documents.
├───tutorials // Tutorial-related documents.
└───README.md // Docs repository description.
```
## Document Construction
MindSpore tutorials and API documents can be generated by [Sphinx](https://www.sphinx-doc.org/en/master/). The following uses the API document as an example to describe the procedure.
1. Download code of the MindSpore Docs repository.
```shell
git clone https://gitee.com/mindspore/docs.git
```
2. Go to the docs directory and install the dependency items in the ```requirements.txt``` file.
```shell
cd docs/api
pip install -r requirements.txt
```
3. Run the following command in the docs directory to create the build_zh_cn/html directory that stores the generated document web page. You can open ```build_zh_cn/html/index.html``` to view the API document.
```
make html
```
## License
- [Apache License 2.0](LICENSE)
- [Creative Commons License version 4.0](LICENSE-CC-BY-4.0)
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source_zh_cn
BUILDDIR = build_zh_cn
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
sphinx
recommonmark
sphinx-markdown-tables
sphinx_rtd_theme
numpy
jieba
\ No newline at end of file
# !/bin/bash
make html
if [ $? -ne 0 ]; then
echo "make html failed"
exit
fi
cd build_zh_cn/html
python -m http.server
mindarmour.attacks
==================
.. automodule:: mindarmour.attacks
:members:
\ No newline at end of file
mindarmour.defenses
===================
.. automodule:: mindarmour.defenses
:members:
\ No newline at end of file
mindarmour.detectors
====================
.. automodule:: mindarmour.detectors
:members:
\ No newline at end of file
mindarmour.evaluations
======================
.. automodule:: mindarmour.evaluations
:members:
\ No newline at end of file
mindarmour
==========
.. automodule:: mindarmour
:members:
\ No newline at end of file
mindarmour.utils
================
.. automodule:: mindarmour.utils
:members:
\ No newline at end of file
mindinsight.lineagemgr
======================
.. automodule:: mindinsight.lineagemgr
:members:
\ No newline at end of file
mindspore.common.initializer
============================
.. automodule:: mindspore.common.initializer
:members:
\ No newline at end of file
mindspore.communication
=======================
.. automodule:: mindspore.communication
:members:
mindspore.context
=================
.. automodule:: mindspore.context
:members:
mindspore.dataset
=================
.. automodule:: mindspore.dataset
:members:
:inherited-members:
:exclude-members: get_args, read_dir
.. autodata:: config
.. autoclass:: mindspore.dataset.core.configuration.ConfigurationManager
:members:
mindspore.dataset.transforms.c_transforms
=========================================
.. automodule:: mindspore.dataset.transforms.c_transforms
:members:
mindspore.dataset.transforms.py_transforms
==========================================
.. automodule:: mindspore.dataset.transforms.py_transforms
:members:
mindspore.dataset.transforms.vision.c_transforms
================================================
.. automodule:: mindspore.dataset.transforms.vision.c_transforms
:members:
mindspore.dataset.transforms.vision.py_transforms
=================================================
.. automodule:: mindspore.dataset.transforms.vision.py_transforms
:members:
mindspore.dtype
===============
Data Type
----------
.. class:: mindspore.dtype
The actual path of ``dtype`` is ``/mindspore/common/dtype.py``.
Run the following command to import the package:
.. code-block::
import mindspore.common.dtype as mstype
or
.. code-block::
from mindspore import dtype as mstype
Numeric Type
~~~~~~~~~~~~
Currently, MindSpore supports ``Int`` type, ``Uint`` type and ``Float`` type.
The following table lists the details.
============================================== =============================
Definition Description
============================================== =============================
``mindspore.int8`` , ``mindspore.byte`` 8-bit integer
``mindspore.int16`` , ``mindspore.short`` 16-bit integer
``mindspore.int32`` , ``mindspore.intc`` 32-bit integer
``mindspore.int64`` , ``mindspore.intp`` 64-bit integer
``mindspore.uint8`` , ``mindspore.ubyte`` unsigned 8-bit integer
``mindspore.uint16`` , ``mindspore.ushort`` unsigned 16-bit integer
``mindspore.uint32`` , ``mindspore.uintc`` unsigned 32-bit integer
``mindspore.uint64`` , ``mindspore.uintp`` unsigned 64-bit integer
``mindspore.float16`` , ``mindspore.half`` 16-bit floating-point number
``mindspore.float32`` , ``mindspore.single`` 32-bit floating-point number
``mindspore.float64`` , ``mindspore.double`` 64-bit floating-point number
============================================== =============================
Other Type
~~~~~~~~~~
For other defined types, see the following table.
============================ =================
Type Description
============================ =================
``tensor`` MindSpore's ``tensor`` type. Data format uses NCHW.
``MetaTensor`` A tensor only has data type and shape.
``bool_`` Bool number.
``int_`` Integer scalar.
``uint`` Unsigned integer scalar.
``float_`` Floating-point scalar.
``number`` Number, including ``int_`` , ``uint`` , ``float_`` and ``bool_`` .
``list_`` List constructed by ``tensor`` , such as ``List[T0,T1,...,Tn]`` , where the element ``Ti`` can be of different types.
``tuple_`` Tuple constructed by ``tensor`` , such as ``Tuple[T0,T1,...,Tn]`` , where the element ``Ti`` can be of different types.
``function`` Function. Return in two ways, one returns ``Func`` directly, the other returns ``Func(args: List[T0,T1,...,Tn], retval: T)`` .
``type_type`` Type of type.
``type_none`` No matching return type, corresponding to the ``type(None)`` in Python.
``symbolic_key`` The value of a variable managed by embd, which is used as a key of the variable in ``env_type`` .
``env_type`` Used to store the gradient of the free variable of a function, where the key is the ``symbolic_key`` of the free variable's node and the value is the gradient.
============================ =================
Tree Topology
~~~~~~~~~~~~~~
The relationships of the above types are as follows:
.. code-block::
└─────── number
│ ├─── bool_
│ ├─── int_
│ │ ├─── int8, byte
│ │ ├─── int16, short
│ │ ├─── int32, intc
│ │ └─── int64, intp
│ ├─── uint
│ │ ├─── uint8, ubyte
│ │ ├─── uint16, ushort
│ │ ├─── uint32, uintc
│ │ └─── uint64, uintp
│ └─── float_
│ ├─── float16
│ ├─── float32
│ └─── float64
├─── tensor
│ ├─── Array[Float32]
│ └─── ...
├─── list_
│ ├─── List[Int32,Float32]
│ └─── ...
├─── tuple_
│ ├─── Tuple[Int32,Float32]
│ └─── ...
├─── function
│ ├─── Func
│ ├─── Func[(Int32, Float32), Int32]
│ └─── ...
├─── MetaTensor
├─── type_type
├─── type_none
├─── symbolic_key
└─── env_type
\ No newline at end of file
mindspore.mindrecord
====================
.. automodule:: mindspore.mindrecord
:members:
\ No newline at end of file
mindspore.nn
============
.. automodule:: mindspore.nn
:members:
mindspore.ops.composite
=======================
.. automodule:: mindspore.ops.composite
:members:
mindspore.ops.operations
========================
.. automodule:: mindspore.ops.operations
:members:
mindspore.ops
=============
.. automodule:: mindspore.ops
:members:
:exclude-members: signature_rw, signature_kind
mindspore.parallel
==================
.. automodule:: mindspore.parallel
:members:
mindspore
=========
.. automodule:: mindspore
:members:
\ No newline at end of file
mindspore.train
===============
.. automodule:: mindspore.train.summary
:members:
.. automodule:: mindspore.train.callback
:members:
.. automodule:: mindspore.train.serialization
:members:
.. automodule:: mindspore.train.amp
:members:
.. automodule:: mindspore.train.loss_scale_manager
:members:
\ No newline at end of file
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
# import sys
# sys.path.append('..')
# sys.path.insert(0, os.path.abspath('.'))
import mindspore
# If you don't want to generate MindInsight APIs, comment this line.
import mindinsight
# If you don't want to generate MindArmour APIs, comment this line.
import mindarmour
# -- Project information -----------------------------------------------------
project = 'MindSpore'
copyright = '2020, MindSpore'
author = 'MindSpore'
# The full version, including alpha/beta/rc tags
release = '0.1.0-alpha'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.autosummary',
'sphinx.ext.doctest',
'sphinx.ext.intersphinx',
'sphinx.ext.todo',
'sphinx.ext.coverage',
'sphinx.ext.napoleon',
'sphinx.ext.viewcode',
'sphinx_markdown_tables',
'recommonmark',
]
source_suffix = {
'.rst': 'restructuredtext',
'.md': 'markdown',
}
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
pygments_style = 'sphinx'
autodoc_inherit_docstrings = False
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
# -- Options for Texinfo output -------------------------------------------
# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {
'python': ('https://docs.python.org/', '../python_objects.inv'),
'numpy': ('https://docs.scipy.org/doc/numpy/', '../numpy_objects.inv'),
}
.. MindSpore documentation master file, created by
sphinx-quickstart on Thu Mar 24 11:00:00 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
MindSpore API
=============
.. toctree::
:maxdepth: 1
:caption: Python API
api/python/mindspore/mindspore
api/python/mindspore/mindspore.dtype
api/python/mindspore/mindspore.common.initializer
api/python/mindspore/mindspore.communication
api/python/mindspore/mindspore.context
api/python/mindspore/mindspore.nn
api/python/mindspore/mindspore.ops
api/python/mindspore/mindspore.ops.composite
api/python/mindspore/mindspore.ops.operations
api/python/mindspore/mindspore.parallel
api/python/mindspore/mindspore.train
api/python/mindspore/mindspore.dataset
api/python/mindspore/mindspore.dataset.transforms.c_transforms
api/python/mindspore/mindspore.dataset.transforms.vision.c_transforms
api/python/mindspore/mindspore.dataset.transforms.py_transforms
api/python/mindspore/mindspore.dataset.transforms.vision.py_transforms
api/python/mindspore/mindspore.mindrecord
api/python/mindinsight/mindinsight.lineagemgr
api/python/mindarmour/mindarmour
api/python/mindarmour/mindarmour.utils
api/python/mindarmour/mindarmour.evaluations
api/python/mindarmour/mindarmour.detectors
api/python/mindarmour/mindarmour.attacks
api/python/mindarmour/mindarmour.defenses
.. toctree::
:maxdepth: 1
:caption: C++ API
predict <https://www.mindspore.cn/apicc/en/0.1.0-alpha/predict/namespacemembers.html>
mindarmour.attacks
==================
.. automodule:: mindarmour.attacks
:members:
\ No newline at end of file
mindarmour.defenses
===================
.. automodule:: mindarmour.defenses
:members:
\ No newline at end of file
mindarmour.detectors
====================
.. automodule:: mindarmour.detectors
:members:
\ No newline at end of file
mindarmour.evaluations
======================
.. automodule:: mindarmour.evaluations
:members:
\ No newline at end of file
mindarmour
==========
.. automodule:: mindarmour
:members:
\ No newline at end of file
mindarmour.utils
================
.. automodule:: mindarmour.utils
:members:
\ No newline at end of file
mindinsight.lineagemgr
======================
.. automodule:: mindinsight.lineagemgr
:members:
\ No newline at end of file
mindspore.common.initializer
============================
.. automodule:: mindspore.common.initializer
:members:
\ No newline at end of file
mindspore.communication
=======================
.. automodule:: mindspore.communication
:members:
mindspore.context
=================
.. automodule:: mindspore.context
:members:
mindspore.dataset
=================
.. automodule:: mindspore.dataset
:members:
:inherited-members:
:exclude-members: get_args, read_dir
.. autodata:: config
.. autoclass:: mindspore.dataset.core.configuration.ConfigurationManager
:members:
mindspore.dataset.transforms.c_transforms
=========================================
.. automodule:: mindspore.dataset.transforms.c_transforms
:members:
mindspore.dataset.transforms.py_transforms
==========================================
.. automodule:: mindspore.dataset.transforms.py_transforms
:members:
mindspore.dataset.transforms.vision.c_transforms
================================================
.. automodule:: mindspore.dataset.transforms.vision.c_transforms
:members:
mindspore.dataset.transforms.vision.py_transforms
=================================================
.. automodule:: mindspore.dataset.transforms.vision.py_transforms
:members:
mindspore.dtype
===============
Data Type
----------
.. class:: mindspore.dtype
The actual path of ``dtype`` is ``/mindspore/common/dtype.py``.
Run the following command to import the package:
.. code-block::
import mindspore.common.dtype as mstype
or
.. code-block::
from mindspore import dtype as mstype
Numeric Type
~~~~~~~~~~~~
Currently, MindSpore supports ``Int`` type, ``Uint`` type and ``Float`` type.
The following table lists the details.
============================================== =============================
Definition Description
============================================== =============================
``mindspore.int8`` , ``mindspore.byte`` 8-bit integer
``mindspore.int16`` , ``mindspore.short`` 16-bit integer
``mindspore.int32`` , ``mindspore.intc`` 32-bit integer
``mindspore.int64`` , ``mindspore.intp`` 64-bit integer
``mindspore.uint8`` , ``mindspore.ubyte`` unsigned 8-bit integer
``mindspore.uint16`` , ``mindspore.ushort`` unsigned 16-bit integer
``mindspore.uint32`` , ``mindspore.uintc`` unsigned 32-bit integer
``mindspore.uint64`` , ``mindspore.uintp`` unsigned 64-bit integer
``mindspore.float16`` , ``mindspore.half`` 16-bit floating-point number
``mindspore.float32`` , ``mindspore.single`` 32-bit floating-point number
``mindspore.float64`` , ``mindspore.double`` 64-bit floating-point number
============================================== =============================
Other Type
~~~~~~~~~~
For other defined types, see the following table.
============================ =================
Type Description
============================ =================
``tensor`` MindSpore's ``tensor`` type. Data format uses NCHW.
``MetaTensor`` A tensor only has data type and shape.
``bool_`` Bool number.
``int_`` Integer scalar.
``uint`` Unsigned integer scalar.
``float_`` Floating-point scalar.
``number`` Number, including ``int_`` , ``uint`` , ``float_`` and ``bool_`` .
``list_`` List constructed by ``tensor`` , such as ``List[T0,T1,...,Tn]`` , where the element ``Ti`` can be of different types.
``tuple_`` Tuple constructed by ``tensor`` , such as ``Tuple[T0,T1,...,Tn]`` , where the element ``Ti`` can be of different types.
``function`` Function. Return in two ways, one returns ``Func`` directly, the other returns ``Func(args: List[T0,T1,...,Tn], retval: T)`` .
``type_type`` Type of type.
``type_none`` No matching return type, corresponding to the ``type(None)`` in Python.
``symbolic_key`` The value of a variable managed by embd, which is used as a key of the variable in ``env_type`` .
``env_type`` Used to store the gradient of the free variable of a function, where the key is the ``symbolic_key`` of the free variable's node and the value is the gradient.
============================ =================
Tree Topology
~~~~~~~~~~~~~~
The relationships of the above types are as follows:
.. code-block::
└─── mindspore.dtype
├─── number
│ ├─── bool_
│ ├─── int_
│ │ ├─── int8, byte
│ │ ├─── int16, short
│ │ ├─── int32, intc
│ │ └─── int64, intp
│ ├─── uint
│ │ ├─── uint8, ubyte
│ │ ├─── uint16, ushort
│ │ ├─── uint32, uintc
│ │ └─── uint64, uintp
│ └─── float_
│ ├─── float16
│ ├─── float32
│ └─── float64
├─── tensor
│ ├─── Array[float32]
│ └─── ...
├─── list_
│ ├─── List[int32,float32]
│ └─── ...
├─── tuple_
│ ├─── Tuple[int32,float32]
│ └─── ...
├─── function
│ ├─── Func
│ ├─── Func[(int32, float32), int32]
│ └─── ...
├─── MetaTensor
├─── type_type
├─── type_none
├─── symbolic_key
└─── env_type
\ No newline at end of file
mindspore.mindrecord
====================
.. automodule:: mindspore.mindrecord
:members:
\ No newline at end of file
mindspore.nn
============
.. automodule:: mindspore.nn
:members:
mindspore.ops.composite
=======================
.. automodule:: mindspore.ops.composite
:members:
mindspore.ops.operations
========================
.. automodule:: mindspore.ops.operations
:members:
mindspore.ops
=============
.. automodule:: mindspore.ops
:members:
:exclude-members: signature_rw, signature_kind
mindspore.parallel
==================
.. automodule:: mindspore.parallel
:members:
mindspore
=========
.. automodule:: mindspore
:members:
\ No newline at end of file
mindspore.train
===============
.. automodule:: mindspore.train.summary
:members:
.. automodule:: mindspore.train.callback
:members:
.. automodule:: mindspore.train.serialization
:members:
.. automodule:: mindspore.train.amp
:members:
.. automodule:: mindspore.train.loss_scale_manager
:members:
\ No newline at end of file
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
# import sys
# sys.path.append('..')
# sys.path.insert(0, os.path.abspath('.'))
import mindspore
# If you don't want to generate MindInsight APIs, comment this line.
import mindinsight
# If you don't want to generate MindArmour APIs, comment this line.
import mindarmour
# -- Project information -----------------------------------------------------
project = 'MindSpore'
copyright = '2020, MindSpore'
author = 'MindSpore'
# The full version, including alpha/beta/rc tags
release = '0.1.0-alpha'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.autosummary',
'sphinx.ext.doctest',
'sphinx.ext.intersphinx',
'sphinx.ext.todo',
'sphinx.ext.coverage',
'sphinx.ext.napoleon',
'sphinx.ext.viewcode',
'sphinx_markdown_tables',
'recommonmark',
]
source_suffix = {
'.rst': 'restructuredtext',
'.md': 'markdown',
}
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
pygments_style = 'sphinx'
autodoc_inherit_docstrings = False
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_search_language = 'zh'
html_search_options = {'dict': '../resource/jieba.txt'}
# -- Options for Texinfo output -------------------------------------------
# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {
'python': ('https://docs.python.org/', '../python_objects.inv'),
'numpy': ('https://docs.scipy.org/doc/numpy/', '../numpy_objects.inv'),
}
.. MindSpore documentation master file, created by
sphinx-quickstart on Thu Mar 24 11:00:00 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
MindSpore API
=============
.. toctree::
:maxdepth: 1
:caption: Python API
api/python/mindspore/mindspore
api/python/mindspore/mindspore.dtype
api/python/mindspore/mindspore.common.initializer
api/python/mindspore/mindspore.communication
api/python/mindspore/mindspore.context
api/python/mindspore/mindspore.nn
api/python/mindspore/mindspore.ops
api/python/mindspore/mindspore.ops.composite
api/python/mindspore/mindspore.ops.operations
api/python/mindspore/mindspore.parallel
api/python/mindspore/mindspore.train
api/python/mindspore/mindspore.dataset
api/python/mindspore/mindspore.dataset.transforms.c_transforms
api/python/mindspore/mindspore.dataset.transforms.vision.c_transforms
api/python/mindspore/mindspore.dataset.transforms.py_transforms
api/python/mindspore/mindspore.dataset.transforms.vision.py_transforms
api/python/mindspore/mindspore.mindrecord
api/python/mindinsight/mindinsight.lineagemgr
api/python/mindarmour/mindarmour
api/python/mindarmour/mindarmour.utils
api/python/mindarmour/mindarmour.evaluations
api/python/mindarmour/mindarmour.detectors
api/python/mindarmour/mindarmour.attacks
api/python/mindarmour/mindarmour.defenses
.. toctree::
:maxdepth: 1
:caption: C++ API
predict <https://www.mindspore.cn/apicc/zh-CN/0.1.0-alpha/predict/namespacemembers.html>
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source_zh_cn
BUILDDIR = build_zh_cn
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
sphinx
recommonmark
sphinx-markdown-tables
sphinx_rtd_theme
numpy
jieba
\ No newline at end of file
# Overall Architecture
This document describes the overall architecture of MindSpore.
<!-- TOC -->
- [Overall Architecture](#overall-architecture)
<!-- /TOC -->
The MindSpore framework consists of the Frontend Expression layer, Graph Engine layer, and Backend Runtime layer.
![architecture](./images/architecture.png)
- MindSpore Frontend Expression layer
This layer contains Python APIs, MindSpore intermediate representation (IR), and graph high level optimization (GHLO).
- Python APIs provide users with a unified API for model training, inference, and export, and a unified API for data processing and format transformation.
- GHLO includes optimization irrelevant to hardware (such as dead code elimination), auto parallel, and auto differentiation.
- MindSpore IR provides unified intermediate representations, based on which MindSpore performs pass optimization.
- MindSpore Graph Engine layer
This layer contains graph low level optimization (GLLO) and graph execution.
- GLLO includes hardware-related optimization and in-depth optimization related to the combination of hardware and software, such as operator fusion and buffer fusion.
- Graph execution provides communication APIs required for offline graph execution and distributed training.
- MindSpore Backend Runtime layer
This layer contains the efficient running environments on the cloud, edge and device.
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
# -- Project information -----------------------------------------------------
project = 'MindSpore'
copyright = '2020, MindSpore'
author = 'MindSpore'
# The full version, including alpha/beta/rc tags
release = '0.1.0-alpha'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx_markdown_tables',
'recommonmark',
]
source_suffix = {
'.rst': 'restructuredtext',
'.md': 'markdown',
}
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
pygments_style = 'sphinx'
autodoc_inherit_docstrings = False
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
\ No newline at end of file
# Constraints on Network Construction Using Python
<!-- TOC -->
- [Constraints on Network Construction Using Python](#constraints-on-network-construction-using-python)
- [Overview](#overview)
- [Syntax Constraints](#syntax-constraints)
- [Supported Python Data Types](#supported-python-data-types)
- [MindSpore Extended Data Type](#mindspore-extended-data-type)
- [Expression Types](#expression-types)
- [Statement Types](#statement-types)
- [System Functions](#system-functions)
- [Function Parameters](#function-parameters)
- [Operators](#operators)
- [Slicing Operations](#slicing-operations)
- [Unsupported Syntax](#unsupported-syntax)
- [Network Definition Constraints](#network-definition-constraints)
- [Instance Types on the Entire Network](#instance-types-on-the-entire-network)
- [Network Input Type](#network-input-type)
- [Network Graph Optimization](#network-graph-optimization)
- [Network Construction Components](#network-construction-components)
- [Other Constraints](#other-constraints)
<!-- /TOC -->
## Overview
MindSpore can compile user source code based on the Python syntax into computational graphs, and can convert common functions or instances inherited from nn.Cell into computational graphs. Currently, MindSpore does not support conversion of any Python source code into computational graphs. Therefore, there are constraints on source code compilation, including syntax constraints and network definition constraints. As MindSpore evolves, the constraints may change.
## Syntax Constraints
### Supported Python Data Types
* Number: supports `int`, `float`, and `bool`. Complex numbers are not supported.
* String
* List: supports the append method only. Updating a list will generate a new list.
* Tuple
* Dictionary: The type of key should be String.
### MindSpore Extended Data Type
* Tensor: Tensor variables must be defined instances.
### Expression Types
| Operation | Description
| :----------- |:--------
| Unary operator |`+`,`-`, and`not`. The operator `+` supports only scalars.
| Binary operator |`+`, `-`, `*`, `/`, and `%`.
| `if` expression | For example, `a = x if x < y else y`.
| Comparison expression | `>`, `>=`, `<`, `<=`, `==`, and `! =`.
| Logical expression | `and` and `or`.
| `lambda` expression | For example, `lambda x, y: x + y`.
| Reserved keyword type | `True`, `False`, and `None`.
### Statement Types
| Statement | Compared with Python
| :----------- |:--------
| `for` | Nested for loops are partially supported. Iteration sequences must be tuples or lists.
| `while` | Nested while loops are partially supported.
| `if` | Same as that in Python. The input of the `if` condition must be a constant.
| `def` | Same as that in Python.
| Assignment statement | Accessed multiple subscripts of lists and dictionaries cannot be used as l-value.
### System Functions
* len
* partial
* map
* zip
* range
### Function Parameters
* Default parameter value: The data types `int`, `float`, `bool`, `None`, `str`, `tuple`, `list`, and `dict` are supported, whereas `Tensor` is not supported.
* Variable parameter: Functions with variable parameters cannot be used for backward propagation on computational graphs.
* Key-value pair parameter: Functions with key-value pair parameters cannot be used for backward propagation on computational graphs.
* Variable key-value pair parameter: Functions with variable key-value pairs cannot be used for backward propagation on computational graphs.
### Operators
| Operator | Supported Type
| :----------- |:--------
| `+` |Scalar, `Tensor`, and `tuple`
| `-` |Scalar and `Tensor`
| `*` |Scalar and `Tensor`
| `/` |Scalar and `Tensor`
| `[]` |The operation object type can be `list`, `tuple`, or `Tensor`. Accessed multiple subscripts of lists and dictionaries can be used as r-values instead of l-values. The index type cannot be Tensor. For details about access constraints for the tuple and Tensor types, see the description of slicing operations.
### Slicing Operations
* `tuple` slicing operation: `tuple_x[start:stop:step]`
- `tuple_x` indicates a tuple on which the slicing operation is performed.
- `start`: index where the slice starts. The value is of the `int` type, and the value range is `[-length(tuple_x), length(tuple_x) - 1]`. Default values can be used. The default settings are as follows:
- When `step > 0`, the default value is `0`.
- When `step < 0`, the default value is `length(tuple_x) - 1`.
- `end`: index where the slice ends. The value is of the `int` type, and the value range is `[-length(tuple_x) - 1, length(tuple_x)]`. Default values can be used. The default settings are as follows:
- When `step > 0`, the default value is `length(tuple_x)`.
- When `step < 0`, the default value is `-1`.
- `step`: slicing step. The value is of the `int` type, and its range is `step! = 0`. The default value `1` can be used.
* `Tensor` slicing operation: `tensor_x[start0:stop0:step0, start1:stop1:step1, start2:stop2:step2]`
- `tensor_x` indicates a `Tensor` with at least three dimensions. The slicing operation is performed on it.
- `start0`: index where the slice starts in dimension 0. The value is of the `int` type. Default values can be used. The default settings are as follows:
- When `step > 0`, the default value is `0`.
- When `step < 0`, the default value is `-1`.
- `end0`: index where the slice ends in dimension 0. The value is of the `int` type. Default values can be used. The default settings are as follows:
- When `step > 0`, the default value is `length(tuple_x)`.
- When `step < 0`, the default value is `-(1 + length(tuple_x))`.
- `step0`: slicing step in dimension 0. The value is of the `int` type, and its range is `step! = 0`. The default value `1` can be used.
- If the number of dimensions for slicing is less than that for `Tensor`, all elements are used by default if no slice dimension is specified.
- Slice dimension reduction operation: If an integer index is transferred to a dimension, the elements of the corresponding index in the dimension is obtained and the dimension is eliminated. For example, after `tensor_x[2:4:1, 1, 0:5:2]` with shape (4, 3, 6) is sliced, a `Tensor` with shape (2, 3) is generated. The first dimension of the original `Tensor` is eliminated.
### Unsupported Syntax
Currently, the following syntax is not supported in network constructors:
`break`, `continue`, `pass`, `raise`, `yield`, `async for`, `with`, `async with`, `assert`, `import`, and `await`.
## Network Definition Constraints
### Instance Types on the Entire Network
* Common Python function with the [@ms_function](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.html#mindspore.ms_function) decorator.
* Cell subclass inherited from [nn.Cell](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Cell).
### Network Input Type
* The training data input parameters of the entire network must be of the Tensor type.
* The generated ANF diagram cannot contain the following constant nodes: string constants, constants with nested tuples, and constants with nested lists.
### Network Graph Optimization
During graph optimization at the ME frontend, the dataclass, dictionary, list, and key-value pair types are converted to tuple types, and the corresponding operations are converted to tuple operations.
### Network Construction Components
| Category | Content
| :----------- |:--------
| `Cell` instance |[mindspore/nn/*](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html), and custom [Cell](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Cell).
| Member function of a `Cell` instance | Member functions of other classes in the construct function of Cell can be called.
| Function | Custom Python functions and system functions listed in the preceding content.
| Dataclass instance | Class decorated with @dataclass.
| Primitive operator |[mindspore/ops/operations/*](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html).
| Composite operator |[mindspore/ops/composite/*](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.composite.html).
| Operator generated by constexpr |Uses the value generated by [@constexpr](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.html#mindspore.ops.constexpr) to calculate operators.
### Other Constraints
Input parameters of the construct function on the entire network and parameters of functions modified by the ms_function decorator are generalized during the graph compilation. Therefore, they cannot be transferred to operators as constant input, as shown in the following example:
* The following is an example of incorrect input:
```python
class ExpandDimsTest(Cell):
def __init__(self):
super(ExpandDimsTest, self).__init__()
self.expandDims = P.ExpandDims()
def construct(self, input_x, input_axis):
return self.expandDims(input_x, input_axis)
expand_dim = ExpandDimsTest()
input_x = Tensor(np.random.randn(2,2,2,2).astype(np.float32))
expand_dim(input_x, 0)
```
In the example, ExpandDimsTest is a single-operator network with two inputs: input_x and input_axis. The second input of the ExpandDims operator must be a constant. This is because input_axis is required when the output dimension of the ExpandDims operator is deduced during graph compilation. As the network parameter input, the value of input_axis is generalized into a variable and cannot be determined. As a result, the output dimension of the operator cannot be deduced, causing the graph compilation failure. Therefore, the input required by deduction in the graph compilation phase must be a constant. In APIs, the "constant input is needed" is marked for parameters that require constant input of these operators.
* Directly enter the needed value or a member variable in a class for the constant input of the operator in the construct function. The following is an example of correct input:
```python
class ExpandDimsTest(Cell):
def __init__(self, axis):
super(ExpandDimsTest, self).__init__()
self.expandDims = P.ExpandDims()
self.axis = axis
def construct(self, input_x):
return self.expandDims(input_x, self.axis)
axis = 0
expand_dim = ExpandDimsTest(axis)
input_x = Tensor(np.random.randn(2,2,2,2).astype(np.float32))
expand_dim(input_x)
```
# Glossary
<!-- TOC -->
- [Glossary](#glossary)
<!-- /TOC -->
| Acronym and Abbreviation | Description |
| ----- | ----- |
| Ascend | Name of Huawei Ascend series chips. |
| CCE | Cube-based Computing Engine, which is an operator development tool oriented to hardware architecture programming. |
| CCE-C | Cube-based Computing Engine C, which is C code developed by the CCE. |
| CheckPoint | MindSpore model training check point, which is used to save model parameters for inference or retraining. |
| CIFAR-10 | An open-source image data set that contains 60000 32 x 32 color images of 10 categories, with 6000 images of each category. There are 50000 training images and 10000 test images. |
| CIFAR-100 | An open-source image data set that contains 100 categories. Each category contains 600 images. Each course has 500 training images and 100 test images. |
| DaVinci | DaVinci architecture, Huawei-developed new chip architecture. |
| EulerOS | Euler operating system, which is developed by Huawei based on the standard Linux kernel. |
| FC Layer | Fully connected layer, which acts as a classifier in the entire convolutional neural network. |
| FE | Fusion Engine, which connects to GE and TBE operators and has the capabilities of loading and managing the operator information library and managing convergence rules. |
| FP16 | 16-bit floating point, which is a half-precision floating point arithmetic format, consuming less memory. |
| FP32 | 32-bit floating point, which is a single-precision floating point arithmetic format. |
| GE | Graph Engine, MindSpore computational graph execution engine, which is responsible for optimizing hardware (such as operator fusion and memory overcommitment) based on the front-end computational graph and starting tasks on the device side. |
| GHLO | Graph High Level Optimization. GHLO includes optimization irrelevant to hardware (such as dead code elimination), auto parallel, and auto differentiation. |
| GLLO | Graph Low Level Optimization. GLLO includes hardware-related optimization and in-depth optimization related to the combination of hardware and software, such as operator fusion and buffer fusion. |
| Graph Mode | MindSpore static graph mode. In this mode, the neural network model is compiled into an entire graph and then delivered for execution, featuring high performance. |
| HCCL | Huawei Collective Communication Library, which implements multi-device and multi-card communication based on the Da Vinci architecture chip. |
| ImageNet | Image database organized based on the WordNet hierarchy (currently nouns only). |
| LeNet | A classical convolutional neural network architecture proposed by Yann LeCun and others. |
| Loss | Difference between the predicted value and the actual value, which is a standard for determining the model quality of deep learning. |
| LSTM | Long short-term memory, an artificial recurrent neural network (RNN) architecture used for processing and predicting an important event with a long interval and delay in a time sequence. |
| Manifest | A data format file. Huawei ModelArt adopts this format. For details, see <https://support.huaweicloud.com/engineers-modelarts/modelarts_23_0009.html>. |
| ME | Mind Expression, MindSpore frontend, which is used to compile tasks from user source code to computational graphs, control execution during training, maintain contexts (in non-sink mode), and dynamically generate graphs (in PyNative mode). |
| MindArmour | MindSpore security component, which is used for AI adversarial example management, AI model attack defense and enhancement, and AI model robustness evaluation. |
| MindData | MindSpore data framework, which provides data loading, enhancement, dataset management, and visualization. |
| MindInsight | MindSpore visualization component, which visualizes information such as scalars, images, computational graphs, and model hyperparameters. |
| MindSpore | Huawei-leaded open-source deep learning framework. |
| MindSpore Predict | A lightweight deep neural network inference engine that provides the inference function for models trained by MindSpore on the device side. |
| MNIST database | Modified National Handwriting of Images and Technology database, a large handwritten digit database, which is usually used to train various image processing systems. |
| PyNative Mode | MindSpore dynamic graph mode. In this mode, operators in the neural network are delivered and executed one by one, facilitating the compilation and debugging of the neural network model. |
| ResNet-50 | Residual Neural Network 50, a residual neural network proposed by four Chinese people, including Kaiming He from Microsoft Research Institute. |
| Schema | Data set structure definition file, which defines the fields contained in a data set and the field types. |
| Summary | An operator that monitors the values of tensors on the network. It is a peripheral operation in the figure and does not affect the data flow. |
| TBE | Tensor Boost Engine, an operator development tool that is extended based on the Tensor Virtual Machine (TVM) framework. |
| TFRecord | Data format defined by TensorFlow. |
.. MindSpore documentation master file, created by
sphinx-quickstart on Thu Mar 24 10:00:00 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
MindSpore Documentation
=======================
.. toctree::
:glob:
:maxdepth: 1
architecture
roadmap
constraints_on_network_construction
operator_list
glossary
# Operator List
<!-- TOC -->
- [Operator List](#operator-list)
- [mindspore.nn](#mindsporenn)
- [mindspore.ops.operations](#mindsporeopsoperations)
<!-- /TOC -->
## mindspore.nn
| Operation | Ascend | GPU | CPU |Operator Type
| :----------- |:------ |:------ |:-----|:---
| [mindspore.nn.Softmax](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Softmax) | Supported | Supported | Supported |layer/activation
| [mindspore.nn.LogSoftmax](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.LogSoftmax) | Supported |Doing | Doing |layer/activation
| [mindspore.nn.ReLU](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.ReLU) | Supported | Supported | Supported |layer/activation
| [mindspore.nn.ReLU6](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.ReLU6) |Doing | Supported | Doing |layer/activation
| [mindspore.nn.LeakyReLU](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.LeakyReLU) |Doing |Doing | Doing |layer/activation
| [mindspore.nn.Tanh](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Tanh) | Supported |Doing | Doing |layer/activation
| [mindspore.nn.GELU](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.GELU) | Supported |Doing | Doing |layer/activation
| [mindspore.nn.Sigmoid](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Sigmoid) | Supported |Doing | Doing |layer/activation
| [mindspore.nn.PReLU](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.PReLU) |Doing |Doing | Doing |layer/activation
| [mindspore.nn.Dropout](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Dropout) |Doing |Doing | Doing |layer/basic
| [mindspore.nn.Flatten](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Flatten) |Doing | Supported | Doing |layer/basic
| [mindspore.nn.Dense](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Dense) |Doing | Supported | Doing |layer/basic
| [mindspore.nn.ClipByNorm](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.ClipByNorm) |Doing |Doing | Doing |layer/basic
| [mindspore.nn.Norm](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Norm) |Doing |Doing | Doing |layer/basic
| [mindspore.nn.OneHot](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.OneHot) | Supported | Supported | Doing |layer/basic
| [mindspore.nn.SequentialCell](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.SequentialCell) |Doing | Supported | Doing |layer/container
| [mindspore.nn.CellList](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.CellList) | Supported | Supported | Doing |layer/container
| [mindspore.nn.Conv2d](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Conv2d) | Supported | Supported | Supported |layer/conv
| [mindspore.nn.Conv2dTranspose](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Conv2dTranspose) |Doing | Supported | Doing |layer/conv
| [mindspore.nn.Embedding](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Embedding) |Doing | Supported | Doing |layer/embedding
| [mindspore.nn.LSTM](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.LSTM) |Doing | Supported | Doing |layer/lstm
| [mindspore.nn.BatchNorm1d](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.BatchNorm1d) | Supported |Doing | Doing |layer/normalization
| [mindspore.nn.BatchNorm2d](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.BatchNorm2d) | Supported | Supported | Doing |layer/normalization
| [mindspore.nn.LayerNorm](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.LayerNorm) | Supported |Doing | Doing |layer/normalization
| [mindspore.nn.MaxPool2d](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.MaxPool2d) | Supported | Supported | Supported |layer/pooling
| [mindspore.nn.AvgPool2d](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.AvgPool2d) |Doing | Supported | Doing |layer/pooling
| [mindspore.nn.L1Loss](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.L1Loss) |Doing |Doing | Doing |loss/loss
| [mindspore.nn.MSELoss](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.MSELoss) |Doing |Doing | Doing |loss/loss
| [mindspore.nn.SmoothL1Loss](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.SmoothL1Loss) |Doing |Doing | Doing |loss/loss
| [mindspore.nn.SoftmaxCrossEntropyWithLogits](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.SoftmaxCrossEntropyWithLogits) | Supported | Supported | Supported |loss/loss
| [mindspore.nn.SoftmaxCrossEntropyExpand](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.SoftmaxCrossEntropyExpand) |Doing |Doing | Doing |loss/loss
| [mindspore.nn.Adam](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Adam) |Doing |Doing | Doing |optim/adam
| [mindspore.nn.AdamWeightDecay](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.AdamWeightDecay) | Supported |Doing | Doing |optim/adam
| [mindspore.nn.AdamWeightDecayDynamicLR](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.AdamWeightDecayDynamicLR) | Supported |Doing | Doing |optim/adam
| [mindspore.nn.Lamb](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Lamb) |Doing |Doing | Doing |optim/lamb
| [mindspore.nn.LARS](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.LARS) |Doing |Doing | Doing |optim/lars
| [mindspore.nn.Momentum](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Momentum) | Supported | Supported | Supported |optim/momentum
| [mindspore.nn.Optimizer](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Optimizer) |Doing | Supported | Doing |optim/optimizer
| [mindspore.nn.SGD](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.SGD) |Doing |Doing | Doing |optim/sgd
| [mindspore.nn.WithLossCell](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.WithLossCell) | Supported | Supported | Doing |wrap/cell_wrapper
| [mindspore.nn.WithGradCell](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.WithGradCell) | Supported |Doing | Doing |wrap/cell_wrapper
| [mindspore.nn.TrainOneStepCell](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.TrainOneStepCell) | Supported | Supported | Doing |wrap/cell_wrapper
| [mindspore.nn.DataWrapper](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.DataWrapper) |Doing |Doing | Doing |wrap/cell_wrapper
| [mindspore.nn.GetNextSingleOp](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.GetNextSingleOp) |Doing | Supported | Doing |wrap/cell_wrapper
| [mindspore.nn.WithEvalCell](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.WithEvalCell) | Supported | Supported | Doing |wrap/cell_wrapper
| [mindspore.nn.ParameterUpdate](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.ParameterUpdate) |Doing |Doing | Doing |wrap/cell_wrapper
| [mindspore.nn.DistributedGradReducer](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.DistributedGradReducer) | Supported |Doing | Doing |wrap/grad_reducer
| [mindspore.nn.DynamicLossScaleUpdateCell](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.DynamicLossScaleUpdateCell) | Doing |Doing | Doing |wrap/loss_scale
| [mindspore.nn.FixedLossScaleUpdateCell](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.FixedLossScaleUpdateCell) | Doing |Doing | Doing |wrap/loss_scale
| [mindspore.nn.TrainOneStepWithLossScaleCell](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.TrainOneStepWithLossScaleCell) | Doing |Doing | Doing |wrap/loss_scale
| [mindspore.nn.Cell](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Cell) | Supported | Supported | Supported |cell
## mindspore.ops.operations
| Operation | Ascend | GPU | CPU |Operator Type
| :----------- |:------ |:------ |:-----|:---
| [mindspore.ops.operations.Flatten](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Flatten) | Doing | Supported |Doing | nn_ops
| [mindspore.ops.operations.Softmax](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Softmax) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.LogSoftmax](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LogSoftmax) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.ReLU](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReLU) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.ReLU6](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReLU6) | Doing | Supported |Doing | nn_ops
| [mindspore.ops.operations.Sigmoid](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Sigmoid) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.Tanh](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Tanh) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.FusedBatchNorm](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.FusedBatchNorm) | Doing | Supported |Doing | nn_ops
| [mindspore.ops.operations.BatchNorm](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BatchNorm) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.Conv2D](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Conv2D) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.DepthwiseConv2dNative](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.DepthwiseConv2dNative) | Doing | Doing |Doing | nn_ops
| [mindspore.ops.operations.MaxPoolWithArgmax](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.MaxPoolWithArgmax) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.MaxPool](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.MaxPool) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.AvgPool](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AvgPool) | Doing | Supported |Doing | nn_ops
| [mindspore.ops.operations.Conv2DBackpropInput](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Conv2DBackpropInput) | Doing | Supported |Doing | nn_ops
| [mindspore.ops.operations.BiasAdd](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BiasAdd) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.TopK](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.TopK) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.SoftmaxCrossEntropyWithLogits](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SoftmaxCrossEntropyWithLogits) | Supported | Supported |Doing | nn_ops
| [mindspore.ops.operations.SparseSoftmaxCrossEntropyWithLogits](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SparseSoftmaxCrossEntropyWithLogits) | Doing | Supported | Supported | nn_ops
| [mindspore.ops.operations.ApplyMomentum](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ApplyMomentum) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.SmoothL1Loss](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SmoothL1Loss) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.SGD](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SGD) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.LayerNorm](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LayerNorm) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.L2Normalize](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.L2Normalize) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.DropoutGenMask](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.DropoutGenMask) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.DropoutDoMask](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.DropoutDoMask) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.ResizeBilinear](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ResizeBilinear) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.OneHot](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.OneHot) | Supported | Supported | Doing | nn_ops
| [mindspore.ops.operations.Gelu](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Gelu) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.GetNext](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.GetNext) | Supported | Supported | Doing | nn_ops
| [mindspore.ops.operations.PReLU](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.PReLU) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.LSTM](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LSTM) | Doing | Supported | Doing | nn_ops
| [mindspore.ops.operations.SigmoidCrossEntropyWithLogits](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SigmoidCrossEntropyWithLogits) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.Pad](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Pad) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.ROIAlign](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ROIAlign) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.Adam](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Adam) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.BinaryCrossEntropy](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BinaryCrossEntropy) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.SparseApplyAdagrad](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SparseApplyAdagrad) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.LARSUpdate](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LARSUpdate) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.TensorAdd](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.TensorAdd) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.AssignAdd](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AssignAdd) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.AssignSub](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AssignSub) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.ReduceMean](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceMean) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.ReduceSum](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceSum) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.ReduceAll](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceAll) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.ReduceMax](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceMax) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.ReduceMin](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceMin) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.ReduceProd](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceProd) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.CumProd](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.CumProd) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.MatMul](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.MatMul) | Supported | Supported | Supported | math_ops
| [mindspore.ops.operations.BatchMatMul](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BatchMatMul) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.CumSum](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.CumSum) | Doing | Doing| Doing | math_ops
| [mindspore.ops.operations.AddN](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AddN) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Neg](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Neg) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Sub](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Sub) | Supported | Doing| Doing | math_ops
| [mindspore.ops.operations.Mul](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Mul) | Supported | Supported | Supported | math_ops
| [mindspore.ops.operations.Square](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Square) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Rsqrt](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Rsqrt) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Sqrt](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Sqrt) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Reciprocal](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Reciprocal) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Pow](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Pow) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Exp](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Exp) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Log](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Log) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Minimum](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Minimum) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Maximum](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Maximum) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.RealDiv](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.RealDiv) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Div](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Div) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.FloorDiv](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.FloorDiv) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Floor](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Floor) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Equal](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Equal) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.EqualCount](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.EqualCount) | Doing | Supported | Supported | math_ops
| [mindspore.ops.operations.NotEqual](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.NotEqual) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Greater](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Greater) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.GreaterEqual](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.GreaterEqual) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Less](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Less) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.LessEqual](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LessEqual) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.LogicalNot](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LogicalNot) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.LogicalAnd](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LogicalAnd) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.LogicalOr](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LogicalOr) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.NPUAllocFloatStatus](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.NPUAllocFloatStatus) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.NPUGetFloatStatus](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.NPUGetFloatStatus) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.NPUClearFloatStatus](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.NPUClearFloatStatus) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Cos](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Cos) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Acos](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Acos) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Sin](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Sin) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.NMSWithMask](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.NMSWithMask) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Abs](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Abs) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Sign](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Sign) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Round](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Round) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.ExpandDims](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ExpandDims) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.DType](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.DType) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.SameTypeShape](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SameTypeShape) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Cast](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Cast) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.IsSubClass](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.IsSubClass) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.IsInstance](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.IsInstance) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Reshape](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Reshape) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Shape](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Shape) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Squeeze](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Squeeze) | Doing | Supported | Doing | array_ops
| [mindspore.ops.operations.Transpose](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Transpose) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.GatherV2](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.GatherV2) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.Split](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Split) | Supported | Doing | Doing | array_ops
| [mindspore.ops.operations.Rank](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Rank) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.TruncatedNormal](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.TruncatedNormal) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.Size](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Size) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Fill](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Fill) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.OnesLike](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.OnesLike) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.ZerosLike](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ZerosLike) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.TupleToArray](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.TupleToArray) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.ScalarToArray](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScalarToArray) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.ScalarToTensor](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScalarToTensor) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.InvertPermutation](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.InvertPermutation) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Argmax](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Argmax) | Doing | Supported | Supported | array_ops
| [mindspore.ops.operations.Argmin](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Argmin) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.ArgMaxWithValue](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ArgMaxWithValue) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.ArgMinWithValue](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ArgMinWithValue) | Supported | Doing | Doing | array_ops
| [mindspore.ops.operations.Tile](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Tile) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.UnsortedSegmentSum](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.UnsortedSegmentSum) | Supported | Doing | Doing | array_ops
| [mindspore.ops.operations.Concat](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Concat) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.Slice](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Slice) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.ConcatOffset](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ConcatOffset) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Select](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Select) | Supported | Doing | Doing | array_ops
| [mindspore.ops.operations.StridedSlice](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.StridedSlice) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.Diag](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Diag) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.Eye](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Eye) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.ScatterNd](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScatterNd) | Supported | Doing | Doing | array_ops
| [mindspore.ops.operations.ResizeNearestNeighbor](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ResizeNearestNeighbor) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.GatherNd](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.GatherNd) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.ScatterNdUpdate](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScatterNdUpdate) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.SpaceToDepth](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SpaceToDepth) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.DepthToSpace](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.DepthToSpace) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.ReduceOp](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceOp) | Supported | Supported | Doing | comm_ops
| [mindspore.ops.operations.AllReduce](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AllReduce) | Doing | Supported | Doing | comm_ops
| [mindspore.ops.operations.AllGather](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AllGather) | Doing | Supported | Doing | comm_ops
| [mindspore.ops.operations.ReduceScatter](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceScatter) | Doing | Supported | Doing | comm_ops
| [mindspore.ops.operations.Broadcast](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Broadcast) | Doing | Doing | Doing | comm_ops
| [mindspore.ops.operations.ControlDepend](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ControlDepend) | Supported | Supported | Supported | control_ops
| [mindspore.ops.operations.GeSwitch](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.GeSwitch) | Doing | Doing | Doing | control_ops
| [mindspore.ops.operations.Merge](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Merge) | Doing | Doing | Doing | control_ops
| [mindspore.ops.operations.ScalarSummary](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScalarSummary) | Doing | Supported | Doing | debug_ops
| [mindspore.ops.operations.ImageSummary](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ImageSummary) | Doing | Supported | Doing | debug_ops
| [mindspore.ops.operations.TensorSummary](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.TensorSummary) | Doing | Supported | Doing | debug_ops
| [mindspore.ops.operations.InsertGradientOf](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.InsertGradientOf) | Supported | Supported | Supported | debug_ops
| [mindspore.ops.operations.Print](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Print) | Supported | Doing | Doing | debug_ops
| [mindspore.ops.operations.Assign](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Assign) | Supported | Doing | Doing | other_ops
| [mindspore.ops.operations.BoundingBoxEncode](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BoundingBoxEncode) | Doing | Doing | Doing | other_ops
| [mindspore.ops.operations.BoundingBoxDecode](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BoundingBoxDecode) | Doing | Doing | Doing | other_ops
| [mindspore.ops.operations.CheckValid](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.CheckValid) | Doing | Doing | Doing | other_ops
| [mindspore.ops.operations.IOU](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.IOU) | Doing | Doing | Doing | other_ops
| [mindspore.ops.operations.MakeRefKey](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.MakeRefKey) | Supported | Supported | Supported | other_ops
| [mindspore.ops.operations.RandomChoiceWithMask](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.RandomChoiceWithMask) | Doing| Doing | Doing | random_ops
| [mindspore.ops.operations.ScalarCast](https://www.mindspore.cn/api/en/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScalarCast) | Supported | Supported | Supported | inner_ops
# RoadMap
MindSpore's top priority plans in the year are displayed as follows. We will continuously adjust the priority based on user feedback.
<!-- TOC -->
- [Preset Models](#preset-models)
- [Usability](#usability)
- [Performance Optimization](#performance-optimization)
- [Architecture Evolution](#architecture-evolution)
- [MindInsight Debugging and Optimization](#mindinsight-debugging-and-optimization)
- [MindArmour Security Hardening Package](#mindarmour-security-hardening-package)
- [Inference Framework](#inference-framework)
<!-- /TOC -->
In general, we will make continuous improvements in the following aspects:
1. Support more preset models.
2. Continuously supplement APIs and operator libraries to improve usability and programming experience.
3. Comprehensively support for Huawei Ascend AI processor and continuously optimize the performance and software architecture.
4. Improve visualization, debugging and optimization, and security-related tools.
We sincerely hope that you can join the discussion in the user community and contribute your suggestions.
## Preset Models
* CV: Classic models for object detection, GAN, image segmentation, and posture recognition.
* NLP: RNN and Transformer neural network, expanding the application based on the BERT pre-training model.
* Other: GNN, reinforcement learning, probabilistic programming, and AutoML.
## Usability
* Supplement APIs such as operators, optimizers, and loss functions.
* Complete the native expression support of the Python language.
* Support common Tensor/Math operations.
* Add more application scenarios of automatic parallelization to improve the accuracy of policy search.
## Performance Optimization
* Optimize the compilation time.
* Low-bit mixed precision training and inference.
* Improve memory utilization.
* Provide more fusion optimization methods.
* Improve the execution performance in PyNative.
## Architecture Evolution
* Optimize computational graph and operator fusion. Use fine-grained graph IR to express operators to form intermediate representation (IR) with operator boundaries and explore more layer optimization opportunities.
* Support more programming languages.
* Optimize the automatic scheduling and distributed training data cache mechanism of data augmentation.
* Continuously improve MindSpore IR.
* Support distributed training in parameter server mode.
## MindInsight Debugging and Optimization
* Training process observation
* Histogram
* Optimize the display of computational and data graphs.
* Integrate the performance profiling and debugger tools.
* Support comparison between multiple trainings.
* Training result lineage
* Data augmentation lineage comparison.
* Training process diagnosis
* Performance profiling.
* Graph model-based debugger.
## MindArmour Security Hardening Package
* Test the model security.
* Provide model security hardening tools.
* Protect data privacy during training and inference.
## Inference Framework
* Support TensorFlow, Caffe, and ONNX model formats.
* Support iOS.
* Improve more CPU operators.
* Support more CV/NLP models.
* Online learning.
* Support deployment on IoT devices.
* Low-bit quantization.
* CPU and NPU heterogeneous scheduling.
# 总体架构
本文将为大家介绍MindSpore总体架构。
<!-- TOC -->
- [总体架构](#总体架构)
<!-- /TOC -->
MindSpore框架架构总体分为MindSpore前端表示层、MindSpore计算图引擎和MindSpore后端运行时三层。
![architecture](./images/architecture.png)
- MindSpore前端表示层(Mind Expression,简称ME)
该部分包含Python API、MindSpore IR(Intermediate representation,简称IR)、计算图高级别优化(Graph High Level Optimization,简称GHLO)三部分。
- Python API向用户提供统一的模型训练、推理、导出接口,以及统一的数据处理、增强、格式转换接口。
- GHLO包含硬件无关的优化(如死代码消除等)、自动并行和自动微分等功能。
- MindSpore IR提供统一的中间表示,MindSpore基于此IR进行pass优化。
- MindSpore计算图引擎(Graph Engine,简称GE)
该部分包含计算图低级别优化(Graph Low Level Optimization,简称GLLO)、图执行。
- GLLO包含硬件相关的优化,以及算子融合、Buffer融合等软硬件结合相关的深度优化。
- 图执行提供离线图执行、分布式训练所需要的通信接口等功能。
- MindSpore后端运行时
该部分包含云、边、端上不同环境中的高效运行环境。
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
# -- Project information -----------------------------------------------------
project = 'MindSpore'
copyright = '2020, MindSpore'
author = 'MindSpore'
# The full version, including alpha/beta/rc tags
release = '0.1.0-alpha'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx_markdown_tables',
'recommonmark',
]
source_suffix = {
'.rst': 'restructuredtext',
'.md': 'markdown',
}
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
pygments_style = 'sphinx'
autodoc_inherit_docstrings = False
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_search_language = 'zh'
html_search_options = {'dict': '../resource/jieba.txt'}
# Python源码构造网络约束
<!-- TOC -->
- [Python源码构造网络约束](#python源码构造网络约束)
- [概述](#概述)
- [语法约束](#语法约束)
- [支持的Python数据类型](#支持的python数据类型)
- [MindSpore扩展数据类型](#mindspore扩展数据类型)
- [表达式类型](#表达式类型)
- [语句类型](#语句类型)
- [系统函数](#系统函数)
- [函数参数](#函数参数)
- [操作符](#操作符)
- [切片操作](#切片操作)
- [不支持的语法](#不支持的语法)
- [网络定义约束](#网络定义约束)
- [整网实例类型](#整网实例类型)
- [网络输入类型](#网络输入类型)
- [网络图优化](#网络图优化)
- [网络构造组件](#网络构造组件)
- [其他约束](#其他约束)
<!-- /TOC -->
## 概述
MindSpore完成从用户源码到计算图的编译,用户源码基于Python语法编写,当前MindSpore支持将普通函数或者继承自nn.Cell的实例转换生成计算图,暂不支持将任意Python源码转换成计算图,所以对于用户源码支持的写法有所限制,主要包括语法约束和网络定义约束两方面。随着MindSpore的演进,这些约束可能会发生变化。
## 语法约束
### 支持的Python数据类型
* Number:包括`int``float``bool`,不支持复数类型。
* String
* List:当前只支持append方法;List的更新会拷贝生成新的List。
* Tuple
* Dictionary:当前`key`只支持String类型
### MindSpore扩展数据类型
* Tensor:Tensor变量必须是已定义实例。
### 表达式类型
| 操作名 | 具体操作
| :----------- |:--------
| 一元操作符 |`+``-``not`,其中`+`操作符只支持标量。
| 二元操作符 |`+``-``*``/``%`
| `if`表达式 |例如`a = x if x < y else y`
| 比较表达式 | `>``>=``<``<=``==``!=`
| 逻辑表达式 | `and``or`
| `lambda`表达式 | 例如`lambda x, y: x + y`
| 保留关键字类型 | `True``False``None`
### 语句类型
| 语句 | 与Python对比
| :----------- |:--------
| `for` | 迭代序列必须是Tuple/List,部分嵌套场景支持。
| `while` | 部分嵌套场景支持。
| `if` | 与Python使用原则一致,但if条件的输入只支持常量。
| `def` | 相同。
| 赋值语句 | List和Dictionary的多重下标访问不支持作为左值。
### 系统函数
* len
* partial
* map
* zip
* range
### 函数参数
* 参数默认值:目前不支持默认值设为`Tensor`类型数据,支持`int``float``bool``None``str``tuple``list``dict`类型数据。
* 可变参数:目前不支持带可变参数的函数求反向。
* 键值对参数:目前不支持带键值对参数的函数求反向。
* 可变键值对参数:目前不支持带可变键值对的函数求反向。
### 操作符
| 运算符 | 支持类型
| :----------- |:--------
| `+` |标量、`Tensor``tuple`
| `-` |标量、`Tensor`
| `*` |标量、`Tensor`
| `/` |标量、`Tensor`
| `[]` |操作对象类型支持`list``tuple``Tensor`,支持多重下标访问作为右值,但不支持多重下标访问作为左值,且索引类型不支持Tensor;Tuple、Tensor类型访问限制见切片操作中的说明。
### 切片操作
* `tuple`切片操作:`tuple_x[start:stop:step]`
- `tuple_x`为一个元组,是被执行切片操作的目标。
- `start`:切片的起始位置索引,类型为`int`,取值范围为`[-length(tuple_x), length(tuple_x) - 1]`。可缺省,缺省配置如下:
-`step > 0`时,缺省值为`0`
-`step < 0`时,缺省值为`length(tuple_x) - 1`
- `end`:切片的结束位置索引,类型为`int`,取值范围为`[-length(tuple_x) - 1, length(tuple_x)]`。可缺省,缺省配置如下:
-`step > 0`时,缺省值为`length(tuple_x)`
-`step < 0`是,缺省值为`-1`
- `step`:切片的步长,类型为`int`,取值范围为`step != 0`。可缺省,缺省值为`1`
* `Tensor`切片操作:`tensor_x[start0:stop0:step0, start1:stop1:step1, start2:stop2:step2]`
- `tensor_x`是一个维度不低于3维的`Tensor`,对其进行切片操作。
- `start0`:在第0维上进行切片的起始位置索引,类型为`int`,可缺省,缺省配置如下:
-`step > 0`时,缺省值为`0`
-`step < 0`时,缺省值为`-1`
- `end0`:在第0维上进行切片的结束位置索引,类型为`int`,可缺省,缺省配置如下:
-`step > 0`时,缺省值为`length(tuple_x)`
-`step < 0`是,缺省值为`-(1 + length(tuple_x))`
- `step0`:在第0维上进行切片的步长,类型为`int`,取值范围为`step != 0`。可缺省,缺省值为`1`
- 如果进行切片的维数少于`Tensor`的维数,则未指定切片的维度默认取全部元素。
- 切片降维操作:在某维度上传入整数索引,则取出该维度上对应索引的元素,且消除该维度,如shape为(4, 3, 6)的`tensor_x[2:4:1, 1, 0:5:2]`切片之后,生成一个shape为(2, 3)的`Tensor`,原`Tensor`的第1维被消除。
### 不支持的语法
目前在网络构造函数里面暂不支持以下语法:
`break``continue``pass``raise``yield``async for``with``async with``assert``import``await`
## 网络定义约束
### 整网实例类型
*[@ms_function](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.html#mindspore.ms_function)装饰器的普通Python函数。
* 继承自[nn.Cell](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Cell)的Cell子类。
### 网络输入类型
* 整网的训练数据输入参数只能是Tensor类型。
* 生成的ANF图里面不能包含这几种常量节点:字符串类型常量、带有Tuple嵌套的常量、带有List嵌套的常量。
### 网络图优化
在ME前端图优化过程中,会将DataClass类型、Dictionary、List、键值对操作转换为Tuple相关操作。
### 网络构造组件
| 类别 | 内容
| :----------- |:--------
| `Cell`实例 |[mindspore/nn/*](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html)、自定义[Cell](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Cell)
| `Cell`实例的成员函数 | Cell的construct中可以调用其他类成员函数。
| 函数 | 自定义Python函数、前文中列举的系统函数。
| dataclass实例 | 使用@dataclass装饰的类。
| Primitive算子 |[mindspore/ops/operations/*](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html)
| Composite算子 |[mindspore/ops/composite/*](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.composite.html)
| constexpr生成算子 |使用[@constexpr](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.html#mindspore.ops.constexpr)生成的值计算算子。
### 其他约束
整网construct函数输入的参数以及使用ms_function装饰器修饰的函数的参数在图编译过程中会进行泛化,不能作为常量输入传给算子使用,如下例所示:
* 错误的写法如下:
```python
class ExpandDimsTest(Cell):
def __init__(self):
super(ExpandDimsTest, self).__init__()
self.expandDims = P.ExpandDims()
def construct(self, input_x, input_axis):
return self.expandDims(input_x, input_axis)
expand_dim = ExpandDimsTest()
input_x = Tensor(np.random.randn(2,2,2,2).astype(np.float32))
expand_dim(input_x, 0)
```
在示例中,ExpandDimsTest是一个只有单算子的网络,网络的输入有input_x和input_axis两个。因为ExpandDims算子的第二个输入需要是常量,这是因为在图编译过程中推导ExpandDims算子输出维度的时候需要用到,而input_axis作为网络参数输入会泛化成变量,无法确定其值,从而无法推导算子的输出维度导致图编译失败。所以在图编译阶段需要值推导的输入都应该是常量输入。在API中,这类算子需要常量输入的参数会进行说明,标注"constant input is needed"。
* 正确的写法是在construct函数里面对算子的常量输入直接填入需要的值或者是一个类的成员变量,如下:
```python
class ExpandDimsTest(Cell):
def __init__(self, axis):
super(ExpandDimsTest, self).__init__()
self.expandDims = P.ExpandDims()
self.axis = axis
def construct(self, input_x):
return self.expandDims(input_x, self.axis)
axis = 0
expand_dim = ExpandDimsTest(axis)
input_x = Tensor(np.random.randn(2,2,2,2).astype(np.float32))
expand_dim(input_x)
```
# 术语
<!-- TOC -->
- [术语](#术语)
<!-- /TOC -->
| 术语/缩略语 | 说明 |
| ----- | ----- |
| Ascend | 华为昇腾系列芯片的系列名称。 |
| CCE | Cube-based Computing Engine,面向硬件架构编程的算子开发工具。 |
| CCE-C | Cube-based Computing Engine C,使用CCE开发的C代码。 |
| CheckPoint | MindSpore模型训练检查点,保存模型的参数,可以用于保存模型供推理,或者再训练。 |
| CIFAR-10 | 一个开源的图像数据集,包含10个类别的60000个32x32彩色图像,每个类别6000个图像。有50000张训练图像和10000张测试图像。 |
| CIFAR-100 | 一个开源的图像数据集,它有100个类别,每个类别包含600张图像。每个课程有500张训练图像和100张测试图像。 |
| Davinci | 达芬奇架构,华为自研的新型芯片架构。 |
| EulerOS | 欧拉操作系统,华为自研的基于Linux标准内核的操作系统。 |
| FC Layer | Fully Conneted Layer,全连接层。整个卷积神经网络中起到分类器的作用。 |
| FE | Fusion Engine,负责对接GE和TBE算子,具备算子信息库的加载与管理、融合规则管理等能力。 |
| FP16 | 16位浮点,半精度浮点算术,消耗更小内存。 |
| FP32 | 32位浮点,单精度浮点算术。 |
| GE | Graph Engine,MindSpore计算图执行引擎,主要负责根据前端的计算图完成硬件相关的优化(算子融合、内存复用等等)、device侧任务启动。 |
| GHLO | Graph High Level Optimization,计算图高级别优化。GHLO包含硬件无关的优化(如死代码消除等)、自动并行和自动微分等功能。 |
| GLLO | Graph Low Level Optimization,计算图低级别优化。GLLO包含硬件相关的优化,以及算子融合、Buffer融合等软硬件结合相关的深度优化。 |
| Graph Mode | MindSpore的静态图模式,将神经网络模型编译成一整张图,然后下发执行,性能高。 |
| HCCL | Huawei Collective Communication Library,实现了基于Davinci架构芯片的多机多卡通信。 |
| ImageNet | 根据WordNet层次结构(目前仅名词)组织的图像数据库。 |
| LeNet | 一个经典的卷积神经网络架构,由Yann LeCun等人提出。 |
| Loss | 损失,预测值与实际值的偏差,深度学习用于判断模型好坏的一个标准。 |
| LSTM | Long short-term memory,长短期记忆,对应的网络是一种时间循环神经网络,适合于处理和预测时间序列中间隔和延迟非常长的重要事件。 |
| Manifest | 一种数据格式文件,华为ModelArts采用了该格式,详细说明请参见<https://support.huaweicloud.com/engineers-modelarts/modelarts_23_0009.html>。 |
| ME | Mind Expression,MindSpore前端,主要完成从用户源码到计算图的编译任务、训练中控制执行及上下文维护(非下沉模式配置下)、动态图(PyNative模式)等。 |
| MindArmour | MindSpore安全组件,用于AI对抗样本管理,AI模型防攻击和增强,AI模型健壮性评测。 |
| MindData | MindSpore数据框架,提供数据加载、增强、数据集管理以及可视化。 |
| MindInsight | MindSpore可视化组件,可视化标量、图像、计算图以及模型超参等信息。 |
| MindSpore | 华为主导开源的深度学习框架。 |
| MindSpore Predict | 一个轻量级的深度神经网络推理引擎,提供了将MindSpore训练出的模型在端侧进行推理的功能。 |
| MNIST database | Modified National Institute of Standards and Technology database,一个大型手写数字数据库,通常用于训练各种图像处理系统。 |
| PyNative Mode | MindSpore的动态图模式,将神经网络中的各个算子逐一下发执行,方便用户编写和调试神经网络模型。 |
| ResNet-50 | Residual Neural Network 50,由微软研究院的Kaiming He等四名华人提出的残差神经网络。 |
| Schema | 数据集结构定义文件,用于定义数据集包含哪些字段以及字段的类型。 |
| Summary | 是对网络中Tensor取值进行监测的一种算子,在图中是“外围”操作,不影响数据流本身。 |
| TBE | Tensor Boost Engine,在TVM( Tensor Virtual Machine )框架基础上扩展的算子开发工具。 |
| TFRecord | Tensorflow定义的数据格式。 |
\ No newline at end of file
.. MindSpore documentation master file, created by
sphinx-quickstart on Thu Mar 24 10:00:00 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
MindSpore文档
=================
.. toctree::
:glob:
:maxdepth: 1
architecture
roadmap
constraints_on_network_construction
operator_list
glossary
# 算子支持
<!-- TOC -->
- [算子支持](#算子支持)
- [mindspore.nn](#mindsporenn)
- [mindspore.ops.operations](#mindsporeopsoperations)
<!-- /TOC -->
## mindspore.nn
| 操作名 | Ascend | GPU | CPU |算子类别
| :----------- |:------ |:------ |:-----|:---
| [mindspore.nn.Softmax](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Softmax) | Supported | Supported | Supported |layer/activation
| [mindspore.nn.LogSoftmax](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.LogSoftmax) | Supported |Doing | Doing |layer/activation
| [mindspore.nn.ReLU](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.ReLU) | Supported | Supported | Supported |layer/activation
| [mindspore.nn.ReLU6](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.ReLU6) |Doing | Supported | Doing |layer/activation
| [mindspore.nn.LeakyReLU](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.LeakyReLU) |Doing |Doing | Doing |layer/activation
| [mindspore.nn.Tanh](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Tanh) | Supported |Doing | Doing |layer/activation
| [mindspore.nn.GELU](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.GELU) | Supported |Doing | Doing |layer/activation
| [mindspore.nn.Sigmoid](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Sigmoid) | Supported |Doing | Doing |layer/activation
| [mindspore.nn.PReLU](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.PReLU) |Doing |Doing | Doing |layer/activation
| [mindspore.nn.Dropout](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Dropout) |Doing |Doing | Doing |layer/basic
| [mindspore.nn.Flatten](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Flatten) |Doing | Supported | Doing |layer/basic
| [mindspore.nn.Dense](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Dense) |Doing | Supported | Doing |layer/basic
| [mindspore.nn.ClipByNorm](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.ClipByNorm) |Doing |Doing | Doing |layer/basic
| [mindspore.nn.Norm](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Norm) |Doing |Doing | Doing |layer/basic
| [mindspore.nn.OneHot](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.OneHot) | Supported | Supported | Doing |layer/basic
| [mindspore.nn.SequentialCell](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.SequentialCell) |Doing | Supported | Doing |layer/container
| [mindspore.nn.CellList](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.CellList) | Supported | Supported | Doing |layer/container
| [mindspore.nn.Conv2d](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Conv2d) | Supported | Supported | Supported |layer/conv
| [mindspore.nn.Conv2dTranspose](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Conv2dTranspose) |Doing | Supported | Doing |layer/conv
| [mindspore.nn.Embedding](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Embedding) |Doing | Supported | Doing |layer/embedding
| [mindspore.nn.LSTM](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.LSTM) |Doing | Supported | Doing |layer/lstm
| [mindspore.nn.BatchNorm1d](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.BatchNorm1d) | Supported |Doing | Doing |layer/normalization
| [mindspore.nn.BatchNorm2d](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.BatchNorm2d) | Supported | Supported | Doing |layer/normalization
| [mindspore.nn.LayerNorm](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.LayerNorm) | Supported |Doing | Doing |layer/normalization
| [mindspore.nn.MaxPool2d](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.MaxPool2d) | Supported | Supported | Supported |layer/pooling
| [mindspore.nn.AvgPool2d](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.AvgPool2d) |Doing | Supported | Doing |layer/pooling
| [mindspore.nn.L1Loss](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.L1Loss) |Doing |Doing | Doing |loss/loss
| [mindspore.nn.MSELoss](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.MSELoss) |Doing |Doing | Doing |loss/loss
| [mindspore.nn.SmoothL1Loss](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.SmoothL1Loss) |Doing |Doing | Doing |loss/loss
| [mindspore.nn.SoftmaxCrossEntropyWithLogits](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.SoftmaxCrossEntropyWithLogits) | Supported | Supported | Supported |loss/loss
| [mindspore.nn.SoftmaxCrossEntropyExpand](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.SoftmaxCrossEntropyExpand) |Doing |Doing | Doing |loss/loss
| [mindspore.nn.Adam](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Adam) |Doing |Doing | Doing |optim/adam
| [mindspore.nn.AdamWeightDecay](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.AdamWeightDecay) | Supported |Doing | Doing |optim/adam
| [mindspore.nn.AdamWeightDecayDynamicLR](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.AdamWeightDecayDynamicLR) | Supported |Doing | Doing |optim/adam
| [mindspore.nn.Lamb](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Lamb) |Doing |Doing | Doing |optim/lamb
| [mindspore.nn.LARS](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.LARS) |Doing |Doing | Doing |optim/lars
| [mindspore.nn.Momentum](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Momentum) | Supported | Supported | Supported |optim/momentum
| [mindspore.nn.Optimizer](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Optimizer) |Doing | Supported | Doing |optim/optimizer
| [mindspore.nn.SGD](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.SGD) |Doing |Doing | Doing |optim/sgd
| [mindspore.nn.WithLossCell](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.WithLossCell) | Supported | Supported | Doing |wrap/cell_wrapper
| [mindspore.nn.WithGradCell](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.WithGradCell) | Supported |Doing | Doing |wrap/cell_wrapper
| [mindspore.nn.TrainOneStepCell](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.TrainOneStepCell) | Supported | Supported | Doing |wrap/cell_wrapper
| [mindspore.nn.DataWrapper](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.DataWrapper) |Doing |Doing | Doing |wrap/cell_wrapper
| [mindspore.nn.GetNextSingleOp](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.GetNextSingleOp) |Doing | Supported | Doing |wrap/cell_wrapper
| [mindspore.nn.WithEvalCell](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.WithEvalCell) | Supported | Supported | Doing |wrap/cell_wrapper
| [mindspore.nn.ParameterUpdate](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.ParameterUpdate) |Doing |Doing | Doing |wrap/cell_wrapper
| [mindspore.nn.DistributedGradReducer](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.DistributedGradReducer) | Supported |Doing | Doing |wrap/grad_reducer
| [mindspore.nn.DynamicLossScaleUpdateCell](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.DynamicLossScaleUpdateCell) | Doing |Doing | Doing |wrap/loss_scale
| [mindspore.nn.FixedLossScaleUpdateCell](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.FixedLossScaleUpdateCell) | Doing |Doing | Doing |wrap/loss_scale
| [mindspore.nn.TrainOneStepWithLossScaleCell](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.TrainOneStepWithLossScaleCell) | Doing |Doing | Doing |wrap/loss_scale
| [mindspore.nn.Cell](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.nn.html#mindspore.nn.Cell) | Supported | Supported | Supported |cell
## mindspore.ops.operations
| 操作名 | Ascend | GPU | CPU |算子类别
| :----------- |:------ |:------ |:-----|:---
| [mindspore.ops.operations.Flatten](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Flatten) | Doing | Supported |Doing | nn_ops
| [mindspore.ops.operations.Softmax](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Softmax) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.LogSoftmax](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LogSoftmax) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.ReLU](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReLU) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.ReLU6](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReLU6) | Doing | Supported |Doing | nn_ops
| [mindspore.ops.operations.Sigmoid](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Sigmoid) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.Tanh](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Tanh) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.FusedBatchNorm](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.FusedBatchNorm) | Doing | Supported |Doing | nn_ops
| [mindspore.ops.operations.BatchNorm](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BatchNorm) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.Conv2D](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Conv2D) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.DepthwiseConv2dNative](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.DepthwiseConv2dNative) | Doing | Doing |Doing | nn_ops
| [mindspore.ops.operations.MaxPoolWithArgmax](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.MaxPoolWithArgmax) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.MaxPool](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.MaxPool) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.AvgPool](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AvgPool) | Doing | Supported |Doing | nn_ops
| [mindspore.ops.operations.Conv2DBackpropInput](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Conv2DBackpropInput) | Doing | Supported |Doing | nn_ops
| [mindspore.ops.operations.BiasAdd](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BiasAdd) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.TopK](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.TopK) | Supported | Doing |Doing | nn_ops
| [mindspore.ops.operations.SoftmaxCrossEntropyWithLogits](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SoftmaxCrossEntropyWithLogits) | Supported | Supported |Doing | nn_ops
| [mindspore.ops.operations.SparseSoftmaxCrossEntropyWithLogits](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SparseSoftmaxCrossEntropyWithLogits) | Doing | Supported | Supported | nn_ops
| [mindspore.ops.operations.ApplyMomentum](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ApplyMomentum) | Supported | Supported | Supported | nn_ops
| [mindspore.ops.operations.SmoothL1Loss](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SmoothL1Loss) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.SGD](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SGD) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.LayerNorm](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LayerNorm) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.L2Normalize](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.L2Normalize) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.DropoutGenMask](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.DropoutGenMask) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.DropoutDoMask](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.DropoutDoMask) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.ResizeBilinear](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ResizeBilinear) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.OneHot](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.OneHot) | Supported | Supported | Doing | nn_ops
| [mindspore.ops.operations.Gelu](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Gelu) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.GetNext](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.GetNext) | Supported | Supported | Doing | nn_ops
| [mindspore.ops.operations.PReLU](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.PReLU) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.LSTM](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LSTM) | Doing | Supported | Doing | nn_ops
| [mindspore.ops.operations.SigmoidCrossEntropyWithLogits](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SigmoidCrossEntropyWithLogits) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.Pad](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Pad) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.ROIAlign](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ROIAlign) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.Adam](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Adam) | Supported | Doing | Doing | nn_ops
| [mindspore.ops.operations.BinaryCrossEntropy](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BinaryCrossEntropy) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.SparseApplyAdagrad](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SparseApplyAdagrad) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.LARSUpdate](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LARSUpdate) | Doing | Doing | Doing | nn_ops
| [mindspore.ops.operations.TensorAdd](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.TensorAdd) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.AssignAdd](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AssignAdd) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.AssignSub](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AssignSub) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.ReduceMean](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceMean) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.ReduceSum](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceSum) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.ReduceAll](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceAll) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.ReduceMax](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceMax) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.ReduceMin](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceMin) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.ReduceProd](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceProd) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.CumProd](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.CumProd) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.MatMul](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.MatMul) | Supported | Supported | Supported | math_ops
| [mindspore.ops.operations.BatchMatMul](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BatchMatMul) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.CumSum](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.CumSum) | Doing | Doing| Doing | math_ops
| [mindspore.ops.operations.AddN](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AddN) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Neg](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Neg) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Sub](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Sub) | Supported | Doing| Doing | math_ops
| [mindspore.ops.operations.Mul](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Mul) | Supported | Supported | Supported | math_ops
| [mindspore.ops.operations.Square](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Square) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Rsqrt](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Rsqrt) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Sqrt](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Sqrt) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Reciprocal](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Reciprocal) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Pow](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Pow) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Exp](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Exp) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Log](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Log) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Minimum](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Minimum) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Maximum](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Maximum) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.RealDiv](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.RealDiv) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.Div](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Div) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.FloorDiv](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.FloorDiv) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Floor](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Floor) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Equal](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Equal) | Supported | Supported | Doing | math_ops
| [mindspore.ops.operations.EqualCount](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.EqualCount) | Doing | Supported | Supported | math_ops
| [mindspore.ops.operations.NotEqual](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.NotEqual) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Greater](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Greater) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.GreaterEqual](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.GreaterEqual) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Less](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Less) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.LessEqual](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LessEqual) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.LogicalNot](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LogicalNot) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.LogicalAnd](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LogicalAnd) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.LogicalOr](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.LogicalOr) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.NPUAllocFloatStatus](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.NPUAllocFloatStatus) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.NPUGetFloatStatus](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.NPUGetFloatStatus) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.NPUClearFloatStatus](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.NPUClearFloatStatus) | Supported | Doing | Doing | math_ops
| [mindspore.ops.operations.Cos](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Cos) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Acos](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Acos) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Sin](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Sin) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.NMSWithMask](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.NMSWithMask) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Abs](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Abs) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Sign](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Sign) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.Round](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Round) | Doing | Doing | Doing | math_ops
| [mindspore.ops.operations.ExpandDims](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ExpandDims) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.DType](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.DType) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.SameTypeShape](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SameTypeShape) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Cast](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Cast) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.IsSubClass](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.IsSubClass) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.IsInstance](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.IsInstance) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Reshape](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Reshape) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Shape](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Shape) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Squeeze](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Squeeze) | Doing | Supported | Doing | array_ops
| [mindspore.ops.operations.Transpose](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Transpose) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.GatherV2](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.GatherV2) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.Split](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Split) | Supported | Doing | Doing | array_ops
| [mindspore.ops.operations.Rank](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Rank) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.TruncatedNormal](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.TruncatedNormal) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.Size](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Size) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Fill](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Fill) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.OnesLike](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.OnesLike) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.ZerosLike](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ZerosLike) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.TupleToArray](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.TupleToArray) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.ScalarToArray](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScalarToArray) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.ScalarToTensor](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScalarToTensor) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.InvertPermutation](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.InvertPermutation) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Argmax](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Argmax) | Doing | Supported | Supported | array_ops
| [mindspore.ops.operations.Argmin](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Argmin) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.ArgMaxWithValue](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ArgMaxWithValue) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.ArgMinWithValue](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ArgMinWithValue) | Supported | Doing | Doing | array_ops
| [mindspore.ops.operations.Tile](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Tile) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.UnsortedSegmentSum](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.UnsortedSegmentSum) | Supported | Doing | Doing | array_ops
| [mindspore.ops.operations.Concat](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Concat) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.Slice](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Slice) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.ConcatOffset](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ConcatOffset) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.Select](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Select) | Supported | Doing | Doing | array_ops
| [mindspore.ops.operations.StridedSlice](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.StridedSlice) | Supported | Supported | Doing | array_ops
| [mindspore.ops.operations.Diag](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Diag) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.Eye](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Eye) | Supported | Supported | Supported | array_ops
| [mindspore.ops.operations.ScatterNd](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScatterNd) | Supported | Doing | Doing | array_ops
| [mindspore.ops.operations.ResizeNearestNeighbor](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ResizeNearestNeighbor) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.GatherNd](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.GatherNd) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.ScatterNdUpdate](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScatterNdUpdate) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.SpaceToDepth](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.SpaceToDepth) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.DepthToSpace](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.DepthToSpace) | Doing | Doing | Doing | array_ops
| [mindspore.ops.operations.ReduceOp](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceOp) | Supported | Supported | Doing | comm_ops
| [mindspore.ops.operations.AllReduce](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AllReduce) | Doing | Supported | Doing | comm_ops
| [mindspore.ops.operations.AllGather](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.AllGather) | Doing | Supported | Doing | comm_ops
| [mindspore.ops.operations.ReduceScatter](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ReduceScatter) | Doing | Supported | Doing | comm_ops
| [mindspore.ops.operations.Broadcast](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Broadcast) | Doing | Doing | Doing | comm_ops
| [mindspore.ops.operations.ControlDepend](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ControlDepend) | Supported | Supported | Supported | control_ops
| [mindspore.ops.operations.GeSwitch](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.GeSwitch) | Doing | Doing | Doing | control_ops
| [mindspore.ops.operations.Merge](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Merge) | Doing | Doing | Doing | control_ops
| [mindspore.ops.operations.ScalarSummary](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScalarSummary) | Doing | Supported | Doing | debug_ops
| [mindspore.ops.operations.ImageSummary](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ImageSummary) | Doing | Supported | Doing | debug_ops
| [mindspore.ops.operations.TensorSummary](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.TensorSummary) | Doing | Supported | Doing | debug_ops
| [mindspore.ops.operations.InsertGradientOf](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.InsertGradientOf) | Supported | Supported | Supported | debug_ops
| [mindspore.ops.operations.Print](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Print) | Supported | Doing | Doing | debug_ops
| [mindspore.ops.operations.Assign](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.Assign) | Supported | Doing | Doing | other_ops
| [mindspore.ops.operations.BoundingBoxEncode](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BoundingBoxEncode) | Doing | Doing | Doing | other_ops
| [mindspore.ops.operations.BoundingBoxDecode](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.BoundingBoxDecode) | Doing | Doing | Doing | other_ops
| [mindspore.ops.operations.CheckValid](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.CheckValid) | Doing | Doing | Doing | other_ops
| [mindspore.ops.operations.IOU](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.IOU) | Doing | Doing | Doing | other_ops
| [mindspore.ops.operations.MakeRefKey](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.MakeRefKey) | Supported | Supported | Supported | other_ops
| [mindspore.ops.operations.RandomChoiceWithMask](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.RandomChoiceWithMask) | Doing| Doing | Doing | random_ops
| [mindspore.ops.operations.ScalarCast](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/api/python/mindspore/mindspore.ops.operations.html#mindspore.ops.operations.ScalarCast) | Supported | Supported | Supported | inner_ops
# RoadMap
以下将展示MindSpore近一年的高阶计划,我们会根据用户的反馈诉求,持续调整计划的优先级。
总体而言,我们会努力在以下几个方面不断改进。
1. 提供更多的预置模型支持。
2. 持续补齐API和算子库,改善易用性和编程体验。
3. 提供华为昇腾AI处理器的全面支持,并不断优化性能及软件架构。
4. 完善可视化、调试调优、安全相关工具。
热忱希望各位在用户社区加入讨论,并贡献您的建议。
<!-- TOC -->
- [RoadMap](#roadmap)
- [预置模型](#预置模型)
- [易用性](#易用性)
- [性能优化](#性能优化)
- [架构演进](#架构演进)
- [MindInsight调试调优](#mindinsight调试调优)
- [MindArmour安全增强包](#mindarmour安全增强包)
- [推理框架](#推理框架)
<!-- /TOC -->
## 预置模型
* CV:目标检测、GAN、图像分割、姿态识别等场景经典模型。
* NLP:RNN、Transformer类型神经网络,拓展基于Bert预训练模型的应用。
* 其它:GNN、强化学习、概率编程、AutoML等。
## 易用性
* 补齐算子、优化器、Loss函数等各类API
* 完善Python语言原生表达支持
* 支持常见的Tensor/Math操作
* 增加更多的自动并行适用场景,提高策略搜索的准确性
## 性能优化
* 优化编译时间
* 低比特混合精度训练/推理
* 提升内存使用效率
* 提供更多的融合优化手段
* 加速PyNative执行性能
## 架构演进
* 图算融合优化:使用细粒度Graph IR表达算子,构成带算子边界的中间表达,挖掘更多图层优化机会。
* 支持更多编程语言
* 优化数据增强的自动调度及分布式训练数据缓存机制
* 持续完善MindSpore IR
* Parameter Server模式分布式训练
## MindInsight调试调优
* 训练过程观察
* 直方图
* 计算图/数据图展示优化
* 集成性能Profiling/Debugger工具
* 支持多次训练间的对比
* 训练结果溯源
* 数据增强溯源对比
* 训练过程诊断
* 性能Profiling
* 基于图模型的Debugger
## MindArmour安全增强包
* 测试模型的安全性
* 提供模型安全性增强工具
* 保护训练和推理过程中的数据隐私
## 推理框架
* 提供Tensorflow/Caffe/ONNX模型格式支持
* IOS系统支持
* 完善更多的CPU算子
* 更多CV/NLP模型支持
* 在线学习
* 支持部署在IOT设备
* 低比特量化
* CPU和NPU异构调度
# 安装MindSpore
本文档介绍如何在CPU的环境上快速安装MindSpore。
<!-- TOC -->
- [安装MindSpore](#安装mindspore)
- [环境要求](#环境要求)
- [系统要求和软件依赖](#系统要求和软件依赖)
- [Conda安装(可选)](#conda安装可选)
- [安装指南](#安装指南)
- [通过可执行文件安装](#通过可执行文件安装)
- [从源码编译安装](#从源码编译安装)
- [安装MindArmour](#安装mindarmour)
<!-- /TOC -->
## 环境要求
### 系统要求和软件依赖
| 版本号 | 操作系统 | 可执行文件安装依赖 | 源码编译安装依赖 |
| ---- | :--- | :--- | :--- |
| MindSpore 0.1.0-alpha | Ubuntu 16.04(及以上) x86_64 | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - 其他依赖项参见[requirements.txt](https://gitee.com/mindspore/mindspore/blob/r0.1/requirements.txt) | **编译依赖:**<br> - [Python](https://www.python.org/downloads/) 3.7.5 <br> - [wheel](https://pypi.org/project/wheel/) >= 0.32.0 <br> - [GCC](https://gcc.gnu.org/releases.html) 7.3.0 <br> - [CMake](https://cmake.org/download/) >= 3.14.1 <br> - [patch](http://ftp.gnu.org/gnu/patch/) >= 2.5 <br> - [Autoconf](https://www.gnu.org/software/autoconf) >= 2.64 <br> - [Libtool](https://www.gnu.org/software/libtool) >= 2.4.6 <br> - [Automake](https://www.gnu.org/software/automake) >= 1.15.1 <br> **安装依赖:**<br> 与可执行文件安装依赖相同 |
- Ubuntu版本为18.04时,GCC 7.3.0可以直接通过apt命令安装。
- 在联网状态下,安装whl包时会自动下载requirements.txt中的依赖项,其余情况需自行安装。
### Conda安装(可选)
1. Conda安装包下载路径如下。
- [X86 Anaconda](https://www.anaconda.com/distribution/)[X86 Miniconda](https://docs.conda.io/en/latest/miniconda.html)
2. 创建并激活Python环境。
```bash
conda create -n {your_env_name} python=3.7.5
conda activate {your_env_name}
```
> Conda是强大的Python环境管理工具,建议初学者上网查阅更多资料。
## 安装指南
### 通过可执行文件安装
1.[MindSpore网站下载地址](https://www.mindspore.cn/versions)下载whl包,建议先进行SHA-256完整性校验,执行如下命令安装MindSpore。
```bash
pip install mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
2. 执行如下命令,如果没有提示`No module named 'mindspore'`等加载错误的信息,则说明安装成功。
```bash
python -c 'import mindspore'
```
### 从源码编译安装
1. 从代码仓下载源码。
```bash
git clone https://gitee.com/mindspore/mindspore.git -b r0.1
```
2. 在源码根目录下执行如下命令编译MindSpore。
```bash
bash build.sh -e cpu -z -j4
```
> - 在执行上述命令前,需保证可执行文件cmake和patch所在路径已加入环境变量PATH中。
> - build.sh中会执行git clone获取第三方依赖库的代码,请提前确保git的网络设置正确可用。
> - 如果编译机性能较好,可在执行中增加-j{线程数}来增加线程数量。如`bash build.sh -e cpu -z -j12`。
3. 执行如下命令安装MindSpore。
```bash
chmod +x build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
pip install build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
4. 执行如下命令,如果没有提示`No module named 'mindspore'`等加载错误的信息,则说明安装成功。
```bash
python -c 'import mindspore'
```
# 安装MindArmour
当您进行AI模型安全研究或想要增强AI应用模型的防护能力时,可以选装MindArmour。
## 环境要求
### 系统要求和软件依赖
| 版本号 | 操作系统 | 可执行文件安装依赖 | 源码编译安装依赖 |
| ---------------------- | :------------------ | :----------------------------------------------------------- | :----------------------- |
| MindArmour 0.1.0-alpha | Ubuntu 16.04(及以上) x86_64 | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - MindSpore 0.1.0-alpha<br> - 其他依赖项参见[setup.py](https://gitee.com/mindspore/mindarmour/blob/r0.1/setup.py) | 与可执行文件安装依赖相同 |
- 在联网状态下,安装whl包时会自动下载setup.py中的依赖项,其余情况需自行安装。
## 安装指南
### 通过可执行文件安装
1.[MindSpore网站下载地址](https://www.mindspore.cn/versions)下载whl包,建议先进行SHA-256完整性校验,执行如下命令安装MindArmour。
```bash
pip install mindarmour-{version}-cp37-cp37m-linux_{arch}.whl
```
2. 执行如下命令,如果没有提示`No module named 'mindarmour'`等加载错误的信息,则说明安装成功。
```bash
python -c 'import mindarmour'
```
### 从源码编译安装
1. 从代码仓下载源码。
```bash
git clone https://gitee.com/mindspore/mindarmour.git -b r0.1
```
2. 在源码根目录下,执行如下命令编译并安装MindArmour。
```bash
cd mindarmour
python setup.py install
```
3. 执行如下命令,如果没有提示`No module named 'mindarmour'`等加载错误的信息,则说明安装成功。
```bash
python -c 'import mindarmour'
```
# MindSpore Installation Guide
This document describes how to quickly install MindSpore on a CPU environment.
<!-- TOC -->
- [MindSpore Installation Guide](#mindspore-installation-guide)
- [Environment Requirements](#environment-requirements)
- [System Requirements and Software Dependencies](#system-requirements-and-software-dependencies)
- [(Optional) Installing Conda](#optional-installing-conda)
- [Installation Guide](#installation-guide)
- [Installing Using Executable Files](#installing-using-executable-files)
- [Installing Using the Source Code](#installing-using-the-source-code)
- [Installing MindArmour](#installing-mindarmour)
<!-- /TOC -->
## Environment Requirements
### System Requirements and Software Dependencies
| Version | Operating System | Executable File Installation Dependencies | Source Code Compilation and Installation Dependencies |
| ---- | :--- | :--- | :--- |
| MindSpore 0.1.0-alpha | Ubuntu 16.04 or later x86_64 | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - For details about other dependency items, see [requirements.txt](https://gitee.com/mindspore/mindspore/blob/r0.1/requirements.txt). | **Compilation dependencies:**<br> - [Python](https://www.python.org/downloads/) 3.7.5 <br> - [wheel](https://pypi.org/project/wheel/) >= 0.32.0 <br> - [GCC](https://gcc.gnu.org/releases.html) 7.3.0 <br> - [CMake](https://cmake.org/download/) >= 3.14.1 <br> - [patch](http://ftp.gnu.org/gnu/patch/) >= 2.5 <br> - [Autoconf](https://www.gnu.org/software/autoconf) >= 2.64 <br> - [Libtool](https://www.gnu.org/software/libtool) >= 2.4.6 <br> - [Automake](https://www.gnu.org/software/automake) >= 1.15.1 <br> **Installation dependencies:**<br> same as the executable file installation dependencies. |
- When Ubuntu version is 18.04, GCC 7.3.0 can be installed by using apt command.
- When the network is connected, dependency items in the requirements.txt file are automatically downloaded during .whl package installation. In other cases, you need to manually install dependency items.
### (Optional) Installing Conda
1. Download the Conda installation package from the following path:
- [X86 Anaconda](https://www.anaconda.com/distribution/) or [X86 Miniconda](https://docs.conda.io/en/latest/miniconda.html)
2. Create and activate the Python environment.
```bash
conda create -n {your_env_name} python=3.7.5
conda activate {your_env_name}
```
> Conda is a powerful Python environment management tool. It is recommended that a beginner read related information on the Internet first.
## Installation Guide
### Installing Using Executable Files
1. Download the .whl package from the [MindSpore website](https://www.mindspore.cn/versions/en). It is recommended to perform SHA-256 integrity verification first and run the following command to install MindSpore:
```bash
pip install mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
2. Run the following command. If no loading error message such as `No module named 'mindspore'` is displayed, the installation is successful.
```bash
python -c 'import mindspore'
```
### Installing Using the Source Code
1. Download the source code from the code repository.
```bash
git clone https://gitee.com/mindspore/mindspore.git -b r0.1
```
2. Run the following command in the root directory of the source code to compile MindSpore:
```bash
bash build.sh -e cpu -z -j4
```
> - Before running the preceding command, ensure that the paths where the executable files cmake and patch store have been added to the environment variable PATH.
> - In the build.sh script, the git clone command will be executed to obtain the code in the third-party dependency database. Ensure that the network settings of Git are correct.
> - If the compiler performance is strong, you can add -j{Number of threads} in to script to increase the number of threads. For example, `bash build.sh -e cpu -z -j12`.
3. Run the following command to install MindSpore:
```bash
chmod +x build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
pip install build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
4. Run the following command. If no loading error message such as `No module named 'mindspore'` is displayed, the installation is successful.
```bash
python -c 'import mindspore'
```
# Installing MindArmour
If you need to conduct AI model security research or enhance the security of the model in you applications, you can install MindArmour.
## Environment Requirements
### System Requirements and Software Dependencies
| Version | Operating System | Executable File Installation Dependencies | Source Code Compilation and Installation Dependencies |
| ---- | :--- | :--- | :--- |
| MindArmour 0.1.0-alpha | Ubuntu 16.04 or later x86_64 | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - MindSpore 0.1.0-alpha <br> - For details about other dependency items, see [setup.py](https://gitee.com/mindspore/mindarmour/blob/r0.1/setup.py). | Same as the executable file installation dependencies. |
- When the network is connected, dependency items in the setup.py file are automatically downloaded during .whl package installation. In other cases, you need to manually install dependency items.
## Installation Guide
### Installing Using Executable Files
1. Download the .whl package from the [MindSpore website](https://www.mindspore.cn/versions/en). It is recommended to perform SHA-256 integrity verification first and run the following command to install MindArmour:
```bash
pip install mindarmour-{version}-cp37-cp37m-linux_{arch}.whl
```
2. Run the following command. If no loading error message such as `No module named 'mindarmour'` is displayed, the installation is successful.
```bash
python -c 'import mindarmour'
```
### Installing Using the Source Code
1. Download the source code from the code repository.
```bash
git clone https://gitee.com/mindspore/mindarmour.git -b r0.1
```
2. Run the following command in the root directory of the source code to compile and install MindArmour:
```bash
cd mindarmour
python setup.py install
```
3. Run the following command. If no loading error message such as `No module named 'mindarmour'` is displayed, the installation is successful.
```bash
python -c 'import mindarmour'
```
# 安装MindSpore
本文档介绍如何在Ascend AI处理器的环境上快速安装MindSpore。
<!-- TOC -->
- [安装MindSpore](#安装mindspore)
- [环境要求](#环境要求)
- [硬件要求](#硬件要求)
- [系统要求和软件依赖](#系统要求和软件依赖)
- [Conda安装(可选)](#conda安装可选)
- [配套软件包依赖配置](#配套软件包依赖配置)
- [安装指南](#安装指南)
- [通过可执行文件安装](#通过可执行文件安装)
- [从源码编译安装](#从源码编译安装)
- [配置环境变量](#配置环境变量)
- [安装验证](#安装验证)
- [安装MindInsight](#安装mindinsight)
- [安装MindArmour](#安装mindarmour)
<!-- /TOC -->
## 环境要求
### 硬件要求
- Ascend 910 AI处理器
> - 申请方式:填写[申请表](https://www.mindspore.cn/table)发送至contact@mindspore.cn,审核通过即可获取云上资源。
> - 需为每张卡预留至少32G内存。
### 系统要求和软件依赖
| 版本号 | 操作系统 | 可执行文件安装依赖 | 源码编译安装依赖 |
| ---- | :--- | :--- | :--- |
| MindSpore 0.1.0-alpha | - Ubuntu 16.04(及以上) x86_64 <br> - EulerOS 2.8 arrch64 <br> - EulerOS 2.5 x86_64 | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - Ascend 910 AI处理器配套软件包(对应版本Atlas T 1.1.T106) <br> - 其他依赖项参见[requirements.txt](https://gitee.com/mindspore/mindspore/blob/r0.1/requirements.txt) | **编译依赖:**<br> - [Python](https://www.python.org/downloads/) 3.7.5 <br> - Ascend 910 AI处理器配套软件包(对应版本Atlas T 1.1.T106) <br> - [wheel](https://pypi.org/project/wheel/) >= 0.32.0 <br> - [GCC](https://gcc.gnu.org/releases.html) 7.3.0 <br> - [CMake](https://cmake.org/download/) >= 3.14.1 <br> - [patch](http://ftp.gnu.org/gnu/patch/) >= 2.5 <br> - [Autoconf](https://www.gnu.org/software/autoconf) >= 2.64 <br> - [Libtool](https://www.gnu.org/software/libtool) >= 2.4.6 <br> - [Automake](https://www.gnu.org/software/automake) >= 1.15.1 <br> **安装依赖:**<br> 与可执行文件安装依赖相同 |
- 确认当前用户有权限访问Ascend 910 AI处理器配套软件包(对应版本Atlas T 1.1.T106)的安装路径`/usr/local/HiAI`,若无权限,需要root用户将当前用户添加到`/usr/local/HiAI`所在的用户组,具体配置请详见配套软件包的说明文档。
- Ubuntu版本为18.04时,GCC 7.3.0可以直接通过apt命令安装。
- 在联网状态下,安装whl包时会自动下载requirements.txt中的依赖项,其余情况需自行安装。
### Conda安装(可选)
1. 针对不同的CPU架构,Conda安装包下载路径如下。
- [X86 Anaconda](https://www.anaconda.com/distribution/)[X86 Miniconda](https://docs.conda.io/en/latest/miniconda.html)
- [ARM Anaconda](https://github.com/Archiconda/build-tools/releases/download/0.2.3/Archiconda3-0.2.3-Linux-aarch64.sh)
2. 创建并激活Python环境。
```bash
conda create -n {your_env_name} python=3.7.5
conda activate {your_env_name}
```
> Conda是强大的Python环境管理工具,建议初学者上网查阅更多资料。
### 配套软件包依赖配置
- 安装Ascend 910 AI处理器配套软件包(对应版本Atlas T 1.1.T106)提供的whl包,whl包随配套软件包发布,升级配套软件包之后需要重新安装。
```bash
pip install /usr/local/HiAI/runtime/lib64/topi-{version}-py3-none-any.whl
pip install /usr/local/HiAI/runtime/lib64/te-{version}-py3-none-any.whl
```
## 安装指南
### 通过可执行文件安装
-[MindSpore网站下载地址](https://www.mindspore.cn/versions)下载whl包,建议先进行SHA-256完整性校验,执行如下命令安装MindSpore。
```bash
pip install mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
### 从源码编译安装
必须在Ascend 910 AI处理器的环境上进行编译安装。
1. 从代码仓下载源码。
```bash
git clone https://gitee.com/mindspore/mindspore.git -b r0.1
```
2. 在源码根目录下,执行如下命令编译MindSpore。
```bash
bash build.sh -e d -z
```
> - 在执行上述命令前,需保证可执行文件cmake和patch所在路径已加入环境变量PATH中。
> - build.sh中会执行git clone获取第三方依赖库的代码,请提前确保git的网络设置正确可用。
> - build.sh中默认的编译线程数为8,如果编译机性能较差可能会出现编译错误,可在执行中增加-j{线程数}来减少线程数量。如`bash build.sh -e d -z -j4`。
3. 执行如下命令安装MindSpore。
```bash
chmod +x build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
pip install build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
## 配置环境变量
- 安装好MindSpore之后,需要导出Runtime相关环境变量。
```bash
# control log level. 0-DEBUG, 1-INFO, 2-WARNING, 3-ERROR, default level is WARNING.
export GLOG_v=2
# Conda environmental options
LOCAL_HIAI=/usr/local/HiAI # the root directory of run package
# lib libraries that the run package depends on
export LD_LIBRARY_PATH=${LOCAL_HIAI}/runtime/lib64/:/usr/local/HiAI/driver/lib64:${LD_LIBRARY_PATH}
# Environment variables that must be configured
export PATH=${LOCAL_HIAI}/runtime/ccec_compiler/bin/:${PATH} # TBE operator compilation tool path
export TBE_IMPL_PATH=${LOCAL_HIAI}/runtime/ops/op_impl/built-in/ai_core/tbe/impl/ # TBE operator implementation path
export PYTHONPATH=${LOCAL_HIAI}/runtime/ops/op_impl/built-in/ai_core/tbe/:${PYTHONPATH} # Python library that TBE implementation depends on
```
## 安装验证
- 安装并配置好环境变量后,执行如下python脚本:
```bash
import numpy as np
from mindspore import Tensor
from mindspore.ops import functional as F
import mindspore.context as context
context.set_context(device_target="Ascend")
x = Tensor(np.ones([1,3,3,4]).astype(np.float32))
y = Tensor(np.ones([1,3,3,4]).astype(np.float32))
print(F.tensor_add(x, y))
```
- 若出现如下结果,即安装验证通过。
```
[[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]],
[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]],
[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]]]
```
# 安装MindInsight
当您需要查看训练过程中的标量、图像、计算图以及模型超参等信息时,可以选装MindInsight。
## 环境要求
### 系统要求和软件依赖
| 版本号 | 操作系统 | 可执行文件安装依赖 | 源码编译安装依赖 |
| ---- | :--- | :--- | :--- |
| MindInsight 0.1.0-alpha | - Ubuntu 16.04(及以上) x86_64 <br> - EulerOS 2.8 arrch64 <br> - EulerOS 2.5 x86_64 <br> | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - MindSpore 0.1.0-alpha <br> - 其他依赖项参见[requirements.txt](https://gitee.com/mindspore/mindinsight/blob/r0.1/requirements.txt) | **编译依赖:**<br> - [Python](https://www.python.org/downloads/) 3.7.5 <br> - [CMake](https://cmake.org/download/) >= 3.14.1 <br> - [GCC](https://gcc.gnu.org/releases.html) 7.3.0 <br> - [node.js](https://nodejs.org/en/download/) >= 10.19.0 <br> - [wheel](https://pypi.org/project/wheel/) >= 0.32.0 <br> - [pybind11](https://pypi.org/project/pybind11/) >= 2.4.3 <br> **安装依赖:**<br> 与可执行文件安装依赖相同 |
- 在联网状态下,安装whl包时会自动下载requirements.txt中的依赖项,其余情况需自行安装。
## 安装指南
### 通过可执行文件安装
1.[MindSpore网站下载地址](https://www.mindspore.cn/versions)下载whl包,建议先进行SHA-256完整性校验,执行如下命令安装MindInsight。
```bash
pip install mindinsight-{version}-cp37-cp37m-linux_{arch}.whl
```
2. 执行如下命令,如果提示`web address: http://127.0.0.1:8080`,则说明安装成功。
```bash
mindinsight start
```
### 从源码编译安装
1. 从代码仓下载源码。
```bash
git clone https://gitee.com/mindspore/mindinsight.git -b r0.1
```
2. 可选择以下任意一种安装方式:
(1) 进入源码的根目录,执行安装命令。
```bash
cd mindinsight
pip install -r requirements.txt
python setup.py install
```
(2) 构建whl包进行安装。
进入源码的build目录,执行MindInsight编译脚本。
```bash
cd mindinsight/build
bash build.sh
```
进入源码的output目录,即可查看生成的MindInsight安装包,执行安装命令。
```bash
cd mindinsight/output
pip install mindinsight-{version}-cp37-cp37m-linux_{arch}.whl
```
3. 执行如下命令,如果提示`web address: http://127.0.0.1:8080`,则说明安装成功。
```bash
mindinsight start
```
# 安装MindArmour
当您进行AI模型安全研究或想要增强AI应用模型的防护能力时,可以选装MindArmour。
## 环境要求
### 系统要求和软件依赖
| 版本号 | 操作系统 | 可执行文件安装依赖 | 源码编译安装依赖 |
| ---- | :--- | :--- | :--- |
| MindArmour 0.1.0-alpha | - Ubuntu 16.04(及以上) x86_64 <br> - EulerOS 2.8 arrch64 <br> - EulerOS 2.5 x86_64 <br> | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - MindSpore 0.1.0-alpha <br> - 其他依赖项参见[setup.py](https://gitee.com/mindspore/mindarmour/blob/r0.1/setup.py) | 与可执行文件安装依赖相同 |
- 在联网状态下,安装whl包时会自动下载setup.py中的依赖项,其余情况需自行安装。
## 安装指南
### 通过可执行文件安装
1.[MindSpore网站下载地址](https://www.mindspore.cn/versions)下载whl包,建议先进行SHA-256完整性校验,执行如下命令安装MindArmour。
```bash
pip install mindarmour-{version}-cp37-cp37m-linux_{arch}.whl
```
2. 执行如下命令,如果没有提示`No module named 'mindarmour'`等加载错误的信息,则说明安装成功。
```bash
python -c 'import mindarmour'
```
### 从源码编译安装
1. 从代码仓下载源码。
```bash
git clone https://gitee.com/mindspore/mindarmour.git -b r0.1
```
2. 在源码根目录下,执行如下命令编译并安装MindArmour。
```bash
cd mindarmour
python setup.py install
```
3. 执行如下命令,如果没有提示`No module named 'mindarmour'`等加载错误的信息,则说明安装成功。
```bash
python -c 'import mindarmour'
```
# MindSpore Installation Guide
This document describes how to quickly install MindSpore on an Ascend AI processor environment.
<!-- TOC -->
- [MindSpore Installation Guide](#mindspore-installation-guide)
- [Environment Requirements](#environment-requirements)
- [Hardware Requirements](#hardware-requirements)
- [System Requirements and Software Dependencies](#system-requirements-and-software-dependencies)
- [(Optional) Installing Conda](#optional-installing-conda)
- [Configuring software package Dependencies](#configuring-software-package-dependencies)
- [Installation Guide](#installation-guide)
- [Installing Using Executable Files](#installing-using-executable-files)
- [Installing Using the Source Code](#installing-using-the-source-code)
- [Configuring Environment Variables](#configuring-environment-variables)
- [Installation Verification](#installation-verification)
- [Installing MindInsight](#installing-mindinsight)
- [Installing MindArmour](#installing-mindarmour)
<!-- /TOC -->
## Environment Requirements
### Hardware Requirements
- Ascend 910 AI processor
> - Reserve at least 32 GB memory for each card.
### System Requirements and Software Dependencies
| Version | Operating System | Executable File Installation Dependencies | Source Code Compilation and Installation Dependencies |
| ---- | :--- | :--- | :--- |
| MindSpore 0.1.0-alpha | - Ubuntu 16.04 or later x86_64 <br> - EulerOS 2.8 arrch64 <br> - EulerOS 2.5 x86_64 | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - Ascend 910 AI processor software package(Version:Atlas T 1.1.T106) <br> - For details about other dependency items, see [requirements.txt](https://gitee.com/mindspore/mindspore/blob/r0.1/requirements.txt). | **Compilation dependencies:**<br> - [Python](https://www.python.org/downloads/) 3.7.5 <br> - Ascend 910 AI processor software package(Version:Atlas T 1.1.T106) <br> - [wheel](https://pypi.org/project/wheel/) >= 0.32.0 <br> - [GCC](https://gcc.gnu.org/releases.html) 7.3.0 <br> - [CMake](https://cmake.org/download/) >= 3.14.1 <br> - [patch](http://ftp.gnu.org/gnu/patch/) >= 2.5 <br> - [Autoconf](https://www.gnu.org/software/autoconf) >= 2.64 <br> - [Libtool](https://www.gnu.org/software/libtool) >= 2.4.6 <br> - [Automake](https://www.gnu.org/software/automake) >= 1.15.1 <br> **Installation dependencies:**<br> same as the executable file installation dependencies. |
- Confirm that the current user has the right to access the installation path `/usr/local/hiAI `of Ascend 910 AI processor software package(Version:Atlas T 1.1.T106). If not, the root user needs to add the current user to the user group where `/usr/local/hiAI` is located. For the specific configuration, please refer to the software package instruction document.
- When Ubuntu version is 18.04, GCC 7.3.0 can be installed by using apt command.
- When the network is connected, dependency items in the requirements.txt file are automatically downloaded during .whl package installation. In other cases, you need to manually install dependency items.
### (Optional) Installing Conda
1. Download the Conda installation package from the following path:
- [X86 Anaconda](https://www.anaconda.com/distribution/) or [X86 Miniconda](https://docs.conda.io/en/latest/miniconda.html)
- [ARM Anaconda](https://github.com/Archiconda/build-tools/releases/download/0.2.3/Archiconda3-0.2.3-Linux-aarch64.sh)
2. Create and activate the Python environment.
```bash
conda create -n {your_env_name} python=3.7.5
conda activate {your_env_name}
```
> Conda is a powerful Python environment management tool. It is recommended that a beginner read related information on the Internet first.
### Configuring software package Dependencies
- Install the .whl package provided in Ascend 910 AI processor software package(Version:Atlas T 1.1.T106). The .whl package is released with the software package. After software package is upgraded, reinstall the .whl package.
```bash
pip install /usr/local/HiAI/runtime/lib64/topi-{version}-py3-none-any.whl
pip install /usr/local/HiAI/runtime/lib64/te-{version}-py3-none-any.whl
```
## Installation Guide
### Installing Using Executable Files
- Download the .whl package from the [MindSpore website](https://www.mindspore.cn/versions/en). It is recommended to perform SHA-256 integrity verification first and run the following command to install MindSpore:
```bash
pip install mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
### Installing Using the Source Code
The compilation and installation must be performed on the Ascend 910 AI processor environment.
1. Download the source code from the code repository.
```bash
git clone https://gitee.com/mindspore/mindspore.git -b r0.1
```
2. Run the following command in the root directory of the source code to compile MindSpore:
```bash
bash build.sh -e d -z
```
> - Before running the preceding command, ensure that the paths where the executable files cmake and patch store have been added to the environment variable PATH.
> - In the build.sh script, the git clone command will be executed to obtain the code in the third-party dependency database. Ensure that the network settings of Git are correct.
> - In the build.sh script, the default number of compilation threads is 8. If the compiler performance is poor, compilation errors may occur. You can add -j{Number of threads} in to script to reduce the number of threads. For example, `bash build.sh -e d -z -j4`.
3. Run the following command to install MindSpore:
```bash
chmod +x build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
pip install build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
## Configuring Environment Variables
- After MindSpore is installed, export runtime-related environment variables.
```bash
# control log level. 0-DEBUG, 1-INFO, 2-WARNING, 3-ERROR, default level is WARNING.
export GLOG_v=2
# Conda environmental options
LOCAL_HIAI=/usr/local/HiAI # the root directory of run package
# lib libraries that the run package depends on
export LD_LIBRARY_PATH=${LOCAL_HIAI}/runtime/lib64/:/usr/local/HiAI/driver/lib64:${LD_LIBRARY_PATH}
# Environment variables that must be configured
export PATH=${LOCAL_HIAI}/runtime/ccec_compiler/bin/:${PATH} # TBE operator compilation tool path
export TBE_IMPL_PATH=${LOCAL_HIAI}/runtime/ops/op_impl/built-in/ai_core/tbe/impl/ # TBE operator implementation path
export PYTHONPATH=${LOCAL_HIAI}/runtime/ops/op_impl/built-in/ai_core/tbe/:${PYTHONPATH} # Python library that TBE implementation depends on
```
## Installation Verification
- After configuring the environment variables, execute the following Python script:
```bash
import numpy as np
from mindspore import Tensor
from mindspore.ops import functional as F
import mindspore.context as context
context.set_context(device_target="Ascend")
x = Tensor(np.ones([1,3,3,4]).astype(np.float32))
y = Tensor(np.ones([1,3,3,4]).astype(np.float32))
print(F.tensor_add(x, y))
```
- The outputs should be same as:
```
[[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]],
[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]],
[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]]]
```
# Installing MindInsight
If you need to analyze information such as model scalars, graphs, and model traceback, you can install MindInsight.
## Environment Requirements
### System Requirements and Software Dependencies
| Version | Operating System | Executable File Installation Dependencies | Source Code Compilation and Installation Dependencies |
| ---- | :--- | :--- | :--- |
| MindInsight 0.1.0-alpha | - Ubuntu 16.04 or later x86_64 <br> - EulerOS 2.8 arrch64 <br> - EulerOS 2.5 x86_64 <br> | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - MindSpore 0.1.0-alpha <br> - For details about other dependency items, see [requirements.txt](https://gitee.com/mindspore/mindinsight/blob/r0.1/requirements.txt). | **Compilation dependencies:**<br> - [Python](https://www.python.org/downloads/) 3.7.5 <br> - [CMake](https://cmake.org/download/) >= 3.14.1 <br> - [GCC](https://gcc.gnu.org/releases.html) 7.3.0 <br> - [node.js](https://nodejs.org/en/download/) >= 10.19.0 <br> - [wheel](https://pypi.org/project/wheel/) >= 0.32.0 <br> - [pybind11](https://pypi.org/project/pybind11/) >= 2.4.3 <br> **Installation dependencies:**<br> same as the executable file installation dependencies. |
- When the network is connected, dependency items in the requirements.txt file are automatically downloaded during .whl package installation. In other cases, you need to manually install dependency items.
## Installation Guide
### Installing Using Executable Files
1. Download the .whl package from the [MindSpore website](https://www.mindspore.cn/versions/en). It is recommended to perform SHA-256 integrity verification first and run the following command to install MindInsight:
```bash
pip install mindinsight-{version}-cp37-cp37m-linux_{arch}.whl
```
2. Run the following command. If `web address: http://127.0.0.1:8080` is displayed, the installation is successful.
```bash
mindinsight start
```
### Installing Using the Source Code
1. Download the source code from the code repository.
```bash
git clone https://gitee.com/mindspore/mindinsight.git -b r0.1
```
2. Install MindInsight by using either of the following installation methods:
(1) Access the root directory of the source code and run the following installation command:
```bash
cd mindinsight
pip install -r requirements.txt
python setup.py install
```
(2) Create a .whl package to install MindInsight.
Access the build directory of the source code and run the MindInsight compilation script.
```bash
cd mindinsight/build
bash build.sh
```
Access the output directory of the source code, where the generated MindInsight installation package is stored, and run the installation command.
```bash
cd mindinsight/output
pip install mindinsight-{version}-cp37-cp37m-linux_{arch}.whl
```
3. Run the following command. If `web address: http://127.0.0.1:8080` is displayed, the installation is successful.
```bash
mindinsight start
```
# Installing MindArmour
If you need to conduct AI model security research or enhance the security of the model in you applications, you can install MindArmour.
## Environment Requirements
### System Requirements and Software Dependencies
| Version | Operating System | Executable File Installation Dependencies | Source Code Compilation and Installation Dependencies |
| ---- | :--- | :--- | :--- |
| MindArmour 0.1.0-alpha | - Ubuntu 16.04 or later x86_64 <br> - EulerOS 2.8 arrch64 <br> - EulerOS 2.5 x86_64 <br> | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - MindSpore 0.1.0-alpha <br> - For details about other dependency items, see [setup.py](https://gitee.com/mindspore/mindarmour/blob/r0.1/setup.py). | Same as the executable file installation dependencies. |
- When the network is connected, dependency items in the setup.py file are automatically downloaded during .whl package installation. In other cases, you need to manually install dependency items.
## Installation Guide
### Installing Using Executable Files
1. Download the .whl package from the [MindSpore website](https://www.mindspore.cn/versions/en). It is recommended to perform SHA-256 integrity verification first and run the following command to install MindArmour:
```bash
pip install mindarmour-{version}-cp37-cp37m-linux_{arch}.whl
```
2. Run the following command. If no loading error message such as `No module named 'mindarmour'` is displayed, the installation is successful.
```bash
python -c 'import mindarmour'
```
### Installing Using the Source Code
1. Download the source code from the code repository.
```bash
git clone https://gitee.com/mindspore/mindarmour.git -b r0.1
```
2. Run the following command in the root directory of the source code to compile and install MindArmour:
```bash
cd mindarmour
python setup.py install
```
3. Run the following command. If no loading error message such as `No module named 'mindarmour'` is displayed, the installation is successful.
```bash
python -c 'import mindarmour'
```
# 安装MindSpore
本文档介绍如何在Nvidia GPU的环境上快速安装MindSpore。
<!-- TOC -->
- [安装MindSpore](#安装mindspore)
- [环境要求](#环境要求)
- [硬件要求](#硬件要求)
- [系统要求和软件依赖](#系统要求和软件依赖)
- [Conda安装(可选)](#conda安装可选)
- [安装指南](#安装指南)
- [通过可执行文件安装](#通过可执行文件安装)
- [从源码编译安装](#从源码编译安装)
- [安装验证](#安装验证)
- [安装MindArmour](#安装mindarmour)
<!-- /TOC -->
## 环境要求
### 硬件要求
- Nvidia GPU
### 系统要求和软件依赖
| 版本号 | 操作系统 | 可执行文件安装依赖 | 源码编译安装依赖 |
| ---- | :--- | :--- | :--- |
| MindSpore 0.1.0-alpha | Ubuntu 16.04(及以上) x86_64 | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - [CUDA 9.2](https://developer.nvidia.com/cuda-92-download-archive) / [CUDA 10.1](https://developer.nvidia.com/cuda-10.1-download-archive-base) <br> - [CuDNN](https://developer.nvidia.com/rdp/cudnn-archive) >= 7.6 <br> - [OpenMPI](https://www.open-mpi.org/faq/?category=building#easy-build) 3.1.5 (可选,单机多卡/多机多卡训练需要) <br> - [NCCL](https://docs.nvidia.com/deeplearning/sdk/nccl-install-guide/index.html#debian) 2.4.8-1 (可选,单机多卡/多机多卡训练需要) <br> - 其他依赖项参见[requirements.txt](https://gitee.com/mindspore/mindspore/blob/r0.1/requirements.txt) | **编译依赖:**<br> - [Python](https://www.python.org/downloads/) 3.7.5 <br> - [wheel](https://pypi.org/project/wheel/) >= 0.32.0 <br> - [CMake](https://cmake.org/download/) >= 3.14.1 <br> - [GCC](https://gcc.gnu.org/releases.html) 7.3.0 <br> - [patch](http://ftp.gnu.org/gnu/patch/) >= 2.5 <br> - [Autoconf](https://www.gnu.org/software/autoconf) >= 2.64 <br> - [Libtool](https://www.gnu.org/software/libtool) >= 2.4.6 <br> - [Automake](https://www.gnu.org/software/automake) >= 1.15.1 <br> - [CUDA 9.2](https://developer.nvidia.com/cuda-92-download-archive) / [CUDA 10.1](https://developer.nvidia.com/cuda-10.1-download-archive-base) <br> - [CuDNN](https://developer.nvidia.com/rdp/cudnn-archive) >= 7.6 <br> **安装依赖:**<br> 与可执行文件安装依赖相同 |
- Ubuntu版本为18.04时,GCC 7.3.0可以直接通过apt命令安装。
- 在联网状态下,安装whl包时会自动下载requirements.txt中的依赖项,其余情况需自行安装。
### Conda安装(可选)
1. Conda安装包下载路径如下。
- [X86 Anaconda](https://www.anaconda.com/distribution/)[X86 Miniconda](https://docs.conda.io/en/latest/miniconda.html)
2. 创建并激活Python环境。
```bash
conda create -n {your_env_name} python=3.7.5
conda activate {your_env_name}
```
> Conda是强大的Python环境管理工具,建议初学者上网查阅更多资料。
## 安装指南
### 通过可执行文件安装
-[MindSpore网站下载地址](https://www.mindspore.cn/versions)下载whl包,建议先进行SHA-256完整性校验,执行如下命令安装MindSpore。
```bash
pip install mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
### 从源码编译安装
1. 从代码仓下载源码。
```bash
git clone https://gitee.com/mindspore/mindspore.git -b r0.1
```
2. 在源码根目录下执行如下命令编译MindSpore。
```bash
bash build.sh -e gpu -M on -z
```
> - 在执行上述命令前,需保证可执行文件cmake和patch所在路径已加入环境变量PATH中。
> - build.sh中会执行git clone获取第三方依赖库的代码,请提前确保git的网络设置正确可用。
> - build.sh中默认的编译线程数为8,如果编译机性能较差可能会出现编译错误,可在执行中增加-j{线程数}来减少线程数量。如`bash build.sh -e gpu -M on -z -j4`。
3. 执行如下命令安装MindSpore。
```bash
chmod +x build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
pip install build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
## 安装验证
- 安装好MindSpore之后,执行如下python脚本:
```bash
import numpy as np
from mindspore import Tensor
from mindspore.ops import functional as F
import mindspore.context as context
context.set_context(device_target="GPU")
x = Tensor(np.ones([1,3,3,4]).astype(np.float32))
y = Tensor(np.ones([1,3,3,4]).astype(np.float32))
print(F.tensor_add(x, y))
```
- 若出现如下结果,即安装验证通过。
```bash
[[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]],
[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]],
[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]]]
```
# 安装MindArmour
当您进行AI模型安全研究或想要增强AI应用模型的防护能力时,可以选装MindArmour。
## 环境要求
### 系统要求和软件依赖
| 版本号 | 操作系统 | 可执行文件安装依赖 | 源码编译安装依赖 |
| ---------------------- | :------------------ | :----------------------------------------------------------- | :----------------------- |
| MindArmour 0.1.0-alpha | Ubuntu 16.04(及以上) x86_64 | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - MindSpore 0.1.0-alpha <br> - 其他依赖项参见[setup.py](https://gitee.com/mindspore/mindarmour/blob/r0.1/setup.py) | 与可执行文件安装依赖相同 |
- 在联网状态下,安装whl包时会自动下载setup.py中的依赖项,其余情况需自行安装。
## 安装指南
### 通过可执行文件安装
1.[MindSpore网站下载地址](https://www.mindspore.cn/versions)下载whl包,建议先进行SHA-256完整性校验,执行如下命令安装MindArmour。
```bash
pip install mindarmour-{version}-cp37-cp37m-linux_{arch}.whl
```
2. 执行如下命令,如果没有提示`No module named 'mindarmour'`等加载错误的信息,则说明安装成功。
```bash
python -c 'import mindarmour'
```
### 从源码编译安装
1. 从代码仓下载源码。
```bash
git clone https://gitee.com/mindspore/mindarmour.git -b r0.1
```
2. 在源码根目录下,执行如下命令编译并安装MindArmour。
```bash
cd mindarmour
python setup.py install
```
3. 执行如下命令,如果没有提示`No module named 'mindarmour'`等加载错误的信息,则说明安装成功。
```bash
python -c 'import mindarmour'
```
# MindSpore Installation Guide
This document describes how to quickly install MindSpore on a NVIDIA GPU environment.
<!-- TOC -->
- [MindSpore Installation Guide](#mindspore-installation-guide)
- [Environment Requirements](#environment-requirements)
- [Hardware Requirements](#hardware-requirements)
- [System Requirements and Software Dependencies](#system-requirements-and-software-dependencies)
- [(Optional) Installing Conda](#optional-installing-conda)
- [Installation Guide](#installation-guide)
- [Installing Using Executable Files](#installing-using-executable-files)
- [Installing Using the Source Code](#installing-using-the-source-code)
- [Installation Verification](#installation-verification)
- [Installing MindArmour](#installing-mindarmour)
<!-- /TOC -->
## Environment Requirements
### Hardware Requirements
- Nvidia GPU
### System Requirements and Software Dependencies
| Version | Operating System | Executable File Installation Dependencies | Source Code Compilation and Installation Dependencies |
| ---- | :--- | :--- | :--- |
| MindSpore 0.1.0-alpha | Ubuntu 16.04 or later x86_64 | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - [CUDA 9.2](https://developer.nvidia.com/cuda-92-download-archive) / [CUDA 10.1](https://developer.nvidia.com/cuda-10.1-download-archive-base) <br> - [CuDNN](https://developer.nvidia.com/rdp/cudnn-archive) >= 7.6 <br> - [OpenMPI](https://www.open-mpi.org/faq/?category=building#easy-build) 3.1.5 (optional, required for single-node/multi-GPU and multi-node/multi-GPU training) <br> - [NCCL](https://docs.nvidia.com/deeplearning/sdk/nccl-install-guide/index.html#debian) 2.4.8-1 (optional, required for single-node/multi-GPU and multi-node/multi-GPU training) <br> - For details about other dependency items, see [requirements.txt](https://gitee.com/mindspore/mindspore/blob/r0.1/requirements.txt). | **Compilation dependencies:**<br> - [Python](https://www.python.org/downloads/) 3.7.5 <br> - [wheel](https://pypi.org/project/wheel/) >= 0.32.0 <br> - [CMake](https://cmake.org/download/) >= 3.14.1 <br> - [GCC](https://gcc.gnu.org/releases.html) 7.3.0 <br> - [patch](http://ftp.gnu.org/gnu/patch/) >= 2.5 <br> - [Autoconf](https://www.gnu.org/software/autoconf) >= 2.64 <br> - [Libtool](https://www.gnu.org/software/libtool) >= 2.4.6 <br> - [Automake](https://www.gnu.org/software/automake) >= 1.15.1 <br> - [CUDA 9.2](https://developer.nvidia.com/cuda-92-download-archive) / [CUDA 10.1](https://developer.nvidia.com/cuda-10.1-download-archive-base) <br> - [CuDNN](https://developer.nvidia.com/rdp/cudnn-archive) >= 7.6 <br> **Installation dependencies:**<br> same as the executable file installation dependencies. |
- When Ubuntu version is 18.04, GCC 7.3.0 can be installed by using apt command.
- When the network is connected, dependency items in the requirements.txt file are automatically downloaded during .whl package installation. In other cases, you need to manually install dependency items.
### (Optional) Installing Conda
1. Download the Conda installation package from the following path:
- [X86 Anaconda](https://www.anaconda.com/distribution/) or [X86 Miniconda](https://docs.conda.io/en/latest/miniconda.html)
2. Create and activate the Python environment.
```bash
conda create -n {your_env_name} python=3.7.5
conda activate {your_env_name}
```
> Conda is a powerful Python environment management tool. It is recommended that a beginner read related information on the Internet first.
## Installation Guide
### Installing Using Executable Files
- Download the .whl package from the [MindSpore website](https://www.mindspore.cn/versions/en). It is recommended to perform SHA-256 integrity verification first and run the following command to install MindSpore:
```bash
pip install mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
### Installing Using the Source Code
1. Download the source code from the code repository.
```bash
git clone https://gitee.com/mindspore/mindspore.git -b r0.1
```
2. Run the following command in the root directory of the source code to compile MindSpore:
```bash
bash build.sh -e gpu -M on -z
```
> - Before running the preceding command, ensure that the paths where the executable files cmake and patch store have been added to the environment variable PATH.
> - In the build.sh script, the git clone command will be executed to obtain the code in the third-party dependency database. Ensure that the network settings of Git are correct.
> - In the build.sh script, the default number of compilation threads is 8. If the compiler performance is poor, compilation errors may occur. You can add -j{Number of threads} in to script to reduce the number of threads. For example, `bash build.sh -e gpu -M on -z -j4`.
3. Run the following command to install MindSpore:
```bash
chmod +x build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
pip install build/package/mindspore-{version}-cp37-cp37m-linux_{arch}.whl
```
## Installation Verification
- After Installation, execute the following Python script:
```bash
import numpy as np
from mindspore import Tensor
from mindspore.ops import functional as F
import mindspore.context as context
context.set_context(device_target="GPU")
x = Tensor(np.ones([1,3,3,4]).astype(np.float32))
y = Tensor(np.ones([1,3,3,4]).astype(np.float32))
print(F.tensor_add(x, y))
```
- The outputs should be same as:
```bash
[[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]],
[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]],
[[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.],
[ 2. 2. 2. 2.]]]
```
# Installing MindArmour
If you need to conduct AI model security research or enhance the security of the model in you applications, you can install MindArmour.
## Environment Requirements
### System Requirements and Software Dependencies
| Version | Operating System | Executable File Installation Dependencies | Source Code Compilation and Installation Dependencies |
| ---- | :--- | :--- | :--- |
| MindArmour 0.1.0-alpha | Ubuntu 16.04 or later x86_64 | - [Python](https://www.python.org/downloads/) 3.7.5 <br> - MindSpore 0.1.0-alpha <br> - For details about other dependency items, see [setup.py](https://gitee.com/mindspore/mindarmour/blob/r0.1/setup.py). | Same as the executable file installation dependencies. |
- When the network is connected, dependency items in the setup.py file are automatically downloaded during .whl package installation. In other cases, you need to manually install dependency items.
## Installation Guide
### Installing Using Executable Files
1. Download the .whl package from the [MindSpore website](https://www.mindspore.cn/versions/en). It is recommended to perform SHA-256 integrity verification first and run the following command to install MindArmour:
```bash
pip install mindarmour-{version}-cp37-cp37m-linux_{arch}.whl
```
2. Run the following command. If no loading error message such as `No module named 'mindarmour'` is displayed, the installation is successful.
```bash
python -c 'import mindarmour'
```
### Installing Using the Source Code
1. Download the source code from the code repository.
```bash
git clone https://gitee.com/mindspore/mindarmour.git -b r0.1
```
2. Run the following command in the root directory of the source code to compile and install MindArmour:
```bash
cd mindarmour
python setup.py install
```
3. Run the following command. If no loading error message such as `No module named 'mindarmour'` is displayed, the installation is successful.
```bash
python -c 'import mindarmour'
```
数据集
损失函数
优化器
# Release List
<!-- TOC -->
- [Release List](#release-list)
- [0.1.0-alpha](#010-alpha)
- [Releasenotes](#releasenotes)
- [Downloads](#downloads)
- [Tutorials](#tutorials)
- [API](#api)
- [Docs](#docs)
- [master(unstable)](#masterunstable)
<!-- /TOC -->
## 0.1.0-alpha
### Releasenotes
<https://gitee.com/mindspore/mindspore/blob/r0.1/RELEASE.md>
### Downloads
| Module Name | Hardware Platform | Operating System | Download Links | SHA-256 |
| --- | --- | --- | --- | --- |
| MindSpore | Ascend910 | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/ascend/ubuntu-x86/mindspore-0.1.0-cp37-cp37m-linux_x86_64.whl> | a76df4e96c4cb69b10580fcde2d4ef46b5d426be6d47a3d8fd379c97c3e66638 |
| | | EulerOS-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/ascend/euleros-x86/mindspore-0.1.0-cp37-cp37m-linux_x86_64.whl> | 45d4fcb37bf796b3208b7c1ca70dc0db1387a878ef27836d3d445f311c8c02e0 |
| | | EulerOS-aarch64 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/ascend/euleros-aarch64/mindspore-0.1.0-cp37-cp37m-linux_aarch64.whl> | 7daba2d1739ce19d55695460dce5ef044b4d38baad4f5117056e5f77f49a12b4 |
| | GPU CUDA 9.2 | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/gpu/cuda-9.2/mindspore-0.1.0-cp37-cp37m-linux_x86_64.whl> | b6e5623135b57b8c262f3e32d97fbe1e20e8c19da185a7aba97b9dc98c7ecda1 |
| | GPU CUDA 10.1 | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/gpu/cuda-10.1/mindspore-0.1.0-cp37-cp37m-linux_x86_64.whl> | 43711725cf7e071ca21b5ba25e90d6955789fe3495c62217e70869f52ae20c01 |
| | CPU | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/cpu/ubuntu-x86/mindspore-0.1.0-cp37-cp37m-linux_x86_64.whl> | 45c473a97a6cb227e4221117bfb1b3ebe3f2eab938e0b76d5117e6c3127b8e5c |
| MindInsight | Ascend910 | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindInsight/ubuntu/x86_64/mindinsight-0.1.0-cp37-cp37m-linux_x86_64.whl> | 960b6f485ce545ccce98adfb4c62cdea216c9b7851ffdc0669827c53811c3e59 |
| | | EulerOS-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindInsight/euleros/x86_64/mindinsight-0.1.0-cp37-cp37m-linux_x86_64.whl> | 9f1ef04fec09e5b90be4a6223b3bf2943334746c1f5dac37207db4524b64942f |
| | | EulerOS-aarch64 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindInsight/euleros/aarch64/mindinsight-0.1.0-cp37-cp37m-linux_aarch64.whl> | d64207126542571057572f856010a5a8b3362ccd9e5b5c81da5b78b94face5fe |
| MindArmour | Ascend910 | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindArmour/x86_64/mindarmour-0.1.0-cp37-cp37m-linux_x86_64.whl> | 7796b6c114ee4962ce605da59a9bc47390c8910acbac318ecc0598829aad6e8c |
| | | EulerOS-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindArmour/x86_64/mindarmour-0.1.0-cp37-cp37m-linux_x86_64.whl> | 7796b6c114ee4962ce605da59a9bc47390c8910acbac318ecc0598829aad6e8c |
| | | EulerOS-aarch64 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindArmour/aarch64/mindarmour-0.1.0-cp37-cp37m-linux_aarch64.whl> | f354fcdbb3d8b4022fda5a6636e763f8091aca2167dc23e60b7f7b6d710523cb |
| | GPU CUDA 9.2/GPU CUDA 10.1/CPU | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindArmour/x86_64/mindarmour-0.1.0-cp37-cp37m-linux_x86_64.whl> | 7796b6c114ee4962ce605da59a9bc47390c8910acbac318ecc0598829aad6e8c |
### Tutorials
<https://www.mindspore.cn/tutorial/en/0.1.0-alpha/index.html>
### API
<https://www.mindspore.cn/api/en/0.1.0-alpha/index.html>
### Docs
<https://www.mindspore.cn/docs/en/0.1.0-alpha/index.html>
## master(unstable)
### Tutorials
<https://www.mindspore.cn/tutorial/en/master/index.html>
### API
<https://www.mindspore.cn/api/en/master/index.html>
### Docs
<https://www.mindspore.cn/docs/en/master/index.html>
# 发布版本列表
<!-- TOC -->
- [发布版本列表](#发布版本列表)
- [0.1.0-alpha](#010-alpha)
- [版本说明](#版本说明)
- [下载地址](#下载地址)
- [教程](#教程)
- [API](#api)
- [文档](#文档)
- [master(unstable)](#masterunstable)
<!-- /TOC -->
## 0.1.0-alpha
### 版本说明
<https://gitee.com/mindspore/mindspore/blob/r0.1/RELEASE.md>
### 下载地址
| 组件 | 硬件平台 | 操作系统 | 链接 | SHA-256 |
| --- | --- | --- | --- | --- |
| MindSpore | Ascend910 | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/ascend/ubuntu-x86/mindspore-0.1.0-cp37-cp37m-linux_x86_64.whl> | a76df4e96c4cb69b10580fcde2d4ef46b5d426be6d47a3d8fd379c97c3e66638 |
| | | EulerOS-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/ascend/euleros-x86/mindspore-0.1.0-cp37-cp37m-linux_x86_64.whl> | 45d4fcb37bf796b3208b7c1ca70dc0db1387a878ef27836d3d445f311c8c02e0 |
| | | EulerOS-aarch64 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/ascend/euleros-aarch64/mindspore-0.1.0-cp37-cp37m-linux_aarch64.whl> | 7daba2d1739ce19d55695460dce5ef044b4d38baad4f5117056e5f77f49a12b4 |
| | GPU CUDA 9.2 | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/gpu/cuda-9.2/mindspore-0.1.0-cp37-cp37m-linux_x86_64.whl> | b6e5623135b57b8c262f3e32d97fbe1e20e8c19da185a7aba97b9dc98c7ecda1 |
| | GPU CUDA 10.1 | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/gpu/cuda-10.1/mindspore-0.1.0-cp37-cp37m-linux_x86_64.whl> | 43711725cf7e071ca21b5ba25e90d6955789fe3495c62217e70869f52ae20c01 |
| | CPU | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindSpore/cpu/ubuntu-x86/mindspore-0.1.0-cp37-cp37m-linux_x86_64.whl> | 45c473a97a6cb227e4221117bfb1b3ebe3f2eab938e0b76d5117e6c3127b8e5c |
| MindInsight | Ascend910 | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindInsight/ubuntu/x86_64/mindinsight-0.1.0-cp37-cp37m-linux_x86_64.whl> | 960b6f485ce545ccce98adfb4c62cdea216c9b7851ffdc0669827c53811c3e59 |
| | | EulerOS-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindInsight/euleros/x86_64/mindinsight-0.1.0-cp37-cp37m-linux_x86_64.whl> | 9f1ef04fec09e5b90be4a6223b3bf2943334746c1f5dac37207db4524b64942f |
| | | EulerOS-aarch64 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindInsight/euleros/aarch64/mindinsight-0.1.0-cp37-cp37m-linux_aarch64.whl> | d64207126542571057572f856010a5a8b3362ccd9e5b5c81da5b78b94face5fe |
| MindArmour | Ascend910 | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindArmour/x86_64/mindarmour-0.1.0-cp37-cp37m-linux_x86_64.whl> | 7796b6c114ee4962ce605da59a9bc47390c8910acbac318ecc0598829aad6e8c |
| | | EulerOS-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindArmour/x86_64/mindarmour-0.1.0-cp37-cp37m-linux_x86_64.whl> | 7796b6c114ee4962ce605da59a9bc47390c8910acbac318ecc0598829aad6e8c |
| | | EulerOS-aarch64 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindArmour/aarch64/mindarmour-0.1.0-cp37-cp37m-linux_aarch64.whl> | f354fcdbb3d8b4022fda5a6636e763f8091aca2167dc23e60b7f7b6d710523cb |
| | GPU CUDA 9.2/GPU CUDA 10.1/CPU | Ubuntu-x86 | <https://ms-release.obs.cn-north-4.myhuaweicloud.com/0.1.0-alpha/MindArmour/x86_64/mindarmour-0.1.0-cp37-cp37m-linux_x86_64.whl> | 7796b6c114ee4962ce605da59a9bc47390c8910acbac318ecc0598829aad6e8c |
### 教程
<https://www.mindspore.cn/tutorial/zh-CN/0.1.0-alpha/index.html>
### API
<https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/index.html>
### 文档
<https://www.mindspore.cn/docs/zh-CN/0.1.0-alpha/index.html>
## master(unstable)
### 教程
<https://www.mindspore.cn/tutorial/zh-CN/master/index.html>
### API
<https://www.mindspore.cn/api/zh-CN/master/index.html>
### 文档
<https://www.mindspore.cn/docs/zh-CN/master/index.html>
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source_zh_cn
BUILDDIR = build_zh_cn
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
sphinx
recommonmark
sphinx-markdown-tables
sphinx_rtd_theme
jieba
\ No newline at end of file
Community
=========
Contributing Code
-----------------
If you want to contribute code, please read https://gitee.com/mindspore/mindspore/blob/master/CONTRIBUTING.md .
Contributing Documents
----------------------
If you want to contribute documents, please read https://gitee.com/mindspore/docs/blob/master/CONTRIBUTING_DOC.md .
\ No newline at end of file
# Computer Vision Applications
<!-- TOC -->
- [Computer Vision Applications](#computer-vision-applications)
- [Overview](#overview)
- [Image Classification](#image-classification)
- [Task Description and Preparation](#task-description-and-preparation)
- [Downloading the CIFAR-10 Dataset](#downloading-the-cifar-10-dataset)
- [Data Preloading and Preprocessing](#data-preloading-and-preprocessing)
- [Defining the CNN](#defining-the-cnn)
- [Defining the Loss Function and Optimizer](#defining-the-loss-function-and-optimizer)
- [Calling the High-level `Model` API To Train and Save the Model File](#calling-the-high-level-model-api-to-train-and-save-the-model-file)
- [Loading and Validating the Saved Model](#loading-and-validating-the-saved-model)
- [References](#references)
<!-- /TOC -->
## Overview
Computer vision is the most widely researched and mature technology field of deep learning, and is widely used in scenarios such as mobile phone photographing, intelligent security protection, and automated driving. Since AlexNet won the ImageNet competition in 2012, deep learning has greatly promoted the development of the computer vision field. Almost all the most advanced computer vision algorithms are related to deep learning. Deep neural network can extract image features layer by layer and retain local invariance. It is widely used in visual tasks such as classification, detection, segmentation, tracking, retrieval, recognition, promotion, and reconstruction.
This chapter describes how to apply MindSpore to computer vision scenarios based on image classification tasks.
## Image Classification
Image classification is the most basic computer vision application and belongs to the supervised learning category. For example, determine the class of a digital image, such as cat, dog, airplane, or car. The function is as follows:
```python
def classify(image):
label = model(image)
return label
```
The key point is to select a proper model. The model generally refers to a deep convolutional neural network (CNN), such as AlexNet, VGG, GoogleNet, and ResNet.
MindSpore presets a typical CNN, such as LeNet, which can be directly used by developers. The usage method is as follows:
```python
from mindspore.model_zoo.lenet import LeNet5
network = LeNet(num_classes)
```
MindSpore supports the following image classification networks: LeNet, AlexNet, and ResNet.
## Task Description and Preparation
![cifar10](images/cifar10.jpg)
Figure 1: CIFAR-10 dataset [1]
Figure 1 shows that the CIFAR-10 dataset contains 10 classes of 60,000 images. Each class contains 6000 images. 50,000 images are for training and 10,000 images are for testing. The size of each image is 32 x 32 pixels.
Generally, a training indicator of image classification is accuracy, that is, a ratio of a quantity of accurately predicted examples to a total quantity of predicted examples.
Next, let's use MindSpore to solve the image classification task. The overall process is as follows:
1. Download the CIFAR-10 dataset.
2. Load and preprocess data.
3. Define a convolutional neural network. In this example, the ResNet-50 network is used.
4. Define the loss function and optimizer.
5. Call the high-level `Model` API to train and save the model file.
6. Load the saved model for inference.
> This example is for the hardware platform of the Ascend 910 AI processor, download the complete code at <https://gitee.com/mindspore/docs/blob/r0.1/tutorials/tutorial_code/resnet>.
The key parts of the task process code are explained below.
### Downloading the CIFAR-10 Dataset
CIFAR-10 dataset download address: [the website of Cifar-10 Dataset](https://www.cs.toronto.edu/~kriz/cifar.html) In this example, the data is in binary format. In the Linux environment, run the following command to download the dataset:
```shell
wget https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz
```
Run the following command to decompress the dataset:
```shell
tar -zvxf cifar-10-binary.tar.gz
```
### Data Preloading and Preprocessing
1. Load the dataset.
Data can be loaded through the built-in dataset format `Cifar10Dataset` API.
> `Cifar10Dataset`: The read type is random read. The built-in CIFAR-10 dataset contains images and labels. The default image format is uint8, and the default label data format is uint32. For details, see the description of the `Cifar10Dataset` API.
The data loading code is as follows, where `data_home` indicates the data storage location:
```python
cifar_ds = ds.Cifar10Dataset(data_home)
```
2. Enhance the data.
Data augmentation is to normalize data and enrich the number of data samples. Common data augmentation modes include cropping, flipping, and color change. MindSpore calls the `map` method to perform augmentation operations on images.
```python
resize_height = 224
resize_width = 224
rescale = 1.0 / 255.0
shift = 0.0
# define map operations
random_crop_op = C.RandomCrop((32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT
random_horizontal_op = C.RandomHorizontalFlip()
resize_op = C.Resize((resize_height, resize_width)) # interpolation default BILINEAR
rescale_op = C.Rescale(rescale, shift)
normalize_op = C.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
changeswap_op = C.HWC2CHW()
type_cast_op = C2.TypeCast(mstype.int32)
c_trans = []
if training:
c_trans = [random_crop_op, random_horizontal_op]
c_trans += [resize_op, rescale_op, normalize_op, changeswap_op]
# apply map operations on images
cifar_ds = cifar_ds.map(input_columns="label", operations=type_cast_op)
cifar_ds = cifar_ds.map(input_columns="image", operations=c_trans)
```
3. Shuffle and batch process the data.
Shuffle data randomly to disorder the data sequence and read data in batches for model training:
```python
# apply repeat operations
cifar_ds = cifar_ds.repeat(repeat_num)
# apply shuffle operations
cifar_ds = cifar_ds.shuffle(buffer_size=10)
# apply batch operations
cifar_ds = cifar_ds.batch(batch_size=args_opt.batch_size, drop_remainder=True)
```
### Defining the CNN
CNN is a standard algorithm for image classification tasks. CNN uses a layered structure to perform feature extraction on an image, and is formed by stacking a series of network layers, such as a convolutional layer, a pooling layer, and an activation layer.
ResNet is recommended. First, it is deep enough with 34 layers, 50 layers, or 101 layers. The deeper the hierarchy, the stronger the representation capability, and the higher the classification accuracy. Second, it is learnable. The residual structure is used. The lower layer is directly connected to the upper layer through the shortcut connection, which solves the problem of gradient disappearance caused by the network depth during the reverse propagation. In addition, the ResNet network has good performance, including the recognition accuracy, model size, and parameter quantity.
MindSpore Model Zoo has a built-in ResNet model. In this example, the ResNet-50 network is used. The calling method is as follows:
```python
from mindspore.model_zoo.resnet import resnet50
network = resnet50(class_num=10)
```
For more information about ResNet, see [ResNet Paper](https://arxiv.org/abs/1512.03385).
### Defining the Loss Function and Optimizer
A loss function and an optimizer need to be defined. The loss function is a training objective of the deep learning, and is also referred to as an objective function. The loss function indicates the distance between a logit of a neural network and a label, and is scalar data.
Common loss functions include mean square error, L2 loss, Hinge loss, and cross entropy. Cross entropy is usually used for image classification.
The optimizer is used for neural network solution (training). Because of the large scale of neural network parameters, the stochastic gradient descent (SGD) algorithm and its improved algorithm are used in deep learning to solve the problem. MindSpore encapsulates common optimizers, such as `SGD`, `ADAM`, and `Momemtum`. In this example, the `Momentum` optimizer is used. Generally, two parameters need to be set: `moment` and `weight decay`.
An example of the code for defining the loss function and optimizer in MindSpore is as follows:
```python
# loss function definition
ls = SoftmaxCrossEntropyWithLogits(sparse=True, is_grad=False, reduction="mean")
# optimization definition
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9)
```
### Calling the High-level `Model` API To Train and Save the Model File
After data preprocessing, network definition, and loss function and optimizer definition are complete, model training can be performed. Model training involves two iterations: multi-round iteration (epoch) of datasets and single-step iteration based on the batch size of datasets. The single-step iteration refers to extracting data from a dataset by batch, inputting the data to a network to calculate a loss function, and then calculating and updating a gradient of training parameters by using an optimizer.
To simplify the training process, MindSpore encapsulates the high-level `Model` API. You can enter the network, loss function, and optimizer to complete the `Model` initialization, and then call the `train` API for training. The `train` API parameters include the number of iterations (`epoch`) and dataset (`dataset`).
Model saving is a process of persisting training parameters. In the `Model` class, the model is saved using the callback function, as shown in the following code: You can set the parameters of the callback function by using `CheckpointConfig`. `save_checkpoint_steps` indicates that the model is saved once every fixed number of single-step iterations, and `keep_checkpoint_max` indicates the maximum number of saved models.
```python
'''
network, loss, optimizer are defined before.
batch_num, epoch_size are training parameters.
'''
model = Model(net, loss_fn=ls, optimizer=opt, metrics={'acc'})
# CheckPoint CallBack definition
config_ck = CheckpointConfig(save_checkpoint_steps=batch_num, keep_checkpoint_max=35)
ckpoint_cb = ModelCheckpoint(prefix="train_resnet_cifar10", directory="./", config=config_ck)
# LossMonitor is used to print loss value on screen
loss_cb = LossMonitor()
model.train(epoch_size, dataset, callbacks=[ckpoint_cb, loss_cb])
```
### Loading and Validating the Saved Model
The trained model file (such as resnet.ckpt) can be used to predict the class of a new image. Run the `load_checkpoint` command to load the model file. Then call the `eval` API of `Model` to predict the new image class.
```python
param_dict = load_checkpoint(args_opt.checkpoint_path)
load_param_into_net(net, param_dict)
eval_dataset = create_dataset(1, training=False)
res = model.eval(eval_dataset)
print("result: ", res)
```
## References
[1] https://www.cs.toronto.edu/~kriz/cifar.html
# Customized Debugging Information
<!-- TOC -->
- [Customized Debugging Information](#customized-debugging-information)
- [Overview](#overview)
- [Introduction to Callback](#introduction-to-callback)
- [Callback Capabilities of MindSpore](#callback-capabilities-of-mindspore)
- [Custom Callback](#custom-callback)
- [MindSpore Metrics](#mindspore-metrics)
- [MindSpore Print Operator](#mindspore-print-operator)
- [Log-related Environment Variables and Configurations](#log-related-environment-variables-and-configurations)
<!-- /TOC -->
## Overview
This section describes how to use the customized capabilities provided by MindSpore, such as callback, metrics, and log printing, to help you quickly debug the training network.
## Introduction to Callback
Callback here is not a function but a class. You can use callback to observe the internal status and related information of the network during training or perform specific actions in a specific period.
For example, you can monitor the loss, save model parameters, dynamically adjust parameters, and terminate training tasks in advance.
### Callback Capabilities of MindSpore
MindSpore provides the callback capabilities to allow users to insert customized operations in a specific phase of training or inference, including:
- Callback functions such as ModelCheckpoint, LossMonitor, and SummaryStep provided by the MindSpore framework
- Custom callback functions
Usage: Transfer the callback object in the model.train method. The callback object can be a list, for example:
```python
ckpt_cb = ModelCheckpoint()
loss_cb = LossMonitor()
summary_cb = SummaryStep()
model.train(epoch, dataset, callbacks=[ckpt_cb, loss_cb, summary_cb])
```
ModelCheckpoint can save model parameters for retraining or inference.
LossMonitor can output loss information in logs for users to view. In addition, LossMonitor monitors the loss value change during training. When the loss value is `Nan` or `Inf`, the training terminates.
SummaryStep can save the training information to a file for later use.
### Custom Callback
You can customize callback based on the callback base class as required.
The callback base class is defined as follows:
```python
class Callback():
"""Callback base class"""
def begin(self, run_context):
"""Called once before the network executing."""
pass
def epoch_begin(self, run_context):
"""Called before each epoch beginning."""
pass
def epoch_end(self, run_context):
"""Called after each epoch finished."""
pass
def step_begin(self, run_context):
"""Called before each epoch beginning."""
pass
def step_end(self, run_context):
"""Called after each step finished."""
pass
def end(self, run_context):
"""Called once after network training."""
pass
```
The callback can record important information during training and transfer the information to the callback object through a dictionary variable `cb_params`,
You can obtain related attributes from each custom callback and perform customized operations. You can also customize other variables and transfer them to the `cb_params` object.
The main attributes of `cb_params` are as follows:
- loss_fn: Loss function
- optimizer: Optimizer
- train_dataset: Training dataset
- cur_epoch_num: Number of current epochs
- cur_step_num: Number of current steps
- batch_num: Number of steps in an epoch
- ...
You can inherit the callback base class to customize a callback object.
The following example describes how to use a custom callback function.
```python
class StopAtTime(Callback):
def __init__(self, run_time):
super(StopAtTime, self).__init__()
self.run_time = run_time*60
def begin(self, run_context):
cb_params = run_context.original_args()
cb_params.init_time = time.time()
def step_end(self, run_context):
cb_params = run_context.original_args()
epoch_num = cb_params.cur_epoch_num
step_num = cb_params.cur_step_num
loss = cb_params.cb_params
cur_time = time.time()
if (cur_time - cb_params.init_time) > self.run_time:
print("epoch: ", epoch_num, " step: ", step_num, " loss: ", loss)
run_context.request_stop()
stop_cb = StopAtTime(run_time=10)
model.train(100, dataset, callbacks=stop_cb)
```
The output is as follows:
```
epoch: 20 step: 32 loss: 2.298344373703003
```
This callback function is used to terminate the training within a specified period. You can use the `run_context.original_args()` method to obtain the `cb_params` dictionary, which contains the main attribute information described above.
In addition, you can modify and add values in the dictionary. In the preceding example, an `init_time` object is defined in `begin()` and transferred to the `cb_params` dictionary.
A decision is made at each `step_end`. When the training time is greater than the configured time threshold, a training termination signal will be sent to the `run_context` to terminate the training in advance and the current values of epoch, step, and loss will be printed.
## MindSpore Metrics
After the training is complete, you can use metrics to evaluate the training result.
MindSpore provides multiple metrics, such as `accuracy`, `loss`, `tolerance`, `recall`, and `F1`.
You can define a metrics dictionary object that contains multiple metrics and transfer them to the `model.eval` interface to verify the training precision.
```python
metrics = {
'accuracy': nn.Accuracy(),
'loss': nn.Loss(),
'precision': nn.Precision(),
'recall': nn.Recall(),
'f1_score': nn.F1()
}
net = ResNet()
loss = CrossEntropyLoss()
opt = Momentum()
model = Model(net, loss_fn=loss, optimizer=opt, metrics=metrics)
ds_eval = create_dataset()
output = model.eval(ds_eval)
```
The `model.eval()` method returns a dictionary that contains the metrics and results transferred to the metrics.
You can also define your own metrics class by inheriting the `Metric` base class and rewriting the `clear`, `update`, and `eval` methods.
The `accuracy` operator is used as an example to describe the internal implementation principle.
The `accuracy` inherits the `EvaluationBase` base class and rewrites the preceding three methods.
The `clear()` method initializes related calculation parameters in the class.
The `update()` method accepts the predicted value and tag value and updates the internal variables of accuracy.
The `eval()` method calculates related indicators and returns the calculation result.
By invoking the `eval` method of `accuracy`, you will obtain the calculation result.
You can understand how `accuracy` runs by using the following code:
```python
x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
y = Tensor(np.array([1, 0, 1]))
metric = Accuracy()
metric.clear()
metric.update(x, y)
accuracy = metric.eval()
print('Accuracy is ', accuracy)
```
The output is as follows:
```
Accuracy is 0.6667
```
## MindSpore Print Operator
MindSpore-developed print operator is used to print the tensors or character strings input by users. Multiple strings, multiple tensors, and a combination of tensors and strings are supported, which are separated by comma (,).
The use method of MindSpore print operator is the same that of other operators. You need to assert MindSpore print operator in `__init__`() and invoke using `construct()`. The following is an example.
```python
import numpy as np
from mindspore import Tensor
from mindspore.ops import operations as P
import mindspore.nn as nn
import mindspore.context as context
context.set_context(mode=context.GRAPH_MODE)
class PrintDemo(nn.Cell):
def __init__(self):
super(PrintDemo, self).__init__()
self.print = P.Print()
def construct(self, x, y):
self.print('print Tensor x and Tensor y:', x, y)
return x
x = Tensor(np.ones([2, 1]).astype(np.int32))
y = Tensor(np.ones([2, 2]).astype(np.int32))
net = PrintDemo()
output = net(x, y)
```
The output is as follows:
```
print Tensor x and Tensor y:
Tensor shape:[[const vector][2, 1]]Int32
val:[[1]
[1]]
Tensor shape:[[const vector][2, 2]]Int32
val:[[1 1]
[1 1]]
```
## Log-related Environment Variables and Configurations
MindSpore uses glog to output logs. The following environment variables are commonly used:
1. GLOG_v specifies the log level. The default value is 2, indicating the WARNING level. The values are as follows: 0: DEBUG; 1: INFO; 2: WARNING; 3: ERROR.
2. When GLOG_logtostderr is set to 1, logs are output to the screen. If the value is set to 0, logs are output to a file. Default value: 1
3. GLOG_log_dir=YourPath specifies the log output path. If GLOG_log_dir is specified and the value of GLOG_logtostderr is 1, logs are output to the screen but not to a file.
# Distributed Training
<!-- TOC -->
- [Distributed Training](#distributed-training)
- [Overview](#overview)
- [Preparations](#preparations)
- [Configuring Distributed Environment Variables](#configuring-distributed-environment-variables)
- [Invoking the Collective Communication Library](#invoking-the-collective-communication-library)
- [Loading Datasets](#loading-datasets)
- [Defining the Network](#defining-the-network)
- [Defining the Loss Function and Optimizer](#defining-the-loss-function-and-optimizer)
- [Defining the Loss Function](#defining-the-loss-function)
- [Defining the Optimizer](#defining-the-optimizer)
- [Training the Network](#training-the-network)
- [Running Test Cases](#running-test-cases)
<!-- /TOC -->
## Overview
MindSpore supports `DATA_PARALLEL` and `AUTO_PARALLEL`. Automatic parallel is a distributed parallel mode that integrates data parallel, model parallel, and hybrid parallel. It can automatically establish cost models and select a parallel mode for users.
Among them:
- Data parallel: A parallel mode for dividing data in batches.
- Layerwise parallel: A parallel mode for dividing parameters by channel.
- Hybrid parallel: A parallel mode that covers both data parallel and model parallel.
- Cost model: A cost model built based on the memory computing cost and communication cost, for which an efficient algorithm is designed to find the parallel strategy with the shorter training time.
In this tutorial, we will learn how to train the ResNet-50 network in `DATA_PARALLEL` or `AUTO_PARALLEL` mode on MindSpore.
For sample code, please see at
<https://gitee.com/mindspore/docs/blob/r0.1/tutorials/tutorial_code/distributed_training/resnet50_distributed_training.py>.
> The current sample is for the Ascend AI processor.
## Preparations
### Configuring Distributed Environment Variables
When distributed training is performed in the lab environment, you need to configure the networking information file for the current multi-card environment. If HUAWEI CLOUD is used, skip this section.
The Ascend 910 AI processor and 1980 AIServer are used as an example. The JSON configuration file of a two-card environment is as follows. In this example, the configuration file is named rank_table.json.
```json
{
"board_id": "0x0000",
"chip_info": "910",
"deploy_mode": "lab",
"group_count": "1",
"group_list": [
{
"device_num": "2",
"server_num": "1",
"group_name": "",
"instance_count": "2",
"instance_list": [
{"devices":[{"device_id":"0","device_ip":"192.1.27.6"}],"rank_id":"0","server_id":"10.155.111.140"},
{"devices":[{"device_id":"1","device_ip":"192.2.27.6"}],"rank_id":"1","server_id":"10.155.111.140"}
]
}
],
"para_plane_nic_location": "device",
"para_plane_nic_name": [
"eth0", "eth1"
],
"para_plane_nic_num": "2",
"status": "completed"
}
```
The following parameters need to be modified based on the actual training environment:
1. `server_num` indicates the number of hosts, and `server_id` indicates the IP address of the local host.
2. `device_num`, `para_plane_nic_num`, and `instance_count` indicate the number of cards.
3. `rank_id` indicates the logical sequence number of a card, which starts from 0 fixedly. `device_id` indicates the physical sequence number of a card, that is, the actual sequence number of the host where the card is located.
4. `device_ip` indicates the IP address of the NIC. You can run the `cat /etc/hccn.conf` command on the current host to obtain the IP address of the NIC.
5. `para_plane_nic_name` indicates the name of the corresponding NIC.
After the networking information file is ready, add the file path to the environment variable `MINDSPORE_HCCL_CONFIG_PATH`. In addition, the `device_id` information needs to be transferred to the script. In this example, the information is transferred by configuring the environment variable DEVICE_ID.
```bash
export MINDSPORE_HCCL_CONFIG_PATH="./rank_table.json"
export DEVICE_ID=0
```
### Invoking the Collective Communication Library
You need to enable the distributed API `enable_hccl` in the `context.set_context()` API, set the `device_id` parameter, and invoke `init()` to complete the initialization operation.
In the sample, the graph mode is used during runtime. On the Ascend AI processor, Huawei Collective Communication Library (HCCL) is used.
```python
import os
from mindspore import context
from mindspore.communication.management import init
if __name__ == "__main__":
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", enable_hccl=True, device_id=int(os.environ["DEVICE_ID"]))
init()
...
```
`mindspore.communication.management` encapsulates the collective communication API provided by the HCCL to help users obtain distributed information. The common types include `get_rank` and `get_group_size`, which correspond to the ID of the current card in the cluster and the number of cards, respectively.
> HCCL implements multi-device multi-card communication based on the Da Vinci architecture chip. The restrictions on using the distributed service are as follows:
> 1. In a single-node system, a cluster of 1, 2, 4, or 8 cards is supported. In a multi-node system, a cluster of 8 x N cards is supported.
> 2. Each server has four NICs (numbered 0 to 3) and four NICs (numbered 4 to 7) deployed on two different networks. During training of two or four cards, the NICs must be connected and clusters cannot be created across networks.
> 3. The operating system needs to use the symmetric multiprocessing (SMP) mode.
## Loading Datasets
During distributed training, data is imported in data parallel mode. The following uses Cifar10Dataset as an example to describe how to import the CIFAR-10 data set in parallel mode, `data_path` is the path of the dataset.
Different from a single-node system, the multi-node system needs to transfer `num_shards` and `shard_id` parameters to the dataset API, which correspond to the number of cards and logical sequence number of the NIC, respectively. You are advised to obtain the parameters through the HCCL API.
```python
import mindspore.common.dtype as mstype
import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.transforms.vision.c_transforms as vision
from mindspore.communication.management import get_rank, get_group_size
def create_dataset(repeat_num=1, batch_size=32, rank_id=0, rank_size=1):
resize_height = 224
resize_width = 224
rescale = 1.0 / 255.0
shift = 0.0
# get rank_id and rank_size
rank_id = get_rank()
rank_size = get_group_size()
data_set = ds.Cifar10Dataset(data_path, num_shards=rank_size, shard_id=rank_id)
# define map operations
random_crop_op = vision.RandomCrop((32, 32), (4, 4, 4, 4))
random_horizontal_op = vision.RandomHorizontalFlip()
resize_op = vision.Resize((resize_height, resize_width))
rescale_op = vision.Rescale(rescale, shift)
normalize_op = vision.Normalize((0.4465, 0.4822, 0.4914), (0.2010, 0.1994, 0.2023))
changeswap_op = vision.HWC2CHW()
type_cast_op = C.TypeCast(mstype.int32)
c_trans = [random_crop_op, random_horizontal_op]
c_trans += [resize_op, rescale_op, normalize_op, changeswap_op]
# apply map operations on images
data_set = data_set.map(input_columns="label", operations=type_cast_op)
data_set = data_set.map(input_columns="image", operations=c_trans)
# apply repeat operations
data_set = data_set.repeat(repeat_num)
# apply shuffle operations
data_set = data_set.shuffle(buffer_size=10)
# apply batch operations
data_set = data_set.batch(batch_size=batch_size, drop_remainder=True)
return data_set
```
## Defining the Network
In `DATA_PARALLEL` and `AUTO_PARALLEL` modes, the network definition mode is the same as that of a single-node system. For sample code, see at
<https://gitee.com/mindspore/docs/blob/r0.1/tutorials/tutorial_code/resnet/resnet.py>.
## Defining the Loss Function and Optimizer
### Defining the Loss Function
In the Loss function, the SoftmaxCrossEntropyWithLogits is expanded into multiple small operators for implementation according to a mathematical formula.
Compared with fusion loss, the loss in `AUTO_PARALLEL` mode searches and finds optimal parallel strategy by operator according to an algorithm.
```python
from mindspore.ops import operations as P
from mindspore import Tensor
import mindspore.ops.functional as F
import mindspore.common.dtype as mstype
import mindspore.nn as nn
class SoftmaxCrossEntropyExpand(nn.Cell):
def __init__(self, sparse=False):
super(SoftmaxCrossEntropyExpand, self).__init__()
self.exp = P.Exp()
self.sum = P.ReduceSum(keep_dims=True)
self.onehot = P.OneHot()
self.on_value = Tensor(1.0, mstype.float32)
self.off_value = Tensor(0.0, mstype.float32)
self.div = P.Div()
self.log = P.Log()
self.sum_cross_entropy = P.ReduceSum(keep_dims=False)
self.mul = P.Mul()
self.mul2 = P.Mul()
self.mean = P.ReduceMean(keep_dims=False)
self.sparse = sparse
self.max = P.ReduceMax(keep_dims=True)
self.sub = P.Sub()
def construct(self, logit, label):
logit_max = self.max(logit, -1)
exp = self.exp(self.sub(logit, logit_max))
exp_sum = self.sum(exp, -1)
softmax_result = self.div(exp, exp_sum)
if self.sparse:
label = self.onehot(label, F.shape(logit)[1], self.on_value, self.off_value)
softmax_result_log = self.log(softmax_result)
loss = self.sum_cross_entropy((self.mul(softmax_result_log, label)), -1)
loss = self.mul2(F.scalar_to_array(-1.0), loss)
loss = self.mean(loss, -1)
return loss
```
### Defining the Optimizer
The `Momentum` optimizer is used as the parameter update tool. The definition is the same as that of a single-node system.
```python
from mindspore.nn.optim.momentum import Momentum
lr = 0.01
momentum = 0.9
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr, momentum)
```
## Training the Network
`context.set_auto_parallel_context()` is an API provided for users to set parallel parameters. The parameters are as follows:
- `parallel_mode`: distributed parallel mode. The options are `ParallelMode.DATA_PARALLEL` and `ParallelMode.AUTO_PARALLEL`.
- `mirror_mean`: During backward computation, the framework collects gradients of parameters in data parallel mode across multiple machines, obtains the global gradient value, and transfers the global gradient value to the optimizer for update.
The value True indicates the `allreduce_mean` operation that would be applied, and the value False indicates the `allreduce_sum` operation that would be applied.
In the following example, the parallel mode is set to `AUTO_PARALLEL`. `dataset_sink_mode=False` indicates that the non-sink mode is used. `LossMonitor` can return the loss value through the callback function.
```python
from mindspore.nn.optim.momentum import Momentum
from mindspore.train.callback import LossMonitor
from mindspore.train.model import Model, ParallelMode
from resnet import resnet50
def test_train_cifar(num_classes=10, epoch_size=10):
context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL, mirror_mean=True)
loss_cb = LossMonitor()
dataset = create_dataset(epoch_size)
net = resnet50(32, num_classes)
loss = SoftmaxCrossEntropyExpand(sparse=True)
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9)
model = Model(net, loss_fn=loss, optimizer=opt)
model.train(epoch_size, dataset, callbacks=[loss_cb], dataset_sink_mode=False)
```
## Running Test Cases
Currently, MindSpore distributed execution uses the single-card single-process running mode. The number of processes must be the same as the number of used cards. Each single-process will create a folder to save log and building information. The following is an example of a running script for two-card distributed training:
```bash
#!/bin/bash
export MINDSPORE_HCCL_CONFIG_PATH=./rank_table.json
export RANK_SIZE=2
for((i=0;i<$RANK_SIZE;i++))
do
mkdir device$i
cp ./resnet50_distributed_training.py ./device$i
cd ./device$i
export RANK_ID=$i
export DEVICE_ID=$i
echo "start training for device $i"
env > env$i.log
pytest -s -v ./resnet50_distributed_training.py > log$i 2>&1 &
cd ../
done
```
# Mixed Precision
<!-- TOC -->
- [Mixed Precision](#mixed-precision)
- [Overview](#overview)
- [Computation Process](#computation-process)
- [Automatic Mixed Precision](#automatic-mixed-precision)
- [Manual Mixed Precision](#manual-mixed-precision)
<!-- /TOC -->
## Overview
The mixed precision training method accelerates the deep learning neural network training process by using both the single-precision and half-precision data formats, and maintains the network precision achieved by the single-precision training at the same time.
Mixed precision training can accelerate the computation process, reduce memory usage, and enable a larger model or batch size to be trained on specific hardware.
## Computation Process
The following figure shows the typical computation process of mixed precision in MindSpore.
![mix precision](./images/mix_precision.jpg)
1. Parameters are stored in FP32 format.
2. During the forward computation, if an FP16 operator is involved, the operator input and parameters need to be cast from FP32 to FP16.
3. The loss layer is set to FP32.
4. During backward computation, the value is multiplied by Loss Scale to avoid underflow due to a small gradient.
5. The FP16 parameter is used for gradient computation, and the result is cast back to FP32.
6. Then, the value is divided by Loss scale to restore the multiplied gradient.
7. The optimizer checks whether the gradient overflows. If yes, the optimizer skips the update. If no, the optimizer uses FP32 to update the original parameters.
This document describes the computation process by using examples of automatic and manual mixed precision.
## Automatic Mixed Precision
To use the automatic mixed precision, you need to invoke the corresponding API, which takes the network to be trained and the optimizer as the input. This API converts the operators of the entire network into FP16 operators (except the BatchNorm and Loss operators).
In addition, after the mixed precision is employed, the loss scale must be used to avoid data overflow.
The procedure is as follows:
1. Introduce the MindSpore mixed precision API.
2. Define the network. This step is the same as the common network definition. (You do not need to manually configure the precision of any specific operator.)
3. Use the amp.build_train_network() API to encapsulate the network model and optimizer. In this step, MindSpore automatically converts the operators to the required format.
A code example is as follows:
```python
# The interface of Auto_mixed precision
from mindspore.train import amp
# Define network
class LeNet5(nn.Cell):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
self.fc1 = nn.Dense(16 * 5 * 5, 120)
self.fc2 = nn.Dense(120, 84)
self.fc3 = nn.Dense(84, 10)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2)
self.flatten = P.Flatten()
def construct(self, x):
x = self.max_pool2d(self.relu(self.conv1(x)))
x = self.max_pool2d(self.relu(self.conv2(x)))
x = self.flatten(x)
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
# Initialize network
net = LeNet5()
# Define training data, label and sens
predict = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32) * 0.01)
label = Tensor(np.zeros([1, 10]).astype(np.float32))
scaling_sens = Tensor(np.full((1), 1.0), dtype=mstype.float32)
# Define Loss and Optimizer
net.set_train()
loss = MSELoss()
optimizer = Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
net_with_loss = WithLossCell(net, loss)
train_network = amp.build_train_network(net_with_loss, optimizer, level="O2")
# Run training
output = train_network(inputs, label, scaling_sens)
```
## Manual Mixed Precision
MindSpore also supports manual mixed precision. It is assumed that only one dense layer in the network needs to be calculated by using FP32, and other layers are calculated by using FP16. The mixed precision is configured in the granularity of cell. The default format of a cell is FP32.
The following is the procedure for implementing manual mixed precision:
1. Define the network. This step is similar to step 2 in the automatic mixed precision. NoteThe fc3 operator in LeNet needs to be manually set to FP32.
2. Configure the mixed precision. Use net.add_flags_recursive(fp16=True) to set all operators of the cell and its sub-cells to FP16.
3. Use TrainOneStepWithLossScaleCell to encapsulate the network model and optimizer.
A code example is as follows:
```python
# Define network
class LeNet5(nn.Cell):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
self.fc1 = nn.Dense(16 * 5 * 5, 120)
self.fc2 = nn.Dense(120, 84)
self.fc3 = nn.Dense(84, 10).add_flags_recursive(fp32=True)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2)
self.flatten = P.Flatten()
def construct(self, x):
x = self.max_pool2d(self.relu(self.conv1(x)))
x = self.max_pool2d(self.relu(self.conv2(x)))
x = self.flatten(x)
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
# Initialize network and set mixing precision
net = LeNet5()
net.add_flags_recursive(fp16=True)
# Define training data, label and sens
predict = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32) * 0.01)
label = Tensor(np.zeros([1, 10]).astype(np.float32))
scaling_sens = Tensor(np.full((1), 1.0), dtype=mstype.float32)
# Define Loss and Optimizer
net.set_train()
loss = MSELoss()
optimizer = Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
net_with_loss = WithLossCell(net, loss)
train_network = TrainOneStepWithLossScaleCell(net_with_loss, optimizer)
# Run training
output = train_network(inputs, label, scaling_sens)
```
# Model Security
<!-- TOC -->
- [Model Security](#model-security)
- [Overview](#overview)
- [Creating an Target Model](#creating-an-target-model)
- [Importing Related Packages](#importing-related-packages)
- [Loading the Dataset](#loading-the-dataset)
- [Creating the Model](#creating-the-model)
- [Adversarial Attack](#adversarial-attack)
- [Adversarial Defense](#adversarial-defense)
- [Defense Implementation](#defense-implementation)
- [Defense Effect](#defense-effect)
<!-- /TOC -->
## Overview
This tutorial describes the model security protection methods provided by MindArmour, helping you quickly use MindArmour and provide certain security protection capabilities for your AI model.
At the beginning of AI algorithm design, related security threats are sometimes not considered. As a result, the developed AI model may easily be affected by malicious attackers, leading to inaccurate judgment of the AI system. An attacker adds small perturbations that are not easily perceived by human to the original sample, causing deep learning model misjudgment. This is called an adversarial example attack. MindArmour model security toolkit provides functions such as adversarial example generation, adversarial example detection, model defense, and attack/defense effect evaluation, providing important support for AI model security research and AI application security.
- The adversarial example generation module enables security engineers to quickly and efficiently generate adversarial examples for attacking AI models.
- The adversarial example detection and defense modules allow users to detect and filter adversarial examples and enhance the robustness of AI models to adversarial examples.
- The evaluation module provides multiple metrics to comprehensively evaluate the attack and defense performance of adversarial examples.
This section describes how to use MindArmour in adversarial attack and defense by taking the Fast Gradient Sign Method (FGSM) attack algorithm and Natural Adversarial Defense (NAD) algorithm as examples.
> You can find the complete executable sample code at
> <https://gitee.com/mindspore/docs/tree/r0.1/tutorials/tutorial_code/model_safety/mnist_attack_fgsm.py>
> and <https://gitee.com/mindspore/docs/tree/r0.1/tutorials/tutorial_code/model_safety/mnist_defense_nad.py>
## Creating an Target Model
The MNIST dataset is used as an example to describe how to customize a simple model as the target model.
### Importing Related Packages
```python
import sys
import time
import numpy as np
from scipy.special import softmax
from mindspore import dataset as ds
import mindspore.common.dtype as mstype
import mindspore.dataset.transforms.vision.c_transforms as CV
import mindspore.dataset.transforms.c_transforms as C
from mindspore.dataset.transforms.vision import Inter
import mindspore.nn as nn
import mindspore.ops.operations as P
from mindspore.common.initializer import TruncatedNormal
from mindspore import Model
from mindspore import Tensor
from mindspore import context
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindarmour.attacks.gradient_method import FastGradientSignMethod
from mindarmour.utils.logger import LogUtil
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
LOGGER = LogUtil.get_instance()
TAG = 'demo'
```
### Loading the Dataset
Use the MnistDataset API provided by the MindSpore dataset to load the MNIST dataset.
```python
# generate training data
def generate_mnist_dataset(data_path, batch_size=32, repeat_size=1,
num_parallel_workers=1, sparse=True):
"""
create dataset for training or testing
"""
# define dataset
ds1 = ds.MnistDataset(data_path)
# define operation parameters
resize_height, resize_width = 32, 32
rescale = 1.0 / 255.0
shift = 0.0
# define map operations
resize_op = CV.Resize((resize_height, resize_width),
interpolation=Inter.LINEAR)
rescale_op = CV.Rescale(rescale, shift)
hwc2chw_op = CV.HWC2CHW()
type_cast_op = C.TypeCast(mstype.int32)
one_hot_enco = C.OneHot(10)
# apply map operations on images
if not sparse:
ds1 = ds1.map(input_columns="label", operations=one_hot_enco,
num_parallel_workers=num_parallel_workers)
type_cast_op = C.TypeCast(mstype.float32)
ds1 = ds1.map(input_columns="label", operations=type_cast_op,
num_parallel_workers=num_parallel_workers)
ds1 = ds1.map(input_columns="image", operations=resize_op,
num_parallel_workers=num_parallel_workers)
ds1 = ds1.map(input_columns="image", operations=rescale_op,
num_parallel_workers=num_parallel_workers)
ds1 = ds1.map(input_columns="image", operations=hwc2chw_op,
num_parallel_workers=num_parallel_workers)
# apply DatasetOps
buffer_size = 10000
ds1 = ds1.shuffle(buffer_size=buffer_size)
ds1 = ds1.batch(batch_size, drop_remainder=True)
ds1 = ds1.repeat(repeat_size)
return ds1
```
### Creating the Model
The LeNet model is used as an example. You can also create and train your own model.
1. Define the LeNet model network.
```python
def conv(in_channels, out_channels, kernel_size, stride=1, padding=0):
weight = weight_variable()
return nn.Conv2d(in_channels, out_channels,
kernel_size=kernel_size, stride=stride, padding=padding,
weight_init=weight, has_bias=False, pad_mode="valid")
def fc_with_initialize(input_channels, out_channels):
weight = weight_variable()
bias = weight_variable()
return nn.Dense(input_channels, out_channels, weight, bias)
def weight_variable():
return TruncatedNormal(0.2)
class LeNet5(nn.Cell):
"""
Lenet network
"""
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = conv(1, 6, 5)
self.conv2 = conv(6, 16, 5)
self.fc1 = fc_with_initialize(16*5*5, 120)
self.fc2 = fc_with_initialize(120, 84)
self.fc3 = fc_with_initialize(84, 10)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
self.reshape = P.Reshape()
def construct(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.conv2(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.reshape(x, (-1, 16*5*5))
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
return x
```
2. Load the pre-trained LeNet model. You can also train and save your own MNIST model. For details, see Quick Start. Use the defined data loading function `generate_mnist_dataset` to load data.
```python
ckpt_name = './trained_ckpt_file/checkpoint_lenet-10_1875.ckpt'
net = LeNet5()
load_dict = load_checkpoint(ckpt_name)
load_param_into_net(net, load_dict)
# get test data
data_list = "./MNIST_unzip/test"
batch_size = 32
dataset = generate_mnist_dataset(data_list, batch_size, sparse=False)
```
3. Test the model.
```python
# prediction accuracy before attack
model = Model(net)
batch_num = 3 # the number of batches of attacking samples
test_images = []
test_labels = []
predict_labels = []
i = 0
for data in dataset.create_tuple_iterator():
i += 1
images = data[0].astype(np.float32)
labels = data[1]
test_images.append(images)
test_labels.append(labels)
pred_labels = np.argmax(model.predict(Tensor(images)).asnumpy(),
axis=1)
predict_labels.append(pred_labels)
if i >= batch_num:
break
predict_labels = np.concatenate(predict_labels)
true_labels = np.argmax(np.concatenate(test_labels), axis=1)
accuracy = np.mean(np.equal(predict_labels, true_labels))
LOGGER.info(TAG, "prediction accuracy before attacking is : %s", accuracy)
```
The classification accuracy reaches 98%.
```python
prediction accuracy before attacking is : 0.9895833333333334
```
## Adversarial Attack
Call the FGSM API provided by MindArmour.
```python
# attacking
attack = FastGradientSignMethod(net, eps=0.3)
start_time = time.clock()
adv_data = attack.batch_generate(np.concatenate(test_images),
np.concatenate(test_labels), batch_size=32)
stop_time = time.clock()
np.save('./adv_data', adv_data)
pred_logits_adv = model.predict(Tensor(adv_data)).asnumpy()
# rescale predict confidences into (0, 1).
pred_logits_adv = softmax(pred_logits_adv, axis=1)
pred_labels_adv = np.argmax(pred_logits_adv, axis=1)
accuracy_adv = np.mean(np.equal(pred_labels_adv, true_labels))
LOGGER.info(TAG, "prediction accuracy after attacking is : %s", accuracy_adv)
attack_evaluate = AttackEvaluate(np.concatenate(test_images).transpose(0, 2, 3, 1),
np.concatenate(test_labels),
adv_data.transpose(0, 2, 3, 1),
pred_logits_adv)
LOGGER.info(TAG, 'mis-classification rate of adversaries is : %s',
attack_evaluate.mis_classification_rate())
LOGGER.info(TAG, 'The average confidence of adversarial class is : %s',
attack_evaluate.avg_conf_adv_class())
LOGGER.info(TAG, 'The average confidence of true class is : %s',
attack_evaluate.avg_conf_true_class())
LOGGER.info(TAG, 'The average distance (l0, l2, linf) between original '
'samples and adversarial samples are: %s',
attack_evaluate.avg_lp_distance())
LOGGER.info(TAG, 'The average structural similarity between original '
'samples and adversarial samples are: %s',
attack_evaluate.avg_ssim())
LOGGER.info(TAG, 'The average costing time is %s',
(stop_time - start_time)/(batch_num*batch_size))
```
The attack results are as follows:
```python
prediction accuracy after attacking is : 0.052083
mis-classification rate of adversaries is : 0.947917
The average confidence of adversarial class is : 0.419824
The average confidence of true class is : 0.070650
The average distance (l0, l2, linf) between original samples and adversarial samples are: (1.698870, 0.465888, 0.300000)
The average structural similarity between original samples and adversarial samples are: 0.332538
The average costing time is 0.003125
```
After the untargeted FGSM attack is performed on the model, the accuracy of model decreases from 98.9% to 5.2% on adversarial examples, while the misclassification ratio reaches 95%, and the Average Confidence of Adversarial Class (ACAC) is 0.419824, the Average Confidence of True Class (ACTC) is 0.070650. The zero-norm distance, two-norm distance, and infinity-norm distance between the generated adversarial examples and the original benign examples are provided. The average structural similarity between each adversarial example and the original example is 0.332538. It takes 0.003125s to generate an adversarial example on average.
The following figure shows the effect before and after the attack. The left part is the original example, and the right part is the adversarial example generated after the untargeted FGSM attack. From a visual point of view, there is little difference between the right images and the left images, but all images on the right successfully mislead the model into misclassifying the sample as another incorrect categories.
![adv_attack_result](./images/adv_attack_result.png)
## Adversarial Defense
Natural Adversarial Defense (NAD) is a simple and effective adversarial example defense method, via adversarial training. It constructs adversarial examples during model training and mixes the adversarial examples with original examples to train the model. As the number of training iteration increases, the robustness of the model against adversarial examples improves. The NAD algorithm uses FGSM as the attack algorithm to construct adversarial examples.
### Defense Implementation
Call the NAD API provided by MindArmour.
```python
from mindspore.nn import SoftmaxCrossEntropyWithLogits
from mindarmour.defenses import NaturalAdversarialDefense
loss = SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=False)
opt = nn.Momentum(net.trainable_params(), 0.01, 0.09)
nad = NaturalAdversarialDefense(net, loss_fn=loss, optimizer=opt,
bounds=(0.0, 1.0), eps=0.3)
net.set_train()
nad.batch_defense(np.concatenate(test_images), np.concatenate(test_labels),
batch_size=32, epochs=20)
# get accuracy of test data on defensed model
net.set_train(False)
acc_list = []
pred_logits_adv = []
for i in range(batch_num):
batch_inputs = test_images[i]
batch_labels = test_labels[i]
logits = net(Tensor(batch_inputs)).asnumpy()
pred_logits_adv.append(logits)
label_pred = np.argmax(logits, axis=1)
acc_list.append(np.mean(np.argmax(batch_labels, axis=1) == label_pred))
pred_logits_adv = np.concatenate(pred_logits_adv)
pred_logits_adv = softmax(pred_logits_adv, axis=1)
LOGGER.info(TAG, 'accuracy of TEST data on defensed model is : %s',
np.mean(acc_list))
acc_list = []
for i in range(batch_num):
batch_inputs = adv_data[i * batch_size: (i + 1) * batch_size]
batch_labels = test_labels[i]
logits = net(Tensor(batch_inputs)).asnumpy()
label_pred = np.argmax(logits, axis=1)
acc_list.append(np.mean(np.argmax(batch_labels, axis=1) == label_pred))
attack_evaluate = AttackEvaluate(np.concatenate(test_images),
np.concatenate(test_labels),
adv_data,
pred_logits_adv)
LOGGER.info(TAG, 'accuracy of adv data on defensed model is : %s',
np.mean(acc_list))
LOGGER.info(TAG, 'defense mis-classification rate of adversaries is : %s',
attack_evaluate.mis_classification_rate())
LOGGER.info(TAG, 'The average confidence of adversarial class is : %s',
attack_evaluate.avg_conf_adv_class())
LOGGER.info(TAG, 'The average confidence of true class is : %s',
attack_evaluate.avg_conf_true_class())
LOGGER.info(TAG, 'The average distance (l0, l2, linf) between original '
'samples and adversarial samples are: %s',
attack_evaluate.avg_lp_distance())
```
### Defense Effect
```python
accuracy of TEST data on defensed model is : 0.973958
accuracy of adv data on defensed model is : 0.521835
defense mis-classification rate of adversaries is : 0.026042
The average confidence of adversarial class is : 0.67979
The average confidence of true class is : 0.19144624
The average distance (l0, l2, linf) between original samples and adversarial samples are: (1.544365, 0.439001, 0.300000)
```
After NAD is used to defend against adversarial examples, the model's misclassification ratio of adversarial examples decreases from 95% to 48%, effectively defending against adversarial examples. In addition, the classification accuracy of the model for the original test dataset reaches 97%. The NAD function does not reduce the classification accuracy of the model.
# Natural Language Processing (NLP) Application
<!-- TOC -->
- [Natural Language Processing (NLP) Application](#natural-language-processing-nlp-application)
- [Overview](#overview)
- [Preparation and Design](#preparation-and-design)
- [Downloading the Dataset](#downloading-the-dataset)
- [Determining Evaluation Criteria](#determining-evaluation-criteria)
- [Determining the Network and Process](#determining-the-network-and-process)
- [Implementation](#implementation)
- [Importing Library Files](#importing-library-files)
- [Configuring Environment Information](#configuring-environment-information)
- [Preprocessing the Dataset](#preprocessing-the-dataset)
- [Defining the Network](#defining-the-network)
- [Defining the Optimizer and Loss Function](#defining-the-optimizer-and-loss-function)
- [Training and Saving the Model](#training-and-saving-the-model)
- [Validating the Model](#validating-the-model)
- [Experiment Result](#experiment-result)
- [Downloading Code](#downloading-code)
<!-- /TOC -->
## Overview
Sentiment classification is a subset of text classification in NLP, and is the most basic application of NLP. It is a process of analyzing and inferencing affective states and subjective information, that is, analyzing whether a person's sentiment is positive or negative.
> Generally, sentiments are classified into three categories: positive, negative, and neutral. In most cases, only positive and negative sentiments are used for training regardless of the neutral sentiments. The following dataset is a good example.
[20 Newsgroups](http://qwone.com/~jason/20Newsgroups/) is a typical reference dataset for traditional text classification. It is a collection of approximately 20,000 news documents partitioned across 20 different newsgroups.
Some of the newsgroups are very closely related to each other (such as comp.sys.ibm.pc.hardware and comp.sys.mac.hardware), while others are highly unrelated (such as misc.forsale and soc.religion.christian).
As far as the network itself is concerned, the network structure of text classification is roughly similar to that of sentiment classification. After mastering how to construct the sentiment classification network, it is easy to construct a similar network which can be used in a text classification task with some parameter adjustments.
In the service context, text classification is to analyze the objective content in the text discussion, but sentiment classification is to find a viewpoint from the text. For example, "Forrest Gump has a clear theme and smooth pacing, which is excellent." In the text classification, this sentence is classified into a "movie" theme, but in the sentiment classification, this movie review is used to explore whether the sentiment is positive or negative.
Compared with traditional text classification, sentiment classification is simpler and more practical. High-quality datasets can be collected on common shopping websites and movie websites to benefit the business domains. For example, based on the domain context, the system can automatically analyze opinions of specific types of customers on the current product, analyze sentiments by subject and user type, and recommend products based on the analysis result, improving the conversion rate and bringing more business benefits.
In special fields, some non-polar words also fully express a sentimental tendency of a user. For example, when an app is downloaded and used, "the app is stuck" and "the download speed is so slow" express users' negative sentiments. In the stock market, "bullish" and "bull market" express users' positive sentiments. Therefore, in essence, we hope that the model can be used to mine special expressions in the vertical field as polarity words for the sentiment classification system.
Vertical polarity word = General polarity word + Domain-specific polarity word
According to the text processing granularity, sentiment analysis can be divided into word, phrase, sentence, paragraph, and chapter levels. A sentiment analysis at paragraph level is used as an example. The input is a paragraph, and the output is information about whether the movie review is positive or negative.
## Preparation and Design
### Downloading the Dataset
The IMDb movie review dataset is used as experimental data.
> Dataset download address: <http://ai.stanford.edu/~amaas/data/sentiment/>
The following are cases of negative and positive reviews.
| Review | Label |
|---|---|
| "Quitting" may be as much about exiting a pre-ordained identity as about drug withdrawal. As a rural guy coming to Beijing, class and success must have struck this young artist face on as an appeal to separate from his roots and far surpass his peasant parents' acting success. Troubles arise, however, when the new man is too new, when it demands too big a departure from family, history, nature, and personal identity. The ensuing splits, and confusion between the imaginary and the real and the dissonance between the ordinary and the heroic are the stuff of a gut check on the one hand or a complete escape from self on the other. | Negative |
| This movie is amazing because the fact that the real people portray themselves and their real life experience and do such a good job it's like they're almost living the past over again. Jia Hongsheng plays himself an actor who quit everything except music and drugs struggling with depression and searching for the meaning of life while being angry at everyone especially the people who care for him most. | Positive |
Download the GloVe file and add the following line at the beginning of the file, which means that a total of 400,000 words are read, and each word is represented by a word vector of 300 latitudes.
```
400000 300
```
GloVe file download address: <http://nlp.stanford.edu/data/glove.6B.zip>
### Determining Evaluation Criteria
As a typical classification, the evaluation criteria of sentiment classification can be determined by referring to that of the common classification. For example, accuracy, precision, recall, and F_beta scores can be used as references.
Accuracy = Number of accurately classified samples/Total number of samples
Precision = True positives/(True positives + False positives)
Recall = True positives/(True positives + False negatives)
F1 score = (2 x Precision x Recall)/(Precision + Recall)
In the IMDb dataset, the number of positive and negative samples does not vary greatly. Accuracy can be used as the evaluation criterion of the classification system.
### Determining the Network and Process
Currently, MindSpore GPU supports the long short-term memory (LSTM) network for NLP.
1. Load the dataset in use and process data.
2. Use the LSTM network training data to generate a model.
Long short-term memory (LSTM) is an artificial recurrent neural network (RNN) architecture used for processing and predicting an important event with a long interval and delay in a time sequence. For details, refer to online documentation.
3. After the model is obtained, use the validation dataset to check the accuracy of model.
## Implementation
### Importing Library Files
The following are the required public modules and MindSpore modules and library files.
```python
import os
import shutil
import math
import argparse
import json
from itertools import chain
import numpy as np
from config import lstm_cfg as cfg
# Install gensim with 'pip install gensim'
import gensim
import mindspore.nn as nn
import mindspore.context as context
import mindspore.dataset as ds
from mindspore.ops import operations as P
from mindspore import Tensor
from mindspore.common.initializer import initializer
from mindspore.common.parameter import Parameter
from mindspore.mindrecord import FileWriter
from mindspore.train import Model
from mindspore.nn.metrics import Accuracy
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor
```
### Configuring Environment Information
1. The `parser` module is used to transfer necessary information for running, such as storage paths of the dataset and the GloVe file. In this way, the frequently changed configurations can be entered during code running, which is more flexible.
```python
parser = argparse.ArgumentParser(description='MindSpore LSTM Example')
parser.add_argument('--preprocess', type=str, default='false', choices=['true', 'false'],
help='Whether to perform data preprocessing')
parser.add_argument('--mode', type=str, default="train", choices=['train', 'test'],
help='implement phase, set to train or test')
# Download dataset from 'http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz' and extract to 'aclimdb_path'
parser.add_argument('--aclimdb_path', type=str, default="./aclImdb",
help='path where the dataset is store')
# Download GloVe from 'http://nlp.stanford.edu/data/glove.6B.zip' and extract to 'glove_path'
# Add a new line '400000 300' at the beginning of 'glove.6B.300d.txt' with '40000' for total words and '300' for vector length
parser.add_argument('--glove_path', type=str, default="./glove",
help='path where the GloVe is store')
# Specify the path to save preprocessed data
parser.add_argument('--preprocess_path', type=str, default="./preprocess",
help='path where the pre-process data is store')
# Specify the path to save the CheckPoint file
parser.add_argument('--ckpt_path', type=str, default="./ckpt", help='if mode is test, must provide\
path where the trained ckpt file')
args = parser.parse_args()
```
2. Before implementing code, configure necessary information, including the environment information, execution mode, backend information, and hardware information.
```python
context.set_context(
mode=context.GRAPH_MODE,
save_graphs=False,
device_target="GPU")
```
For details about the API configuration, see the `context.set_context`.
### Preprocessing the Dataset
1. Process the text dataset, including encoding, word segmentation, alignment, and processing the original GloVe data to adapt to the network structure.
```python
# Read data from the specified directory
def read_imdb(path, seg='train'):
""" read imdb dataset """
pos_or_neg = ['pos', 'neg']
data = []
for label in pos_or_neg:
files = os.listdir(os.path.join(path, seg, label))
for file in files:
with open(os.path.join(path, seg, label, file), 'r', encoding='utf8') as rf:
review = rf.read().replace('\n', '')
if label == 'pos':
data.append([review, 1])
elif label == 'neg':
data.append([review, 0])
return data
# Split records into words with spaces as separators
def tokenizer(text):
return [tok.lower() for tok in text.split(' ')]
# Encode words into vectors
def encode_samples(tokenized_samples, word_to_idx):
""" encode word to index """
features = []
for sample in tokenized_samples:
feature = []
for token in sample:
if token in word_to_idx:
feature.append(word_to_idx[token])
else:
feature.append(0)
features.append(feature)
return features
# Align the number of words in each record to 500 words
def pad_samples(features, maxlen=500, pad=0):
""" pad all features to the same length """
padded_features = []
for feature in features:
if len(feature) >= maxlen:
padded_feature = feature[:maxlen]
else:
padded_feature = feature
while len(padded_feature) < maxlen:
padded_feature.append(pad)
padded_features.append(padded_feature)
return padded_features
# Crop GloVe raw data into a table which contains about 25,000 word vectors
def collect_weight(glove_path, vocab, word_to_idx, embed_size):
""" collect weight """
vocab_size = len(vocab)
wvmodel = gensim.models.KeyedVectors.load_word2vec_format(os.path.join(glove_path, 'glove.6B.300d.txt'),
binary=False, encoding='utf-8')
weight_np = np.zeros((vocab_size+1, embed_size)).astype(np.float32)
idx_to_word = {i+1: word for i, word in enumerate(vocab)}
idx_to_word[0] = '<unk>'
for i in range(len(wvmodel.index2word)):
try:
index = word_to_idx[wvmodel.index2word[i]]
except KeyError:
continue
weight_np[index, :] = wvmodel.get_vector(
idx_to_word[word_to_idx[wvmodel.index2word[i]]])
return weight_np
def preprocess(aclimdb_path, glove_path, embed_size):
""" preprocess the train and test data """
train_data = read_imdb(aclimdb_path, 'train')
test_data = read_imdb(aclimdb_path, 'test')
train_tokenized = []
test_tokenized = []
for review, _ in train_data:
train_tokenized.append(tokenizer(review))
for review, _ in test_data:
test_tokenized.append(tokenizer(review))
vocab = set(chain(*train_tokenized))
vocab_size = len(vocab)
print("vocab_size: ", vocab_size)
word_to_idx = {word: i+1 for i, word in enumerate(vocab)}
word_to_idx['<unk>'] = 0
train_features = np.array(pad_samples(encode_samples(train_tokenized, word_to_idx))).astype(np.int32)
train_labels = np.array([score for _, score in train_data]).astype(np.int32)
test_features = np.array(pad_samples(encode_samples(test_tokenized, word_to_idx))).astype(np.int32)
test_labels = np.array([score for _, score in test_data]).astype(np.int32)
weight_np = collect_weight(glove_path, vocab, word_to_idx, embed_size)
return train_features, train_labels, test_features, test_labels, weight_np, vocab_size
```
2. Convert the dataset format to the `mindrecord` format for MindSpore to read.
```python
def get_imdb_data(labels_data, features_data):
data_list = []
for i, (label, feature) in enumerate(zip(labels_data, features_data)):
data_json = {"id": i,
"label": int(label),
"feature": feature.reshape(-1)}
data_list.append(data_json)
return data_list
# Convert the dataset to mindrecord dateset which is supported by MindSpore
def convert_to_mindrecord(embed_size, aclimdb_path, proprocess_path, glove_path):
""" convert imdb dataset to mindrecord """
num_shard = 4
train_features, train_labels, test_features, test_labels, weight_np, _ = \
preprocess(aclimdb_path, glove_path, embed_size)
np.savetxt(os.path.join(proprocess_path, 'weight.txt'), weight_np)
# write mindrecord
schema_json = {"id": {"type": "int32"},
"label": {"type": "int32"},
"feature": {"type": "int32", "shape": [-1]}}
writer = FileWriter(os.path.join(proprocess_path, 'aclImdb_train.mindrecord'), num_shard)
data = get_imdb_data(train_labels, train_features)
writer.add_schema(schema_json, "nlp_schema")
writer.add_index(["id", "label"])
writer.write_raw_data(data)
writer.commit()
writer = FileWriter(os.path.join(proprocess_path, 'aclImdb_test.mindrecord'), num_shard)
data = get_imdb_data(test_labels, test_features)
writer.add_schema(schema_json, "nlp_schema")
writer.add_index(["id", "label"])
writer.write_raw_data(data)
writer.commit()
print("============== Starting Data Pre-processing ==============")
shutil.rmtree(args.preprocess_path)
os.mkdir(args.preprocess_path)
convert_to_mindrecord(cfg.embed_size, args.aclimdb_path, args.preprocess_path, args.glove_path)
```
3. Create a dataset.
```python
def create_dataset(base_path, batch_size, num_epochs, is_train):
"""Create dataset for training."""
columns_list = ["feature", "label"]
num_consumer = 4
if is_train:
path = os.path.join(base_path, 'aclImdb_train.mindrecord0')
else:
path = os.path.join(base_path, 'aclImdb_test.mindrecord0')
dtrain = ds.MindDataset(path, columns_list, num_consumer)
dtrain = dtrain.shuffle(buffer_size=dtrain.get_dataset_size())
dtrain = dtrain.batch(batch_size, drop_remainder=True)
dtrain = dtrain.repeat(count=num_epochs)
return dtrain
ds_train = create_dataset(args.preprocess_path, cfg.batch_size, cfg.num_epochs, True)
```
### Defining the Network
1. Initialize network parameters and status.
```python
def init_lstm_weight(
input_size,
hidden_size,
num_layers,
bidirectional,
has_bias=True):
"""Initialize lstm weight."""
num_directions = 1
if bidirectional:
num_directions = 2
weight_size = 0
gate_size = 4 * hidden_size
for layer in range(num_layers):
for _ in range(num_directions):
input_layer_size = input_size if layer == 0 else hidden_size * num_directions
weight_size += gate_size * input_layer_size
weight_size += gate_size * hidden_size
if has_bias:
weight_size += 2 * gate_size
stdv = 1 / math.sqrt(hidden_size)
w_np = np.random.uniform(-stdv, stdv, (weight_size,
1, 1)).astype(np.float32)
w = Parameter(
initializer(
Tensor(w_np), [
weight_size, 1, 1]), name='weight')
return w
# Initialize short-term memory (h) and long-term memory (c) to 0
def lstm_default_state(batch_size, hidden_size, num_layers, bidirectional):
"""init default input."""
num_directions = 1
if bidirectional:
num_directions = 2
h = Tensor(
np.zeros((num_layers * num_directions, batch_size, hidden_size)).astype(np.float32))
c = Tensor(
np.zeros((num_layers * num_directions, batch_size, hidden_size)).astype(np.float32))
return h, c
```
2. Use the `cell` method to define the network structure.
```python
class SentimentNet(nn.Cell):
"""Sentiment network structure."""
def __init__(self,
vocab_size,
embed_size,
num_hiddens,
num_layers,
bidirectional,
num_classes,
weight,
batch_size):
super(SentimentNet, self).__init__()
# Mapp words to vectors
self.embedding = nn.Embedding(vocab_size,
embed_size,
embedding_table=weight)
self.embedding.embedding_table.requires_grad = False
self.trans = P.Transpose()
self.perm = (1, 0, 2)
self.encoder = nn.LSTM(input_size=embed_size,
hidden_size=num_hiddens,
num_layers=num_layers,
has_bias=True,
bidirectional=bidirectional,
dropout=0.0)
w_init = init_lstm_weight(
embed_size,
num_hiddens,
num_layers,
bidirectional)
self.encoder.weight = w_init
self.h, self.c = lstm_default_state(batch_size, num_hiddens, num_layers, bidirectional)
self.concat = P.Concat(1)
if bidirectional:
self.decoder = nn.Dense(num_hiddens * 4, num_classes)
else:
self.decoder = nn.Dense(num_hiddens * 2, num_classes)
def construct(self, inputs):
# input:(64,500,300)
embeddings = self.embedding(inputs)
embeddings = self.trans(embeddings, self.perm)
output, _ = self.encoder(embeddings, (self.h, self.c))
# states[i] size(64,200) -> encoding.size(64,400)
encoding = self.concat((output[0], output[1]))
outputs = self.decoder(encoding)
return outputs
embedding_table = np.loadtxt(os.path.join(args.preprocess_path, "weight.txt")).astype(np.float32)
network = SentimentNet(vocab_size=embedding_table.shape[0],
embed_size=cfg.embed_size,
num_hiddens=cfg.num_hiddens,
num_layers=cfg.num_layers,
bidirectional=cfg.bidirectional,
num_classes=cfg.num_classes,
weight=Tensor(embedding_table),
batch_size=cfg.batch_size)
```
### Defining the Optimizer and Loss Function
The sample code for defining the optimizer and loss function is as follows:
```python
loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
opt = nn.Momentum(network.trainable_params(), cfg.learning_rate, cfg.momentum)
loss_cb = LossMonitor()
```
### Training and Saving the Model
Load the corresponding dataset, configure the CheckPoint generation information, and train the model using the `model.train` API.
```python
model = Model(network, loss, opt, {'acc': Accuracy()})
print("============== Starting Training ==============")
ds_train = create_dataset(args.preprocess_path, cfg.batch_size, cfg.num_epochs, True)
config_ck = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_steps,
keep_checkpoint_max=cfg.keep_checkpoint_max)
ckpoint_cb = ModelCheckpoint(prefix="lstm", directory=args.ckpt_path, config=config_ck)
model.train(cfg.num_epochs, ds_train, callbacks=[ckpoint_cb, loss_cb])
```
### Validating the Model
Load the validation dataset and saved CheckPoint file, perform validation, and view the model quality.
```python
print("============== Starting Testing ==============")
ds_eval = create_dataset(args.preprocess_path, cfg.batch_size, 1, False)
param_dict = load_checkpoint(args.ckpt_path)
load_param_into_net(network, param_dict)
acc = model.eval(ds_eval)
print("============== Accuracy:{} ==============".format(acc))
```
## Experiment Result
After 10 epochs, the accuracy on the training set converges to about 85%, and the accuracy on the test set is about 86%.
**Training Execution**
1. Run the training code and view the running result.
```shell
$ python main.py --preprocess=true --mode=train --ckpt_path=./ckpt
```
As shown in the following output, the loss value decreases gradually with the training process and reaches about 0.249. That is, after 10 epochs of training, the accuracy of the current text analysis result is about 85%.
```shell
============== Starting Data Pre-processing ==============
vocab_size: 252192
============== Starting Training ==============
[INFO] ME(15831:140036161672960,MainProcess):2020-03-09-16:29:02.785.484 [mindspore/train/serialization.py:118] Execute save checkpoint process.
[INFO] ME(15831:140036161672960,MainProcess):2020-03-09-16:29:03.658.733 [mindspore/train/serialization.py:143] Save checkpoint process finish.
epoch: 1 step: 390 , loss is 0.6926409
...
[INFO] ME(15831:140036161672960,MainProcess):2020-03-09-16:32:18.598.405 [mindspore/train/serialization.py:118] Execute save checkpoint process.
[INFO] ME(15831:140036161672960,MainProcess):2020-03-09-16:32:19.561.926 [mindspore/train/serialization.py:143] Save checkpoint process finish.
epoch: 6 step: 390 , loss is 0.222701
...
epoch: 10 step: 390 , loss is 0.22616856
epoch: 10 step: 390 , loss is 0.24914627
```
2. Check the saved CheckPoint files.
CheckPoint files (model files) are saved during the training. You can view all saved files in the file path.
```shell
$ ls ckpt/
```
The output is as follows:
```shell
lstm-10_390.ckpt lstm-1_390.ckpt lstm-2_390.ckpt lstm-3_390.ckpt lstm-4_390.ckpt lstm-5_390.ckpt lstm-6_390.ckpt lstm-7_390.ckpt lstm-8_390.ckpt lstm-9_390.ckpt
```
**Model Validation**
Use the last saved CheckPoint file to load and validate the dataset.
```shell
$ python main.py --mode=test --ckpt_path=./ckpt/lstm-10_390.ckpt
```
As shown in the following output, the sentiment analysis accuracy of the text is about 86%, which is basically satisfactory.
```shell
RegisterOperatorCreator:OperatorCreators init
============== Starting Testing ==============
[INFO] ME(29963:140462460516096,MainProcess):2020-03-09-16:37:19.333.605 [mindspore/train/serialization.py:169] Execute load checkpoint process.
[INFO] ME(29963:140462460516096,MainProcess):2020-03-09-16:37:20.434.749 [mindspore/train/serialization.py:200] Load checkpoint process finish.
[INFO] ME(29963:140462460516096,MainProcess):2020-03-09-16:37:20.467.336 [mindspore/train/serialization.py:233] Execute parameter into net process.
[INFO] ME(29963:140462460516096,MainProcess):2020-03-09-16:37:20.467.649 [mindspore/train/serialization.py:268] Load parameter into net process finish.
============== Accuracy:{'acc': 0.8599358974358975} ==============
```
## Downloading Code
Complete and executable code download address: <https://gitee.com/mindspore/docs/tree/r0.1/tutorials/tutorial_code/lstm>
- main.py: code file, including code for data preprocessing, network definition, and model training.
- config.py: some configurations on the network, including the batch size and number of training epochs.
# On-Device Inference
<!-- TOC -->
- [On-Device Inference](#on-device-inference)
- [Overview](#overview)
- [Compilation Method](#compilation-method)
- [Use of On-Device Inference](#use-of-on-device-inference)
- [Generating an On-Device Model File](#generating-an-on-device-model-file)
- [Implementing On-Device Inference](#implementing-on-device-inference)
<!-- /TOC -->
## Overview
MindSpore Predict is a lightweight deep neural network inference engine that provides the inference function for models trained by MindSpore on the device side. This tutorial describes how to use and compile MindSpore Predict.
## Compilation Method
You need to compile the MindSpore Predict by yourself. This section describes how to perform cross compilation in the Ubuntu environment.
The environment requirements are as follows:
- Hardware requirements
- Memory: 1 GB or above
- Hard disk space: 10 GB or above
- System requirements
- System: Ubuntu = 16.04.02LTS (availability is checked)
- Kernel: 4.4.0-62-generic (availability is checked)
- Software dependencies
- [cmake](https://cmake.org/download/) >= 3.14.1
- [GCC](https://gcc.gnu.org/releases.html) >= 5.4
- [autoconf](http://ftp.gnu.org/gnu/autoconf/) 2.69
- [LLVM 8.0.0](http://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz)
- [Android_NDK r16b](https://dl.google.com/android/repository/android-ndk-r16b-linux-x86_64.zip)
- numpy >= 1.16
- decorator
- scipy
> numpy, decorator and scipy can be installed through pip. The reference command is as following.
```bash
pip3 install numpy==1.16 decorator scipy
```
The compilation procedure is as follows:
1. Configure environment variables.
```bash
export LLVM_PATH={$LLVM_PATH}/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-16.04/bin/llvm-config #Set the LLVM path.
export ANDROID_NDK={$NDK_PATH}/android-ndk-r16b #Set the NDK path.
```
2. Download source code from the code repository.
```bash
git clone https://gitee.com/mindspore/mindspore.git -b r0.1
```
3. Run the following command in the root directory of the source code to compile MindSpore Predict: -I indicates options for compiling MindSpore Predict and the parameter is the target platform architecture. Currently, only the Android arm64 platform is supported.
```bash
sh build.sh -I arm64
```
4. Obtain the compilation result.
Go to the predict/output directory of the source code to view the generated package. The package name is MSPredict-{Version number}-{Host platform}_{Device platform}.tar.gz, for example, MSPredict-0.1.0-linux_aarch64.tar.gz. The package contains the following directories:
- include: MindSpore Predict header file.
- lib: MindSpore Predict dynamic library.
## Use of On-Device Inference
When MindSpore is used to perform model inference in the APK project of an app, preprocessing input is required before model inference. For example, before an image is converted into the tensor format required by MindSpore inference, the image needs to be resized. After MindSpore completes model inference, postprocess the model inference result and sends the processed output to the app.
This section describes how to use MindSpore to perform model inference. The setup of an APK project and pre- and post-processing of model inference are not described here.
To perform on-device model inference using MindSpore, perform the following steps.
### Generating an On-Device Model File
1. After training is complete, load the generated checkpoint file to the defined network.
```python
param_dict = load_checkpoint(ckpoint_file_name=ckpt_file_path)
load_param_into_net(net, param_dict)
```
2. Call the `export` API to export the .ms model file on the device.
```python
export(net, input_data, file_name="./lenet.ms", file_format='LITE')
```
Take the LeNet network as an example. The generated on-device model file is `lenet.ms`. The complete sample code lenet.py is as follows:
```python
import os
import numpy as np
import mindspore.nn as nn
import mindspore.ops.operations as P
import mindspore.context as context
from mindspore.common.tensor import Tensor
from mindspore.train.serialization import export, load_checkpoint, load_param_into_net
class LeNet(nn.Cell):
def __init__(self):
super(LeNet, self).__init__()
self.relu = P.ReLU()
self.batch_size = 32
self.conv1 = nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=0, has_bias=False, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=0, has_bias=False, pad_mode='valid')
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.reshape = P.Reshape()
self.fc1 = nn.Dense(400, 120)
self.fc2 = nn.Dense(120, 84)
self.fc3 = nn.Dense(84, 10)
def construct(self, input_x):
output = self.conv1(input_x)
output = self.relu(output)
output = self.pool(output)
output = self.conv2(output)
output = self.relu(output)
output = self.pool(output)
output = self.reshape(output, (self.batch_size, -1))
output = self.fc1(output)
output = self.relu(output)
output = self.fc2(output)
output = self.relu(output)
output = self.fc3(output)
return output
if __name__ == '__main__':
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
seed = 0
np.random.seed(seed)
origin_data = np.random.uniform(low=0, high=255, size=(32, 1, 32, 32)).astype(np.float32)
origin_data.tofile("lenet.bin")
input_data = Tensor(origin_data)
net = LeNet()
ckpt_file_path = "path_to/lenet.ckpt"
is_ckpt_exist = os.path.exists(ckpt_file_path)
if is_ckpt_exist:
param_dict = load_checkpoint(ckpoint_file_name=ckpt_file_path)
load_param_into_net(net, param_dict)
export(net, input_data, file_name="./lenet.ms", file_format='LITE')
print("export model success.")
else:
print("checkpoint file does not exist.")
```
### Implementing On-Device Inference
Use the .ms model file and image data as input to create a session and implement inference on the device.
![](./images/side_infer_process.png)
Figure 1 On-device inference sequence diagram
1. Load the .ms model file to the memory buffer. The ReadFile function needs to be implemented by users.
```cpp
// read model file
std::string modelPath = "./models/lenet/lenet.ms";
size_t graphSize = 0;
/* ReadFile() here is a dummy function */
char *graphBuf = ReadFile(modelPath.c_str(), graphSize);
```
2. Call the CreateSession API to create a session. After the session is created, the model file in the memory buffer can be released.
```cpp
// create session
Context ctx;
std::shared_ptr<Session> session = CreateSession(graphBuf, graphSize, ctx);
free(graphBuf);
```
3. Read the input data for inference from the memory buffer and call the SetData() API to set the input data to input tensor.
```cpp
// load input buffer
size_t inputSize = 0;
std::string imagePath = "./data/input/lenet.bin";
char *inputBuf = ReadFile(imagePath.c_str(), inputSize);
//get input tensors
std::vector<Tensor *> inputs = session->GetInput();
//set input buffer
inputs[0]->SetData(inputBuf);
```
4. Call the Run() API in the session to perform inference.
```cpp
// session run
int ret = session->Run(inputs);
```
5. Call the GetAllOutput() API to obtain the output.
```cpp
// get output
std::map<std::string, std::vector<Tensor *>> outputs = session->GetAllOutput();
```
6. Call the Getdata() API to get the output data.
```cpp
// get output data
float *data = nullptr;
for (auto output : outputs) {
auto tensors = output.second;
for (auto tensor : tensors) {
data = (float *)(tensor->GetData());
}
}
```
7. Release input and output tensors after the inference is complete.
```cpp
// free inputs and outputs
for (auto &input : inputs) {
delete input;
}
inputs.clear();
for (auto &output : outputs) {
for (auto &outputTensor : output.second) {
delete outputTensor;
}
}
outputs.clear();
```
Select the LeNet network and set the inference input to lenet.bin. The complete sample code lenet.cpp is as follows:
> MindSpore Predict uses FlatBuffers to define models. The FlatBuffers header file is required for parsing models. Therefore, you need to configure the FlatBuffers header file.
>
> Method: Copy the flatbuffers folder in MindSpore root directory/third_party/flatbuffers/include to the directory at the same level as session.h.
```cpp
#include <string>
#include <vector>
#include "context.h"
#include "session.h"
#include "tensor.h"
#include "errorcode.h"
using namespace mindspore::predict;
int main() {
std::string modelPath = "./models/lenet/lenet.ms";
std::string imagePath = "./data/input/lenet.bin";
// read model file
size_t graphSize = 0;
/* ReadFile() here is a dummy function */
char *graphBuf = ReadFile(modelPath.c_str(), graphSize);
if (graphBuf == nullptr) {
return -1;
}
// create session
Context ctx;
auto session = CreateSession(graphBuf, graphSize, ctx);
if (session == nullptr) {
free(graphBuf);
return -1;
}
free(graphBuf);
// load input buf
size_t inputSize = 0;
char *inputBuf = ReadFile(imagePath.c_str(), inputSize);
if (inputBuf == nullptr) {
return -1;
}
auto inputs = session->GetInput();
inputs[0]->SetData(inputBuf);
// session run
auto ret = session->Run(inputs);
if (ret != RET_OK) {
printf("run failed, error: %d\n", ret);
for (auto &input : inputs) {
delete input;
}
return -1;
}
// get output
auto outputs = session->GetAllOutput();
// get output data
float *data = nullptr;
for (auto output : outputs) {
auto tensors = output.second;
for (auto tensor : tensors) {
data = (float *)(tensor->GetData());
//print the contents of the data
for (size_t i = 0; i < tensor->GetElementSize(); ++i) {
printf(" %f ", data[i]);
}
printf("\n");
}
}
// free inputs and outputs
for (auto &input : inputs) {
delete input;
}
inputs.clear();
for (auto &output : outputs) {
for (auto &outputTensor : output.second) {
delete outputTensor;
}
}
outputs.clear();
return 0;
}
```
# Training Process Visualization
<!-- TOC -->
- [Training Process Visualization](#training-process-visualization)
- [Overview](#overview)
- [Operation Process](#operation-process)
- [Preparing the Training Script](#preparing-the-training-script)
- [Basic Script Editing](#basic-script-editing)
- [Recording the Computational Graph After Operator Fusion](#recording-the-computational-graph-after-operator-fusion)
- [MindInsight Commands](#mindinsight-commands)
- [Visualization Components](#visualization-components)
- [Computational Graph Visualization](#computational-graph-visualization)
- [Scalar Visualization](#scalar-visualization)
- [Image Visualization](#image-visualization)
- [Model Lineage Visualization](#model-lineage-visualization)
- [Dataset Graph Visualization](#dataset-graph-visualization)
- [Dataset Lineage Visualization](#dataset-lineage-visualization)
<!-- /TOC -->
## Overview
Scalars, images, computational graphs, and model hyperparameters during training are recorded in files and can be viewed on the web page.
## Operation Process
- Prepare a training script, specify scalars, images, computational graphs, and model hyperparameters in the training script, record them in the summary log file, and run the training script.
- Start MindInsight and specify the summary log file directory using startup parameters. After MindInsight is started, access the visualization page based on the IP address and port number. The default access IP address is `http://127.0.0.1:8080`.
- During the training, when data is written into the summary log file, you can view the data on the web page.
## Preparing the Training Script
Currently, MindSpore uses the `Callback` mechanism to save scalars, images, computational graphs, and model hyperparameters to summary log files and display them on the web page.
Scalar and image data is recorded by using the `Summary` operator. A computational graph is saved to the summary log file by using `SummaryRecord` after network compilation is complete.
Model parameters are saved to the summary log file by using `TrainLineage` or `EvalLineage`.
### Basic Script Editing
Step 1: Call the `Summary` operator in the `construct` function of the derived class that inherits `nn.Cell` to collect image or scalar data.
For example, when a network is defined, image data is recorded in `construct` of the network. When the loss function is defined, the loss value is recorded in `construct` of the loss function.
Record the dynamic learning rate in `construct` of the optimizer when defining the optimizer.
The sample code is as follows:
```python
from mindspore import context, Tensor, nn
from mindspore.common import dtype as mstype
from mindspore.ops import operations as P
from mindspore.ops import functional as F
from mindspore.nn import Optimizer
class CrossEntropyLoss(nn.Cell):
"""Loss function definition."""
def __init__(self):
super(CrossEntropyLoss, self).__init__()
self.cross_entropy = P.SoftmaxCrossEntropyWithLogits()
self.mean = P.ReduceMean()
self.one_hot = P.OneHot()
self.on_value = Tensor(1.0, mstype.float32)
self.off_value = Tensor(0.0, mstype.float32)
# Init ScalarSummary
self.sm_scalar = P.ScalarSummary()
def construct(self, logits, label):
label = self.one_hot(label, F.shape(logits)[1], self.on_value, self.off_value)
loss = self.cross_entropy(logits, label)[0]
loss = self.mean(loss, (-1,))
# Record loss
self.sm_scalar("loss", loss)
return loss
class Net(nn.Cell):
"""Net definition."""
def __init__(self):
super(Net, self).__init__()
......
# Init ImageSummary
self.sm_image = P.ImageSummary()
def construct(self, data):
# Record image by Summary operator
self.sm_image("image", data)
......
return out
class MyOptimizer(Optimizer):
"""Optimizer definition."""
def __init__(self, learning_rate, ......):
......
# Initialize ScalarSummary
self.sm_scalar = P.ScalarSummary()
def construct(self, grads):
......
# Record learning rate here
self.sm_scalar("learning_rate", learning_rate)
......
```
Step 2: Use the `Callback` mechanism to add the required callback instance to specify the data to be recorded during training.
- `SummaryStep` specifies the step interval for recording summary data.
- `TrainLineage` records parameters related to model training.
- `EvalLineage` records parameters related to the model test.
The `network` parameter needs to be specified when `SummaryRecord` is called to record the computational graph. By default, the computational graph is not recorded.
The sample code is as follows:
```python
from mindinsight.lineagemgr import TrainLineage, EvalLineage
from mindspore import Model, nn, context
from mindspore.train.callback import SummaryStep
from mindspore.train.summary.summary_record import SummaryRecord
def test_summary():
# Init context env
context.set_context(mode=context.GRAPH_MODE, device_target='Ascend')
# Init hyperparameter
epoch = 2
# Init network and Model
net = Net()
loss_fn = CrossEntropyLoss()
optim = MyOptimizer(learning_rate=0.01)
model = Model(net, loss_fn=loss_fn, optimizer=optim, metrics=None)
# Init SummaryRecord and specify a folder for storing summary log files
# and specify the graph that needs to be recorded
summary_writer = SummaryRecord(log_dir='./summary', network=net)
summary_callback = SummaryStep(summary_writer, flush_step=10)
# Init TrainLineage to record the training information
train_callback = TrainLineage(summary_writer)
# Prepare mindrecord_dataset for training
train_ds = create_mindrecord_dataset_for_training()
model.train(epoch, train_ds, callbacks=[summary_callback, train_callback])
# Init EvalLineage to record the evaluation information
eval_callback = EvalLineage(summary_writer)
# Prepare mindrecord_dataset for testing
eval_ds = create_mindrecord_dataset_for_testing()
model.eval(eval_ds, callbacks=[eval_callback])
# Close summary
summary_writer.close()
```
### Recording the Computational Graph After Operator Fusion
After completing the script by referring to "Basic Writing", use the `save_graphs` option of `context` to record the computational graph after operator fusion.
`ms_output_after_hwopt.pb` is the computational graph after operator fusion.
## MindInsight Commands
### View the command help information.
```bash
mindinsight --help
```
### View the version information.
```bash
mindinsight --version
```
### Start the service.
```bash
mindinsight start [-h] [--config <CONFIG>] [--workspace <WORKSPACE>]
[--port <PORT>] [--reload-interval <RELOAD_INTERVAL>]
[--summary-base-dir <SUMMARY_BASE_DIR>]
```
Optional parameters as follows:
- `-h, --help` : Displays the help information about the startup command.
- `--config <CONFIG>` : Specifies the configuration file or module. CONFIG indicates the physical file path (file:/path/to/config.py), or a module path (python:path.to.config.module) that can be identified by Python.
- `--workspace <WORKSPACE>` : Specifies the working directory. The default value of WORKSPACE is $HOME/mindinsight.
- `--port <PORT>` : Specifies the port number of the web visualization service. The value ranges from 1 to 65535. The default value of PORT is 8080.
- `--reload-interval <RELOAD_INTERVAL>` : Specifies the interval (unit: second) for loading data. The value 0 indicates that data is loaded only once. The default value of RELOAD_INTERVAL is 3 seconds.
- `--summary-base-dir <SUMMARY_BASE_DIR>` : Specifies the root directory for loading training log data. MindInsight traverses the direct subdirectories in this directory and searches for log files whose names comply with the regular expression 'summary.\d+' or '\.pb$'. If a direct subdirectory contains log files, it is identified as the log file directory. If a root directory contains log files, it is identified as the log file directory. SUMMARY_BASE_DIR is the current directory path by default.
> When the service is started, the parameter values of the command line are saved as the environment variables of the process and start with `MINDINSIGHT_`, for example, `MINDINSIGHT_CONFIG`, `MINDINSIGHT_WORKSPACE`, and `MINDINSIGHT_PORT`.
### Stop the service.
```bash
mindinsight stop [-h] [--port PORT]
```
Optional parameters as follows:
- `-h, --help` : Displays the help information about the stop command.
- `--port <PORT>` : Specifies the port number of the web visualization service. The value ranges from 1 to 65535. The default value of PORT is 8080.
### View the service process information.
MindInsight provides user with web services. Run the following command to view the running web service process:
```bash
ps -ef | grep mindinsight
```
Run the following command to access the working directory WORKSPACE corresponding to the service process based on the service process ID:
```bash
lsof -p <PID> | grep access
```
Output with the working directory WORKSPACE as follows:
```bash
gunicorn <PID> <USER> <FD> <TYPE> <DEVICE> <SIZE/OFF> <NODE> <WORKSPACE>/log/gunicorn/access.log
```
## Visualization Components
### Computational Graph Visualization
Computational graph visualization is used to display the graph structure, data flow direction, and control flow direction of a computational graph.
![graph.png](./images/graph.png)
Figure 1: Computational graph display area
Figure 1 shows the network structure of a computational graph. As shown in the figure, select an operator in the right area of the display area. The operator has two inputs and three outputs (the solid line indicates the data flow direction of the operator).
![graph_sidebar.png](./images/graph_sidebar.png)
Figure 2 Computational graph function area
Figure 2 shows the function area of the computational graph, including:
* File selection box: View the computational graphs of different files.
* Search box: Enter a node name and press Enter to view the node.
* Thumbnail: Display the thumbnail of the entire network structure. When viewing an extra large image structure, you can view the currently browsed area.
* Node information: Display the basic information of the selected node, including the node name, properties, input node, and output node.
* Legend: Display the meaning of each icon in the computational graph.
### Scalar Visualization
Scalar visualization is used to display the change trend of scalars during training.
![scalar.png](./images/scalar.png)
Figure 3: Scalar trend chart
Figure 3 shows a change process of loss values during the neural network training. The horizontal coordinate indicates the training step, and the vertical coordinate indicates the loss value.
Buttons from left to right in the upper right corner of the figure are used to display the chart in full screen, switch the Y-axis scale, enable or disable the rectangle selection, roll back the chart step by step, and restore the chart.
- Full-screen Display: Display the scalar curve in full screen. Click the button again to restore it.
- Switch Y-axis Scale: Perform logarithmic conversion on the Y-axis coordinate.
- Enable/Disable Rectangle Selection: Draw a rectangle to select and zoom in a part of the chart. You can perform rectangle selection again on the zoomed-in chart.
- Step-by-step Rollback: Cancel operations step by step after continuously drawing rectangles to select and zooming in the same area.
- Restore Chart: Restore a chart to the original state.
![scalar_select.png](./images/scalar_select.png)
Figure 4: Scalar visualization function area
Figure 4 shows the scalar visualization function area, which allows you to view scalar information by selecting different tags, different dimensions of the horizontal axis, and smoothness.
- Tag: Select the required tags to view the corresponding scalar information.
- Horizontal Axis: Select any of Step, Relative Time, and Absolute Time as the horizontal axis of the scalar curve.
- Smoothness: Adjust the smoothness to smooth the scalar curve.
- Scalar Synthesis: Synthesize two scalar curves and display them in a chart to facilitate comparison between the two curves or view the synthesized chart.
![scalar_compound.png](./images/scalar_compound.png)
Figure 5: Scalar synthesis of Accuracy and Loss curves
Figure 5 shows the scalar synthesis of the Accuracy and Loss curves. The function area of scalar synthesis is similar to that of scalar visualization. Different from the scalar visualization function area, the scalar synthesis function allows you to select a maximum of two tags at a time to synthesize and display their curves.
### Image Visualization
Image visualization is used to display images specified by users.
![image.png](./images/image_vi.png)
Figure 6: Image visualization
Figure 6 shows how to view images of different steps by sliding the Step slider.
![image_function.png](./images/image_function.png)
Figure 7: Image visualization function area
Figure 7 shows the function area of image visualization. You can view image information by selecting different tags, brightness, and contrast.
- Tag: Select the required tags to view the corresponding image information.
- Brightness Adjustment: Adjust the brightness of all displayed images.
- Contrast Adjustment: Adjust the contrast of all displayed images.
### Model Lineage Visualization
Model lineage visualization is used to display the parameter information of all training models.
![image.png](./images/lineage_label.png)
Figure 8: Model parameter selection area
Figure 8 shows the model parameter selection area, which lists the model parameter tags that can be viewed. You can select required tags to view the corresponding model parameters.
![image.png](./images/lineage_model_chart.png)
Figure 9: Model lineage function area
Figure 9 shows the model lineage function area, which visualizes the model parameter information. You can select a specific area in the column to display the model information within the area.
![image.png](./images/lineage_model_table.png)
Figure 10: Model list
Figure 10 shows all model information. You can sort the model information in ascending or descending order by specified column.
### Dataset Graph Visualization
Dataset graph visualization is used to display data processing and augmentation information of a single model training.
![data_function.png](./images/data_function.png)
Figure 11: Dataset graph function area
Figure 11 shows the dataset graph function area which includes the following content:
* Legend: Display the meaning of each icon in the data lineage graph.
* Data Processing Pipeline: Display the data processing pipeline used for training. Select a single node in the graph to view details.
* Node Information: Display basic information about the selected node, including names and parameters of the data processing and augmentation operators.
### Dataset Lineage Visualization
Dataset lineage visualization is used to display data processing and augmentation information of all model trainings.
![data_label.png](./images/data_label.png)
Figure 12: Data processing and augmentation operator selection area
Figure 12 shows the data processing and augmentation operator selection area, which lists names of data processing and augmentation operators that can be viewed. You can select required tags to view related parameters.
![data_chart.png](./images/data_chart.png)
Figure 13: Dataset lineage function area
Figure 13 shows the dataset lineage function area, which visualizes the parameter information used for data processing and augmentation. You can select a specific area in the column to display the parameter information within the area.
![data_table.png](./images/data_table.png)
Figure 14: Dataset lineage list
Figure 14 shows the data processing and augmentation information of all model trainings.
\ No newline at end of file
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
# -- Project information -----------------------------------------------------
project = 'MindSpore'
copyright = '2020, MindSpore'
author = 'MindSpore'
# The full version, including alpha/beta/rc tags
release = '0.1.0-alpha'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'recommonmark',
'sphinx_markdown_tables',
]
source_suffix = {
'.rst': 'restructuredtext',
'.md': 'markdown',
}
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
pygments_style = 'sphinx'
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
.. MindSpore documentation master file, created by
sphinx-quickstart on Thu Mar 24 09:00:00 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
MindSpore Tutorials
===================
.. toctree::
:glob:
:maxdepth: 1
:caption: Quick Start
quick_start/quick_start
.. toctree::
:glob:
:maxdepth: 1
:caption: Use
use/data_preparation/data_preparation
use/saving_and_loading_model_parameters
use/debugging_in_pynative_mode
.. toctree::
:glob:
:maxdepth: 1
:caption: Advanced Use
advanced_use/visualization_tutorials
advanced_use/mixed_precision
advanced_use/distributed_training
advanced_use/computer_vision_application
advanced_use/nlp_application
advanced_use/customized_debugging_information
advanced_use/on_device_inference
advanced_use/model_security
advanced_use/community
.. toctree::
:glob:
:maxdepth: 1
:caption: Statement
statement/legal_statement
statement/privacy_policy
\ No newline at end of file
# Implementing an Image Classification Application
<!-- TOC -->
- [Implementing an Image Classification Application](#implementing-an-image-classification-application)
- [Overview](#overview)
- [Preparations](#preparations)
- [Downloading the Dataset](#downloading-the-dataset)
- [Importing Python Libraries and Modules](#importing-python-libraries-and-modules)
- [Configuring the Running Information](#configuring-the-running-information)
- [Processing Data](#processing-data)
- [Defining the Dataset and Data Operations](#defining-the-dataset-and-data-operations)
- [Defining the Network](#defining-the-network)
- [Defining the Loss Function and Optimizer](#defining-the-loss-function-and-optimizer)
- [Basic Concepts](#basic-concepts)
- [Defining the Loss Function](#defining-the-loss-function)
- [Defining the Optimizer](#defining-the-optimizer)
- [Training the Network](#training-the-network)
- [Saving the Configured Model](#saving-the-configured-model)
- [Configuring the Network Training](#configuring-the-network-training)
- [Running and Viewing the Result](#running-and-viewing-the-result)
- [Validating the Model](#validating-the-model)
<!-- /TOC -->
## Overview
This document uses a practice example to demonstrate the basic functions of MindSpore. For common users, it takes 20 to 30 minutes to complete the practice.
During the practice, a simple image classification function is implemented. The overall process is as follows:
1. Process the required dataset. The MNIST dataset is used in this example.
2. Define a network. The LeNet network is used in this example.
3. Define the loss function and optimizer.
4. Load dataset, perform training. After the training is complete, check the result and save the model file.
5. Load the saved model for inference.
6. Validate the model, load the test dataset and trained model, and validate the result accuracy.
> You can find the complete executable sample code at <https://gitee.com/mindspore/docs/blob/r0.1/tutorials/tutorial_code/lenet.py>.
This is a simple and basic application process. For other advanced and complex applications, extend this basic process as needed.
## Preparations
Before you start, check whether MindSpore has been correctly installed. If no, install MindSpore on your computer by visiting [MindSpore installation page](https://www.mindspore.cn/install).
In addition, you shall have basic mathematical knowledge such as Python coding basics, probability, and matrix.
Start your MindSpore experience now.
### Downloading the Dataset
The `MNIST` dataset used in this example consists of 10 classes of 28 x 28 pixels grayscale images. It has a training set of 60,000 examples, and a test set of 10,000 examples.
> Download the MNIST dataset at <http://yann.lecun.com/exdb/mnist/>. This page provides four download links of dataset files. The first two links are required for data training, and the last two links are required for data test.
Download the files, decompress them, and store them in the workspace directories `./MNIST_Data/train` and `./MNIST_Data/test`.
The directory structure is as follows:
```
└─MNIST_Data
├─test
│ t10k-images.idx3-ubyte
│ t10k-labels.idx1-ubyte
└─train
train-images.idx3-ubyte
train-labels.idx1-ubyte
```
> For ease of use, we added the function of automatically downloading datasets in the sample script.
### Importing Python Libraries and Modules
Before start, you need to import Python libraries.
Currently, the `os` libraries are required. For ease of understanding, other required libraries will not be described here.
```python
import os
```
For details about MindSpore modules, search on the [MindSpore API Page](https://www.mindspore.cn/api/en/0.1.0-alpha/index.html).
### Configuring the Running Information
Before compiling code, you need to learn basic information about the hardware and backend required for MindSpore running.
You can use `context.set_context()` to configure the information required for running, such as the running mode, backend information, and hardware information.
Import the `context` module and configure the required information.
```python
import argparse
from mindspore import context
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='MindSpore LeNet Example')
parser.add_argument('--device_target', type=str, default="Ascend", choices=['Ascend', 'GPU', 'CPU'],
help='device where the code will be implemented (default: Ascend)')
args = parser.parse_args()
context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target,
enable_mem_reuse=False)
...
```
This example runs in graph mode. You can configure hardware information based on site requirements. For example, if the code runs on the Ascend AI processor, set `--device_target` to `Ascend`. This rule also applies to the code running on the CPU and GPU. For details about parameters, see the API description for `context.set_context()`.
## Processing Data
Datasets are important for training. A good dataset can effectively improve training accuracy and efficiency. Generally, before loading a dataset, you need to perform some operations on the dataset.
### Defining the Dataset and Data Operations
Define the `create_dataset()` function to create a dataset. In this function, define the data augmentation and processing operations to be performed.
1. Define the dataset.
2. Define parameters required for data augmentation and processing.
3. Generate corresponding data augmentation operations according to the parameters.
4. Use the `map()` mapping function to apply data operations to the dataset.
5. Process the generated dataset.
```python
import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.transforms.vision.c_transforms as CV
from mindspore.dataset.transforms.vision import Inter
from mindspore.common import dtype as mstype
def create_dataset(data_path, batch_size=32, repeat_size=1,
num_parallel_workers=1):
""" create dataset for train or test
Args:
data_path: Data path
batch_size: The number of data records in each group
repeat_size: The number of replicated data records
num_parallel_workers: The number of parallel workers
"""
# define dataset
mnist_ds = ds.MnistDataset(data_path)
# define operation parameters
resize_height, resize_width = 32, 32
rescale = 1.0 / 255.0
shift = 0.0
rescale_nml = 1 / 0.3081
shift_nml = -1 * 0.1307 / 0.3081
# define map operations
resize_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR) # resize images to (32, 32)
rescale_nml_op = CV.Rescale(rescale_nml, shift_nml) # normalize images
rescale_op = CV.Rescale(rescale, shift) # rescale images
hwc2chw_op = CV.HWC2CHW() # change shape from (height, width, channel) to (channel, height, width) to fit network.
type_cast_op = C.TypeCast(mstype.int32) # change data type of label to int32 to fit network
# apply map operations on images
mnist_ds = mnist_ds.map(input_columns="label", operations=type_cast_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=resize_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=rescale_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=rescale_nml_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=hwc2chw_op, num_parallel_workers=num_parallel_workers)
# apply DatasetOps
buffer_size = 10000
mnist_ds = mnist_ds.shuffle(buffer_size=buffer_size) # 10000 as in LeNet train script
mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True)
mnist_ds = mnist_ds.repeat(repeat_size)
return mnist_ds
```
In the preceding information:
`batch_size`: number of data records in each group. Currently, each group contains 32 data records.
`repeat_size`: number of replicated data records.
Perform the shuffle and batch operations, and then perform the repeat operation to ensure that data during an epoch is unique.
> MindSpore supports multiple data processing and augmentation operations, which are usually combined. For details, see section "Data Processing and Augmentation" in the MindSpore Tutorials (https://www.mindspore.cn/tutorial/en/0.1.0-alpha/use/data_preparation/data_processing_and_augmentation.html).
## Defining the Network
The LeNet network is relatively simple. In addition to the input layer, the LeNet network has seven layers, including two convolutional layers, two down-sample layers (pooling layers), and three full connection layers. Each layer contains different numbers of training parameters, as shown in the following figure:
![LeNet-5](./images/LeNet_5.jpg)
> For details about the LeNet network, visit <http://yann.lecun.com/exdb/lenet/>.
You need to initialize the full connection layers and convolutional layers.
`TruncatedNormal`: parameter initialization method. MindSpore supports multiple parameter initialization methods, such as `TruncatedNormal`, `Normal`, and `Uniform`. For details, see the description of the `mindspore.common.initializer` module of the MindSpore API.
The following is the sample code for initialization:
```python
import mindspore.nn as nn
from mindspore.common.initializer import TruncatedNormal
def weight_variable():
"""
weight initial
"""
return TruncatedNormal(0.02)
def conv(in_channels, out_channels, kernel_size, stride=1, padding=0):
"""
conv layer weight initial
"""
weight = weight_variable()
return nn.Conv2d(in_channels, out_channels,
kernel_size=kernel_size, stride=stride, padding=padding,
weight_init=weight, has_bias=False, pad_mode="valid")
def fc_with_initialize(input_channels, out_channels):
"""
fc layer weight initial
"""
weight = weight_variable()
bias = weight_variable()
return nn.Dense(input_channels, out_channels, weight, bias)
```
To use MindSpore for neural network definition, inherit `mindspore.nn.cell.Cell`. `Cell` is the base class of all neural networks (such as `Conv2d`).
Define each layer of a neural network in the `__init__()` method in advance, and then define the `construct()` method to complete the forward construction of the neural network. According to the structure of the LeNet network, define the network layers as follows:
```python
import mindspore.ops.operations as P
class LeNet5(nn.Cell):
"""
Lenet network structure
"""
#define the operator required
def __init__(self):
super(LeNet5, self).__init__()
self.batch_size = 32
self.conv1 = conv(1, 6, 5)
self.conv2 = conv(6, 16, 5)
self.fc1 = fc_with_initialize(16 * 5 * 5, 120)
self.fc2 = fc_with_initialize(120, 84)
self.fc3 = fc_with_initialize(84, 10)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
self.reshape = P.Reshape()
#use the preceding operators to construct networks
def construct(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.conv2(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.reshape(x, (self.batch_size, -1))
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
return x
```
## Defining the Loss Function and Optimizer
### Basic Concepts
Before definition, this section briefly describes concepts of loss function and optimizer.
- Loss function: It is also called objective function and is used to measure the difference between a predicted value and an actual value. Deep learning reduces the value of the loss function by continuous iteration. Defining a good loss function can effectively improve the model performance.
- Optimizer: It is used to minimize the loss function, improving the model during training.
After the loss function is defined, the weight-related gradient of the loss function can be obtained. The gradient is used to indicate the weight optimization direction for the optimizer, improving model performance.
### Defining the Loss Function
Loss functions supported by MindSpore include `SoftmaxCrossEntropyWithLogits`, `L1Loss`, `MSELoss`. The loss function `SoftmaxCrossEntropyWithLogits` is used in this example.
```python
from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
```
Call the defined loss function in the `__main__` function.
```python
if __name__ == "__main__":
...
#define the loss function
net_loss = SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True, reduction='mean')
...
```
### Defining the Optimizer
Optimizers supported by MindSpore include `Adam`, `AdamWeightDecay` and `Momentum`.
The popular Momentum optimizer is used in this example.
```python
if __name__ == "__main__":
...
#learning rate setting
lr = 0.01
momentum = 0.9
#create the network
network = LeNet5()
#define the optimizer
net_opt = nn.Momentum(network.trainable_params(), lr, momentum)
...
```
## Training the Network
### Saving the Configured Model
MindSpore provides the callback mechanism to execute customized logic during training. `ModelCheckpoint` and `LossMonitor` provided by the framework are used in this example.
`ModelCheckpoint` can save network models and parameters for subsequent fine-tuning. `LossMonitor` can monitor the changes of the `loss` value during training.
```python
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig
if __name__ == "__main__":
...
# set parameters of check point
config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10)
# apply parameters of check point
ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet", config=config_ck)
...
```
### Configuring the Network Training
Use the `model.train` API provided by MindSpore to easily train the network.
In this example, set `epoch_size` to 1 to train the dataset for five iterations.
```python
from mindspore.nn.metrics import Accuracy
from mindspore.train.callback import LossMonitor
from mindspore.train import Model
...
def train_net(args, model, epoch_size, mnist_path, repeat_size, ckpoint_cb):
"""define the training method"""
print("============== Starting Training ==============")
#load training dataset
ds_train = create_dataset(os.path.join(mnist_path, "train"), 32, repeat_size)
model.train(epoch_size, ds_train, callbacks=[ckpoint_cb, LossMonitor()], dataset_sink_mode=False) # train
...
if __name__ == "__main__":
...
epoch_size = 1
mnist_path = "./MNIST_Data"
repeat_size = epoch_size
model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
train_net(args, model, epoch_size, mnist_path, repeat_size, ckpoint_cb)
...
```
In the preceding information:
In the `train_net` method, we loaded the training dataset, `MNIST path` is MNIST dataset path.
## Running and Viewing the Result
Run the script using the following command:
```
python lenet.py --device_target=CPU
```
In the preceding information:
`Lenet. Py`: the script file you wrote.
`--device_target CPU`: Specify the hardware platform.The parameters are 'CPU', 'GPU' or 'Ascend'.
Loss values are output during training, as shown in the following figure. Although loss values may fluctuate, they gradually decrease and the accuracy gradually increases in general. Loss values displayed each time may be different because of their randomicity.
The following is an example of loss values output during training:
```bash
...
epoch: 1 step: 262, loss is 1.9212162
epoch: 1 step: 263, loss is 1.8498616
epoch: 1 step: 264, loss is 1.7990671
epoch: 1 step: 265, loss is 1.9492403
epoch: 1 step: 266, loss is 2.0305142
epoch: 1 step: 267, loss is 2.0657792
epoch: 1 step: 268, loss is 1.9582214
epoch: 1 step: 269, loss is 0.9459006
epoch: 1 step: 270, loss is 0.8167224
epoch: 1 step: 271, loss is 0.7432692
...
```
The following is an example of model files saved after training:
```bash
checkpoint_lenet-1_1875.ckpt
```
In the preceding information:
`checkpoint_lenet-1_1875.ckpt`: saved model parameter file. The following refers to saved files as well. The file name format is checkpoint_{network name}-{epoch No.}_{step No.}.ckpt.
## Validating the Model
After get the model file, we verify the generalization ability of the model.
```python
from mindspore.train.serialization import load_checkpoint, load_param_into_net
def test_net(args,network,model,mnist_path):
"""define the evaluation method"""
print("============== Starting Testing ==============")
#load the saved model for evaluation
param_dict = load_checkpoint("checkpoint_lenet-1_1875.ckpt")
#load parameter to the network
load_param_into_net(network, param_dict)
#load testing dataset
ds_eval = create_dataset(os.path.join(mnist_path, "test")) # test
acc = model.eval(ds_eval, dataset_sink_mode=False)
print("============== Accuracy:{} ==============".format(acc))
if __name__ == "__main__":
...
test_net(args, network, model, mnist_path)
```
In the preceding information:
`load_checkpoint()`: This API is used to load the CheckPoint model parameter file and return a parameter dictionary.
`checkpoint_lenet-3_1404.ckpt`: name of the saved CheckPoint model file.
`load_param_into_net`: This API is used to load parameters to the network.
Run the script using the following command:
```
python lenet.py --device_target=CPU
```
In the preceding information:
`Lenet. Py`: the script file you wrote.
`--device_target CPU`: Specify the hardware platform.The parameters are 'CPU', 'GPU' or 'Ascend'.
Command output similar to the following is displayed:
```
============== Starting Testing ==============
============== Accuracy:{'Accuracy': 0.9742588141025641} ==============
```
The model accuracy data is displayed in the output content. In the example, the accuracy reaches 97.4%, indicating a good model quality.
# Legal Statement
## Rules and Conditions for Accessing the Website of MindSpore Open Source Project
[MindSpore open source project website] is operated by MindSpore open source project. Its domain name is <https://www.mindspore.cn>. The following rules apply to all visitors or users accessing this website. MindSpore open source project may, without notice to you, at any time revise these terms according to laws, regulations, and market changes. The right to access this website is authorized by MindSpore open source project according to the following terms. If you do not agree to these terms, please do not use this website. Your use of services provided by <https://www.mindspore.cn> will be deemed as your acknowledgment of the whole Statement. MindSpore open source project has the right to take legal and fair remedies for violations of these conditions.
## Disclaimer
Although MindSpore open source project has attempted to provide accurate information on the website (<https://www.mindspore.cn>), including but not limited to the texts, pictures, data, opinions, suggestions, web pages, and links, the project does not guarantee the accuracy, completeness, fullness or reliability of these materials and content, and explicitly states that it does not take responsibility for errors or omissions in these data and content, or make any express or implied warranty on these materials and information, including but not limited to guarantee concerning relevant ownerships, free from infringement upon third-party rights, quality or computer viruses.
MindSpore open source project may change the information on the website (<https://www.mindspore.cn>) at any time without notice. You shall periodically visit this website to obtain the latest version information. Mention of products or services that are not related to MindSpore open source project is for information purposes only and constitutes neither an endorsement nor a recommendation. MindSpore open source project does not provide any statement, guarantee or authorization for any of the products or services appearing on the website (<https://www.mindspore.cn>). All products and services are governed by the sales agreement of MindSpore open source project.
Whereas some of the services provided on the website (<https://www.mindspore.cn>) are bulletin board system (BBS) service, the relevant information about or posted by its members on the website (including but not limited to usernames, company names, contact persons and details, related images and videos) is all provided by the members, who shall be full responsible for any information they provide.
Any entity or individual who considers the content on the website (<https://www.mindspore.cn>) (including but not limited to the commodity information posted by the members of the website) suspected of infringing their legal interest shall make an infringement statement in writing to MindSpore open source project, together with their identification, ownership certification, particular URL and detailed proof of infringement. MindSpore open source project will remove the content alleged to be infringing by law upon receiving the foregoing legal document.
## Copyrights
All materials and content on the website (<https://www.mindspore.cn>) are protected by law. Other than quotes from other sources, all copyrights belong to MindSpore open source project. Without a prior written permission of MindSpore open source project or other parties concerned, no person or organization shall reproduce, distribute, reprint, or play any content of this website (<https://www.mindspore.cn>) in any form; link to or transmit the content through hyperlinks; upload the content to other servers using the method of images; store the content in information retrieval systems; or use the content for any other commercial purposes. For non-commercial and personal use, the content of the website may be downloaded or printed on condition that the content is not modified and all rights statements are reserved.
## Trademarks
All logos and trademarks used on this website (<https://www.mindspore.cn>) belong to MindSpore open source project, except trademarks, logos and service marks of other companies with indications. No content provided on the website (<https://www.mindspore.cn>) shall be deemed as a grant of the approval or right to use any trademark or logo aforesaid by implication, no objection or other means without prior written consent of MindSpore open source project or such third party which may own the mark. Without prior written permission, no one may use the name, trademark or logo of MindSpore open source project in any way.
## Third-Party Links
This website (<https://www.mindspore.cn>) may contain links to third party sites. Access to any other Internet site linked to this website is at the user's own risk and MindSpore open source project is not responsible for the accuracy or reliability of any information, data, opinions, suggestions or statements made on these sites. MindSpore open source project provides these links merely as a convenience and the inclusion of such links does not imply an endorsement, recommendation, or advertisement.
# Privacy Policy
Please carefully read and understand the Privacy Policy before using MindSpore and other services on this website.
To ensure the proper running of MindSpore and provide better services for you, we will collect the following personal data:
1. You can subscribe to the latest MindSpore news on this website. To fulfill the business function, you need to agree us to collect and use your personal and sensitive personal information, including your email address.
2. To participate in community development, you need to sign the Contribution License Agreement (CLA), which contains the following personal information:
- Your name or account
- Your address
- Your email address
- Your phone number
- Your fax number
- Your ID on Gitee
3. The community provides a mailing list for developers to communicate and collaborate with each other. When you join a mailing list, we will collect your email address.
4. To learn about the running status of the MindSpore community, we will collect your information when you access the website, including but not limited to your IP address, browser type, used language, access date and time, software and hardware features, web page you accessed, device model, device identity code, operating system, resolution, telecom carrier, and download clicks.
Your information is securely saved and protected. Protection methods include, but are not limited to, data encryption.
MindSpore does not share, transfer, or disclose your information without your prior consent unless otherwise required by laws and regulations.
According to applicable laws and regulations, you may have rights to access, modify, delete, deny, limit, or transfer the above information. To exercise your rights, contact us through the contact information provided in this document.
The above information is stored in the People's Republic of China.
**You must ensure that your use of MindSpore is in compliance with laws and regulations. MindSpore only provides the standard service upon your request and is not responsible for the legal compliance of your use.**
**You understand that the preceding information is mandatory for providing services. If you do not provide or disallow us to collect and use the above information, we will not be able to provide the services.**
**How do we use cookies?**
To ensure the normal operation of our website, sometimes we may store small data files named cookies on computers or portable devices. A cookie is a text-only file stored on computers or mobile devices by network servers. The content of a cookie can only be retrieved or read by the server that created it. Each cookie is unique to your web browser or mobile application. Cookies usually contain identifiers, site names, and some numbers and characters. Websites can store data about user preferences and other information using cookies.
MindSpore enables cookies for the same purposes of most website or Internet service providers, that is, to improve user experience. Specifically, the purposes are as follows:
1. Saving settings With cookies, the website can save settings, such as search result records, to acquire returned search results and optimize browser services.
2. Authentication When you visit this website, a unique ID is generated to identify you. If the website does not use cookies with the authentication function, it will consider its users as new visitors each time they visit the website. For example, if you log in to the website and switch to another web page, the website will no longer recognize you and you will be regarded as a new visitor.
Other third parties, such as Google and Baidu, will set cookies on our websites. They use these cookies to receive IP addresses, information about your browser configurations, and access information, but do not collect your personal identity information. In this way, they can learn how you interact with the website. You can visit the websites of the third parties to learn how they use cookies.
You can manage or delete cookies based on your preferences. For details, see [AboutCookies](https://www.aboutcookies.org/). You can clear all cookies stored on your computer, and most web browsers can be configured to block cookies. However, if you do so, you may have to manually change your user settings every time you visit our website.
If you have any questions, contact us at contact@mindspore.cn.
Last update: March, 2020
\ No newline at end of file
# Converting Datasets to the Mindspore Data Format
<!-- TOC -->
- [Converting Datasets to the Mindspore Data Format](#converting-datasets-to-the-mindspore-data-format)
- [Overview](#overview)
- [Converting Non-Standard Datasets to the Mindspore Data Format](#converting-non-standard-datasets-to-the-mindspore-data-format)
- [Converting Images and Labels](#converting-images-and-labels)
- [Converting Common Datasets to the MindSpore Data Format](#converting-common-datasets-to-the-mindspore-data-format)
- [Converting the CIFAR-10 Dataset](#converting-the-cifar-10-dataset)
- [Converting the CIFAR-100 Dataset](#converting-the-cifar-100-dataset)
- [Converting the ImageNet Dataset](#converting-the-imagenet-dataset)
- [Converting the MNIST Dataset](#converting-the-mnist-dataset)
<!-- /TOC -->
## Overview
You can convert non-standard datasets and common datasets into the MindSpore data format so that they can be easily loaded to MindSpore for training. In addition, the performance of MindSpore in some scenarios is optimized, which delivers better user experience when you use datasets in the MindSpore data format.
The MindSpore data format has the following features:
1. Unified storage and access of user data are implemented, simplifying training data reading.
2. Data is aggregated for storage, efficient reading, and easy management and transfer.
3. Data encoding and decoding are efficient and transparent to users.
4. The partition size is flexibly controlled to implement distributed training.
## Converting Non-Standard Datasets to the Mindspore Data Format
MindSpore provides write operation tools to write user-defined raw data in MindSpore format.
### Converting Images and Labels
1. Import the `FileWriter` class for file writing.
```python
from mindspore.mindrecord import FileWriter
```
2. Define a dataset schema which defines dataset fields and field types.
```python
cv_schema_json = {"file_name": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
```
Schema specifications are as follows:
A file name can contain only letters, digits, and underscores (_).
The field type can be int32, int64, float32, float64, string, or bytes.
The field shape can be a one-dimensional array represented by [-1], a two-dimensional array represented by [m, n], or a three-dimensional array represented by [x, y, z].
> 1. The type of a field with the shape attribute can only be int32, int64, float32, or float64.
> 2. If the field has the shape attribute, prepare the data of numpy.ndarray type and transfer the data to the write_raw_data API.
Examples:
- Image classification
```python
cv_schema_json = {"file_name": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
```
- Natural Language Processing (NLP)
```python
cv_schema_json = {"id": {"type": "int32"}, "masks": {"type": "int32", "shape": [-1]}, "inputs": {"type": "int64", "shape": [4, 32]}, "labels": {"type": "int64", "shape": [-1]}}
```
3. Prepare the data sample list to be written based on the user-defined schema format.
```python
data = [{"file_name": "1.jpg", "label": 0, "data": b"\x10c\xb3w\xa8\xee$o&<q\x8c\x8e(\xa2\x90\x90\x96\xbc\xb1\x1e\xd4QER\x13?\xff\xd9"},
{"file_name": "2.jpg", "label": 56, "data": b"\xe6\xda\xd1\xae\x07\xb8>\xd4\x00\xf8\x129\x15\xd9\xf2q\xc0\xa2\x91YFUO\x1dsE1\x1ep"},
{"file_name": "3.jpg", "label": 99, "data": b"\xaf\xafU<\xb8|6\xbd}\xc1\x99[\xeaj+\x8f\x84\xd3\xcc\xa0,i\xbb\xb9-\xcdz\xecp{T\xb1\xdb\"}]
```
4. Prepare index fields. Adding index fields can accelerate data reading. This step is optional.
```python
indexes = ["file_name", "label"]
```
5. Create a `FileWriter` object, transfer the file name and number of slices, add the schema and index, call the `write_raw_data` API to write data, and call the `commit` API to generate a local data file.
```python
writer = FileWriter(file_name="testWriter.mindrecord", shard_num=4)
writer.add_schema(cv_schema_json, "test_schema")
writer.add_index(indexes)
writer.write_raw_data(data)
writer.commit()
```
In the preceding information:
`write_raw_data`: writes data to the memory.
`commit`: writes the data in the memory to the disk.
6. Add data to the existing data format file, call the `open_for_append` API to open the existing data file, call the `write_raw_data` API to write new data, and then call the `commit` API to generate a local data file.
```python
writer = FileWriter.open_for_append("testWriter.mindrecord0")
writer.write_raw_data(data)
writer.commit()
```
## Converting Common Datasets to the MindSpore Data Format
MindSpore provides utility classes to convert common datasets to the MindSpore data format. The following table lists common datasets and called utility classes:
| Dataset | Called Utility Class |
| -------- | ------------ |
| CIFAR-10 | Cifar10ToMR |
| CIFAR-100| Cifar100ToMR |
| ImageNet | ImageNetToMR |
| MNIST | MnistToMR |
### Converting the CIFAR-10 Dataset
You can use the `Cifar10ToMR` class to convert the raw CIFAR-10 data into the MindSpore data format.
1. Prepare the CIFAR-10 python version dataset and decompress the file to a specified directory (the `cifar10` directory in the example), as the following shows:
```
% ll cifar10/cifar-10-batches-py/
batches.meta
data_batch_1
data_batch_2
data_batch_3
data_batch_4
data_batch_5
readme.html
test_batch
```
> CIFAR-10 dataset download address: <https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz>
2. Import the `Cifar10ToMR` class for dataset converting.
```python
from mindspore.mindrecord import Cifar10ToMR
```
3. Instantiate the `Cifar10ToMR` object and call the `transform` API to convert the CIFAR-10 dataset to the MindSpore data format.
```python
CIFAR10_DIR = "./cifar10/cifar-10-batches-py"
MINDRECORD_FILE = "./cifar10.mindrecord"
cifar10_transformer = Cifar10ToMR(CIFAR10_DIR, MINDRECORD_FILE)
cifar10_transformer.transform(['label'])
```
In the preceding information:
`CIFAR10_DIR`: path where the CIFAR-10 dataset folder is stored.
`MINDRECORD_FILE`: path where the output file in the MindSpore data format is stored.
### Converting the CIFAR-100 Dataset
You can use the `Cifar100ToMR` class to convert the raw CIFAR-100 data to the MindSpore data format.
1. Prepare the CIFAR-100 dataset and decompress the file to a specified directory (the `cifar100` directory in the example).
```
% ll cifar100/cifar-100-python/
meta
test
train
```
> CIFAR-100 dataset download address: <https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz>
2. Import the `Cifar100ToMR` class for dataset converting.
```python
from mindspore.mindrecord import Cifar100ToMR
```
3. Instantiate the `Cifar100ToMR` object and call the `transform` API to convert the CIFAR-100 dataset to the MindSpore data format.
```python
CIFAR100_DIR = "./cifar100/cifar-100-python"
MINDRECORD_FILE = "./cifar100.mindrecord"
cifar100_transformer = Cifar100ToMR(CIFAR100_DIR, MINDRECORD_FILE)
cifar100_transformer.transform(['fine_label', 'coarse_label'])
```
In the preceding information:
`CIFAR100_DIR`: path where the CIFAR-100 dataset folder is stored.
`MINDRECORD_FILE`: path where the output file in the MindSpore data format is stored.
### Converting the ImageNet Dataset
You can use the `ImageNetToMR` class to convert the raw ImageNet data (images and labels) to the MindSpore data format.
1. Download and prepare the ImageNet dataset as required.
> ImageNet dataset download address: <http://image-net.org/download>
Store the downloaded ImageNet dataset in a folder. The folder contains all images and a mapping file that records labels of the images.
In the mapping file, there are three columns, which are separated by spaces. They indicate image classes, label IDs, and label names. The following is an example of the mapping file:
```
n02119789 1 pen
n02100735 2 notbook
n02110185 3 mouse
n02096294 4 orange
```
2. Import the `ImageNetToMR` class for dataset converting.
```python
from mindspore.mindrecord import ImageNetToMR
```
3. Instantiate the `ImageNetToMR` object and call the `transform` API to convert the dataset to the MindSpore data format.
```python
IMAGENET_MAP_FILE = "./testImageNetDataWhole/labels_map.txt"
IMAGENET_IMAGE_DIR = "./testImageNetDataWhole/images"
MINDRECORD_FILE = "./testImageNetDataWhole/imagenet.mindrecord"
PARTITION_NUMBER = 4
imagenet_transformer = ImageNetToMR(IMAGENET_MAP_FILE, IMAGENET_IMAGE_DIR, MINDRECORD_FILE, PARTITION_NUMBER)
imagenet_transformer.transform()
```
In the preceding information:
`IMAGENET_MAP_FILE`: path where the label mapping file of the ImageNetToMR dataset is stored.
`IMAGENET_IMAGE_DIR`: path where all ImageNet images are stored.
`MINDRECORD_FILE`: path where the output file in the MindSpore data format is stored.
### Converting the MNIST Dataset
You can use the `MnistToMR` class to convert the raw MNIST data to the MindSpore data format.
1. Prepare the MNIST dataset and save the downloaded file to a specified directory, as the following shows:
```
% ll mnist_data/
train-images-idx3-ubyte.gz
train-labels-idx1-ubyte.gz
t10k-images-idx3-ubyte.gz
t10k-labels-idx1-ubyte.gz
```
> MNIST dataset download address: <http://yann.lecun.com/exdb/mnist>
2. Import the `MnistToMR` class for dataset converting.
```python
from mindspore.mindrecord import MnistToMR
```
3. Instantiate the `MnistToMR` object and call the `transform` API to convert the MNIST dataset to the MindSpore data format.
```python
MNIST_DIR = "./mnist_data"
MINDRECORD_FILE = "./mnist.mindrecord"
mnist_transformer = MnistToMR(MNIST_DIR, MINDRECORD_FILE)
mnist_transformer.transform()
```
In the preceding information:
`MNIST_DIR`: path where the MNIST dataset folder is stored.
`MINDRECORD_FILE`: path where the output file in the MindSpore data format is stored.
Data Preparation
================
.. toctree::
:maxdepth: 1
loading_the_datasets
converting_datasets
data_processing_and_augmentation
\ No newline at end of file
# Data Processing and Augmentation
<!-- TOC -->
- [Data Processing and Augmentation](#data-processing-and-augmentation)
- [Overview](#overview)
- [Data Processing Operations Supported by Mindspore](#data-processing-operations-supported-by-mindspore)
- [repeat](#repeat)
- [batch](#batch)
- [shuffle](#shuffle)
- [map](#map)
- [zip](#zip)
- [Data Augmentation](#data-augmentation)
- [Using the `c_transforms` Module](#using-the-c_transforms-module)
- [Using the `py_transforms` Module](#using-the-py_transforms-module)
<!-- /TOC -->
## Overview
Data is the basis of deep learning. Data input plays an important role in the deep neural network training. Therefore, after the original dataset is obtained and before data is loaded and trained, data processing or augmentation is often required due to data size and performance restrictions, to obtain optimized data input.
MindSpore provides users with data processing and augmentation functions.
> Essentially, data augmentation is implemented through the data processing operation `map`. Yet data augmentation is described separately due to its diversified transform operations.
## Data Processing Operations Supported by Mindspore
MindSpore supports multiple data processing operations, including repeat, batch, shuffle, and map, as shown in the following table.
| Operation | Description |
| -------- | -------------------------------------- |
| repeat | Repeat a dataset to increase the data size. |
| batch | Process data in batches to accelerate the training process. |
| shuffle | Shuffle data. |
| map | Apply the provided functions or operators to the specified column data. |
| zip | Combine multiple datasets into one dataset. |
The operations can be performed separately. In practice, they are often used together as needed. You are advised to use them in the following sequence:
![avatar](../images/dataset_pipeline.png)
In the following example, the shuffle, batch, and repeat operations are performed when the MNIST dataset is read.
```python
import mindspore.dataset as ds
ds1 = ds.MnistDataset(MNIST_DATASET_PATH, MNIST_SCHEMA) # Create MNIST dataset.
ds1 = ds1.shuffle(buffer_size=10000)
ds1 = ds1.batch(32, drop_remainder=True)
ds1 = ds1.repeat(10)
```
In the preceding operations, data is shuffled, every 32 data records are combined into a batch, and then the dataset is repeated for 10 times.
The following describes how to construct a simple dataset `ds1` and perform data processing operations on it.
1. Import the module on which data processing depends.
```python
import mindspore.dataset as ds
```
2. Define the `generator_func()` function for dataset generating.
```python
def generator_func():
for i in range(5):
yield (np.array([i, i+1, i+2]),)
```
3. Use `GeneratorDataset` to create the dataset `ds1` for data processing.
```python
ds1 = ds.GeneratorDataset(generator_func, ["data"])
print("ds1:")
for data in ds1.create_dict_iterator():
print(data["data"])
```
The output is as follows:
```
ds1:
[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]
```
### repeat
In limited datasets, to optimize the network, a dataset is usually trained for multiple times.
![avatar](../images/repeat.png)
> In machine learning, an epoch refers to one cycle through the full training dataset.
During multiple epochs, `repeat()` can be used to increase the data size. The definition of `repeat()` is as follows:
```python
def repeat(self, count=None):
```
You can define the dataset `ds2` and call `repeat` to increase the data size. The sample code is as follows:
```python
ds2 = ds.GeneratorDataset(generator_func, ["data"])
ds2 = ds2.repeat(2)
print("ds2:")
for data in ds2.create_dict_iterator():
print(data["data"])
```
Set the multiple to 2. Therefore, the data size of `ds2` is twice that of the original dataset `ds1`. The output is as follows:
```
ds2:
[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]
[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]
```
### batch
Combine data records in datasets into batches. In practice, data can be processed in batches. Training data in batches can reduce training steps and accelerate the training process. MindSpore uses the `batch()` function to implement the batch operation. The function is defined as follows:
![avatar](../images/batch.png)
```python
def batch(self, batch_size, drop_remainder=False, num_parallel_workers=None)
```
Use the dataset `ds1` generated by GeneratorDataset to construct two datasets.
- In the first dataset `ds2`, combine every two data records into a batch.
- In the second dataset `ds3`, combine every three data records into a batch, and remove the remaining data records that are less than three.
The sample code of `ds2` is as follows:
```python
ds2 = ds1.batch(batch_size=2) # Default drop_remainder is False, the last remainder batch isn't dropped.
print("batch size:2 drop remainder:False")
for data in ds2.create_dict_iterator():
print(data["data"])
```
The output is as follows:
```
batch size:2 drop remainder:False
[[0 1 2]
[1 2 3]]
[[2 3 4]
[3 4 5]]
[[4 5 6]]
```
The sample code of `ds3` is as follows:
```python
ds3 = ds1.batch(batch_size=3, drop_remainder=True) # When drop_remainder is True, the last remainder batch will be dropped.
print("batch size:3 drop remainder:True")
for data in ds3.create_dict_iterator():
print(data["data"])
```
The output is as follows:
```
batch size:3 drop remainder:True
[[0 1 2]
[1 2 3]
[2 3 4]]
```
### shuffle
You can shuffle ordered or repeated datasets.
![avatar](../images/shuffle.png)
The shuffle operation is used to shuffle data. A larger value of buffer_size indicates a higher shuffling degree, consuming more time and computing resources.
The definition of `shuffle()` is as follows:
```python
def shuffle(self, buffer_size):
```
Call `shuffle()` to shuffle the dataset `ds1`. The sample code is as follows:
```python
print("Before shuffle:")
for data in ds1.create_dict_iterator():
print(data["data"])
ds2 = ds1.shuffle(buffer_size=5)
print("After shuffle:")
for data in ds2.create_dict_iterator():
print(data["data"])
```
The possible output is as follows. After data is shuffled, the data sequence changes randomly.
```
Before shuffle:
[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]
After shuffle:
[3 4 5]
[2 3 4]
[4 5 6]
[1 2 3]
[0 1 2]
```
### map
The map operation is used to process data. For example, convert the dataset of color images into the dataset of grayscale images. You can flexibly perform the operation as required.
MindSpore provides the `map()` function to map datasets. You can apply the provided functions or operators to the specified column data.
You can customize the function or use `c_transforms` or `py_transforms` for data augmentation.
> For details about data augmentation operations, see [Data Augmentation](#Data Augmentation).
![avatar](../images/map.png)
The definition of `map()` is as follows:
```python
def map(self, input_columns=None, operations=None, output_columns=None, columns_order=None,
num_parallel_workers=None):
```
In the following example, the `map()` function is used to apply the defined anonymous function (lambda function) to the dataset `ds1` so that the data values in the dataset are multiplied by 2.
```python
func = lambda x : x*2 # Define lambda function to multiply each element by 2.
ds2 = ds1.map(input_columns="data", operations=func)
for data in ds2.create_dict_iterator():
print(data["data"])
```
The code output is as follows. Data values in each row of the dataset `ds2` is multiplied by 2.
```
[0 2 4]
[2 4 6]
[4 6 8]
[6 8 10]
[8 10 12]
```
### zip
MindSpore provides the `zip()` function to combine multiple datasets into one dataset.
> If the column names in the two datasets are the same, the two datasets are not combined. Therefore, pay attention to column names.
> If the number of rows in the two datasets is different, the number of rows after combination is the same as the smaller number.
```python
def zip(self, datasets):
```
1. Use the preceding construction method of the dataset `ds1` to construct the dataset `ds2`.
```python
def generator_func2():
for i in range(5):
yield (np.array([i-3, i-2, i-1]),)
ds2 = ds.GeneratorDataset(generator_func2, ["data2"])
```
2. Use `zip()` to combine the `data1` column of the dataset `ds1`and the `data2` column of the dataset `ds2` into the dataset `ds3`.
```python
ds3 = ds.zip((ds1, ds2))
for data in ds3.create_dict_iterator():
print(data)
```
The output is as follows:
```
{'data1': array([0, 1, 2], dtype=int64), 'data2': array([-3, -2, -1], dtype=int64)}
{'data1': array([1, 2, 3], dtype=int64), 'data2': array([-2, -1, 0], dtype=int64)}
{'data1': array([2, 3, 4], dtype=int64), 'data2': array([-1, 0, 1], dtype=int64)}
{'data1': array([3, 4, 5], dtype=int64), 'data2': array([0, 1, 2], dtype=int64)}
{'data1': array([4, 5, 6], dtype=int64), 'data2': array([1, 2, 3], dtype=int64)}
```
## Data Augmentation
During image training, especially when the dataset size is relatively small, you can preprocess images by using a series of data augmentation operations, thereby enriching the datasets.
MindSpore provides the `c_transforms` and `py_transforms` module functions for users to perform data augmentation. You can also customize functions or operators to perform data augmentation. The following table describes the two modules provided by MindSpore. For details, see the related description in the API reference document.
| Module | Implementation | Description |
| ---------------| ------------------------------------------------------ | --- |
| `c_transforms` | C++-based [OpenCV](https://opencv.org/) implementation | The performance is high. |
| `py_transforms` | Python-based [PIL](https://pypi.org/project/Pillow/) implementation | This module provides multiple image augmentation functions and the method for converting PIL images into NumPy arrays. |
For users who would like to use Python PIL in image learning tasks, the `py_transforms` module is a good tool for image augmentation. You can use Python PIL to customize extensions.
Data augmentation requires the `map()` function. For details about how to use the `map()` function, see [map](#map).
### Using the `c_transforms` Module
1. Import the module to the code.
```python
import mindspore.dataset.transforms.vision.c_transforms as transforms
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
```
2. Define data augmentation operators. The following uses `Resize` as an example:
```python
dataset = ds.ImageFolderDatasetV2(DATA_DIR, decode=True) # Deocde images.
resize_op = transforms.Resize(size=(500,500), interpolation=Inter.LINEAR)
dataset.map(input_columns="image", operations=resize_op)
for data in dataset.create_dict_iterator():
imgplot_resized = plt.imshow(data["image"])
plt.show()
```
The running result shows that the original image is changed from 1024 x 683 pixels to 500 x 500 pixels after data processing by using `Resize()`.
![avatar](../images/image.png)
Figure 1: Original image
![avatar](../images/image_resized.png)
Figure 2: Image after its size is reset
### Using the `py_transforms` Module
1. Import the module to the code.
```python
import mindspore.dataset.transforms.vision.py_transforms as transforms
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
```
2. Define data augmentation operators and use the `ComposeOp` API to combine multiple data augmentation operations. The following uses `RandomCrop` as an example:
```python
dataset = ds.ImageFolderDatasetV2(DATA_DIR)
transforms_list = [
transforms.Decode(), # Decode images to PIL format.
transforms.RandomCrop(size=(500,500)),
transforms.ToTensor() # Convert PIL images to Numpy ndarray.
]
compose = transforms.ComposeOp(transforms_list)
dataset = dataset.map(input_columns="image", operations=compose())
for data in dataset.create_dict_iterator():
print(data["image"])
imgplot_resized = plt.imshow(data["image"].transpose(1, 2, 0))
plt.show()
```
The running result shows that the original image is changed from 1024 x 683 pixels to 500 x 500 pixels after data processing by using `RandomCrop()`.
![avatar](../images/image.png)
Figure 1: Original image
![avatar](../images/image_random_crop.png)
Figure 2: 500 x 500 image that is randomly cropped from the original image
# Loading the Dataset
<!-- TOC -->
- [Loading the Dataset](#loading-the-dataset)
- [Overview](#overview)
- [Loading Common Datasets](#loading-common-datasets)
- [Loading Datasets of a Specific Data Format](#loading-datasets-of-a-specific-data-format)
- [MindSpore Data Format](#mindspore-data-format)
- [`Manifest` Data Format](#manifest-data-format)
- [`TFRecord` Data Format](#tfrecord-data-format)
- [Loading a Custom Dataset](#loading-a-custom-dataset)
<!-- /TOC -->
## Overview
MindSpore helps you load common datasets, datasets of specific data formats, or custom datasets. Before loading a dataset, you need to import the required library `mindspore.dataset`.
```python
import mindspore.dataset as ds
```
## Loading Common Datasets
MindSpore can load common standard datasets. The following table lists the supported datasets:
| Dataset | Description |
| --------- | -------------------------------------------------------------------------------------------------------------------------- |
| ImageNet | An image database organized based on the WordNet hierarchical structure. Each node in the hierarchical structure is represented by hundreds of images. |
| MNIST | A large database of handwritten digit images, which is usually used to train various image processing systems. |
| CIFAR-10 | A collection of images that are commonly used to train machine learning and computer vision algorithms. The CIFAR-10 dataset contains 60,000 32x32 color images in 10 different classes. |
| CIFAR-100 | The dataset is similar to CIFAR-10. The difference is that this dataset has 100 classes, and each class contains 600 images, including 500 training images and 100 test images. |
| PASCAL-VOC | The data content is diversified and can be used to train computer vision models (such as classification, positioning, detection, segmentation, and action recognition). |
| CelebA | CelebA face dataset contains tens of thousands of face images of celebrities with 40 attribute annotations, which are usually used for face-related training tasks. |
The procedure for loading common datasets is as follows. The following describes how to create the `CIFAR-10` object to load supported datasets.
1. Download and decompress the [CIFAR-10 Dataset](https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz). The dataset in binary format (CIFAR-10 binary version) is used.
2. Configure the dataset directory and define the dataset instance to be loaded.
```python
DATA_DIR = "cifar10_dataset_dir/"
cifar10_dataset = ds.Cifar10Dataset(DATA_DIR)
```
3. Create an iterator and read data through the iterator.
```python
for data in cifar10_dataset.create_dict_iterator():
# In CIFAR-10 dataset, each dictionary of data has keys "image" and "label".
print(data["image"])
print(data["label"])
```
## Loading Datasets of a Specific Data Format
### MindSpore Data Format
MindSpore supports reading of datasets stored in MindSpore data format, that is, `MindRecord` which has better performance and features.
> For details about how to convert datasets to the MindSpore data format, see the [Converting the Dataset to MindSpore Data Format](converting_datasets.md).
To read a dataset using the `MindDataset` object, perform the following steps:
1. Create `MindDataset` for reading data.
```python
CV_FILE_NAME = os.path.join(MODULE_PATH, "./imagenet.mindrecord")
data_set = ds.MindDataset(dataset_file=CV_FILE_NAME)
```
In the preceding information:
`dataset_file`: specifies the MindRecord file, including the path and file name.
2. Create a dictionary iterator and read data records through the iterator.
```python
num_iter = 0
for data in data_set.create_dict_iterator():
print(data["label"])
num_iter += 1
```
### `Manifest` Data Format
`Manifest` is a data format file supported by Huawei ModelArts. For details, see <https://support.huaweicloud.com/engineers-modelarts/modelarts_23_0009.html>.
MindSpore provides dataset classes for datasets in Manifest format. Run the following commands to configure the dataset directory and define the dataset instance to be loaded:
```python
DATA_DIR = "manifest_dataset_path"
manifest_dataset = ds.ManifestDataset(DATA_DIR)
```
Currently, ManifestDataset supports only datasets of images and labels. The default column names are "image" and "label".
### `TFRecord` Data Format
MindSpore can also read datasets in the `TFRecord` data format through the `TFRecordDataset` object.
1. Input the dataset path or the .tfrecord file list to create the `TFRecordDataset`.
```python
DATA_DIR = ["tfrecord_dataset_path/train-0000-of-0001.tfrecord"]
dataset = ds.TFRecordDataset(DATA_DIR)
```
2. Create schema files or schema classes to set the dataset format and features.
The following is an example of the schema file:
```
{
"datasetType": "TF",
"numRows": 3,
"columns": {
"image": {
"type": "uint8",
"rank": 1
},
"label" : {
"type": "int64",
"rank": 1
}
}
}
```
In the preceding information:
`datasetType`: data format. TF indicates the TFRecord data format.
`columns`: column information field, which is defined based on the actual column names of the dataset. In the preceding schema file example, the dataset columns are image and label.
`numRows`: row information field, which controls the maximum number of rows for loading data. If the number of defined rows is greater than the actual number of rows, the actual number of rows prevails during loading.
When creating the TFRecordDataset, input the schema file path. An example is as follows:
```python
DATA_DIR = ["tfrecord_dataset_path/train-0000-of-0001.tfrecord"]
SCHEMA_DIR = "dataset_schema_path/schema.json"
dataset = ds.TFRecordDataset(DATA_DIR, schema=SCHEMA_DIR)
```
An example of creating a schema class is as follows:
```python
import mindspore.common.dtype as mstype
schema = ds.Schema()
schema.add_column('image', de_type=mstype.uint8) # Binary data usually use uint8 here.
schema.add_column('label', de_type=mstype.int32)
dataset = ds.TFRecordDataset(DATA_DIR, schema=schema)
```
3. Create a dictionary iterator and read data through the iterator.
```python
for data in dataset.create_dict_iterator():
# The dictionary of data has keys "image" and "label" which are consistent with columns names in its schema.
print(data["image"])
print(data["label"])
```
## Loading a Custom Dataset
You can load a custom dataset using the `GeneratorDataset` object.
1. Define a function (for example, `Generator1D`) to generate a dataset.
> The custom generation function returns the objects that can be called. Each time, tuples of `numpy array` are returned as a row of data.
An example of a custom function is as follows:
```python
import numpy as np # Import numpy lib.
def Generator1D():
for i in range(64):
yield (np.array([i]),) # Notice, tuple of only one element needs following a comma at the end.
```
2. Transfer `Generator1D` to `GeneratorDataset` to create a dataset and set `column` to data.
```python
dataset = ds.GeneratorDataset(Generator1D, ["data"])
```
3. After creating a dataset, create an iterator for the dataset to obtain the corresponding data. Iterator creation methods are as follows:
- Create an iterator whose return value is of the sequence type.
```python
for data in dataset.create_tuple_iterator(): # each data is a sequence
print(data[0])
```
- Create an iterator whose return value is of the dictionary type.
```python
for data in dataset.create_dict_iterator(): # each data is a dictionary
print(data["data"])
```
# Debugging in PyNative Mode
<!-- TOC -->
- [Debugging in PyNative Mode](#debugging-in-pynative-mode)
- [Overview](#overview)
- [Executing a Single Operator](#executing-a-single-operator)
- [Executing a Common Function](#executing-a-common-function)
- [Improving PyNative Performance](#improving-pynative-performance)
- [Debugging Network Train Model](#Debugging-network-train-model)
<!-- /TOC -->
## Overview
MindSpore supports the following running modes which are optimized in terms of debugging or running:
- PyNative mode: dynamic graph mode. In this mode, operators in the neural network are delivered and executed one by one, facilitating the compilation and debugging of the neural network model.
- Graph mode: static graph mode. In this mode, the neural network model is compiled into an entire graph and then delivered for execution. This mode uses technologies such as graph optimization to improve the running performance and facilitates large-scale deployment and cross-platform running.
By default, MindSpore is in PyNative mode. You can switch it to the graph mode by calling `context.set_context(mode=context.GRAPH_MODE)`. Similarly, MindSpore in graph mode can be switched to the PyNative mode through `context.set_context(mode=context.PYNATIVE_MODE)`.
In PyNative mode, single operators, common functions, network inference, and separated gradient calculation can be executed. The following describes the usage and precautions.
## Executing a Single Operator
Execute a single operator and output the result, as shown in the following example.
```python
import numpy as np
import mindspore.nn as nn
from mindspore import context, Tensor
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
conv = nn.Conv2d(3, 4, 3, bias_init='zeros')
input_data = Tensor(np.ones([1, 3, 5, 5]).astype(np.float32))
output = conv(input_data)
print(output.asnumpy())
```
Output:
```python
[[[[-0.02190447 -0.05208071 -0.05208071 -0.05208071 -0.06265172]
[-0.01529094 -0.05286242 -0.05286242 -0.05286242 -0.04228776]
[-0.01529094 -0.05286242 -0.05286242 -0.05286242 -0.04228776]
[-0.01529094 -0.05286242 -0.05286242 -0.05286242 -0.04228776]
[-0.01430791 -0.04892948 -0.04892948 -0.04892948 -0.01096004]]
[[ 0.00802889 -0.00229866 -0.00229866 -0.00229866 -0.00471579]
[ 0.01172971 0.02172665 0.02172665 0.02172665 0.03261888]
[ 0.01172971 0.02172665 0.02172665 0.02172665 0.03261888]
[ 0.01172971 0.02172665 0.02172665 0.02172665 0.03261888]
[ 0.01784375 0.01185635 0.01185635 0.01185635 0.01839031]]
[[ 0.04841832 0.03321705 0.03321705 0.03321705 0.0342317 ]
[ 0.0651359 0.04310361 0.04310361 0.04310361 0.03355784]
[ 0.0651359 0.04310361 0.04310361 0.04310361 0.03355784]
[ 0.0651359 0.04310361 0.04310361 0.04310361 0.03355784]
[ 0.04680437 0.03465693 0.03465693 0.03465693 0.00171057]]
[[-0.01783456 -0.00459451 -0.00459451 -0.00459451 0.02316688]
[ 0.01295831 0.00879035 0.00879035 0.00879035 0.01178642]
[ 0.01295831 0.00879035 0.00879035 0.00879035 0.01178642]
[ 0.01295831 0.00879035 0.00879035 0.00879035 0.01178642]
[ 0.05016355 0.03958241 0.03958241 0.03958241 0.03443141]]]]
```
## Executing a Common Function
Combine multiple operators into a function, call the function to execute the operators, and output the result, as shown in the following example:
**Example Code**
```python
import numpy as np
from mindspore import context, Tensor
from mindspore.ops import functional as F
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
def tensor_add_func(x, y):
z = F.tensor_add(x, y)
z = F.tensor_add(z, x)
return z
x = Tensor(np.ones([3, 3], dtype=np.float32))
y = Tensor(np.ones([3, 3], dtype=np.float32))
output = tensor_add_func(x, y)
print(output.asnumpy())
```
**Output**
```python
[[3. 3. 3.]
[3. 3. 3.]
[3. 3. 3.]]
```
### Improving PyNative Performance
MindSpore provides the staging function to improve the execution speed of inference tasks in PyNative mode. This function compiles Python functions or Python class methods into computational graphs in PyNative mode and improves the execution speed by using graph optimization technologies, as shown in the following example:
```python
import numpy as np
import numpy as np
import mindspore.nn as nn
from mindspore import context, Tensor
import mindspore.ops.operations as P
from mindspore.common.api import ms_function
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
class TensorAddNet(nn.Cell):
def __init__(self):
super(TensorAddNet, self).__init__()
self.add = P.TensorAdd()
@ms_function
def construct(self, x, y):
res = self.add(x, y)
return res
x = Tensor(np.ones([4, 4]).astype(np.float32))
y = Tensor(np.ones([4, 4]).astype(np.float32))
net = TensorAddNet()
z = net(x, y) # Staging mode
tensor_add = P.TensorAdd()
res = tensor_add(x, z) # PyNative mode
print(res.asnumpy())
```
**Output**
```python
[[3. 3. 3. 3.]
[3. 3. 3. 3.]
[3. 3. 3. 3.]
[3. 3. 3. 3.]]
```
In the preceding code, the `ms_function` decorator is added before `construct` of the `TensorAddNet` class. The decorator compiles the `construct` method into a computational graph. After the input is given, the graph is delivered and executed, `F.tensor_add` in the preceding code is executed in the common PyNative mode.
It should be noted that, in a function to which the `ms_function` decorator is added, if an operator (such as `pooling` or `tensor_add`) that does not need parameter training is included, the operator can be directly called in the decorated function, as shown in the following example:
**Example Code**
```python
import numpy as np
import mindspore.nn as nn
from mindspore import context, Tensor
import mindspore.ops.operations as P
from mindspore.common.api import ms_function
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
tensor_add = P.TensorAdd()
@ms_function
def tensor_add_fn(x, y):
res = tensor_add(x, y)
return res
x = Tensor(np.ones([4, 4]).astype(np.float32))
y = Tensor(np.ones([4, 4]).astype(np.float32))
z = tensor_add_fn(x, y)
print(z.asnumpy())
```
**Output**
```shell
[[2. 2. 2. 2.]
[2. 2. 2. 2.]
[2. 2. 2. 2.]
[2. 2. 2. 2.]]
```
If the decorated function contains operators (such as `Convolution` and `BatchNorm`) that require parameter training, these operators must be instantiated before the decorated function is called, as shown in the following example:
**Example Code**
```python
import numpy as np
import mindspore.nn as nn
from mindspore import context, Tensor
from mindspore.common.api import ms_function
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
conv_obj = nn.Conv2d(in_channels=3, out_channels=4, kernel_size=3, stride=2, padding=0)
@ms_function
def conv_fn(x):
res = conv_obj(x)
return res
input_data = np.random.randn(2, 3, 6, 6).astype(np.float32)
z = conv_fn(Tensor(input_data))
print(z.asnumpy())
```
**Output**
```shell
[[[[ 0.10377571 -0.0182163 -0.05221086]
[ 0.1428334 -0.01216263 0.03171652]
[-0.00673915 -0.01216291 0.02872104]]
[[ 0.02906547 -0.02333629 -0.0358406 ]
[ 0.03805163 -0.00589525 0.04790922]
[-0.01307234 -0.00916951 0.02396654]]
[[ 0.01477884 -0.06549098 -0.01571796]
[ 0.00526886 -0.09617482 0.04676902]
[-0.02132788 -0.04203424 0.04523344]]
[[ 0.04590619 -0.00251453 -0.00782715]
[ 0.06099087 -0.03445276 0.00022781]
[ 0.0563223 -0.04832596 -0.00948266]]]
[[[ 0.08444098 -0.05898955 -0.039262 ]
[ 0.08322686 -0.0074796 0.0411371 ]
[-0.02319113 0.02128408 -0.01493311]]
[[ 0.02473745 -0.02558945 -0.0337843 ]
[-0.03617039 -0.05027632 -0.04603915]
[ 0.03672804 0.00507637 -0.08433761]]
[[ 0.09628943 0.01895323 -0.02196114]
[ 0.04779419 -0.0871575 0.0055248 ]
[-0.04382382 -0.00511185 -0.01168541]]
[[ 0.0534859 0.02526264 0.04755395]
[-0.03438103 -0.05877855 0.06530266]
[ 0.0377498 -0.06117418 0.00546303]]]]
```
## Debugging Network Train Model
In PyNative mode, the gradient can be calculated separately. As shown in the following example, `grad_all` is used to calculate all input gradients of the function or the network.
**Example Code**
```python
from mindspore.ops import composite as C
import mindspore.context as context
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
def mul(x, y):
return x * y
def mainf(x, y):
return C.grad_all(mul)(x, y)
print(mainf(1,2))
```
**Output**
```python
(2, 1)
```
During network training, obtain the gradient, call the optimizer to optimize parameters (the breakpoint cannot be set during the reverse gradient calculation), and calculate the loss values. Then, network training is implemented in PyNative mode.
**Complete LeNet Sample Code**
```python
import numpy as np
import mindspore.nn as nn
import mindspore.ops.operations as P
from mindspore.nn import Dense
from mindspore import context, Tensor, ParameterTuple
from mindspore.common.initializer import TruncatedNormal
from mindspore.ops import composite as C
from mindspore.common import dtype as mstype
from mindspore.nn.wrap.cell_wrapper import WithLossCell
from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
from mindspore.nn.optim import Momentum
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
def conv(in_channels, out_channels, kernel_size, stride=1, padding=0):
"""weight initial for conv layer"""
weight = weight_variable()
return nn.Conv2d(in_channels, out_channels,
kernel_size=kernel_size, stride=stride, padding=padding,
weight_init=weight, has_bias=False, pad_mode="valid")
def fc_with_initialize(input_channels, out_channels):
"""weight initial for fc layer"""
weight = weight_variable()
bias = weight_variable()
return nn.Dense(input_channels, out_channels, weight, bias)
def weight_variable():
"""weight initial"""
return TruncatedNormal(0.02)
class LeNet5(nn.Cell):
"""
Lenet network
Args:
num_class (int): Num classes. Default: 10.
Returns:
Tensor, output tensor
Examples:
>>> LeNet(num_class=10)
"""
def __init__(self, num_class=10):
super(LeNet5, self).__init__()
self.num_class = num_class
self.batch_size = 32
self.conv1 = conv(1, 6, 5)
self.conv2 = conv(6, 16, 5)
self.fc1 = fc_with_initialize(16 * 5 * 5, 120)
self.fc2 = fc_with_initialize(120, 84)
self.fc3 = fc_with_initialize(84, self.num_class)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
self.reshape = P.Reshape()
def construct(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.conv2(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.reshape(x, (self.batch_size, -1))
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
return x
class GradWrap(nn.Cell):
""" GradWrap definition """
def __init__(self, network):
super(GradWrap, self).__init__(auto_prefix=False)
self.network = network
self.weights = ParameterTuple(filter(lambda x: x.requires_grad, network.get_parameters()))
def construct(self, x, label):
weights = self.weights
return C.grad_by_list(self.network, weights)(x, label)
net = LeNet5()
optimizer = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.1, 0.9)
criterion = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
net_with_criterion = WithLossCell(net, criterion)
train_network = GradWrap(net_with_criterion)
train_network.set_train()
input_data = Tensor(np.ones([net.batch_size, 1, 32, 32]).astype(np.float32) * 0.01)
label = Tensor(np.ones([net.batch_size]).astype(np.int32))
output = net(Tensor(input_data))
loss_output = criterion(output, label)
grads = train_network(input_data, label)
success = optimizer(grads)
loss = loss_output.asnumpy()
print(loss)
```
**Output**
```python
2.3050091
```
In the preceding execution, an intermediate result of network execution can be obtained at any required place in construct function, and the network can be debugged by using the Python Debugger (pdb).
\ No newline at end of file
# Saving and Loading Model Parameters
<!-- TOC -->
- [Saving and Loading Model Parameters](#saving-and-loading-model-parameters)
- [Overview](#overview)
- [Saving Model Parameters](#saving-model-parameters)
- [CheckPoint Configuration Policies](#checkpoint-configuration-policies)
- [Loading Model Parameters](#loading-model-parameters)
- [For Inference Validation](#for-inference-validation)
- [For Retraining](#for-retraining)
<!-- /TOC -->
## Overview
During model training, you can add CheckPoints to save model parameters for inference and retraining after interruption.
The application scenarios are as follows:
- Inference after training.
- After model training, save model parameters for inference or prediction.
- During training, through real-time accuracy validation, save the model parameters with the highest accuracy for prediction.
- Retraining.
- During a long-time training, save the generated CheckPoint files to prevent the training from starting from the beginning after it exits abnormally.
- Train a model, save parameters, and perform fine-tuning for different tasks.
A CheckPoint file of MindSpore is a binary file that stores the values of all training parameters. The Google Protocol Buffers mechanism with good scalability is adopted, which is independent of the development language and platform.
The protocol format of CheckPoints is defined in `mindspore/ccsrc/utils/checkpoint.proto`.
The following uses an example to describe the saving and loading functions of MindSpore. The ResNet-50 network and the MNIST dataset are selected.
## Saving Model Parameters
During model training, use the callback mechanism to transfer the object of the callback function `ModelCheckpoint` to save model parameters and generate CheckPoint files.
You can use the `CheckpointConfig` object to set the CheckPoint saving policies.
The saved parameters are classified into network parameters and optimizer parameters.
`ModelCheckpoint()` provides default configuration policies for users to quickly get started.
The following describes the usage:
```python
from mindspore.train.callback import ModelCheckpoint
ckpoint_cb = ModelCheckpoint()
model.train(epoch_num, dataset, callbacks=ckpoint_cb)
```
You can configure the CheckPoint policies as required.
The following describes the usage:
```python
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig
config_ck = CheckpointConfig(save_checkpoint_steps=32, keep_checkpoint_max=10)
ckpoint_cb = ModelCheckpoint(prefix='resnet50', directory=None, config=config_ck)
model.train(epoch_num, dataset, callbacks=ckpoint_cb)
```
In the preceding code, initialize a `TrainConfig` class object to set the saving policies.
`save_checkpoint_steps` indicates the saving frequency. That is, parameters are saved every specified number of steps. `keep_checkpoint_max` indicates the maximum number of CheckPoint files that can be saved.
`prefix` indicates the prefix name of the generated CheckPoint file. `directory` indicates the directory for storing the file.
Create a `ModelCheckpoint` object and transfer it to the model.train method. Then you can use the CheckPoint function during training.
Generated CheckPoint files are as follows:
> - resnet50-graph.meta # Generate compiled computation graph.
> - resnet50-1_32.ckpt # The file name extension is .ckpt.
> - resnet50-2_32.ckpt # The file name format contains the epoch and step correspond to the saved parameters.
> - resnet50-3_32.ckpt # The file name indicates that the model parameters generated during the 32th step of the third epoch are saved.
> - ...
If you use the same prefix and run the training script for multiple times, CheckPoint files with the same name may be generated.
MindSpore adds underscores (_) and digits at the end of the user-defined prefix to distinguish CheckPoints with the same name.
For example, `resnet50_3-2_32.ckpt` indicates the CheckPoint file generated during the 32th step of the second epoch after the script is executed for the third time.
### CheckPoint Configuration Policies
MindSpore provides two types of CheckPoint saving policies: iteration policy and time policy. You can create the `CheckpointConfig` object to set the corresponding policies.
CheckpointConfig contains the following four parameters:
- save_checkpoint_steps: indicates the step interval for saving a CheckPoint file. That is, parameters are saved every specified number of steps. The default value is 1.
- save_checkpoint_seconds: indicates the interval for saving a CheckPoint file. That is, parameters are saved every specified number of seconds. The default value is 0.
- keep_checkpoint_max: indicates the maximum number of CheckPoint files that can be saved. The default value is 5.
- keep_checkpoint_per_n_minutes: indicates the interval for saving a CheckPoint file. That is, parameters are saved every specified number of minutes. The default value is 0.
`save_checkpoint_steps` and `keep_checkpoint_max` are iteration policies, which can be configured based on the number of training iterations.
`save_checkpoint_seconds` and `keep_checkpoint_per_n_minutes` are time policies, which can be configured during training.
The two types of policies cannot be used together. Iteration policies have a higher priority than time policies. When the two types of policies are configured at the same time, only iteration policies take effect.
If a parameter is set to None, the related policy is canceled.
After the training script is normally executed, the CheckPoint file generated during the last step is saved by default.
## Loading Model Parameters
After saving CheckPoint files, you can load parameters.
### For Inference Validation
In inference-only scenarios, use `load_checkpoint` to directly load parameters to the network for subsequent inference validation.
The sample code is as follows:
```python
resnet = ResNet50()
load_checkpoint("resnet50-2_32.ckpt", net=resnet)
dateset_eval = create_dataset(os.path.join(mnist_path, "test"), 32, 1) # define the test dataset
loss = CrossEntropyLoss()
model = Model(resnet, loss)
acc = model.eval(dataset_eval)
```
The `load_checkpoint` method loads network parameters in the parameter file to the model. After the loading, parameters in the network are those saved in CheckPoints.
The `eval` method validates the accuracy of the trained model.
### For Retraining
In the retraining after task interruption and fine-tuning scenarios, you can load network parameters and optimizer parameters to the model.
The sample code is as follows:
```python
# return a parameter dict for model
param_dict = load_checkpoint("resnet50-2_32.ckpt")
resnet = ResNet50()
opt = Momentum()
# load the parameter into net
load_param_into_net(resnet, param_dict)
# load the parameter into operator
load_param_into_net(opt, param_dict)
loss = SoftmaxCrossEntropyWithLogits()
model = Model(resnet, loss, opt)
model.train(epoch, dataset)
```
The `load_checkpoint` method returns a parameter dictionary and then the `load_param_into_net` method loads parameters in the parameter dictionary to the network or optimizer.
如何参与社区
===============
贡献代码
-----------
如何贡献代码,请参见链接 https://gitee.com/mindspore/mindspore/blob/master/CONTRIBUTING.md 。
贡献文档
-----------
如何贡献文档,请参见链接 https://gitee.com/mindspore/docs/blob/master/CONTRIBUTING_DOC.md 。
\ No newline at end of file
# 计算机视觉应用
<!-- TOC -->
- [计算机视觉应用](#计算机视觉应用)
- [概述](#概述)
- [图像分类](#图像分类)
- [任务描述及准备](#任务描述及准备)
- [下载CIFAR-10数据集](#下载cifar-10数据集)
- [数据预加载和预处理](#数据预加载和预处理)
- [定义卷积神经网络](#定义卷积神经网络)
- [定义损失函数和优化器](#定义损失函数和优化器)
- [调用`Model`高阶API进行训练和保存模型文件](#调用model高阶api进行训练和保存模型文件)
- [加载保存的模型,并进行验证](#加载保存的模型并进行验证)
- [参考文献](#参考文献)
<!-- /TOC -->
## 概述
计算机视觉是当前深度学习研究最广泛、落地最成熟的技术领域,在手机拍照、智能安防、自动驾驶等场景有广泛应用。从2012年AlexNet在ImageNet比赛夺冠以来,深度学习深刻推动了计算机视觉领域的发展,当前最先进的计算机视觉算法几乎都是深度学习相关的。深度神经网络可以逐层提取图像特征,并保持局部不变性,被广泛应用于分类、检测、分割、跟踪、检索、识别、提升、重建等视觉任务中。
本章结合图像分类任务,介绍MindSpore如何应用于计算机视觉场景。
## 图像分类
图像分类是最基础的计算机视觉应用,属于有监督学习类别。给定一张数字图像,判断图像所属的类别,如猫、狗、飞机、汽车等等。用函数来表示这个过程如下:
```python
def classify(image):
label = model(image)
return label
```
选择合适的model是关键。这里的model一般指的是深度卷积神经网络,如AlexNet、VGG、GoogLeNet、ResNet等等。
MindSpore预置了典型的卷积神经网络,开发者可以直接使用,如LeNet,使用方式如下:
```python
from mindspore.model_zoo.lenet import LeNet5
network = LeNet(num_classes)
```
MindSpore当前支持的图像分类网络包括:典型网络LeNet、AlexNet、ResNet。
## 任务描述及准备
![cifar10](images/cifar10.jpg)
图1:CIFAR-10数据集[1]
如图1所示,CIFAR-10数据集共包含10类、共60000张图片。其中,每类图片6000张,50000张是训练集,10000张是测试集。每张图片大小为32*32。
图像分类的训练指标通常是精度(Accuracy),即正确预测的样本数占总预测样本数的比值。
接下来我们介绍利用MindSpore解决图片分类任务,整体流程如下:
1. 下载CIFAR-10数据集
2. 数据加载和预处理
3. 定义卷积神经网络,本例采用ResNet-50网络
4. 定义损失函数和优化器
5. 调用`Model`高阶API进行训练和保存模型文件
6. 加载保存的模型进行推理
> 本例面向Ascend 910 AI处理器硬件平台,样例的完整代码下载<https://gitee.com/mindspore/docs/blob/r0.1/tutorials/tutorial_code/resnet>
下面对任务流程中各个环节及代码关键片段进行解释说明。
## 下载CIFAR-10数据集
先从[CIFAR-10数据集官网](https://www.cs.toronto.edu/~kriz/cifar.html)上下载CIFAR-10数据集。本例中采用binary格式的数据,Linux环境可以通过下面的命令下载:
```shell
wget https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz
```
接下来需要解压数据集,解压命令如下:
```shell
tar -zvxf cifar-10-binary.tar.gz
```
### 数据预加载和预处理
1. 加载数据集
数据加载可以通过内置数据集格式`Cifar10Dataset`接口完成。
> `Cifar10Dataset`,读取类型为随机读取,内置CIFAR-10数据集,包含图像和标签,图像格式默认为uint8,标签数据格式默认为uint32。更多说明请查看API中`Cifar10Dataset`接口说明。
数据加载代码如下,其中`data_home`为数据存储位置:
```python
cifar_ds = ds.Cifar10Dataset(data_home)
```
2. 数据增强
数据增强主要是对数据进行归一化和丰富数据样本数量。常见的数据增强方式包括裁剪、翻转、色彩变化等等。MindSpore通过调用`map`方法在图片上执行增强操作:
```python
resize_height = 224
resize_width = 224
rescale = 1.0 / 255.0
shift = 0.0
# define map operations
random_crop_op = C.RandomCrop((32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT
random_horizontal_op = C.RandomHorizontalFlip()
resize_op = C.Resize((resize_height, resize_width)) # interpolation default BILINEAR
rescale_op = C.Rescale(rescale, shift)
normalize_op = C.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
changeswap_op = C.HWC2CHW()
type_cast_op = C2.TypeCast(mstype.int32)
c_trans = []
if training:
c_trans = [random_crop_op, random_horizontal_op]
c_trans += [resize_op, rescale_op, normalize_op, changeswap_op]
# apply map operations on images
cifar_ds = cifar_ds.map(input_columns="label", operations=type_cast_op)
cifar_ds = cifar_ds.map(input_columns="image", operations=c_trans)
```
3. 数据混洗和批处理
最后通过数据混洗(shuffle)随机打乱数据的顺序,并按batch读取数据,进行模型训练:
```python
# apply repeat operations
cifar_ds = cifar_ds.repeat(repeat_num)
# apply shuffle operations
cifar_ds = cifar_ds.shuffle(buffer_size=10)
# apply batch operations
cifar_ds = cifar_ds.batch(batch_size=args_opt.batch_size, drop_remainder=True)
```
### 定义卷积神经网络
卷积神经网络已经是图像分类任务的标准算法了。卷积神经网络采用分层的结构对图片进行特征提取,由一系列的网络层堆叠而成,比如卷积层、池化层、激活层等等。
ResNet通常是较好的选择。首先,它足够深,常见的有34层,50层,101层。通常层次越深,表征能力越强,分类准确率越高。其次,可学习,采用了残差结构,通过shortcut连接把低层直接跟高层相连,解决了反向传播过程中因为网络太深造成的梯度消失问题。此外,ResNet网络的性能很好,既表现为识别的准确率,也包括它本身模型的大小和参数量。
MindSpore Model Zoo中已经内置了ResNet模型,可以采用ResNet-50网络,调用方法如下:
```python
from mindspore.model_zoo.resnet import resnet50
network = resnet50(class_num=10)
```
更多ResNet的介绍请参考:[ResNet论文](https://arxiv.org/abs/1512.03385)
### 定义损失函数和优化器
接下来需要定义损失函数(Loss)和优化器(Optimizer)。损失函数是深度学习的训练目标,也叫目标函数,可以理解为神经网络的输出(Logits)和标签(Labels)之间的距离,是一个标量数据。
常见的损失函数包括均方误差、L2损失、Hinge损失、交叉熵等等。图像分类应用通常采用交叉熵损失(CrossEntropy)。
优化器用于神经网络求解(训练)。由于神经网络参数规模庞大,无法直接求解,因而深度学习中采用随机梯度下降算法(SGD)及其改进算法进行求解。MindSpore封装了常见的优化器,如`SGD``ADAM``Momemtum`等等。本例采用`Momentum`优化器,通常需要设定两个参数,动量(`moment`)和权重衰减项(`weight decay`)。
MindSpore中定义损失函数和优化器的代码样例如下:
```python
# loss function definition
ls = SoftmaxCrossEntropyWithLogits(sparse=True, is_grad=False, reduction="mean")
# optimization definition
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9)
```
### 调用`Model`高阶API进行训练和保存模型文件
完成数据预处理、网络定义、损失函数和优化器定义之后,就可以进行模型训练了。模型训练包含两层迭代,数据集的多轮迭代(epoch)和一轮数据集内按分组(batch)大小进行的单步迭代。其中,单步迭代指的是按分组从数据集中抽取数据,输入到网络中计算得到损失函数,然后通过优化器计算和更新训练参数的梯度。
为了简化训练过程,MindSpore封装了`Model`高阶接口。用户输入网络、损失函数和优化器完成`Model`的初始化,然后调用`train`接口进行训练,`train`接口参数包括迭代次数(`epoch`)和数据集(`dataset`)。
模型保存是对训练参数进行持久化的过程。`Model`类中通过回调函数(callback)的方式进行模型保存,如下面代码所示。用户通过`CheckpointConfig`设置回调函数的参数,其中,`save_checkpoint_steps`指每经过固定的单步迭代次数保存一次模型,`keep_checkpoint_max`指最多保存的模型个数。
```python
'''
network, loss, optimizer are defined before.
batch_num, epoch_size are training parameters.
'''
model = Model(net, loss_fn=ls, optimizer=opt, metrics={'acc'})
# CheckPoint CallBack definition
config_ck = CheckpointConfig(save_checkpoint_steps=batch_num, keep_checkpoint_max=35)
ckpoint_cb = ModelCheckpoint(prefix="train_resnet_cifar10", directory="./", config=config_ck)
# LossMonitor is used to print loss value on screen
loss_cb = LossMonitor()
model.train(epoch_size, dataset, callbacks=[ckpoint_cb, loss_cb])
```
### 加载保存的模型,并进行验证
训练得到的模型文件(如resnet.ckpt)可以用来预测新图像的类别。首先通过`load_checkpoint`加载模型文件。然后调用`Model``eval`接口预测新图像类别。
```python
param_dict = load_checkpoint(args_opt.checkpoint_path)
load_param_into_net(net, param_dict)
eval_dataset = create_dataset(1, training=False)
res = model.eval(eval_dataset)
print("result: ", res)
```
## 参考文献
[1] https://www.cs.toronto.edu/~kriz/cifar.html
# 自定义调试信息
<!-- TOC -->
- [自定义调试信息](#自定义调试信息)
- [概述](#概述)
- [Callback介绍](#callback介绍)
- [MindSpore的Callback能力](#mindspore的callback能力)
- [自定义Callback](#自定义callback)
- [MindSpore metrics功能介绍](#mindspore-metrics功能介绍)
- [print算子功能介绍](#print算子功能介绍)
- [日志相关的环境变量和配置](#日志相关的环境变量和配置)
<!-- /TOC -->
## 概述
本文介绍如何使用MindSpore提供的Callback、metrics、print算子、日志打印等自定义能力,帮助用户快速调试训练网络。
## Callback介绍
Callback是回调函数的意思,但它其实不是一个函数而是一个类,用户可以使用回调函数来观察训练过程中网络内部的状态和相关信息,或在特定时期执行特定动作。
例如监控loss、保存模型参数、动态调整参数、提前终止训练任务等。
### MindSpore的Callback能力
MindSpore提供Callback能力,支持用户在训练/推理的特定阶段,插入自定义的操作。包括:
- MindSpore框架提供的ModelCheckpoint、LossMonitor、SummaryStep等Callback函数。
- MindSpore支持用户自定义Callback。
使用方法:在model.train方法中传入Callback对象,它可以是一个Callback列表,例:
```python
ckpt_cb = ModelCheckpoint()
loss_cb = LossMonitor()
summary_cb = SummaryStep()
model.train(epoch, dataset, callbacks=[ckpt_cb, loss_cb, summary_cb])
```
ModelCheckpoint可以保存模型参数,以便进行再训练或推理。
LossMonitor可以在日志中输出loss,方便用户查看,同时它还会监控训练过程中的loss值变化情况,当loss值为`Nan``Inf`时终止训练。
SummaryStep可以把训练过程中的信息存储到文件中,以便后续进行查看或可视化展示。
### 自定义Callback
用户可以基于Callback基类,根据自身的需求,实现自定义Callback。
Callback基类定义如下所示:
```python
class Callback():
"""Callback base class"""
def begin(self, run_context):
"""Called once before the network executing."""
pass
def epoch_begin(self, run_context):
"""Called before each epoch beginning."""
pass
def epoch_end(self, run_context):
"""Called after each epoch finished."""
pass
def step_begin(self, run_context):
"""Called before each epoch beginning."""
pass
def step_end(self, run_context):
"""Called after each step finished."""
pass
def end(self, run_context):
"""Called once after network training."""
pass
```
Callback可以把训练过程中的重要信息记录下来,通过一个字典类型变量`cb_params`传递给Callback对象,
用户可以在各个自定义的Callback中获取到相关属性,执行自定义操作。也可以自定义其他变量传递给`cb_params`对象。
`cb_params`中的主要属性包括:
- loss_fn:损失函数
- optimizer:优化器
- train_dataset:训练的数据集
- cur_epoch_num:当前的epoch数
- cur_step_num:当前的step数
- batch_num:一个epoch中step的数量
- ...
用户可以继承Callback基类自定义Callback对象。
下面通过介绍一个例子,更深一步地了解自定义Callback的用法。
```python
class StopAtTime(Callback):
def __init__(self, run_time):
super(StopAtTime, self).__init__()
self.run_time = run_time*60
def begin(self, run_context):
cb_params = run_context.original_args()
cb_params.init_time = time.time()
def step_end(self, run_context):
cb_params = run_context.original_args()
epoch_num = cb_params.cur_epoch_num
step_num = cb_params.cur_step_num
loss = cb_params.cb_params
cur_time = time.time()
if (cur_time - cb_params.init_time) > self.run_time:
print("epoch: ", epoch_num, " step: ", step_num, " loss: ", loss)
run_context.request_stop()
stop_cb = StopAtTime(run_time=10)
model.train(100, dataset, callbacks=stop_cb)
```
输出:
```
epoch: 20 step: 32 loss: 2.298344373703003
```
此Callback的功能为:在规定时间内终止训练。通过`run_context.original_args()`方法可以获取到`cb_params`字典,字典里会包含前文描述的主要属性信息。
同时可以对字典内的值进行修改和添加,上述用例中,在`begin()`中定义一个`init_time`对象传递给`cb_params`字典。
在每次`step_end`会做出判断,当训练时间大于设置的时间阈值时,会向`run_context`传递终止训练的信号,提前终止训练,并打印当前的epoch、step、loss的值。
## MindSpore metrics功能介绍
当训练结束后,可以使用metrics评估训练结果的好坏。
MindSpore提供了多种metrics评估指标,如:`accuracy``loss``precision``recall``F1`
用户可以定义一个metrics字典对象,里面包含多种指标,传递给`model.eval`接口用来验证训练精度。
```python
metrics = {
'accuracy': nn.Accuracy(),
'loss': nn.Loss(),
'precision': nn.Precision(),
'recall': nn.Recall(),
'f1_score': nn.F1()
}
net = ResNet()
loss = CrossEntropyLoss()
opt = Momentum()
model = Model(net, loss_fn=loss, optimizer=opt, metrics=metrics)
ds_eval = create_dataset()
output = model.eval(ds_eval)
```
`model.eval()`方法会返回一个字典,里面是传入metrics的指标和结果。
用户也可以定义自己的metrics类,通过继承`Metric`基类,并重写`clear``update``eval`三个方法即可实现。
`accuracy`算子举例说明其内部实现原理:
`accuracy`继承了`EvaluationBase`基类,重写了上述三个方法。
`clear()`方法会把类中相关计算参数初始化。
`update()`方法接受预测值和标签值,更新accuracy内部变量。
`eval()`方法会计算相关指标,返回计算结果。
调用`accuracy``eval`方法,即可得到计算结果。
通过如下代码可以更清楚了解到`accuracy`是如何运行的:
```python
x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
y = Tensor(np.array([1, 0, 1]))
metric = Accuracy()
metric.clear()
metric.update(x, y)
accuracy = metric.eval()
print('Accuracy is ', accuracy)
```
输出:
```
Accuracy is 0.6667
```
## print算子功能介绍
MindSpore的自研print算子可以将用户输入的Tensor或字符串信息打印出来,支持多字符串输入,多Tensor输入和字符串与Tensor的混合输入,输入参数以逗号隔开。
print算子使用方法与其他算子相同,在网络中的`__init__`()声明算子并在`construct()`进行调用,具体使用实例及输出结果如下:
```python
import numpy as np
from mindspore import Tensor
from mindspore.ops import operations as P
import mindspore.nn as nn
import mindspore.context as context
context.set_context(mode=context.GRAPH_MODE)
class PrintDemo(nn.Cell):
def __init__(self):
super(PrintDemo, self).__init__()
self.print = P.Print()
def construct(self, x, y):
self.print('print Tensor x and Tensor y:', x, y)
return x
x = Tensor(np.ones([2, 1]).astype(np.int32))
y = Tensor(np.ones([2, 2]).astype(np.int32))
net = PrintDemo()
output = net(x, y)
```
输出:
```
print Tensor x and Tensor y:
Tensor shape:[[const vector][2, 1]]Int32
val:[[1]
[1]]
Tensor shape:[[const vector][2, 2]]Int32
val:[[1 1]
[1 1]]
```
## 日志相关的环境变量和配置
MindSpore采用glog来输出日志,常用的几个环境变量如下:
1. GLOG_v 控制日志的级别,默认值为2,即WARNING级别,对应关系如下:0-DEBUG、1-INFO、2-WARNING、3-ERROR。
2. GLOG_logtostderr 值设置为1时,日志输出到屏幕;值设置为0时,日志输出到文件。默认值为1。
3. GLOG_log_dir=YourPath 指定日志输出的路径。若指定了GLOG_log_dir且GLOG_logtostderr的值为1时,则日志输出到屏幕,不输出到文件。
# 分布式并行训练
<!-- TOC -->
- [分布式并行训练](#分布式并行训练)
- [概述](#概述)
- [准备环节](#准备环节)
- [配置分布式环境变量](#配置分布式环境变量)
- [调用集合通信库](#调用集合通信库)
- [加载数据集](#加载数据集)
- [定义网络](#定义网络)
- [定义损失函数及优化器](#定义损失函数及优化器)
- [定义损失函数](#定义损失函数)
- [定义优化器](#定义优化器)
- [训练网络](#训练网络)
- [运行测试用例](#运行测试用例)
<!-- /TOC -->
## 概述
MindSpore支持数据并行及自动并行。自动并行是MindSpore融合了数据并行、模型并行及混合并行的一种分布式并行模式,可以自动建立代价模型,为用户选择一种并行模式。
其中:
- 数据并行(Data Parallel):对数据batch维度切分的一种并行模式。
- 模型并行(Layerwise Parallel):对参数channel维度切分的一种并行模式。
- 混合并行(Hybrid Parallel):涵盖数据并行和模型并行的一种并行模式。
- 代价模型(Cost Model):同时考虑内存的计算代价和通信代价对训练时间建模,并设计了高效的算法来找到训练时间较短的并行策略。
本篇教程我们主要了解如何在MindSpore上通过数据并行及自动并行模式训练ResNet-50网络。
样例代码请参考 <https://gitee.com/mindspore/docs/blob/r0.1/tutorials/tutorial_code/distributed_training/resnet50_distributed_training.py>
> 当前样例面向Ascend AI处理器。
## 准备环节
### 配置分布式环境变量
在实验室环境进行分布式训练时,需要配置当前多卡环境的组网信息文件。如果使用华为云环境,可以跳过本小节。
以Ascend 910 AI处理器、1980 AIServer为例,一个两卡环境的json配置文件示例如下,本样例将该配置文件命名为rank_table.json。
```json
{
"board_id": "0x0000",
"chip_info": "910",
"deploy_mode": "lab",
"group_count": "1",
"group_list": [
{
"device_num": "2",
"server_num": "1",
"group_name": "",
"instance_count": "2",
"instance_list": [
{"devices":[{"device_id":"0","device_ip":"192.1.27.6"}],"rank_id":"0","server_id":"10.155.111.140"},
{"devices":[{"device_id":"1","device_ip":"192.2.27.6"}],"rank_id":"1","server_id":"10.155.111.140"}
]
}
],
"para_plane_nic_location": "device",
"para_plane_nic_name": [
"eth0", "eth1"
],
"para_plane_nic_num": "2",
"status": "completed"
}
```
其中需要根据实际训练环境修改的参数项有:
1. `server_num`表示机器数量, `server_id`表示本机IP地址。
2. `device_num``para_plane_nic_num``instance_count`表示卡的数量。
3. `rank_id`表示卡逻辑序号,固定从0开始编号,`device_id`表示卡物理序号,即卡所在机器中的实际序号。
4. `device_ip`表示网卡IP地址,可以在当前机器执行指令`cat /etc/hccn.conf`获取网卡IP地址。
5. `para_plane_nic_name`对应网卡名称。
组网信息文件准备好后,将文件路径加入环境变量`MINDSPORE_HCCL_CONFIG_PATH`中。此外需要将`device_id`信息传入脚本中,本样例通过配置环境变量DEVICE_ID的方式传入。
```bash
export MINDSPORE_HCCL_CONFIG_PATH="./rank_table.json"
export DEVICE_ID=0
```
### 调用集合通信库
我们需要在`context.set_context()`接口中使能分布式接口`enable_hccl`,设置`device_id`参数,并通过调用`init()`完成初始化操作。
在样例中,我们指定运行时使用图模式,在Ascend AI处理器上,使用华为集合通信库`Huawei Collective Communication Library`(以下简称HCCL)。
```python
import os
from mindspore import context
from mindspore.communication.management import init
if __name__ == "__main__":
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", enable_hccl=True, device_id=int(os.environ["DEVICE_ID"]))
init()
...
```
`mindspore.communication.management`中封装了HCCL提供的集合通信接口,方便用户获取分布式信息。常用的包括`get_rank``get_group_size`,分别对应当前设备在集群中的ID和集群数量。
> HCCL实现了基于Davinci架构芯片的多机多卡通信。当前使用分布式服务存在如下约束:
> 1. 单机场景下支持1、2、4、8卡设备集群,多机场景下支持8*n卡设备集群。
> 2. 每台机器的0-3卡和4-7卡各为一个组网,2卡和4卡训练时网卡必须相连且不支持跨组网创建集群。
> 3. 操作系统需使用SMP (symmetric multiprocessing)处理模式。
## 加载数据集
分布式训练时,数据是以数据并行的方式导入的。下面我们以Cifar10Dataset为例,介绍以数据并行方式导入CIFAR-10数据集的方法,`data_path`是指数据集的路径。
与单机不同的是,在数据集接口需要传入`num_shards``shard_id`参数,分别对应网卡数量和逻辑序号,建议通过HCCL接口获取。
```python
import mindspore.common.dtype as mstype
import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.transforms.vision.c_transforms as vision
from mindspore.communication.management import get_rank, get_group_size
def create_dataset(repeat_num=1, batch_size=32, rank_id=0, rank_size=1):
resize_height = 224
resize_width = 224
rescale = 1.0 / 255.0
shift = 0.0
# get rank_id and rank_size
rank_id = get_rank()
rank_size = get_group_size()
data_set = ds.Cifar10Dataset(data_path, num_shards=rank_size, shard_id=rank_id)
# define map operations
random_crop_op = vision.RandomCrop((32, 32), (4, 4, 4, 4))
random_horizontal_op = vision.RandomHorizontalFlip()
resize_op = vision.Resize((resize_height, resize_width))
rescale_op = vision.Rescale(rescale, shift)
normalize_op = vision.Normalize((0.4465, 0.4822, 0.4914), (0.2010, 0.1994, 0.2023))
changeswap_op = vision.HWC2CHW()
type_cast_op = C.TypeCast(mstype.int32)
c_trans = [random_crop_op, random_horizontal_op]
c_trans += [resize_op, rescale_op, normalize_op, changeswap_op]
# apply map operations on images
data_set = data_set.map(input_columns="label", operations=type_cast_op)
data_set = data_set.map(input_columns="image", operations=c_trans)
# apply repeat operations
data_set = data_set.repeat(repeat_num)
# apply shuffle operations
data_set = data_set.shuffle(buffer_size=10)
# apply batch operations
data_set = data_set.batch(batch_size=batch_size, drop_remainder=True)
return data_set
```
## 定义网络
数据并行及自动并行模式下,网络定义方式与单机一致。代码请参考: <https://gitee.com/mindspore/docs/blob/r0.1/tutorials/tutorial_code/resnet/resnet.py>
## 定义损失函数及优化器
### 定义损失函数
在Loss部分,我们采用SoftmaxCrossEntropyWithLogits的展开形式,即按照数学公式,将其展开为多个小算子进行实现。
相较于融合loss,自动并行以展开loss中的算子为粒度,通过算法搜索得到最优并行策略。
```python
from mindspore.ops import operations as P
from mindspore import Tensor
import mindspore.ops.functional as F
import mindspore.common.dtype as mstype
import mindspore.nn as nn
class SoftmaxCrossEntropyExpand(nn.Cell):
def __init__(self, sparse=False):
super(SoftmaxCrossEntropyExpand, self).__init__()
self.exp = P.Exp()
self.sum = P.ReduceSum(keep_dims=True)
self.onehot = P.OneHot()
self.on_value = Tensor(1.0, mstype.float32)
self.off_value = Tensor(0.0, mstype.float32)
self.div = P.Div()
self.log = P.Log()
self.sum_cross_entropy = P.ReduceSum(keep_dims=False)
self.mul = P.Mul()
self.mul2 = P.Mul()
self.mean = P.ReduceMean(keep_dims=False)
self.sparse = sparse
self.max = P.ReduceMax(keep_dims=True)
self.sub = P.Sub()
def construct(self, logit, label):
logit_max = self.max(logit, -1)
exp = self.exp(self.sub(logit, logit_max))
exp_sum = self.sum(exp, -1)
softmax_result = self.div(exp, exp_sum)
if self.sparse:
label = self.onehot(label, F.shape(logit)[1], self.on_value, self.off_value)
softmax_result_log = self.log(softmax_result)
loss = self.sum_cross_entropy((self.mul(softmax_result_log, label)), -1)
loss = self.mul2(F.scalar_to_array(-1.0), loss)
loss = self.mean(loss, -1)
return loss
```
### 定义优化器
采用`Momentum`优化器作为参数更新工具,这里定义与单机一致。
```python
from mindspore.nn.optim.momentum import Momentum
lr = 0.01
momentum = 0.9
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr, momentum)
```
## 训练网络
`context.set_auto_parallel_context()`是提供给用户设置并行参数的接口。主要参数包括:
- `parallel_mode`:分布式并行模式。可选数据并行`ParallelMode.DATA_PARALLEL`及自动并行`ParallelMode.AUTO_PARALLEL`
- `mirror_mean`: 反向计算时,框架内部会将数据并行参数分散在多台机器的梯度进行收集,得到全局梯度值后再传入优化器中更新。
设置为True对应`allreduce_mean`操作,False对应`allreduce_sum`操作。
在下面的样例中我们指定并行模式为自动并行,其中`dataset_sink_mode=False`表示采用数据非下沉模式,`LossMonitor`能够通过回调函数返回loss值。
```python
from mindspore.nn.optim.momentum import Momentum
from mindspore.train.callback import LossMonitor
from mindspore.train.model import Model, ParallelMode
from resnet import resnet50
def test_train_cifar(num_classes=10, epoch_size=10):
context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL, mirror_mean=True)
loss_cb = LossMonitor()
dataset = create_dataset(epoch_size)
net = resnet50(32, num_classes)
loss = SoftmaxCrossEntropyExpand(sparse=True)
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9)
model = Model(net, loss_fn=loss, optimizer=opt)
model.train(epoch_size, dataset, callbacks=[loss_cb], dataset_sink_mode=False)
```
## 运行测试用例
目前MindSpore分布式执行采用单卡单进程运行方式,进程数量应当与卡的使用数量保持一致。每个进程创建一个目录,用来保存日志信息以及算子编译信息。下面以一个2卡分布式训练的运行脚本为例:
```bash
#!/bin/bash
export MINDSPORE_HCCL_CONFIG_PATH=./rank_table.json
export RANK_SIZE=2
for((i=0;i<$RANK_SIZE;i++))
do
mkdir device$i
cp ./resnet50_distributed_training.py ./device$i
cd ./device$i
export RANK_ID=$i
export DEVICE_ID=$i
echo "start training for device $i"
env > env$i.log
pytest -s -v ./resnet50_distributed_training.py > log$i 2>&1 &
cd ../
done
```
# 混合精度
<!-- TOC -->
- [混合精度](#混合精度)
- [概述](#概述)
- [计算流程](#计算流程)
- [自动混合精度](#自动混合精度)
- [手动混合精度](#手动混合精度)
<!-- /TOC -->
## 概述
混合精度训练方法通过混合使用单精度和半精度数据格式来加速深度神经网络训练过程,同时保持了单精度训练所能达到的网络精度。混合精度训练能够加速计算过程,同时减少内存使用和存取,并在特定的硬件上可以训练更大的模型或batch size。
## 计算流程
MindSpore混合精度典型的计算流程如下图所示:
![mix precision](./images/mix_precision.jpg)
1. 参数以FP32存储;
2. 正向计算过程中,遇到FP16算子,需要把算子输入和参数从FP32 cast成FP16进行计算;
3. 将Loss层设置为FP32进行计算;
4. 反向计算过程中,首先乘以Loss Scale值,避免反向梯度过小而产生下溢;
5. FP16参数参与梯度计算,其结果将被cast回FP32;
6. 除以Loss scale值,还原被放大的梯度;
7. 判断梯度是否存在溢出,如果溢出则跳过更新,否则优化器以FP32对原始参数进行更新。
本文通过自动混合精度和手动混合精度的样例来讲解计算流程。
## 自动混合精度
使用自动混合精度,需要调用相应的接口,将待训练网络和优化器作为输入传进去;该接口会将整张网络的算子转换成FP16算子(除BatchNorm算子和Loss涉及到的算子外)。
另外要注意:使用混合精度后,一般要用上Loss Scale,避免数值计算溢出。
具体的实现步骤为:
1. 引入MindSpore的混合精度的接口amp;
2. 定义网络:该步骤和普通的网络定义没有区别(无需手动配置某个算子的精度);
3. 使用amp.build_train_network()接口封装网络模型和优化器,在该步骤中MindSpore会将有需要的算子自动进行类型转换。
代码样例如下:
```python
# The interface of Auto_mixed precision
from mindspore.train import amp
# Define network
class LeNet5(nn.Cell):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
self.fc1 = nn.Dense(16 * 5 * 5, 120)
self.fc2 = nn.Dense(120, 84)
self.fc3 = nn.Dense(84, 10)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2)
self.flatten = P.Flatten()
def construct(self, x):
x = self.max_pool2d(self.relu(self.conv1(x)))
x = self.max_pool2d(self.relu(self.conv2(x)))
x = self.flatten(x)
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
# Initialize network
net = LeNet5()
# Define training data, label and sens
predict = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32) * 0.01)
label = Tensor(np.zeros([1, 10]).astype(np.float32))
scaling_sens = Tensor(np.full((1), 1.0), dtype=mstype.float32)
# Define Loss and Optimizer
net.set_train()
loss = MSELoss()
optimizer = Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
net_with_loss = WithLossCell(net, loss)
train_network = amp.build_train_network(net_with_loss, optimizer, level="O2")
# Run training
output = train_network(inputs, label, scaling_sens)
```
## 手动混合精度
MindSpore还支持手动混合精度。假定在网络中只有一个Dense Layer要用FP32计算,其他Layer都用FP16计算。混合精度配置以Cell为粒度,Cell默认是FP32类型。
以下是一个手动混合精度的实现步骤:
1. 定义网络: 该步骤与自动混合精度中的步骤2类似;注意:在LeNet中的fc3算子,需要手动配置成FP32;
2. 配置混合精度: LeNet通过net.add_flags_recursive(fp16=True),把该Cell及其子Cell中所有的算子都配置成FP16;
3. 使用TrainOneStepWithLossScaleCell封装网络模型和优化器。
代码样例如下:
```python
# Define network
class LeNet5(nn.Cell):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
self.fc1 = nn.Dense(16 * 5 * 5, 120)
self.fc2 = nn.Dense(120, 84)
self.fc3 = nn.Dense(84, 10).add_flags_recursive(fp32=True)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2)
self.flatten = P.Flatten()
def construct(self, x):
x = self.max_pool2d(self.relu(self.conv1(x)))
x = self.max_pool2d(self.relu(self.conv2(x)))
x = self.flatten(x)
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
# Initialize network and set mixing precision
net = LeNet5()
net.add_flags_recursive(fp16=True)
# Define training data, label and sens
predict = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32) * 0.01)
label = Tensor(np.zeros([1, 10]).astype(np.float32))
scaling_sens = Tensor(np.full((1), 1.0), dtype=mstype.float32)
# Define Loss and Optimizer
net.set_train()
loss = MSELoss()
optimizer = Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
net_with_loss = WithLossCell(net, loss)
train_network = TrainOneStepWithLossScaleCell(net_with_loss, optimizer)
# Run training
output = train_network(inputs, label, scaling_sens)
```
\ No newline at end of file
# 模型安全
<!-- TOC -->
- [模型安全](#模型安全)
- [概述](#概述)
- [建立被攻击模型](#建立被攻击模型)
- [引入相关包](#引入相关包)
- [加载数据集](#加载数据集)
- [建立模型](#建立模型)
- [对抗性攻击](#对抗性攻击)
- [对抗性防御](#对抗性防御)
- [防御实现](#防御实现)
- [防御效果](#防御效果)
<!-- /TOC -->
## 概述
本教程介绍MindArmour提供的模型安全防护手段,引导您快速使用MindArmour,为您的AI模型提供一定的安全防护能力。
AI算法设计之初普遍未考虑相关的安全威胁,使得AI算法的判断结果容易被恶意攻击者影响,导致AI系统判断失准。攻击者在原始样本处加入人类不易察觉的微小扰动,导致深度学习模型误判,称为对抗样本攻击。MindArmour模型安全提供对抗样本生成、对抗样本检测、模型防御、攻防效果评估等功能,为AI模型安全研究和AI应用安全提供重要支撑。
- 对抗样本生成模块支持安全工程师快速高效地生成对抗样本,用于攻击AI模型。
- 对抗样本检测、防御模块支持用户检测过滤对抗样本、增强AI模型对于对抗样本的鲁棒性。
- 评估模块提供多种指标全面评估对抗样本攻防性能。
这里通过图像分类任务上的对抗性攻防,以攻击算法FGSM和防御算法NAD为例,介绍MindArmour在对抗攻防上的使用方法。
> 你可以在这里找到完整可运行的样例代码:
> 攻击代码:<https://gitee.com/mindspore/docs/tree/r0.1/tutorials/tutorial_code/model_safety/mnist_attack_fgsm.py>
> 防御代码:<https://gitee.com/mindspore/docs/tree/r0.1/tutorials/tutorial_code/model_safety/mnist_defense_nad.py>
## 建立被攻击模型
以MNIST为示范数据集,自定义的简单模型作为被攻击模型。
### 引入相关包
```python
import sys
import time
import numpy as np
from scipy.special import softmax
from mindspore import dataset as ds
import mindspore.common.dtype as mstype
import mindspore.dataset.transforms.vision.c_transforms as CV
import mindspore.dataset.transforms.c_transforms as C
from mindspore.dataset.transforms.vision import Inter
import mindspore.nn as nn
import mindspore.ops.operations as P
from mindspore.common.initializer import TruncatedNormal
from mindspore import Model
from mindspore import Tensor
from mindspore import context
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindarmour.attacks.gradient_method import FastGradientSignMethod
from mindarmour.utils.logger import LogUtil
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
LOGGER = LogUtil.get_instance()
TAG = 'demo'
```
### 加载数据集
利用MindSpore的dataset提供的MnistDataset接口加载MNIST数据集。
```python
# generate training data
def generate_mnist_dataset(data_path, batch_size=32, repeat_size=1,
num_parallel_workers=1, sparse=True):
"""
create dataset for training or testing
"""
# define dataset
ds1 = ds.MnistDataset(data_path)
# define operation parameters
resize_height, resize_width = 32, 32
rescale = 1.0 / 255.0
shift = 0.0
# define map operations
resize_op = CV.Resize((resize_height, resize_width),
interpolation=Inter.LINEAR)
rescale_op = CV.Rescale(rescale, shift)
hwc2chw_op = CV.HWC2CHW()
type_cast_op = C.TypeCast(mstype.int32)
one_hot_enco = C.OneHot(10)
# apply map operations on images
if not sparse:
ds1 = ds1.map(input_columns="label", operations=one_hot_enco,
num_parallel_workers=num_parallel_workers)
type_cast_op = C.TypeCast(mstype.float32)
ds1 = ds1.map(input_columns="label", operations=type_cast_op,
num_parallel_workers=num_parallel_workers)
ds1 = ds1.map(input_columns="image", operations=resize_op,
num_parallel_workers=num_parallel_workers)
ds1 = ds1.map(input_columns="image", operations=rescale_op,
num_parallel_workers=num_parallel_workers)
ds1 = ds1.map(input_columns="image", operations=hwc2chw_op,
num_parallel_workers=num_parallel_workers)
# apply DatasetOps
buffer_size = 10000
ds1 = ds1.shuffle(buffer_size=buffer_size)
ds1 = ds1.batch(batch_size, drop_remainder=True)
ds1 = ds1.repeat(repeat_size)
return ds1
```
### 建立模型
这里以LeNet模型为例,您也可以建立训练自己的模型。
1. 定义LeNet模型网络。
```python
def conv(in_channels, out_channels, kernel_size, stride=1, padding=0):
weight = weight_variable()
return nn.Conv2d(in_channels, out_channels,
kernel_size=kernel_size, stride=stride, padding=padding,
weight_init=weight, has_bias=False, pad_mode="valid")
def fc_with_initialize(input_channels, out_channels):
weight = weight_variable()
bias = weight_variable()
return nn.Dense(input_channels, out_channels, weight, bias)
def weight_variable():
return TruncatedNormal(0.2)
class LeNet5(nn.Cell):
"""
Lenet network
"""
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = conv(1, 6, 5)
self.conv2 = conv(6, 16, 5)
self.fc1 = fc_with_initialize(16*5*5, 120)
self.fc2 = fc_with_initialize(120, 84)
self.fc3 = fc_with_initialize(84, 10)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
self.reshape = P.Reshape()
def construct(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.conv2(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.reshape(x, (-1, 16*5*5))
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
return x
```
2. 加载预训练的LeNet模型,您也可以训练并保存自己的MNIST模型,参考快速入门。利用上面定义的数据加载函数`generate_mnist_dataset`载入数据。
```python
ckpt_name = './trained_ckpt_file/checkpoint_lenet-10_1875.ckpt'
net = LeNet5()
load_dict = load_checkpoint(ckpt_name)
load_param_into_net(net, load_dict)
# get test data
data_list = "./MNIST_unzip/test"
batch_size = 32
dataset = generate_mnist_dataset(data_list, batch_size, sparse=False)
```
3. 测试模型。
```python
# prediction accuracy before attack
model = Model(net)
batch_num = 3 # the number of batches of attacking samples
test_images = []
test_labels = []
predict_labels = []
i = 0
for data in dataset.create_tuple_iterator():
i += 1
images = data[0].astype(np.float32)
labels = data[1]
test_images.append(images)
test_labels.append(labels)
pred_labels = np.argmax(model.predict(Tensor(images)).asnumpy(),
axis=1)
predict_labels.append(pred_labels)
if i >= batch_num:
break
predict_labels = np.concatenate(predict_labels)
true_labels = np.argmax(np.concatenate(test_labels), axis=1)
accuracy = np.mean(np.equal(predict_labels, true_labels))
LOGGER.info(TAG, "prediction accuracy before attacking is : %s", accuracy)
```
测试结果中分类精度达到了98%。
```python
prediction accuracy before attacking is : 0.9895833333333334
```
## 对抗性攻击
调用MindArmour提供的FGSM接口(FastGradientSignMethod)。
```python
# attacking
attack = FastGradientSignMethod(net, eps=0.3)
start_time = time.clock()
adv_data = attack.batch_generate(np.concatenate(test_images),
np.concatenate(test_labels), batch_size=32)
stop_time = time.clock()
np.save('./adv_data', adv_data)
pred_logits_adv = model.predict(Tensor(adv_data)).asnumpy()
# rescale predict confidences into (0, 1).
pred_logits_adv = softmax(pred_logits_adv, axis=1)
pred_labels_adv = np.argmax(pred_logits_adv, axis=1)
accuracy_adv = np.mean(np.equal(pred_labels_adv, true_labels))
LOGGER.info(TAG, "prediction accuracy after attacking is : %s", accuracy_adv)
attack_evaluate = AttackEvaluate(np.concatenate(test_images).transpose(0, 2, 3, 1),
np.concatenate(test_labels),
adv_data.transpose(0, 2, 3, 1),
pred_logits_adv)
LOGGER.info(TAG, 'mis-classification rate of adversaries is : %s',
attack_evaluate.mis_classification_rate())
LOGGER.info(TAG, 'The average confidence of adversarial class is : %s',
attack_evaluate.avg_conf_adv_class())
LOGGER.info(TAG, 'The average confidence of true class is : %s',
attack_evaluate.avg_conf_true_class())
LOGGER.info(TAG, 'The average distance (l0, l2, linf) between original '
'samples and adversarial samples are: %s',
attack_evaluate.avg_lp_distance())
LOGGER.info(TAG, 'The average structural similarity between original '
'samples and adversarial samples are: %s',
attack_evaluate.avg_ssim())
LOGGER.info(TAG, 'The average costing time is %s',
(stop_time - start_time)/(batch_num*batch_size))
```
攻击结果如下:
```python
prediction accuracy after attacking is : 0.052083
mis-classification rate of adversaries is : 0.947917
The average confidence of adversarial class is : 0.419824
The average confidence of true class is : 0.070650
The average distance (l0, l2, linf) between original samples and adversarial samples are: (1.698870, 0.465888, 0.300000)
The average structural similarity between original samples and adversarial samples are: 0.332538
The average costing time is 0.003125
```
对模型进行FGSM无目标攻击后,模型精度由98.9%降到5.2%,误分类率高达95%,成功攻击的对抗样本的预测类别的平均置信度(ACAC)为 0.419824,成功攻击的对抗样本的真实类别的平均置信度(ACTC)为 0.070650,同时给出了生成的对抗样本与原始样本的零范数距离、二范数距离和无穷范数距离,平均每个对抗样本与原始样本间的结构相似性为0.332538,平均每生成一张对抗样本所需时间为0.003125s。
攻击前后效果如下图,左侧为原始样本,右侧为FGSM无目标攻击后生成的对抗样本。从视觉角度而言,右侧图片与左侧图片几乎没有明显变化,但是均成功误导了模型,使模型将其误分类为其他非正确类别。
![adv_attack_result](./images/adv_attack_result.png)
## 对抗性防御
NaturalAdversarialDefense(NAD)是一种简单有效的对抗样本防御方法,使用对抗训练的方式,在模型训练的过程中构建对抗样本,并将对抗样本与原始样本混合,一起训练模型。随着训练次数的增加,模型在训练的过程中提升对于对抗样本的鲁棒性。NAD算法使用FGSM作为攻击算法,构建对抗样本。
### 防御实现
调用MindArmour提供的NAD防御接口(NaturalAdversarialDefense)。
```python
from mindspore.nn import SoftmaxCrossEntropyWithLogits
from mindarmour.defenses import NaturalAdversarialDefense
loss = SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=False)
opt = nn.Momentum(net.trainable_params(), 0.01, 0.09)
nad = NaturalAdversarialDefense(net, loss_fn=loss, optimizer=opt,
bounds=(0.0, 1.0), eps=0.3)
net.set_train()
nad.batch_defense(np.concatenate(test_images), np.concatenate(test_labels),
batch_size=32, epochs=20)
# get accuracy of test data on defensed model
net.set_train(False)
acc_list = []
pred_logits_adv = []
for i in range(batch_num):
batch_inputs = test_images[i]
batch_labels = test_labels[i]
logits = net(Tensor(batch_inputs)).asnumpy()
pred_logits_adv.append(logits)
label_pred = np.argmax(logits, axis=1)
acc_list.append(np.mean(np.argmax(batch_labels, axis=1) == label_pred))
pred_logits_adv = np.concatenate(pred_logits_adv)
pred_logits_adv = softmax(pred_logits_adv, axis=1)
LOGGER.info(TAG, 'accuracy of TEST data on defensed model is : %s',
np.mean(acc_list))
acc_list = []
for i in range(batch_num):
batch_inputs = adv_data[i * batch_size: (i + 1) * batch_size]
batch_labels = test_labels[i]
logits = net(Tensor(batch_inputs)).asnumpy()
label_pred = np.argmax(logits, axis=1)
acc_list.append(np.mean(np.argmax(batch_labels, axis=1) == label_pred))
attack_evaluate = AttackEvaluate(np.concatenate(test_images),
np.concatenate(test_labels),
adv_data,
pred_logits_adv)
LOGGER.info(TAG, 'accuracy of adv data on defensed model is : %s',
np.mean(acc_list))
LOGGER.info(TAG, 'defense mis-classification rate of adversaries is : %s',
attack_evaluate.mis_classification_rate())
LOGGER.info(TAG, 'The average confidence of adversarial class is : %s',
attack_evaluate.avg_conf_adv_class())
LOGGER.info(TAG, 'The average confidence of true class is : %s',
attack_evaluate.avg_conf_true_class())
LOGGER.info(TAG, 'The average distance (l0, l2, linf) between original '
'samples and adversarial samples are: %s',
attack_evaluate.avg_lp_distance())
```
### 防御效果
```python
accuracy of TEST data on defensed model is : 0.973958
accuracy of adv data on defensed model is : 0.521835
defense mis-classification rate of adversaries is : 0.026042
The average confidence of adversarial class is : 0.67979
The average confidence of true class is : 0.19144624
The average distance (l0, l2, linf) between original samples and adversarial samples are: (1.544365, 0.439001, 0.300000)
```
使用NAD进行对抗样本防御后,模型对于对抗样本的误分类率从95%降至48%,模型有效地防御了对抗样本。同时,模型对于原来测试数据集的分类精度达97%,使用NAD防御功能,并未降低模型的分类精度。
# 自然语言处理应用
<!-- TOC -->
- [自然语言处理应用](#自然语言处理应用)
- [概述](#概述)
- [准备及设计](#准备及设计)
- [下载数据集](#下载数据集)
- [确定评价标准](#确定评价标准)
- [确定网络及流程](#确定网络及流程)
- [实现阶段](#实现阶段)
- [导入需要的库文件](#导入需要的库文件)
- [配置环境信息](#配置环境信息)
- [预处理数据集](#预处理数据集)
- [定义网络](#定义网络)
- [定义优化器及损失函数](#定义优化器及损失函数)
- [训练并保存模型](#训练并保存模型)
- [模型验证](#模型验证)
- [实验结果](#实验结果)
- [下载代码](#下载代码)
<!-- /TOC -->
## 概述
情感分类是自然语言处理中文本分类问题的子集,属于自然语言处理最基础的应用。它是对带有感情色彩的主观性文本进行分析和推理的过程,即分析说话人的态度,是倾向正面还是反面。
> 通常情况下,我们会把情感类别分为正面、反面和中性三类。虽然“面无表情”的评论也有不少;不过,大部分时候会只采用正面和反面的案例进行训练,下面这个数据集就是很好的例子。
传统的文本主题分类问题的典型参考数据集为[20 Newsgroups](http://qwone.com/~jason/20Newsgroups/),该数据集由20组新闻数据组成,包含约20000个新闻文档。
其主题列表中有些类别的数据比较相似,例如comp.sys.ibm.pc.hardware和comp.sys.mac.hardware都是和电脑系统硬件相关的题目,相似度比较高。而有些主题类别的数据相对来说就毫无关联,例如misc.forsale和soc.religion.christian。
就网络本身而言,文本主题分类的网络结构和情感分类的网络结构大致相似。在掌握了情感分类网络如何构造之后,很容易可以构造一个类似的网络,稍作调参即可用于文本主题分类任务。
但在业务上下文侧,文本主题分类是分析文本讨论的客观内容,而情感分类是要从文本中得到它是否支持某种观点的信息。比如,“《阿甘正传》真是好看极了,影片主题明确,节奏流畅。”这句话,在文本主题分类是要将其归为类别为“电影”主题,而情感分类则要挖掘出这一影评的态度是正面还是负面。
相对于传统的文本主题分类,情感分类较为简单,实用性也较强。常见的购物网站、电影网站都可以采集到相对高质量的数据集,也很容易给业务领域带来收益。例如,可以结合领域上下文,自动分析特定类型客户对当前产品的意见,可以分主题分用户类型对情感进行分析,以作针对性的处理,甚至基于此进一步推荐产品,提高转化率,带来更高的商业收益。
特殊领域中,某些非极性词也充分表达了用户的情感倾向,比如下载使用APP时,“卡死了”、“下载太慢了”就表达了用户的负面情感倾向;股票领域中,“看涨”、“牛市”表达的就是用户的正面情感倾向。所以,本质上,我们希望模型能够在垂直领域中,挖掘出一些特殊的表达,作为极性词给情感分类系统使用:
$垂直极性词 = 通用极性词 + 领域特有极性词$
按照处理文本的粒度不同,情感分析可分为词语级、短语级、句子级、段落级以及篇章级等几个研究层次。这里以“段落级”为例,输入为一个段落,输出为影评是正面还是负面的信息。
## 准备及设计
### 下载数据集
采用IMDB影评数据集作为实验数据。
> 数据集下载地址:<http://ai.stanford.edu/~amaas/data/sentiment/>
以下是负面影评(Negative)和正面影评(Positive)的案例。
| Review | Label |
|---|---|
| "Quitting" may be as much about exiting a pre-ordained identity as about drug withdrawal. As a rural guy coming to Beijing, class and success must have struck this young artist face on as an appeal to separate from his roots and far surpass his peasant parents' acting success. Troubles arise, however, when the new man is too new, when it demands too big a departure from family, history, nature, and personal identity. The ensuing splits, and confusion between the imaginary and the real and the dissonance between the ordinary and the heroic are the stuff of a gut check on the one hand or a complete escape from self on the other. | Negative |
| This movie is amazing because the fact that the real people portray themselves and their real life experience and do such a good job it's like they're almost living the past over again. Jia Hongsheng plays himself an actor who quit everything except music and drugs struggling with depression and searching for the meaning of life while being angry at everyone especially the people who care for him most. | Positive |
同时,我们要下载GloVe文件,并在文件开头处添加新的一行,意思是总共读取400000个单词,每个单词用300纬度的词向量表示。
```
400000 300
```
GloVe文件下载地址:<http://nlp.stanford.edu/data/glove.6B.zip>
### 确定评价标准
作为典型的分类问题,情感分类的评价标准可以比照普通的分类问题处理。常见的精度(Accuracy)、精准度(Precision)、召回率(Recall)和F_beta分数都可以作为参考。
$精度(Accuracy)= 分类正确的样本数目 / 总样本数目$
$精准度(Precision)= 真阳性样本数目 / 所有预测类别为阳性的样本数目$
$召回率(Recall)= 真阳性样本数目 / 所有真实类别为阳性的样本数目$
$F1分数 = (2 * Precision * Recall) / (Precision + Recall)$
在IMDB这个数据集中,正负样本数差别不大,可以简单地用精度(accuracy)作为分类器的衡量标准。
### 确定网络及流程
当前,MindSpore GPU版本支持LSTM网络,我们使用LSTM网络进行自然语言处理。
1. 加载使用的数据集,并进行必要的数据处理。
2. 使用LSTM网络训练数据,生成模型。
> LSTM(Long short-term memory,长短期记忆)网络是一种时间循环神经网络,适合于处理和预测时间序列中间隔和延迟非常长的重要事件。具体介绍可参考网上资料,在此不再赘述。
3. 得到模型之后,使用验证数据集,查看模型精度情况。
## 实现阶段
### 导入需要的库文件
下列是我们所需要的公共模块及MindSpore的模块及库文件。
```python
import os
import shutil
import math
import argparse
import json
from itertools import chain
import numpy as np
from config import lstm_cfg as cfg
# Install gensim with 'pip install gensim'
import gensim
import mindspore.nn as nn
import mindspore.context as context
import mindspore.dataset as ds
from mindspore.ops import operations as P
from mindspore import Tensor
from mindspore.common.initializer import initializer
from mindspore.common.parameter import Parameter
from mindspore.mindrecord import FileWriter
from mindspore.train import Model
from mindspore.nn.metrics import Accuracy
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor
```
### 配置环境信息
1. 使用`parser`模块,传入运行必要的信息,如数据集存放路径,GloVe存放路径,这样的好处是,对于经常变化的配置,可以在运行代码时输入,使用更加灵活。
```python
parser = argparse.ArgumentParser(description='MindSpore LSTM Example')
parser.add_argument('--preprocess', type=str, default='false', choices=['true', 'false'],
help='Whether to perform data preprocessing')
parser.add_argument('--mode', type=str, default="train", choices=['train', 'test'],
help='implement phase, set to train or test')
# Download dataset from 'http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz' and extract to 'aclimdb_path'
parser.add_argument('--aclimdb_path', type=str, default="./aclImdb",
help='path where the dataset is store')
# Download GloVe from 'http://nlp.stanford.edu/data/glove.6B.zip' and extract to 'glove_path'
# Add a new line '400000 300' at the beginning of 'glove.6B.300d.txt' with '40000' for total words and '300' for vector length
parser.add_argument('--glove_path', type=str, default="./glove",
help='path where the GloVe is store')
# Specify the path to save preprocessed data
parser.add_argument('--preprocess_path', type=str, default="./preprocess",
help='path where the pre-process data is store')
# Specify the path to save the CheckPoint file
parser.add_argument('--ckpt_path', type=str, default="./ckpt", help='if mode is test, must provide\
path where the trained ckpt file')
args = parser.parse_args()
```
2. 实现代码前,需要配置必要的信息,包括环境信息、执行的模式、后端信息及硬件信息。
```python
context.set_context(
mode=context.GRAPH_MODE,
save_graphs=False,
device_target="GPU")
```
详细的接口配置信息,请参见`context.set_context`接口说明。
### 预处理数据集
1. 对文本数据集进行处理,包括编码、分词、对齐、处理GloVe原始数据,使之能够适应网络结构。
```python
# Read data from the specified directory
def read_imdb(path, seg='train'):
""" read imdb dataset """
pos_or_neg = ['pos', 'neg']
data = []
for label in pos_or_neg:
files = os.listdir(os.path.join(path, seg, label))
for file in files:
with open(os.path.join(path, seg, label, file), 'r', encoding='utf8') as rf:
review = rf.read().replace('\n', '')
if label == 'pos':
data.append([review, 1])
elif label == 'neg':
data.append([review, 0])
return data
# Split records into words with spaces as separators
def tokenizer(text):
return [tok.lower() for tok in text.split(' ')]
# Encode words into vectors
def encode_samples(tokenized_samples, word_to_idx):
""" encode word to index """
features = []
for sample in tokenized_samples:
feature = []
for token in sample:
if token in word_to_idx:
feature.append(word_to_idx[token])
else:
feature.append(0)
features.append(feature)
return features
# Align the number of words in each record to 500 words
def pad_samples(features, maxlen=500, pad=0):
""" pad all features to the same length """
padded_features = []
for feature in features:
if len(feature) >= maxlen:
padded_feature = feature[:maxlen]
else:
padded_feature = feature
while len(padded_feature) < maxlen:
padded_feature.append(pad)
padded_features.append(padded_feature)
return padded_features
# Crop GloVe raw data into a table which contains about 25,000 word vectors
def collect_weight(glove_path, vocab, word_to_idx, embed_size):
""" collect weight """
vocab_size = len(vocab)
wvmodel = gensim.models.KeyedVectors.load_word2vec_format(os.path.join(glove_path, 'glove.6B.300d.txt'),
binary=False, encoding='utf-8')
weight_np = np.zeros((vocab_size+1, embed_size)).astype(np.float32)
idx_to_word = {i+1: word for i, word in enumerate(vocab)}
idx_to_word[0] = '<unk>'
for i in range(len(wvmodel.index2word)):
try:
index = word_to_idx[wvmodel.index2word[i]]
except KeyError:
continue
weight_np[index, :] = wvmodel.get_vector(
idx_to_word[word_to_idx[wvmodel.index2word[i]]])
return weight_np
def preprocess(aclimdb_path, glove_path, embed_size):
""" preprocess the train and test data """
train_data = read_imdb(aclimdb_path, 'train')
test_data = read_imdb(aclimdb_path, 'test')
train_tokenized = []
test_tokenized = []
for review, _ in train_data:
train_tokenized.append(tokenizer(review))
for review, _ in test_data:
test_tokenized.append(tokenizer(review))
vocab = set(chain(*train_tokenized))
vocab_size = len(vocab)
print("vocab_size: ", vocab_size)
word_to_idx = {word: i+1 for i, word in enumerate(vocab)}
word_to_idx['<unk>'] = 0
train_features = np.array(pad_samples(encode_samples(train_tokenized, word_to_idx))).astype(np.int32)
train_labels = np.array([score for _, score in train_data]).astype(np.int32)
test_features = np.array(pad_samples(encode_samples(test_tokenized, word_to_idx))).astype(np.int32)
test_labels = np.array([score for _, score in test_data]).astype(np.int32)
weight_np = collect_weight(glove_path, vocab, word_to_idx, embed_size)
return train_features, train_labels, test_features, test_labels, weight_np, vocab_size
```
2. 将数据集格式转化为`mindrecord`格式,便于MindSpore读取。
```python
def get_imdb_data(labels_data, features_data):
data_list = []
for i, (label, feature) in enumerate(zip(labels_data, features_data)):
data_json = {"id": i,
"label": int(label),
"feature": feature.reshape(-1)}
data_list.append(data_json)
return data_list
# Convert the dataset to mindrecord dateset which is supported by MindSpore
def convert_to_mindrecord(embed_size, aclimdb_path, proprocess_path, glove_path):
""" convert imdb dataset to mindrecord """
num_shard = 4
train_features, train_labels, test_features, test_labels, weight_np, _ = \
preprocess(aclimdb_path, glove_path, embed_size)
np.savetxt(os.path.join(proprocess_path, 'weight.txt'), weight_np)
# write mindrecord
schema_json = {"id": {"type": "int32"},
"label": {"type": "int32"},
"feature": {"type": "int32", "shape": [-1]}}
writer = FileWriter(os.path.join(proprocess_path, 'aclImdb_train.mindrecord'), num_shard)
data = get_imdb_data(train_labels, train_features)
writer.add_schema(schema_json, "nlp_schema")
writer.add_index(["id", "label"])
writer.write_raw_data(data)
writer.commit()
writer = FileWriter(os.path.join(proprocess_path, 'aclImdb_test.mindrecord'), num_shard)
data = get_imdb_data(test_labels, test_features)
writer.add_schema(schema_json, "nlp_schema")
writer.add_index(["id", "label"])
writer.write_raw_data(data)
writer.commit()
print("============== Starting Data Pre-processing ==============")
shutil.rmtree(args.preprocess_path)
os.mkdir(args.preprocess_path)
convert_to_mindrecord(cfg.embed_size, args.aclimdb_path, args.preprocess_path, args.glove_path)
```
3. 创建数据集。
```python
def create_dataset(base_path, batch_size, num_epochs, is_train):
"""Create dataset for training."""
columns_list = ["feature", "label"]
num_consumer = 4
if is_train:
path = os.path.join(base_path, 'aclImdb_train.mindrecord0')
else:
path = os.path.join(base_path, 'aclImdb_test.mindrecord0')
dtrain = ds.MindDataset(path, columns_list, num_consumer)
dtrain = dtrain.shuffle(buffer_size=dtrain.get_dataset_size())
dtrain = dtrain.batch(batch_size, drop_remainder=True)
dtrain = dtrain.repeat(count=num_epochs)
return dtrain
ds_train = create_dataset(args.preprocess_path, cfg.batch_size, cfg.num_epochs, True)
```
### 定义网络
1. 初始化网络参数及网络状态。
```python
def init_lstm_weight(
input_size,
hidden_size,
num_layers,
bidirectional,
has_bias=True):
"""Initialize lstm weight."""
num_directions = 1
if bidirectional:
num_directions = 2
weight_size = 0
gate_size = 4 * hidden_size
for layer in range(num_layers):
for _ in range(num_directions):
input_layer_size = input_size if layer == 0 else hidden_size * num_directions
weight_size += gate_size * input_layer_size
weight_size += gate_size * hidden_size
if has_bias:
weight_size += 2 * gate_size
stdv = 1 / math.sqrt(hidden_size)
w_np = np.random.uniform(-stdv, stdv, (weight_size,
1, 1)).astype(np.float32)
w = Parameter(
initializer(
Tensor(w_np), [
weight_size, 1, 1]), name='weight')
return w
# Initialize short-term memory (h) and long-term memory (c) to 0
def lstm_default_state(batch_size, hidden_size, num_layers, bidirectional):
"""init default input."""
num_directions = 1
if bidirectional:
num_directions = 2
h = Tensor(
np.zeros((num_layers * num_directions, batch_size, hidden_size)).astype(np.float32))
c = Tensor(
np.zeros((num_layers * num_directions, batch_size, hidden_size)).astype(np.float32))
return h, c
```
2. 使用`cell`方法,定义网络结构。
```python
class SentimentNet(nn.Cell):
"""Sentiment network structure."""
def __init__(self,
vocab_size,
embed_size,
num_hiddens,
num_layers,
bidirectional,
num_classes,
weight,
batch_size):
super(SentimentNet, self).__init__()
# Mapp words to vectors
self.embedding = nn.Embedding(vocab_size,
embed_size,
embedding_table=weight)
self.embedding.embedding_table.requires_grad = False
self.trans = P.Transpose()
self.perm = (1, 0, 2)
self.encoder = nn.LSTM(input_size=embed_size,
hidden_size=num_hiddens,
num_layers=num_layers,
has_bias=True,
bidirectional=bidirectional,
dropout=0.0)
w_init = init_lstm_weight(
embed_size,
num_hiddens,
num_layers,
bidirectional)
self.encoder.weight = w_init
self.h, self.c = lstm_default_state(batch_size, num_hiddens, num_layers, bidirectional)
self.concat = P.Concat(1)
if bidirectional:
self.decoder = nn.Dense(num_hiddens * 4, num_classes)
else:
self.decoder = nn.Dense(num_hiddens * 2, num_classes)
def construct(self, inputs):
# input:(64,500,300)
embeddings = self.embedding(inputs)
embeddings = self.trans(embeddings, self.perm)
output, _ = self.encoder(embeddings, (self.h, self.c))
# states[i] size(64,200) -> encoding.size(64,400)
encoding = self.concat((output[0], output[1]))
outputs = self.decoder(encoding)
return outputs
embedding_table = np.loadtxt(os.path.join(args.preprocess_path, "weight.txt")).astype(np.float32)
network = SentimentNet(vocab_size=embedding_table.shape[0],
embed_size=cfg.embed_size,
num_hiddens=cfg.num_hiddens,
num_layers=cfg.num_layers,
bidirectional=cfg.bidirectional,
num_classes=cfg.num_classes,
weight=Tensor(embedding_table),
batch_size=cfg.batch_size)
```
### 定义优化器及损失函数
定义优化器及损失函数的示例代码如下:
```python
loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
opt = nn.Momentum(network.trainable_params(), cfg.learning_rate, cfg.momentum)
loss_cb = LossMonitor()
```
### 训练并保存模型
加载对应数据集并配置好CheckPoint生成信息,然后使用`model.train`接口,进行模型训练。
```python
model = Model(network, loss, opt, {'acc': Accuracy()})
print("============== Starting Training ==============")
ds_train = create_dataset(args.preprocess_path, cfg.batch_size, cfg.num_epochs, True)
config_ck = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_steps,
keep_checkpoint_max=cfg.keep_checkpoint_max)
ckpoint_cb = ModelCheckpoint(prefix="lstm", directory=args.ckpt_path, config=config_ck)
model.train(cfg.num_epochs, ds_train, callbacks=[ckpoint_cb, loss_cb])
```
### 模型验证
加载验证数据集及保存的CheckPoint文件,进行验证,查看模型质量。
```python
print("============== Starting Testing ==============")
ds_eval = create_dataset(args.preprocess_path, cfg.batch_size, 1, False)
param_dict = load_checkpoint(args.ckpt_path)
load_param_into_net(network, param_dict)
acc = model.eval(ds_eval)
print("============== Accuracy:{} ==============".format(acc))
```
## 实验结果
在经历了10轮epoch之后,在训练集上的精度收敛到约85%,在测试集上的精度约为86%。
**执行训练**
1. 运行训练代码,查看运行结果。
```shell
$ python main.py --preprocess=true --mode=train --ckpt_path=./ckpt
```
输出如下,可以看到loss值随着训练逐步降低,最后达到0.249左右,即经过10个epoch的训练,对当前文本分析的结果正确率在85%左右:
```shell
============== Starting Data Pre-processing ==============
vocab_size: 252192
============== Starting Training ==============
[INFO] ME(15831:140036161672960,MainProcess):2020-03-09-16:29:02.785.484 [mindspore/train/serialization.py:118] Execute save checkpoint process.
[INFO] ME(15831:140036161672960,MainProcess):2020-03-09-16:29:03.658.733 [mindspore/train/serialization.py:143] Save checkpoint process finish.
epoch: 1 step: 390 , loss is 0.6926409
...
[INFO] ME(15831:140036161672960,MainProcess):2020-03-09-16:32:18.598.405 [mindspore/train/serialization.py:118] Execute save checkpoint process.
[INFO] ME(15831:140036161672960,MainProcess):2020-03-09-16:32:19.561.926 [mindspore/train/serialization.py:143] Save checkpoint process finish.
epoch: 6 step: 390 , loss is 0.222701
...
epoch: 10 step: 390 , loss is 0.22616856
epoch: 10 step: 390 , loss is 0.24914627
```
2. 查看保存的CheckPoint文件。
训练过程中保存了CheckPoint文件,即模型文件,我们可以查看文件保存的路径下的所有保存文件。
```shell
$ ls ckpt/
```
输出如下:
```shell
lstm-10_390.ckpt lstm-1_390.ckpt lstm-2_390.ckpt lstm-3_390.ckpt lstm-4_390.ckpt lstm-5_390.ckpt lstm-6_390.ckpt lstm-7_390.ckpt lstm-8_390.ckpt lstm-9_390.ckpt
```
**验证模型**
使用最后保存的CheckPoint文件,加载验证数据集,进行验证。
```shell
$ python main.py --mode=test --ckpt_path=./ckpt/lstm-10_390.ckpt
```
输出如下,可以看到使用验证的数据集,对文本的情感分析正确率在86%左右,达到一个基本满意的结果。
```shell
RegisterOperatorCreator:OperatorCreators init
============== Starting Testing ==============
[INFO] ME(29963:140462460516096,MainProcess):2020-03-09-16:37:19.333.605 [mindspore/train/serialization.py:169] Execute load checkpoint process.
[INFO] ME(29963:140462460516096,MainProcess):2020-03-09-16:37:20.434.749 [mindspore/train/serialization.py:200] Load checkpoint process finish.
[INFO] ME(29963:140462460516096,MainProcess):2020-03-09-16:37:20.467.336 [mindspore/train/serialization.py:233] Execute parameter into net process.
[INFO] ME(29963:140462460516096,MainProcess):2020-03-09-16:37:20.467.649 [mindspore/train/serialization.py:268] Load parameter into net process finish.
============== Accuracy:{'acc': 0.8599358974358975} ==============
```
## 下载代码
完整可运行代码下载地址:<https://gitee.com/mindspore/docs/tree/r0.1/tutorials/tutorial_code/lstm>
- main.py:代码文件,包括数据预处理、网络定义、模型训练等代码。
- config.py:网络中的一些配置,包括batch size、进行几次epoch训练等。
# 端侧推理
<!-- TOC -->
- [端侧推理](#端侧推理)
- [概述](#概述)
- [编译方法](#编译方法)
- [端侧推理使用](#端侧推理使用)
- [生成端侧模型文件](#生成端侧模型文件)
- [在端侧实现推理](#在端侧实现推理)
<!-- /TOC -->
## 概述
MindSpore Predict是一个轻量级的深度神经网络推理引擎,提供了将MindSpore训练出的模型在端侧进行推理的功能。本教程介绍MindSpore Predict的编译方法和使用指南。
## 编译方法
用户需要自行编译,这里介绍在Ubuntu环境下进行交叉编译的具体步骤。
环境要求如下:
- 硬件要求
- 内存1GB以上
- 硬盘空间10GB以上
- 系统要求
- 系统:Ubuntu = 16.04.02LTS(验证可用)
- 内核:4.4.0-62-generic(验证可用)
- 软件依赖
- [cmake](https://cmake.org/download/) >= 3.14.1
- [GCC](https://gcc.gnu.org/releases.html) >= 5.4
- [autoconf](http://ftp.gnu.org/gnu/autoconf/) 2.69
- [LLVM 8.0.0](http://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz)
- [Android_NDK r16b](https://dl.google.com/android/repository/android-ndk-r16b-linux-x86_64.zip)
- numpy >= 1.16
- decorator
- scipy
> numpy, decorator和scipy可以通过pip安装,参考命令如下。
```bash
pip3 install numpy==1.16 decorator scipy
```
编译步骤如下:
1. 配置环境变量。
```bash
export LLVM_PATH={$LLVM_PATH}/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-16.04/bin/llvm-config #设定llvm路径
export ANDROID_NDK={$NDK_PATH}/android-ndk-r16b #设定ndk路径
```
2. 从代码仓下载源码。
```bash
git clone https://gitee.com/mindspore/mindspore.git -b r0.1
```
3. 在源码根目录下,执行如下命令编译MindSpore Predict。-I为编译MindSpore Predict的编译参数,-I的参数为目标端侧平台,目前仅支持安卓arm64平台。
```bash
sh build.sh -I arm64
```
4. 获取编译结果。
进入源码的predict/output目录,即可查看生成的压缩包,包名为MSPredict-{版本号}-{HOST平台}_{DEVICE平台}.tar.gz,例如:MSPredict-0.1.0-linux_aarch64.tar.gz。 该压缩包包含以下目录:
- include:MindSpore Predict的头文件。
- lib:MindSpore Predict的动态库。
## 端侧推理使用
在APP的APK工程中使用MindSpore对进行模型推理时,模型推理前需要对输入进行必要的前处理,比如将图片转换成MindSpore推理要求的tensor格式、对图片进行resize等处理。在MindSpore完成模型推理后,对模型推理的结果进行后处理,并将处理的输出发送给APP应用。
本章主要描述用户如何使用MindSpore进行模型推理,APK工程的搭建和模型推理的前后处理,不在此列举。
MindSpore进行端侧模型推理的步骤如下。
### 生成端侧模型文件
1. 加载训练完毕所生成的CheckPoint文件至定义好的网络中。
```python
param_dict = load_checkpoint(ckpoint_file_name=ckpt_file_path)
load_param_into_net(net, param_dict)
```
2. 调用`export`接口,导出端侧模型文件(.ms)。
```python
export(net, input_data, file_name="./lenet.ms", file_format='LITE')
```
以LeNet网络为例,生成的端侧模型文件为`lenet.ms`,完整示例代码lenet.py如下。
```python
import os
import numpy as np
import mindspore.nn as nn
import mindspore.ops.operations as P
import mindspore.context as context
from mindspore.common.tensor import Tensor
from mindspore.train.serialization import export, load_checkpoint, load_param_into_net
class LeNet(nn.Cell):
def __init__(self):
super(LeNet, self).__init__()
self.relu = P.ReLU()
self.batch_size = 32
self.conv1 = nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=0, has_bias=False, pad_mode='valid')
self.conv2 = nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=0, has_bias=False, pad_mode='valid')
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.reshape = P.Reshape()
self.fc1 = nn.Dense(400, 120)
self.fc2 = nn.Dense(120, 84)
self.fc3 = nn.Dense(84, 10)
def construct(self, input_x):
output = self.conv1(input_x)
output = self.relu(output)
output = self.pool(output)
output = self.conv2(output)
output = self.relu(output)
output = self.pool(output)
output = self.reshape(output, (self.batch_size, -1))
output = self.fc1(output)
output = self.relu(output)
output = self.fc2(output)
output = self.relu(output)
output = self.fc3(output)
return output
if __name__ == '__main__':
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
seed = 0
np.random.seed(seed)
origin_data = np.random.uniform(low=0, high=255, size=(32, 1, 32, 32)).astype(np.float32)
origin_data.tofile("lenet.bin")
input_data = Tensor(origin_data)
net = LeNet()
ckpt_file_path = "path_to/lenet.ckpt"
is_ckpt_exist = os.path.exists(ckpt_file_path)
if is_ckpt_exist:
param_dict = load_checkpoint(ckpoint_file_name=ckpt_file_path)
load_param_into_net(net, param_dict)
export(net, input_data, file_name="./lenet.ms", file_format='LITE')
print("export model success.")
else:
print("checkpoint file does not exist.")
```
### 在端侧实现推理
将.ms模型文件和图片数据作为输入,创建session在端侧实现推理。
![](./images/side_infer_process.png)
图1:端侧推理时序图
1. 加载.ms模型文件到内存缓冲区,ReadFile函数功能需要用户自行实现。
```cpp
// read model file
std::string modelPath = "./models/lenet/lenet.ms";
size_t graphSize = 0;
/* ReadFile() here is a dummy function */
char *graphBuf = ReadFile(modelPath.c_str(), graphSize);
```
2. 调用CreateSession接口创建Session,创建完成后可释放内存缓冲区中的模型文件。
```cpp
// create session
Context ctx;
std::shared_ptr<Session> session = CreateSession(graphBuf, graphSize, ctx);
free(graphBuf);
```
3. 从内存缓冲区中读取推理的输入数据,调用SetData()接口将输入数据设置到input tensor中。
```cpp
// load input buffer
size_t inputSize = 0;
std::string imagePath = "./data/input/lenet.bin";
char *inputBuf = ReadFile(imagePath.c_str(), inputSize);
//get input tensors
std::vector<Tensor *> inputs = session->GetInput();
//set input buffer
inputs[0]->SetData(inputBuf);
```
4. 调用Session中的Run()接口执行推理。
```cpp
// session run
int ret = session->Run(inputs);
```
5. 调用GetAllOutput()接口获取输出。
```cpp
// get output
std::map<std::string, std::vector<Tensor *>> outputs = session->GetAllOutput();
```
6. 调用Tensor的GetData()接口获取输出数据。
```cpp
// get output data
float *data = nullptr;
for (auto output : outputs) {
auto tensors = output.second;
for (auto tensor : tensors) {
data = (float *)(tensor->GetData());
}
}
```
7. 推理结束释放input tensor和output tensor。
```cpp
// free inputs and outputs
for (auto &input : inputs) {
delete input;
}
inputs.clear();
for (auto &output : outputs) {
for (auto &outputTensor : output.second) {
delete outputTensor;
}
}
outputs.clear();
```
选取LeNet网络,推理输入为“lenet.bin”,完整示例代码lenet.cpp如下。
> MindSpore Predict使用FlatBuffers定义模型,解析模型需要使用到FlatBuffers头文件,因此用户需要自行配置FlatBuffers头文件。
>
> 具体做法:将MindSpore根目录/third_party/flatbuffers/include下的flatbuffers文件夹拷贝到session.h的同级目录。
```cpp
#include <string>
#include <vector>
#include "context.h"
#include "session.h"
#include "tensor.h"
#include "errorcode.h"
using namespace mindspore::predict;
int main() {
std::string modelPath = "./models/lenet/lenet.ms";
std::string imagePath = "./data/input/lenet.bin";
// read model file
size_t graphSize = 0;
/* ReadFile() here is a dummy function */
char *graphBuf = ReadFile(modelPath.c_str(), graphSize);
if (graphBuf == nullptr) {
return -1;
}
// create session
Context ctx;
auto session = CreateSession(graphBuf, graphSize, ctx);
if (session == nullptr) {
free(graphBuf);
return -1;
}
free(graphBuf);
// load input buf
size_t inputSize = 0;
char *inputBuf = ReadFile(imagePath.c_str(), inputSize);
if (inputBuf == nullptr) {
return -1;
}
auto inputs = session->GetInput();
inputs[0]->SetData(inputBuf);
// session run
auto ret = session->Run(inputs);
if (ret != RET_OK) {
printf("run failed, error: %d\n", ret);
for (auto &input : inputs) {
delete input;
}
return -1;
}
// get output
auto outputs = session->GetAllOutput();
// get output data
float *data = nullptr;
for (auto output : outputs) {
auto tensors = output.second;
for (auto tensor : tensors) {
data = (float *)(tensor->GetData());
//print the contents of the data
for (size_t i = 0; i < tensor->GetElementSize(); ++i) {
printf(" %f ", data[i]);
}
printf("\n");
}
}
// free inputs and outputs
for (auto &input : inputs) {
delete input;
}
inputs.clear();
for (auto &output : outputs) {
for (auto &outputTensor : output.second) {
delete outputTensor;
}
}
outputs.clear();
return 0;
}
```
# 训练过程可视
<!-- TOC -->
- [训练过程可视](#训练过程可视)
- [概述](#概述)
- [操作流程](#操作流程)
- [准备训练脚本](#准备训练脚本)
- [基础写法](#基础写法)
- [记录算子融合后的计算图](#记录算子融合后的计算图)
- [MindInsight相关命令](#mindinsight相关命令)
- [查看命令帮助信息](#查看命令帮助信息)
- [查看版本信息](#查看版本信息)
- [启动服务](#启动服务)
- [停止服务](#停止服务)
- [查看服务进程信息](#查看服务进程信息)
- [可视化组件](#可视化组件)
- [计算图可视化](#计算图可视化)
- [标量可视化](#标量可视化)
- [图像可视化](#图像可视化)
- [模型溯源可视化](#模型溯源可视化)
- [数据图可视化](#数据图可视化)
- [数据溯源可视化](#数据溯源可视化)
<!-- /TOC -->
## 概述
训练过程中的标量、图像、计算图以及模型超参等信息记录到文件中,通过可视化界面供用户查看。
## 操作流程
- 准备训练脚本,并在训练脚本中指定标量、图像、计算图、模型超参等信息记录到summary日志文件,接着运行训练脚本。
- 启动MindInsight,并通过启动参数指定summary日志文件目录,启动成功后,根据IP和端口访问可视化界面,默认访问地址为 `http://127.0.0.1:8080`
- 在训练过程中,有数据写入summary日志文件时,即可在页面中查看可视的数据。
## 准备训练脚本
当前MindSpore利用 `Callback` 机制将标量、图像、计算图、模型超参等信息保存到summary日志文件中,并通过可视化界面进行展示。
其中标量、图像是通过Summary算子实现记录数据,计算图是在网络编译完成后,通过 `SummaryRecord` 将其保存到summary日志文件中,
模型参数是通过 `TrainLineage``EvalLineage` 保存到summary日志文件中。
### 基础写法
步骤一:在继承 `nn.Cell` 的衍生类的 `construct` 函数中调用Summary算子来采集图像或标量数据。
比如,在定义网络时,在网络的 `construct` 中记录图像数据;在定义损失函数时,在损失函数的 `construct`中记录损失值。
如果要记录动态学习率,可以在定义优化器时,在优化器的 `construct` 中记录学习率。
样例代码如下:
```python
from mindspore import context, Tensor, nn
from mindspore.common import dtype as mstype
from mindspore.ops import operations as P
from mindspore.ops import functional as F
from mindspore.nn import Optimizer
class CrossEntropyLoss(nn.Cell):
"""Loss function definition."""
def __init__(self):
super(CrossEntropyLoss, self).__init__()
self.cross_entropy = P.SoftmaxCrossEntropyWithLogits()
self.mean = P.ReduceMean()
self.one_hot = P.OneHot()
self.on_value = Tensor(1.0, mstype.float32)
self.off_value = Tensor(0.0, mstype.float32)
# Init ScalarSummary
self.sm_scalar = P.ScalarSummary()
def construct(self, logits, label):
label = self.one_hot(label, F.shape(logits)[1], self.on_value, self.off_value)
loss = self.cross_entropy(logits, label)[0]
loss = self.mean(loss, (-1,))
# Record loss
self.sm_scalar("loss", loss)
return loss
class Net(nn.Cell):
"""Net definition."""
def __init__(self):
super(Net, self).__init__()
......
# Init ImageSummary
self.sm_image = P.ImageSummary()
def construct(self, data):
# Record image by Summary operator
self.sm_image("image", data)
......
return out
class MyOptimizer(Optimizer):
"""Optimizer definition."""
def __init__(self, learning_rate, ......):
......
# Initialize ScalarSummary
self.sm_scalar = P.ScalarSummary()
def construct(self, grads):
......
# Record learning rate here
self.sm_scalar("learning_rate", learning_rate)
......
```
步骤二:通过 `Callback` 的机制,添加所需的Callback实例来指定训练过程中所需要记录的数据。
- `SummaryStep` 用于指定记录summary数据的步骤间隔,每隔指定步骤记录一次数据。
- `TrainLineage` 用于记录模型训练相关的参数信息。
- `EvalLineage` 用于记录模型测试相关的参数信息。
其中,记录计算图需要在调用 `SummaryRecord` 时,指定 `network` 参数,默认不记录。
样例代码如下:
```python
from mindinsight.lineagemgr import TrainLineage, EvalLineage
from mindspore import Model, nn, context
from mindspore.train.callback import SummaryStep
from mindspore.train.summary.summary_record import SummaryRecord
def test_summary():
# Init context env
context.set_context(mode=context.GRAPH_MODE, device_target='Ascend')
# Init hyperparameter
epoch = 2
# Init network and Model
net = Net()
loss_fn = CrossEntropyLoss()
optim = MyOptimizer(learning_rate=0.01)
model = Model(net, loss_fn=loss_fn, optimizer=optim, metrics=None)
# Init SummaryRecord and specify a folder for storing summary log files
# and specify the graph that needs to be recorded
summary_writer = SummaryRecord(log_dir='./summary', network=net)
summary_callback = SummaryStep(summary_writer, flush_step=10)
# Init TrainLineage to record the training information
train_callback = TrainLineage(summary_writer)
# Prepare mindrecord_dataset for training
train_ds = create_mindrecord_dataset_for_training()
model.train(epoch, train_ds, callbacks=[summary_callback, train_callback])
# Init EvalLineage to record the evaluation information
eval_callback = EvalLineage(summary_writer)
# Prepare mindrecord_dataset for testing
eval_ds = create_mindrecord_dataset_for_testing()
model.eval(eval_ds, callbacks=[eval_callback])
# Close summary
summary_writer.close()
```
### 记录算子融合后的计算图
参照“基础写法”完成脚本后,可以通过`context``save_graphs`选项配置记录算子融合后的计算图。
其中`ms_output_after_hwopt.pb`为算子融合后的计算图。
## MindInsight相关命令
### 查看命令帮助信息
```bash
mindinsight --help
```
### 查看版本信息
```bash
mindinsight --version
```
### 启动服务
```bash
mindinsight start [-h] [--config <CONFIG>] [--workspace <WORKSPACE>]
[--port <PORT>] [--reload-interval <RELOAD_INTERVAL>]
[--summary-base-dir <SUMMARY_BASE_DIR>]
```
参数含义如下:
- `-h, --help` : 显示启动命令的帮助信息。
- `--config <CONFIG>` : 指定配置文件或配置模块,CONFIG为物理文件路径(file:/path/to/config.py)或Python可识别的模块路径(python:path.to.config.module)。
- `--workspace <WORKSPACE>` : 指定工作目录路径,WORKSPACE默认为 $HOME/mindinsight。
- `--port <PORT>` : 指定Web可视化服务端口,取值范围是1~65535,PORT默认为8080。
- `--reload-interval <RELOAD_INTERVAL>` : 指定加载数据的时间间隔(单位:秒),设置为0时表示只加载一次数据,RELOAD_INTERVAL默认为3秒。
- `--summary-base-dir <SUMMARY_BASE_DIR>` : 指定加载训练日志数据的根目录路径,MindInsight将遍历此路径下的直属子目录,查找文件名符合正则表达式'summary.\d+'或'\.pb$'的日志文件。若某个直属子目录包含日志文件,则该子目录被识别为日志文件目录,若根目录包含日志文件,则根目录被识别为日志文件目录。SUMMARY_BASE_DIR默认为当前目录路径。
> 服务启动时,命令行参数值将被保存为进程的环境变量,并以 `MINDINSIGHT_` 开头作为标识,如 `MINDINSIGHT_CONFIG`,`MINDINSIGHT_WORKSPACE`,`MINDINSIGHT_PORT` 等。
### 停止服务
```bash
mindinsight stop [-h] [--port PORT]
```
参数含义如下:
- `-h, --help` : 显示停止命令的帮助信息。
- `--port <PORT>` : 指定Web可视化服务端口,取值范围是1~65535,PORT默认为8080。
### 查看服务进程信息
MindInsight向用户提供Web服务,可通过以下命令,查看当前运行的Web服务进程。
```bash
ps -ef | grep mindinsight
```
根据服务进程PID,可通过以下命令,查看当前服务进程对应的工作目录WORKSPACE。
```bash
lsof -p <PID> | grep access
```
输出如下,可查看WORKSPACE。
```bash
gunicorn <PID> <USER> <FD> <TYPE> <DEVICE> <SIZE/OFF> <NODE> <WORKSPACE>/log/gunicorn/access.log
```
## 可视化组件
### 计算图可视化
计算图可视化用于展示计算图的图结构,数据流以及控制流的走向。
![graph.png](./images/graph.png)
图1:计算图展示区
图1展示了计算图的网络结构。如图中所展示的,在展示区右边的区域中,选中其中一个算子(图中圈红算子),可以看到该算子有两个输入和三个输出(实线代表算子的数据流走向)。
![graph_sidebar.png](./images/graph_sidebar.png)
图2:计算图功能区
图2展示了计算图可视化的功能区,包含以下内容:
* 文件选择框: 可以选择查看不同文件的计算图。
* 搜索框:可以对节点进行搜索,输入节点名称点击回车,即可展示该节点。
* 缩略图:展示整个网络图结构的缩略图,在查看超大图结构时,方便查看当前浏览的区域。
* 节点信息:展示选中的节点的基本信息,包括节点的名称、属性、输入节点、输出节点等信息。
* 图例:展示的是计算图中各个图标的含义。
### 标量可视化
标量可视化用于展示训练过程中,标量的变化趋势情况。
![scalar.png](./images/scalar.png)
图3:标量趋势图
图3展示了神经网络在训练过程中损失值的变化过程。横坐标是训练步骤,纵坐标是损失值。
图中右上角有几个按钮功能,从左到右功能分别是全屏展示,切换Y轴比例,开启/关闭框选,分步回退和还原图形。
- 全屏展示即全屏展示该标量曲线,再点击一次即可恢复。
- 切换Y轴比例是指可以将Y轴坐标进行对数转换。
- 开启/关闭框选是指可以框选图中部分区域,并放大查看该区域, 可以在已放大的图形上叠加框选。
- 分步回退是指对同一个区域连续框选并放大查看时,可以逐步撤销操作。
- 还原图形是指进行了多次框选后,点击此按钮可以将图还原回原始状态。
![scalar_select.png](./images/scalar_select.png)
图4:标量可视化功能区
图4展示的标量可视化的功能区,提供了根据选择不同标签,水平轴的不同维度和平滑度来查看标量信息的功能。
- 标签:提供了对所有标签进行多项选择的功能,用户可以通过勾选所需的标签,查看对应的标量信息。
- 水平轴:可以选择“步骤”、“相对时间”、“绝对时间”中的任意一项,来作为标量曲线的水平轴。
- 平滑度:可以通过调整平滑度,对标量曲线进行平滑处理。
- 标量合成:可以选中两条标量曲线进行合成并展示在一个图中,以方便对两条曲线进行对比或者查看合成后的图。
![scalar_compound.png](./images/scalar_compound.png)
图5:Accuracy和Loss的标量合成图
图5展示Accuracy曲线和Loss曲线的标量合成图。标量合成的功能区与标量可视化的功能区相似。其中与标量可视化功能区不一样的地方,在于标签选择时,标量合成功能最多只能同时选择两个标签,将其曲线合成并展示。
### 图像可视化
图像可视化用于展示用户所指定的图片。
![image.png](./images/image_vi.png)
图6:图像可视化
图6展示通过滑动图中“步骤”滑条,查看不同步骤的图片。
![image_function.png](./images/image_function.png)
图7:图像可视化功能区
图7展示图像可视化的功能区,提供了选择查看不同标签,不同亮度和不同对比度来查看图片信息。
- 标签:提供了对所有标签进行多项选择的功能,用户可以通过勾选所需的标签,查看对应的图片信息。
- 亮度调整:可以调整所展示的所有图片亮度。
- 对比度调整:可以调整所展示的所有图片对比度。
### 模型溯源可视化
模型溯源可视化用于展示所有训练的模型参数信息。
![image.png](./images/lineage_label.png)
图8:模型参数选择区
图8展示的模型参数选择区,列举了可供查看的模型参数标签。用户可以通过勾选所需的标签,查看相应的模型参数。
![image.png](./images/lineage_model_chart.png)
图9:模型溯源功能区
图9展示的模型溯源功能区,图像化展示了模型的参数信息。用户可以通过选择列的特定区域,展示区域范围内的模型信息。
![image.png](./images/lineage_model_table.png)
图10:模型列表
图10展示所有模型信息,用户可以按指定列进行升序或降序展示模型信息。
### 数据图可视化
数据图可视化用于展示单次模型训练的数据处理和数据增强信息。
![data_function.png](./images/data_function.png)
图11:数据图功能区
图11展示的数据图功能区包含以下内容:
* 图例:展示数据溯源图中各个图标的含义。
* 数据处理流水线:展示训练所使用的数据处理流水线,可以选择图中的单个节点查看详细信息。
* 节点信息:展示选中的节点的基本信息,包括使用的数据处理和增强算子的名称、参数等。
### 数据溯源可视化
数据溯源可视化用于展示所有训练的数据处理和数据增强信息。
![data_label.png](./images/data_label.png)
图12:数据处理和增强算子选择区
图12展示的数据处理和数据增强算子选择区,列举了可供查看的数据处理和增强算子的名称。用户可以通过勾选所需的标签,查看相应的参数等信息。
![data_chart.png](./images/data_chart.png)
图13:数据溯源功能区
图13展示的数据溯源功能区,图像化展示了数据处理和数据增强使用的参数信息。用户可以通过选择列的特定区域,展示区域范围内的参数信息。
![data_table.png](./images/data_table.png)
图14:数据溯源列表
图14展示所有模型训练的数据处理和数据增强信息。
\ No newline at end of file
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
# -- Project information -----------------------------------------------------
project = 'MindSpore'
copyright = '2020, MindSpore'
author = 'MindSpore'
# The full version, including alpha/beta/rc tags
release = '0.1.0-alpha'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'recommonmark',
'sphinx_markdown_tables',
]
source_suffix = {
'.rst': 'restructuredtext',
'.md': 'markdown',
}
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
pygments_style = 'sphinx'
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_search_language = 'zh'
html_search_options = {'dict': '../resource/jieba.txt'}
.. MindSpore documentation master file, created by
sphinx-quickstart on Thu Mar 24 09:00:00 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
MindSpore教程
=============
.. toctree::
:glob:
:maxdepth: 1
:caption: 快速入门
quick_start/quick_start
.. toctree::
:glob:
:maxdepth: 1
:caption: 使用指南
use/data_preparation/data_preparation
use/saving_and_loading_model_parameters
use/debugging_in_pynative_mode
.. toctree::
:glob:
:maxdepth: 1
:caption: 进阶使用
advanced_use/visualization_tutorials
advanced_use/mixed_precision
advanced_use/distributed_training
advanced_use/computer_vision_application
advanced_use/nlp_application
advanced_use/customized_debugging_information
advanced_use/on_device_inference
advanced_use/model_security
advanced_use/community
.. toctree::
:glob:
:maxdepth: 1
:caption: 声明
statement/legal_statement
statement/privacy_policy
\ No newline at end of file
# 实现一个图片分类应用
<!-- TOC -->
- [实现一个图片分类应用](#实现一个图片分类应用)
- [概述](#概述)
- [准备环节](#准备环节)
- [下载数据集](#下载数据集)
- [导入Python库&模块](#导入python库模块)
- [配置运行信息](#配置运行信息)
- [数据处理](#数据处理)
- [定义数据集及数据操作](#定义数据集及数据操作)
- [定义网络](#定义网络)
- [定义损失函数及优化器](#定义损失函数及优化器)
- [基本概念](#基本概念)
- [定义损失函数](#定义损失函数)
- [定义优化器](#定义优化器)
- [训练网络](#训练网络)
- [配置模型保存](#配置模型保存)
- [配置训练网络](#配置训练网络)
- [运行并查看结果](#运行并查看结果)
- [验证模型](#验证模型)
<!-- /TOC -->
## 概述
下面我们通过一个实际样例,带领大家体验MindSpore基础的功能,对于一般的用户而言,完成整个样例实践会持续20~30分钟。
本例子会实现一个简单的图片分类的功能,整体流程如下:
1. 处理需要的数据集,这里使用了MNIST数据集。
2. 定义一个网络,这里我们使用LeNet网络。
3. 定义损失函数和优化器。
4. 加载数据集并进行训练,训练完成后,查看结果及保存模型文件。
5. 加载保存的模型,进行推理。
6. 验证模型,加载测试数据集和训练后的模型,验证结果精度。
> 你可以在这里找到完整可运行的样例代码:<https://gitee.com/mindspore/docs/blob/r0.1/tutorials/tutorial_code/lenet.py> 。
这是简单、基础的应用流程,其他高级、复杂的应用可以基于这个基本流程进行扩展。
## 准备环节
在动手进行实践之前,确保,你已经正确安装了MindSpore。如果没有,可以通过[MindSpore安装页面](https://www.mindspore.cn/install)将MindSpore安装在你的电脑当中。
同时希望你拥有Python编码基础和概率、矩阵等基础数学知识。
那么接下来,就开始MindSpore的体验之旅吧。
### 下载数据集
我们示例中用到的`MNIST`数据集是由10类28*28的灰度图片组成,训练数据集包含60000张图片,测试数据集包含10000张图片。
> MNIST数据集下载页面:<http://yann.lecun.com/exdb/mnist/>。页面提供4个数据集下载链接,其中前2个文件是训练数据需要,后2个文件是测试结果需要。
将数据集下载并解压到本地路径下,这里将数据集解压分别存放到工作区的`./MNIST_Data/train``./MNIST_Data/test`路径下。
目录结构如下:
```
└─MNIST_Data
├─test
│ t10k-images.idx3-ubyte
│ t10k-labels.idx1-ubyte
└─train
train-images.idx3-ubyte
train-labels.idx1-ubyte
```
> 为了方便样例使用,我们在样例脚本中添加了自动下载数据集的功能。
### 导入Python库&模块
在使用前,需要导入需要的Python库。
目前使用到`os`库,为方便理解,其他需要的库,我们在具体使用到时再说明。
```python
import os
```
详细的MindSpore的模块说明,可以在[MindSpore API页面](https://www.mindspore.cn/api/zh-CN/0.1.0-alpha/index.html)中搜索查询。
### 配置运行信息
在正式编写代码前,需要了解MindSpore运行所需要的硬件、后端等基本信息。
可以通过`context.set_context()`来配置运行需要的信息,譬如运行模式、后端信息、硬件等信息。
导入`context`模块,配置运行需要的信息。
```python
import argparse
from mindspore import context
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='MindSpore LeNet Example')
parser.add_argument('--device_target', type=str, default="Ascend", choices=['Ascend', 'GPU', 'CPU'],
help='device where the code will be implemented (default: Ascend)')
args = parser.parse_args()
context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target,
enable_mem_reuse=False)
...
```
在样例中我们配置样例运行使用图模式。根据实际情况配置硬件信息,譬如代码运行在Ascend AI处理器上,则`--device_target`选择`Ascend`,代码运行在CPU、GPU同理。详细参数说明,请参见`context.set_context()`接口说明。
## 数据处理
数据集对于训练非常重要,好的数据集可以有效提高训练精度和效率。在加载数据集前,我们通常会对数据集进行一些处理。
### 定义数据集及数据操作
我们定义一个函数`create_dataset()`来创建数据集。在这个函数中,我们定义好需要进行的数据增强和处理操作:
1. 定义数据集。
2. 定义进行数据增强和处理所需要的一些参数。
3. 根据参数,生成对应的数据增强操作。
4. 使用`map()`映射函数,将数据操作应用到数据集。
5. 对生成的数据集进行处理。
```python
import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.transforms.vision.c_transforms as CV
from mindspore.dataset.transforms.vision import Inter
from mindspore.common import dtype as mstype
def create_dataset(data_path, batch_size=32, repeat_size=1,
num_parallel_workers=1):
""" create dataset for train or test
Args:
data_path: Data path
batch_size: The number of data records in each group
repeat_size: The number of replicated data records
num_parallel_workers: The number of parallel workers
"""
# define dataset
mnist_ds = ds.MnistDataset(data_path)
# define operation parameters
resize_height, resize_width = 32, 32
rescale = 1.0 / 255.0
shift = 0.0
rescale_nml = 1 / 0.3081
shift_nml = -1 * 0.1307 / 0.3081
# define map operations
resize_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR) # resize images to (32, 32)
rescale_nml_op = CV.Rescale(rescale_nml, shift_nml) # normalize images
rescale_op = CV.Rescale(rescale, shift) # rescale images
hwc2chw_op = CV.HWC2CHW() # change shape from (height, width, channel) to (channel, height, width) to fit network.
type_cast_op = C.TypeCast(mstype.int32) # change data type of label to int32 to fit network
# apply map operations on images
mnist_ds = mnist_ds.map(input_columns="label", operations=type_cast_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=resize_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=rescale_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=rescale_nml_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=hwc2chw_op, num_parallel_workers=num_parallel_workers)
# apply DatasetOps
buffer_size = 10000
mnist_ds = mnist_ds.shuffle(buffer_size=buffer_size) # 10000 as in LeNet train script
mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True)
mnist_ds = mnist_ds.repeat(repeat_size)
return mnist_ds
```
其中,
`batch_size`:每组包含的数据个数,现设置每组包含32个数据。
`repeat_size`:数据集复制的数量。
先进行shuffle、batch操作,再进行repeat操作,这样能保证1个epoch内数据不重复。
> MindSpore支持进行多种数据处理和增强的操作,各种操作往往组合使用,具体可以参考[数据处理与数据增强](https://www.mindspore.cn/tutorial/zh-CN/0.1.0-alpha/use/data_preparation/data_processing_and_augmentation.html)章节。
## 定义网络
我们选择相对简单的LeNet网络。LeNet网络不包括输入层的情况下,共有7层:2个卷积层、2个下采样层(池化层)、3个全连接层。每层都包含不同数量的训练参数,如下图所示:
![LeNet-5](./images/LeNet_5.jpg)
> 更多的LeNet网络的介绍不在此赘述,希望详细了解LeNet网络,可以查询<http://yann.lecun.com/exdb/lenet/>。
我们需要对全连接层以及卷积层进行初始化。
`TruncatedNormal`:参数初始化方法,MindSpore支持`TruncatedNormal``Normal``Uniform`等多种参数初始化方法,具体可以参考MindSpore API的`mindspore.common.initializer`模块说明。
初始化示例代码如下:
```python
import mindspore.nn as nn
from mindspore.common.initializer import TruncatedNormal
def weight_variable():
"""
weight initial
"""
return TruncatedNormal(0.02)
def conv(in_channels, out_channels, kernel_size, stride=1, padding=0):
"""
conv layer weight initial
"""
weight = weight_variable()
return nn.Conv2d(in_channels, out_channels,
kernel_size=kernel_size, stride=stride, padding=padding,
weight_init=weight, has_bias=False, pad_mode="valid")
def fc_with_initialize(input_channels, out_channels):
"""
fc layer weight initial
"""
weight = weight_variable()
bias = weight_variable()
return nn.Dense(input_channels, out_channels, weight, bias)
```
使用MindSpore定义神经网络需要继承`mindspore.nn.cell.Cell``Cell`是所有神经网络(`Conv2d`等)的基类。
神经网络的各层需要预先在`__init__()`方法中定义,然后通过定义`construct()`方法来完成神经网络的前向构造。按照LeNet的网络结构,定义网络各层如下:
```python
import mindspore.ops.operations as P
class LeNet5(nn.Cell):
"""
Lenet network structure
"""
#define the operator required
def __init__(self):
super(LeNet5, self).__init__()
self.batch_size = 32
self.conv1 = conv(1, 6, 5)
self.conv2 = conv(6, 16, 5)
self.fc1 = fc_with_initialize(16 * 5 * 5, 120)
self.fc2 = fc_with_initialize(120, 84)
self.fc3 = fc_with_initialize(84, 10)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
self.reshape = P.Reshape()
#use the preceding operators to construct networks
def construct(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.conv2(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.reshape(x, (self.batch_size, -1))
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
return x
```
## 定义损失函数及优化器
### 基本概念
在进行定义之前,先简单介绍损失函数及优化器的概念。
- 损失函数:又叫目标函数,用于衡量预测值与实际值差异的程度。深度学习通过不停地迭代来缩小损失函数的值。定义一个好的损失函数,可以有效提高模型的性能。
- 优化器:用于最小化损失函数,从而在训练过程中改进模型。
定义了损失函数后,可以得到损失函数关于权重的梯度。梯度用于指示优化器优化权重的方向,以提高模型性能。
### 定义损失函数
MindSpore支持的损失函数有`SoftmaxCrossEntropyWithLogits``L1Loss``MSELoss`等。这里使用`SoftmaxCrossEntropyWithLogits`损失函数。
```python
from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
```
`__main__`函数中调用定义好的损失函数:
```python
if __name__ == "__main__":
...
#define the loss function
net_loss = SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True, reduction='mean')
...
```
### 定义优化器
MindSpore支持的优化器有`Adam``AdamWeightDecay``Momentum`等。
这里使用流行的`Momentum`优化器。
```python
if __name__ == "__main__":
...
#learning rate setting
lr = 0.01
momentum = 0.9
#create the network
network = LeNet5()
#define the optimizer
net_opt = nn.Momentum(network.trainable_params(), lr, momentum)
...
```
## 训练网络
### 配置模型保存
MindSpore提供了callback机制,可以在训练过程中执行自定义逻辑,这里使用框架提供的`ModelCheckpoint``LossMonitor`为例。
`ModelCheckpoint`可以保存网络模型和参数,以便进行后续的微调(fune-tune)操作,`LossMonitor`可以监控训练过程中`loss`值的变化。
```python
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig
if __name__ == "__main__":
...
# set parameters of check point
config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10)
# apply parameters of check point
ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet", config=config_ck)
...
```
### 配置训练网络
通过MindSpore提供的`model.train`接口可以方便地进行网络的训练。
这里把`epoch_size`设置为1,对数据集进行1个迭代的训练。
```python
from mindspore.nn.metrics import Accuracy
from mindspore.train.callback import LossMonitor
from mindspore.train import Model
...
def train_net(args, model, epoch_size, mnist_path, repeat_size, ckpoint_cb):
"""define the training method"""
print("============== Starting Training ==============")
#load training dataset
ds_train = create_dataset(os.path.join(mnist_path, "train"), 32, repeat_size)
model.train(epoch_size, ds_train, callbacks=[ckpoint_cb, LossMonitor()], dataset_sink_mode=False)
...
if __name__ == "__main__":
...
epoch_size = 1
mnist_path = "./MNIST_Data"
repeat_size = epoch_size
model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
train_net(args, model, epoch_size, mnist_path, repeat_size, ckpoint_cb)
...
```
其中,
`train_net`方法中,我们加载了之前下载的训练数据集,`mnist_path`是MNIST数据集路径。
## 运行并查看结果
使用以下命令运行脚本:
```
python lenet.py --device_target=CPU
```
其中,
`lenet.py`:为你根据教程编写的脚本文件。
`--device_target CPU`:指定运行硬件平台,参数为`CPU``GPU`或者`Ascend`,根据你的实际运行硬件平台来指定。
训练过程中会打印loss值,类似下图。loss值会波动,但总体来说loss值会逐步减小,精度逐步提高。每个人运行的loss值有一定随机性,不一定完全相同。
训练过程中loss打印示例如下:
```bash
...
epoch: 1 step: 262, loss is 1.9212162
epoch: 1 step: 263, loss is 1.8498616
epoch: 1 step: 264, loss is 1.7990671
epoch: 1 step: 265, loss is 1.9492403
epoch: 1 step: 266, loss is 2.0305142
epoch: 1 step: 267, loss is 2.0657792
epoch: 1 step: 268, loss is 1.9582214
epoch: 1 step: 269, loss is 0.9459006
epoch: 1 step: 270, loss is 0.8167224
epoch: 1 step: 271, loss is 0.7432692
...
```
训练完后,即保存的模型文件,示例如下:
```bash
checkpoint_lenet-1_1875.ckpt
```
其中,
`checkpoint_lenet-1_1875.ckpt`:指保存的模型参数文件。名称具体含义checkpoint_{网络名称}-{第几个epoch}_{第几个step}.ckpt。
## 验证模型
在得到模型文件后,通过模型运行测试数据集得到的结果,验证模型的泛化能力。
1. 使用`model.eval()`接口读入测试数据集。
2. 使用保存后的模型参数进行推理。
```python
from mindspore.train.serialization import load_checkpoint, load_param_into_net
...
def test_net(args,network,model,mnist_path):
"""define the evaluation method"""
print("============== Starting Testing ==============")
#load the saved model for evaluation
param_dict = load_checkpoint("checkpoint_lenet-1_1875.ckpt")
#load parameter to the network
load_param_into_net(network, param_dict)
#load testing dataset
ds_eval = create_dataset(os.path.join(mnist_path, "test"))
acc = model.eval(ds_eval, dataset_sink_mode=False)
print("============== Accuracy:{} ==============".format(acc))
if __name__ == "__main__":
...
test_net(args, network, model, mnist_path)
```
其中,
`load_checkpoint()`:通过该接口加载CheckPoint模型参数文件,返回一个参数字典。
`checkpoint_lenet-1_1875.ckpt`:之前保存的CheckPoint模型文件名称。
`load_param_into_net`:通过该接口把参数加载到网络中。
使用运行命令,运行你的代码脚本。
```bash
python lenet.py --device_target=CPU
```
其中,
`lenet.py`:为你根据教程编写的脚本文件。
`--device_target CPU`:指定运行硬件平台,参数为`CPU``GPU`或者`Ascend`,根据你的实际运行硬件平台来指定。
运行结果示例如下:
```
...
============== Starting Testing ==============
============== Accuracy:{'Accuracy': 0.9742588141025641} ==============
```
可以在打印信息中看出模型精度数据,示例中精度数据达到97.4%,模型质量良好。
# 法律声明
## 规则及MindSpore开源项目网址访问条件
【MindSpore开源项目网站】指由MindSpore开源项目运营的网站,域名为<https://www.mindspore.cn>。以下规则适用于所有访问MindSpore开源项目网站的用户或浏览者,MindSpore开源项目保留根据国家法律法规及市场行情等变化修改这些规则的权利。访问MindSpore开源项目网站的权利由MindSpore开源项目根据下列条款授予。如果您不同意下列任何条款、请停止使用本网址。如果您使用<https://www.mindspore.cn>服务,您的使用行为将被视为对本声明全部内容的认可。对于违反这些规则的行为,MindSpore开源项目有权采取法律和公平的补救措施。
## 不承诺责任声明
MindSpore开源项目网站所载的材料和信息,包括但不限于文本、图片、数据、观点、建议、网页或链接,MindSpore开源项目力图在网站上提供准确的材料和信息,但MindSpore开源项目并不保证这些材料和内容的准确、完整、充分和可靠性,并且明确声明不对这些材料和内容的错误或遗漏承担责任,也不对这些材料和内容作出任何明示或默示的、包括但不限于有关所有权担保、没有侵犯第三方权利、质量和没有计算机病毒的保证。
MindSpore开源项目可以在没有任何通知或提示的情况下随时对MindSpore开源项目服务网站上的内容进行修改,为了得到最新版本的信息,请您定时访问MindSpore开源项目网站。MindSpore开源项目在MindSpore开源项目网站上所提及的非MindSpore开源项目产品或服务仅仅是为了提供相关信息,并不构成对这些产品、服务的认可或推荐。MindSpore开源项目并不就网址上提供的任何产品、服务或信息做出任何声明、保证或认可,所有销售的产品和服务应受MindSpore开源项目的销售合同和条款的约束。
鉴于<https://www.mindspore.cn>提供的部分服务属于电子公告牌(BBS)服务,<https://www.mindspore.cn>上关于其会员或其会员发布的相关信息(包括但不限于用户名称、公司名称、 联系人及联络信息,相关图片、视讯等)的信息均是由会员自行提供,会员依法应对其提供的任何信息承担全部责任。
任何企业或个人认为<https://www.mindspore.cn>网页内容(包括但不限于<https://www.mindspore.cn>会员发布的商品信息)可能涉嫌侵犯其合法权益,应该及时向MindSpore开源项目提出书面权利通知,并提供身份证明、权属证明、具体链接(URL)及详细侵权情况证明。MindSpore开源项目在收到上述法律文件后,将会依法尽快移除相关涉嫌侵权的内容。
## 著作权说明
MindSpore开源项目网站所载的所有材料或内容受版权法的保护,所有版权拥有,但注明引用其他方的内容除外。未经MindSpore开源项目或其他方事先书面许可,任何人不得将MindSpore开源项目网站上的任何内容以任何方式进行复制、经销、翻印、传播、以超级链路连接或传送、以镜像法载入其他服务器上、存储于信息检索系统或者其他任何商业目的的使用,但对于非商业目的的、用户使用的下载或打印(条件是不得修改,且须保留该材料中的版权说明或其他所有权的说明)除外。
## 商标
MindSpore开源项目网站上使用和显示的所有商标、标志皆属MindSpore开源项目所有,但注明属于其他方拥有的商标、标志、商号除外。MindSpore开源项目网站所载的任何内容不应被视作未经MindSpore开源项目或其他方书面许可,以暗示、不反对或其他形式授予使用前述任何商标、标志的许可或权利。未经事先书面许可,任何人不得以任何方式使用MindSpore开源项目名称及MindSpore开源项目的商标、标记。
## 第三方链接
MindSpore开源项目网站可能保留有与第三方网站或网址的链接,访问这些链接将由用户自己作出决定,MindSpore开源项目并不保证这些链接上所提供的任何信息、数据、观点、图片、陈述或建议的准确性、完整性、充分性和可靠性。MindSpore开源项目提供这些链接仅仅在于提供方便,并不表示MindSpore开源项目对这些信息的认可和推荐,也不是用于宣传或广告目的。
# 个人信息保护政策
请您在使用本网站的各项服务以及我们的MindSpore产品前,仔细阅读并了解本《个人信息保护政策》。
为了MindSpore网站的健康运行,以便我们更好地为您服务,我们将收集您的以下个人数据:
1. 您可以在本网站订阅MindSpore最新资讯。为了完成该业务功能所需,您同意授权我们收集和使用您的个人信息或个人敏感信息,包括电子邮箱地址。
2. 如果您参与社区开发,我们需要您签署贡献者协议(CLA), 协议会包含以下个人信息:
- 您的姓名或账户
- 您的地址
- 您的电子邮件地址
- 您的电话号码
- 您的传真号码
- 您在码云(Gitee)的ID
3. 社区也提供邮件列表,方便开发者相互交流和协作,当您注册邮件列表时,我们会收集您的邮件地址信息。
4. 为持续了解MindSpore社区的运行状况,我们将收集您浏览本网站期间包括但不限于您的IP地址、浏览器的类型、使用的语言、访问日期和时间、软硬件特征信息及您需求的网页记录、设备型号、设备识别码、操作系统、分辨率、电信运营商,下载点击等数据。
我们将会为您的上述信息采取相关安全保障措施,包括但不限于加密,请您放心。
对于您提供的上述信息,MindSpore不进行对外共享、转让、公开披露,除非依据您的指示或法律法规的强制性要求。
根据适用的法律法规,您可能有访问、更改、删除、拒绝、限制或移植您上述信息的权利。对于您权利的行使,请通过文末的方式联系我们。
上述所有信息均存储于中华人民共和国境内。
**您应确保,您使用本产品(或服务)符合法律法规的要求。我们仅按照您的指示,为您提供标准MindSpore产品(或服务),并不对您使用本产品(或服务)的合法合规性负责。**
**您理解上述信息均是为您提供订阅服务之必需。如您选择不提供或不同意我们收集、使用以上这些信息,将导致您无法正常使用我们的产品(或服务),我们将无法为您订阅服务。**
**我们如何使用Cookies**
为确保网站正常运转,我们有时会在计算机或移动设备上存储 Cookie 的小数据文件。Cookie 是一种网络服务器存储在计算机或移动设备上的纯文本文件。Cookie 的内容只能由创建它的服务器检索或读取。每个 Cookie 对您的网络浏览器或移动应用程序都是唯一的。Cookie 通常包含标识符、站点名称以及一些号码和字符。借助于 Cookie,网站能够存储用户偏好等数据。
我们启用Cookie的目的与大多数网站或互联网服务提供商启用 Cookie 的目的一样,即改善用户体验。具体而言,有以下目的:
(1)保存设置。借助于 Cookie,网站能够保存设置,例如搜索结果记录,便于返回搜索结果,从而优化浏览器服务。
(2)认证。当您访问本网站时,我们会为您生成一个单独的ID,以标识您的身份。如果网站不使用具有该功能的 Cookie,那么在用户每一次打开网页时,该网站都会将其视为新访客。例如,如果您登录本网站后转到另一个网页,本网站就不会识别出您,而您会被视为新访客。
其它第三方会将Cookie设置在我们网站上,例如谷歌、百度。它们使用Cookie接收IP地址、您浏览器配置相关的信息和访问信息,但不收集您的个人身份信息。它们使用Cookie以了解您和网站的互动情况。您可以通过访问它们的网站以了解这些第三方如何适用Cookie。
您可根据自己的偏好管理或删除 Cookie。有关详情,请参见[AboutCookies](https://www.aboutcookies.org/)。您可以清除计算机上保存的所有 Cookie,大部分网络浏览器都设有阻止 Cookie 的功能。但如果您这么做,则需要在每一次访问我们的网站时亲自更改用户设置。
如您有任何疑问,可通过contact@mindspore.cn联系我们。
最近更新时间:2020年3月
\ No newline at end of file
# 将数据集转换为MindSpore数据格式
<!-- TOC -->
- [将数据集转换为MindSpore数据格式](#将数据集转换为mindspore数据格式)
- [概述](#概述)
- [将非标准数据集转换为MindSpore数据格式](#将非标准数据集转换为mindspore数据格式)
- [转换图片及标注数据](#转换图片及标注数据)
- [将常见数据集转换为MindSpore数据格式](#将常见数据集转换为mindspore数据格式)
- [转换CIFAR-10数据集](#转换cifar-10数据集)
- [转换CIFAR-100数据集](#转换cifar-100数据集)
- [转换ImageNet数据集](#转换imagenet数据集)
- [转换MNIST数据集](#转换mnist数据集)
<!-- /TOC -->
## 概述
用户可以将非标准的数据集和常见的数据集转换为MindSpore数据格式,从而方便地加载到MindSpore中进行训练。同时,MindSpore在部分场景做了性能优化,使用MindSpore数据格式可以获得更好的性能体验。
MindSpore数据格式具备的特征如下:
1. 实现多变的用户数据统一存储、访问,训练数据读取更简便。
2. 数据聚合存储,高效读取,且方便管理、移动。
3. 高效数据编解码操作,对用户透明、无感知。
4. 灵活控制分区大小,实现分布式训练。
## 将非标准数据集转换为MindSpore数据格式
MindSpore提供写操作工具,可将用户定义的原始数据写为MindSpore数据格式。
### 转换图片及标注数据
1. 导入文件写入工具类`FileWriter`
```python
from mindspore.mindrecord import FileWriter
```
2. 定义数据集Schema,Schema用于定义数据集包含哪些字段以及字段的类型。
```python
cv_schema_json = {"file_name": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
```
其中,Schema的相关规范如下:
字段名:字母、数字、下划线。
字段属性type:int32、int64、float32、float64、string、bytes。
字段属性shape:[...], ...可以是一维数组,用[-1]表示; 可以是二维数组,用[m, n]表示;可以是三维数组,用[x, y, z]表示。
> 1. 如果字段有属性Shape,暂时只支持type为int32、int64、float32、float64类型。
> 2. 如果字段有属性Shape,则用户在准备数据并传入write_raw_data接口时必须是numpy.ndarray类型。
举例:
- 图片分类
```python
cv_schema_json = {"file_name": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
```
- NLP类
```python
cv_schema_json = {"id": {"type": "int32"}, "masks": {"type": "int32", "shape": [-1]}, "inputs": {"type": "int64", "shape": [4, 32]}, "labels": {"type": "int64", "shape": [-1]}}
```
3. 准备需要写入的数据,按照用户定义的Schema形式,准备需要写入的样本列表。
```python
data = [{"file_name": "1.jpg", "label": 0, "data": b"\x10c\xb3w\xa8\xee$o&<q\x8c\x8e(\xa2\x90\x90\x96\xbc\xb1\x1e\xd4QER\x13?\xff\xd9"},
{"file_name": "2.jpg", "label": 56, "data": b"\xe6\xda\xd1\xae\x07\xb8>\xd4\x00\xf8\x129\x15\xd9\xf2q\xc0\xa2\x91YFUO\x1dsE1\x1ep"},
{"file_name": "3.jpg", "label": 99, "data": b"\xaf\xafU<\xb8|6\xbd}\xc1\x99[\xeaj+\x8f\x84\xd3\xcc\xa0,i\xbb\xb9-\xcdz\xecp{T\xb1\xdb\"}]
```
4. 准备索引字段,添加索引字段可以加速数据读取,该步骤非必选。
```python
indexes = ["file_name", "label"]
```
5. 创建`FileWriter`对象,传入文件名,分片数量,然后添加Schema,添加索引,调用`write_raw_data`接口写入数据,最后调用`commit`接口生成本地数据文件。
```python
writer = FileWriter(file_name="testWriter.mindrecord", shard_num=4)
writer.add_schema(cv_schema_json, "test_schema")
writer.add_index(indexes)
writer.write_raw_data(data)
writer.commit()
```
其中,
`write_raw_data`:会将数据写入到内存中。
`commit`:最终将内存中的数据写入到磁盘。
6. 在现有数据格式文件中增加新数据,调用`open_for_append`接口打开已存在的数据文件,继续调用`write_raw_data`接口写入新数据,最后调用`commit`接口生成本地数据文件。
```python
writer = FileWriter.open_for_append("testWriter.mindrecord0")
writer.write_raw_data(data)
writer.commit()
```
## 将常见数据集转换为MindSpore数据格式
MindSpore提供转换常见数据集的工具类,将常见数据集转换为MindSpore数据格式。对于常见的数据集以及调用的工具类列表如下:
| 数据集 | 调用的工具类 |
| -------- | ------------ |
| CIFAR-10 | Cifar10ToMR |
| CIFAR-100| Cifar100ToMR |
| ImageNet | ImageNetToMR |
| MNIST | MnistToMR |
### 转换CIFAR-10数据集
用户可以通过`Cifar10ToMR`类,将CIFAR-10原始数据转换为MindSpore数据格式。
1. 准备好CIFAR-10数据集,这里使用的是用于python解析的数据集(CIFAR-10 python version),将文件解压至指定的目录(示例中将数据集保存到`cifar10`目录),如下所示:
```
% ll cifar10/cifar-10-batches-py/
batches.meta
data_batch_1
data_batch_2
data_batch_3
data_batch_4
data_batch_5
readme.html
test_batch
```
> CIFAR-10数据集下载地址:<https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz>
2. 导入转换数据集的工具类`Cifar10ToMR`
```python
from mindspore.mindrecord import Cifar10ToMR
```
3. 实例化`Cifar10ToMR`对象,调用`transform`接口,将CIFAR-10数据集转换为MindSpore数据格式。
```python
CIFAR10_DIR = "./cifar10/cifar-10-batches-py"
MINDRECORD_FILE = "./cifar10.mindrecord"
cifar10_transformer = Cifar10ToMR(CIFAR10_DIR, MINDRECORD_FILE)
cifar10_transformer.transform(['label'])
```
其中,
`CIFAR10_DIR`:CIFAR-10数据集的文件夹路径。
`MINDRECORD_FILE`:输出的MindSpore数据格式文件路径。
### 转换CIFAR-100数据集
用户可以通过`Cifar100ToMR`类,将CIFAR-100原始数据转换为MindSpore数据格式。
1. 准备好CIFAR-100数据集,将文件解压至指定的目录(示例中将数据集保存到`cifar100`目录),如下所示:
```
% ll cifar100/cifar-100-python/
meta
test
train
```
> CIFAR-100数据集下载地址:<https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz>
2. 导入转换数据集的工具类`Cifar100ToMR`
```python
from mindspore.mindrecord import Cifar100ToMR
```
3. 实例化`Cifar100ToMR`对象,调用`transform`接口,将CIFAR-100数据集转换为MindSpore数据格式。
```python
CIFAR100_DIR = "./cifar100/cifar-100-python"
MINDRECORD_FILE = "./cifar100.mindrecord"
cifar100_transformer = Cifar100ToMR(CIFAR100_DIR, MINDRECORD_FILE)
cifar100_transformer.transform(['fine_label', 'coarse_label'])
```
其中,
`CIFAR100_DIR`:CIFAR-100数据集的文件夹路径。
`MINDRECORD_FILE`:输出的MindSpore数据格式文件路径。
### 转换ImageNet数据集
用户可以通过`ImageNetToMR`类,将ImageNet原始数据(图片、标注)转换为MindSpore数据格式。
1. 下载并按照要求准备好ImageNet数据集。
> ImageNet数据集下载地址:<http://image-net.org/download>
对下载后的ImageNet数据集,整理数据集组织形式为一个包含所有图片的文件夹,以及一个记录图片对应标签的映射文件。
标签映射文件包含3列,分别为各类别图片目录、标签ID、标签名,用空格隔开,映射文件示例如下:
```
n02119789 1 pen
n02100735 2 notbook
n02110185 3 mouse
n02096294 4 orange
```
2. 导入转换数据集的工具类`ImageNetToMR`
```python
from mindspore.mindrecord import ImageNetToMR
```
3. 实例化`ImageNetToMR`对象,调用`transform`接口,将数据集转换为MindSpore数据格式。
```python
IMAGENET_MAP_FILE = "./testImageNetDataWhole/labels_map.txt"
IMAGENET_IMAGE_DIR = "./testImageNetDataWhole/images"
MINDRECORD_FILE = "./testImageNetDataWhole/imagenet.mindrecord"
PARTITION_NUMBER = 4
imagenet_transformer = ImageNetToMR(IMAGENET_MAP_FILE, IMAGENET_IMAGE_DIR, MINDRECORD_FILE, PARTITION_NUMBER)
imagenet_transformer.transform()
```
其中,
`IMAGENET_MAP_FILE`:ImageNetToMR数据集的标签映射文件路径。
`IMAGENET_IMAGE_DIR`:包含ImageNet所有图片的文件夹路径。
`MINDRECORD_FILE`:输出的MindSpore数据格式文件路径。
### 转换MNIST数据集
用户可以通过`MnistToMR`类,将MNIST原始数据转换为MindSpore数据格式。
1. 准备MNIST数据集,将下载好的文件放至指定的目录,如下所示:
```
% ll mnist_data/
train-images-idx3-ubyte.gz
train-labels-idx1-ubyte.gz
t10k-images-idx3-ubyte.gz
t10k-labels-idx1-ubyte.gz
```
> MNIST数据集下载地址:<http://yann.lecun.com/exdb/mnist>
2. 导入转换数据集的工具类`MnistToMR`
```python
from mindspore.mindrecord import MnistToMR
```
3. 实例化`MnistToMR`对象,调用`transform`接口,将MNIST数据集转换为MindSpore数据格式。
```python
MNIST_DIR = "./mnist_data"
MINDRECORD_FILE = "./mnist.mindrecord"
mnist_transformer = MnistToMR(MNIST_DIR, MINDRECORD_FILE)
mnist_transformer.transform()
```
其中,
`MNIST_DIR`:MNIST数据集的文件夹路径。
`MINDRECORD_FILE`:输出的MindSpore数据格式文件路径。
准备数据
========
.. toctree::
:maxdepth: 1
loading_the_datasets
converting_datasets
data_processing_and_augmentation
\ No newline at end of file
# 数据处理与数据增强
<!-- TOC -->
- [数据处理与数据增强](#数据处理与数据增强)
- [概述](#概述)
- [MindSpore支持的数据处理操作](#mindspore支持的数据处理操作)
- [repeat](#repeat)
- [batch](#batch)
- [shuffle](#shuffle)
- [map](#map)
- [zip](#zip)
- [数据增强](#数据增强)
- [使用`c_transforms`模块进行数据增强](#使用c_transforms模块进行数据增强)
- [使用`py_transforms`模块进行数据增强](#使用py_transforms模块进行数据增强)
<!-- /TOC -->
## 概述
数据是深度学习的基础,有好的数据输入,可以对整个深度神经网络训练起到非常积极的作用。所以在获取到原始的数据集后,数据加载训练前,因为数据量、性能等等限制,往往会需要先进行数据处理或者数据增强,从而获得更加优化的数据输入。
同样,MindSpore也为用户提供了数据处理以及数据增强的功能。
> 从本质上来说,数据增强是通过数据处理中的`map`(映射)进行实现,但是因为数据增强提供丰富的变换操作,所以将其单独提出进行描述。
## MindSpore支持的数据处理操作
MindSpore支持多种处理数据操作,包括复制、分批、洗牌、映射等等,详细见下表:
| 数据处理 | 说明 |
| -------- | -------------------------------------- |
| repeat | 对数据集进行复制,来加倍数据量。 |
| batch | 将数据分批处理,有益于加速训练过程。 |
| shuffle | 对数据进行混洗。 |
| map | 将提供的函数或算子作用于指定的列数据。 |
| zip | 将多个数据集合并成一个数据集。 |
各个操作可以单独进行使用,实际使用过程中往往会根据实际需要进行组合使用,组合使用时推荐使用的顺序如下所示:
![avatar](../images/dataset_pipeline.png)
如下示例中,读取MNIST数据集时,对数据进行shuffle、batch、repeat操作。
```python
import mindspore.dataset as ds
ds1 = ds.MnistDataset(MNIST_DATASET_PATH, MNIST_SCHEMA) # Create MNIST dataset.
ds1 = ds1.shuffle(buffer_size=10000)
ds1 = ds1.batch(32, drop_remainder=True)
ds1 = ds1.repeat(10)
```
上面操作先对数据进行混洗,再将每32条数据组成一个batch,最后将数据集重复10次。
下面将构造一个简单数据集`ds1`,并对其进行数据处理操作,来介绍各类数据处理操作的详细使用。
1. 导入数据处理操作依赖的模块。
```python
import mindspore.dataset as ds
```
2. 定义一个生成数据集的函数——`generator_func()`用于生成数据集。
```python
def generator_func():
for i in range(5):
yield (np.array([i, i+1, i+2]),)
```
3. 通过`GeneratorDataset`创建`ds1`数据集,用于执行数据处理操作。
```python
ds1 = ds.GeneratorDataset(generator_func, ["data"])
print("ds1:")
for data in ds1.create_dict_iterator():
print(data["data"])
```
输出:
```
ds1:
[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]
```
### repeat
在有限的数据集内,为了优化网络,通常会将一个数据集训练多次。
![avatar](../images/repeat.png)
> 在机器学习中,每训练完一个完整的数据集,我们称为训练完了一个epoch。
加倍数据集,通常用在多个epoch(迭代)训练中,通过`repeat()`来加倍数据量。`repeat()`定义如下:
```python
def repeat(self, count=None):
```
我们可以定义`ds2`数据集,调用`repeat`来加倍数据量。示例代码如下:
```python
ds2 = ds.GeneratorDataset(generator_func, ["data"])
ds2 = ds2.repeat(2)
print("ds2:")
for data in ds2.create_dict_iterator():
print(data["data"])
```
其中,将倍数设为2,故`ds2`数据量为原始数据集`ds1`的2倍,输出:
```
ds2:
[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]
[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]
```
### batch
将数据集进行分批。在实际训练中,可将数据分批处理,将几个数据作为1组,进行训练,减少训练轮次,达到加速训练过程的目的。MindSpore通过`batch()`函数来实现数据集分批,函数定义如下:
![avatar](../images/batch.png)
```python
def batch(self, batch_size, drop_remainder=False, num_parallel_workers=None)
```
使用GeneratorDataset产生的数据集`ds1`构建2个数据集。
- 第1个数据集`ds2`,每2条数据为1组。
- 第2个数据集`ds3`,每3条数据为1组,并将不足1组的数据截掉。
`ds2`示例代码如下:
```python
ds2 = ds1.batch(batch_size=2) # Default drop_remainder is False, the last remainder batch isn't dropped.
print("batch size:2 drop remainder:False")
for data in ds2.create_dict_iterator():
print(data["data"])
```
输出如下所示:
```
batch size:2 drop remainder:False
[[0 1 2]
[1 2 3]]
[[2 3 4]
[3 4 5]]
[[4 5 6]]
```
`ds3`示例代码如下:
```python
ds3 = ds1.batch(batch_size=3, drop_remainder=True) # When drop_remainder is True, the last remainder batch will be dropped.
print("batch size:3 drop remainder:True")
for data in ds3.create_dict_iterator():
print(data["data"])
```
输出如下所示:
```
batch size:3 drop remainder:True
[[0 1 2]
[1 2 3]
[2 3 4]]
```
### shuffle
对于有序的数据集或者进行过repeat的数据集可以进行混洗。
![avatar](../images/shuffle.png)
shuffle操作主要用来将数据混洗,设定的buffer_size越大,混洗程度越大,但时间、计算资源消耗会大。
`shuffle()`定义如下:
```python
def shuffle(self, buffer_size):
```
调用`shuffle()`对数据集`ds1`进行混洗,示例代码如下:
```python
print("Before shuffle:")
for data in ds1.create_dict_iterator():
print(data["data"])
ds2 = ds1.shuffle(buffer_size=5)
print("After shuffle:")
for data in ds2.create_dict_iterator():
print(data["data"])
```
可能的输出如下,可以看到进行数据混洗后,数据的顺序发生了随机的变化。
```
Before shuffle:
[0 1 2]
[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]
After shuffle:
[3 4 5]
[2 3 4]
[4 5 6]
[1 2 3]
[0 1 2]
```
### map
map(映射)即对数据进行处理,譬如将彩色图片的数据集转化为灰色图片的数据集等,应用非常灵活。
MindSpore提供`map()`函数对数据集进行映射操作,用户可以将提供的函数或算子作用于指定的列数据。
用户可以自定义函数,也可以直接使用`c_transforms``py_transforms`做数据增强。
> 详细的数据增强操作,将在文后数据增强章节进行介绍。
![avatar](../images/map.png)
`map()`函数定义如下:
```python
def map(self, input_columns=None, operations=None, output_columns=None, columns_order=None,
num_parallel_workers=None):
```
在以下示例中,使用`map()`函数,将定义的匿名函数(lambda函数)作用于数据集`ds1`,使数据集中数据乘以2。
```python
func = lambda x : x*2 # Define lambda function to multiply each element by 2.
ds2 = ds1.map(input_columns="data", operations=func)
for data in ds2.create_dict_iterator():
print(data["data"])
```
代码输出如下,可以看到数据集`ds2`中各行的数据均乘以了2。
```
[0 2 4]
[2 4 6]
[4 6 8]
[6 8 10]
[8 10 12]
```
### zip
MindSpore提供`zip()`函数,可将多个数据集合并成1个数据集。
> 如果两个数据集的列名相同,则不会合并,请注意列的命名。
> 如果两个数据集的行数不同,合并后的行数将和较小行数保持一致。
```python
def zip(self, datasets):
```
1. 采用前面构造数据集`ds1`的方法,构造1个数据集`ds2`
```python
def generator_func2():
for i in range(5):
yield (np.array([i-3, i-2, i-1]),)
ds2 = ds.GeneratorDataset(generator_func2, ["data2"])
```
2. 通过`zip()`将数据集`ds1``data1`列和数据集`ds2``data2`列合并成数据集`ds3`
```python
ds3 = ds.zip((ds1, ds2))
for data in ds3.create_dict_iterator():
print(data)
```
输出如下所示:
```
{'data1': array([0, 1, 2], dtype=int64), 'data2': array([-3, -2, -1], dtype=int64)}
{'data1': array([1, 2, 3], dtype=int64), 'data2': array([-2, -1, 0], dtype=int64)}
{'data1': array([2, 3, 4], dtype=int64), 'data2': array([-1, 0, 1], dtype=int64)}
{'data1': array([3, 4, 5], dtype=int64), 'data2': array([0, 1, 2], dtype=int64)}
{'data1': array([4, 5, 6], dtype=int64), 'data2': array([1, 2, 3], dtype=int64)}
```
## 数据增强
在图片训练中,尤其在数据集较小的情况下,用户可以通过一系列的数据增强操作对图片进行预处理,从而丰富了数据集。
MindSpore提供`c_transforms`模块以及`py_transforms`模块函数供用户进行数据增强操作,用户也可以自定义函数或者算子进行数据增强。MindSpore提供的两个模块的简要说明如下表,详细的介绍请参考API中对应模块的说明。
| 模块名称 | 实现 | 说明 |
| ---------------| ------------------------------------------------------ | --- |
| `c_transforms` | 基于C++的[OpenCV](https://opencv.org/)实现 | 具有较高的性能。 |
| `py_transforms` | 基于Python的[PIL](https://pypi.org/project/Pillow/)实现 | 该模块提供了多种图像增强功能,并提供了PIL Image和numpy数组之间的传输方法。 |
对于喜欢在图像学习任务中使用Python PIL的用户,`py_transforms`模块是处理图像增强的好工具。用户还可以使用Python PIL自定义自己的扩展。
数据增强需要使用`map()`函数,详细`map()`函数的使用,可参考[map](#map)章节。
### 使用`c_transforms`模块进行数据增强
1. 将该模块引入进代码。
```python
import mindspore.dataset.transforms.vision.c_transforms as transforms
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
```
2. 定义数据增强算子,以`Resize`为例:
```python
dataset = ds.ImageFolderDatasetV2(DATA_DIR, decode=True) # Deocde images.
resize_op = transforms.Resize(size=(500,500), interpolation=Inter.LINEAR)
dataset.map(input_columns="image", operations=resize_op)
for data in dataset.create_dict_iterator():
imgplot_resized = plt.imshow(data["image"])
plt.show()
```
运行结果可以看到,原始图片与进行数据处理(`Resize()`)后的图片对比,可以看到图片由原来的1024\*683像素,变化为500\*500像素。
![avatar](../images/image.png)
图1:原始图片
![avatar](../images/image_resized.png)
图2:重设尺寸后的图片
### 使用`py_transforms`模块进行数据增强
1. 将该模块引入到代码。
```python
import mindspore.dataset.transforms.vision.py_transforms as transforms
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
```
2. 定义数据增强算子,通过`ComposeOp`接口将多个数据增强组合使用, 以`RandomCrop`为例:
```python
dataset = ds.ImageFolderDatasetV2(DATA_DIR)
transforms_list = [
transforms.Decode(), # Decode images to PIL format.
transforms.RandomCrop(size=(500,500)),
transforms.ToTensor() # Convert PIL images to Numpy ndarray.
]
compose = transforms.ComposeOp(transforms_list)
dataset = dataset.map(input_columns="image", operations=compose())
for data in dataset.create_dict_iterator():
print(data["image"])
imgplot_resized = plt.imshow(data["image"].transpose(1, 2, 0))
plt.show()
```
运行结果可以看到,原始图片与进行数据处理(`RandomCrop()`)后的图片对比,可以看到图片由原来的1024\*683像素,变化为500\*500像素。
![avatar](../images/image.png)
图1:原始图片
![avatar](../images/image_random_crop.png)
图2:按500*500随机裁剪后的图片
# 加载数据集
<!-- TOC -->
- [加载数据集](#加载数据集)
- [概述](#概述)
- [加载常见的数据集](#加载常见的数据集)
- [加载特定数据格式的数据集](#加载特定数据格式的数据集)
- [MindSpore数据格式](#mindspore数据格式)
- [`Manifest`数据格式](#manifest数据格式)
- [`TFRecord`数据格式](#tfrecord数据格式)
- [加载自定义数据集](#加载自定义数据集)
<!-- /TOC -->
## 概述
MindSpore可以帮助你加载常见的数据集、特定数据格式的数据集或自定义的数据集。加载数据集时,需先导入所需要依赖的库`mindspore.dataset`
```python
import mindspore.dataset as ds
```
## 加载常见的数据集
MindSpore可以加载常见的标准数据集。支持的数据集如下表:
| 数据集 | 简要说明 |
| --------- | -------------------------------------------------------------------------------------------------------------------------- |
| ImageNet | ImageNet是根据WordNet层次结构组织的图像数据库,其中层次结构的每个节点都由成百上千个图像表示。 |
| MNIST | 是一个手写数字图像的大型数据库,通常用于训练各种图像处理系统。 |
| CIFAR-10 | 常用于训练图像的采集机器学习和计算机视觉算法。CIFAR-10数据集包含10种不同类别的60,000张32x32彩色图像。 |
| CIFAR-100 | 该数据集类似于CIFAR-10,不同之处在于它有100个类别,每个类别包含600张图像。每个课程有500张训练图像和100张测试图像。 |
| PASCAL-VOC | 数据内容多样,可用于训练计算机视觉模型(分类、定位、检测、分割、动作识别等)。 |
| CelebA | CelebA人脸数据集包含上万个名人身份的人脸图片,每张图片有40个特征标记,常用于人脸相关的训练任务。 |
加载常见数据集的详细步骤如下,以创建`CIFAR-10`对象为例,用于加载支持的数据集。
1. 下载[CIFAR-10数据集](https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz),并解压。这里使用的是二进制格式的数据集(CIFAR-10 binary version)。
2. 配置数据集目录,定义需要加载的数据集实例。
```python
DATA_DIR = "cifar10_dataset_dir/"
cifar10_dataset = ds.Cifar10Dataset(DATA_DIR)
```
3. 创建迭代器,通过迭代器读取数据。
```python
for data in cifar10_dataset.create_dict_iterator():
# In CIFAR-10 dataset, each dictionary of data has keys "image" and "label".
print(data["image"])
print(data["label"])
```
## 加载特定数据格式的数据集
### MindSpore数据格式
MindSpore天然支持读取MindSpore数据格式——`MindRecord`存储的数据集,在性能和特性上有更好的支持。
> 阅读[将数据集转换为MindSpore数据格式](converting_datasets.md)章节,了解如何将数据集转化为MindSpore数据格式。
可以通过`MindDataset`对象对数据集进行读取。详细方法如下所示:
1. 创建`MindDataset`,用于读取数据。
```python
CV_FILE_NAME = os.path.join(MODULE_PATH, "./imagenet.mindrecord")
data_set = ds.MindDataset(dataset_file=CV_FILE_NAME)
```
其中,
`dataset_file`:指定MindRecord的文件,含路径及文件名。
2. 创建字典迭代器,通过迭代器读取数据记录。
```python
num_iter = 0
for data in data_set.create_dict_iterator():
print(data["label"])
num_iter += 1
```
### `Manifest`数据格式
`Manifest`是华为ModelArts支持的数据格式文件,详细说明请参见:<https://support.huaweicloud.com/engineers-modelarts/modelarts_23_0009.html>
Mindspore对Manifest格式的数据集提供了对应的数据集类。如下所示,配置数据集目录,定义需要加载的数据集实例。
```python
DATA_DIR = "manifest_dataset_path"
manifest_dataset = ds.ManifestDataset(DATA_DIR)
```
目前ManifestDataset仅支持加载图片、标签类型的数据集,默认列名为"image"和"label"。
### `TFRecord`数据格式
MindSpore也支持读取`TFRecord`数据格式的数据集,可以通过`TFRecordDataset`对象进行数据集读取。
1. 只需传入数据集路径或.tfrecord文件列表,即可创建`TFRecordDataset`
```python
DATA_DIR = ["tfrecord_dataset_path/train-0000-of-0001.tfrecord"]
dataset = ds.TFRecordDataset(DATA_DIR)
```
2. 用户可以通过创建Schema文件或Schema类,设定数据集格式及特征。
Schema文件示例如下所示:
```
{
"datasetType": "TF",
"numRows": 3,
"columns": {
"image": {
"type": "uint8",
"rank": 1
},
"label" : {
"type": "int64",
"rank": 1
}
}
}
```
其中,
`datasetType`: 数据格式的类型,这里“TF”是指TFrecord数据格式。
`columns`:列信息字段,需要根据数据集的实际列名定义,上面Schema文件示例中,数据集列为image和label两列。
`numRows`:行数信息字段,控制加载数据的最大行数。如果定义的行数大于实际行数,加载时则以实际行数为准。
在创建TFRecordDataset时将Schema文件路径传入,使用样例如下:
```python
DATA_DIR = ["tfrecord_dataset_path/train-0000-of-0001.tfrecord"]
SCHEMA_DIR = "dataset_schema_path/schema.json"
dataset = ds.TFRecordDataset(DATA_DIR, schema=SCHEMA_DIR)
```
创建Schema类使用样例如下:
```python
import mindspore.common.dtype as mstype
schema = ds.Schema()
schema.add_column('image', de_type=mstype.uint8) # Binary data usually use uint8 here.
schema.add_column('label', de_type=mstype.int32)
dataset = ds.TFRecordDataset(DATA_DIR, schema=schema)
```
3. 创建字典迭代器,通过迭代器读取数据。
```python
for data in dataset.create_dict_iterator():
# The dictionary of data has keys "image" and "label" which are consistent with columns names in its schema.
print(data["image"])
print(data["label"])
```
## 加载自定义数据集
对于自定义数据集,可以通过`GeneratorDataset`对象加载。
1. 定义一个函数(示例函数名为`Generator1D`)用于生成数据集的函数。
> 自定义的生成函数返回的是可调用的对象,每次返回`numpy array`的元组,作为一行数据。
自定义函数示例如下:
```python
import numpy as np # Import numpy lib.
def Generator1D():
for i in range(64):
yield (np.array([i]),) # Notice, tuple of only one element needs following a comma at the end.
```
2.`Generator1D`传入`GeneratorDataset`创建数据集,并设定`column`名为“data”。
```python
dataset = ds.GeneratorDataset(Generator1D, ["data"])
```
3. 在创建数据集后,可以通过给数据创建迭代器的方式,获取相应的数据。有两种创建迭代器的方法。
- 创建返回值为序列类型的迭代器。
```python
for data in dataset.create_tuple_iterator(): # each data is a sequence
print(data[0])
```
- 创建返回值为字典类型的迭代器。
```python
for data in dataset.create_dict_iterator(): # each data is a dictionary
print(data["data"])
```
# 使用PyNative模式调试
<!-- TOC -->
- [使用PyNative模式调试](#使用pynative模式调试)
- [概述](#概述)
- [执行单算子](#执行单算子)
- [执行普通函数](#执行普通函数)
- [提升PyNative性能](#提升PyNative性能)
- [调试网络训练模型](#调试网络训练模型)
<!-- /TOC -->
## 概述
MindSpore支持两种运行模式,在调试或者运行方面做了不同的优化:
- PyNative模式:也称动态图模式,将神经网络中的各个算子逐一下发执行,方便用户编写和调试神经网络模型。
- Graph模式:也称静态图模式或者图模式,将神经网络模型编译成一整张图,然后下发执行。该模式利用图优化等技术提高运行性能,同时有助于规模部署和跨平台运行。
默认情况下,MindSpore处于PyNative模式,可以通过`context.set_context(mode=context.GRAPH_MODE)`切换为Graph模式;同样地,MindSpore处于Graph模式时,可以通过 `context.set_context(mode=context.PYNATIVE_MODE)`切换为PyNative模式。
PyNative模式下,支持执行单算子、普通函数和网络,以及单独求梯度的操作。下面将详细介绍使用方法和注意事项。
## 执行单算子
执行单个算子,并打印相关结果,如下例所示。
```python
import numpy as np
import mindspore.nn as nn
from mindspore import context, Tensor
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
conv = nn.Conv2d(3, 4, 3, bias_init='zeros')
input_data = Tensor(np.ones([1, 3, 5, 5]).astype(np.float32))
output = conv(input_data)
print(output.asnumpy())
```
输出:
```python
[[[[-0.02190447 -0.05208071 -0.05208071 -0.05208071 -0.06265172]
[-0.01529094 -0.05286242 -0.05286242 -0.05286242 -0.04228776]
[-0.01529094 -0.05286242 -0.05286242 -0.05286242 -0.04228776]
[-0.01529094 -0.05286242 -0.05286242 -0.05286242 -0.04228776]
[-0.01430791 -0.04892948 -0.04892948 -0.04892948 -0.01096004]]
[[ 0.00802889 -0.00229866 -0.00229866 -0.00229866 -0.00471579]
[ 0.01172971 0.02172665 0.02172665 0.02172665 0.03261888]
[ 0.01172971 0.02172665 0.02172665 0.02172665 0.03261888]
[ 0.01172971 0.02172665 0.02172665 0.02172665 0.03261888]
[ 0.01784375 0.01185635 0.01185635 0.01185635 0.01839031]]
[[ 0.04841832 0.03321705 0.03321705 0.03321705 0.0342317 ]
[ 0.0651359 0.04310361 0.04310361 0.04310361 0.03355784]
[ 0.0651359 0.04310361 0.04310361 0.04310361 0.03355784]
[ 0.0651359 0.04310361 0.04310361 0.04310361 0.03355784]
[ 0.04680437 0.03465693 0.03465693 0.03465693 0.00171057]]
[[-0.01783456 -0.00459451 -0.00459451 -0.00459451 0.02316688]
[ 0.01295831 0.00879035 0.00879035 0.00879035 0.01178642]
[ 0.01295831 0.00879035 0.00879035 0.00879035 0.01178642]
[ 0.01295831 0.00879035 0.00879035 0.00879035 0.01178642]
[ 0.05016355 0.03958241 0.03958241 0.03958241 0.03443141]]]]
```
## 执行普通函数
将若干算子组合成一个函数,然后直接通过函数调用的方式执行这些算子,并打印相关结果,如下例所示。
**示例代码**
```python
import numpy as np
from mindspore import context, Tensor
from mindspore.ops import functional as F
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
def tensor_add_func(x, y):
z = F.tensor_add(x, y)
z = F.tensor_add(z, x)
return z
x = Tensor(np.ones([3, 3], dtype=np.float32))
y = Tensor(np.ones([3, 3], dtype=np.float32))
output = tensor_add_func(x, y)
print(output.asnumpy())
```
**输出**
```python
[[3. 3. 3.]
[3. 3. 3.]
[3. 3. 3.]]
```
### 提升PyNative性能
为了提高PyNative模式下的前向计算任务执行速度,MindSpore提供了Staging功能,该功能可以在PyNative模式下将Python函数或者Python类的方法编译成计算图,通过图优化等技术提高运行速度,如下例所示。
```python
import numpy as np
import numpy as np
import mindspore.nn as nn
from mindspore import context, Tensor
import mindspore.ops.operations as P
from mindspore.common.api import ms_function
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
class TensorAddNet(nn.Cell):
def __init__(self):
super(TensorAddNet, self).__init__()
self.add = P.TensorAdd()
@ms_function
def construct(self, x, y):
res = self.add(x, y)
return res
x = Tensor(np.ones([4, 4]).astype(np.float32))
y = Tensor(np.ones([4, 4]).astype(np.float32))
net = TensorAddNet()
z = net(x, y) # Staging mode
tensor_add = P.TensorAdd()
res = tensor_add(x, z) # PyNative mode
print(res.asnumpy())
```
**输出**
```python
[[3. 3. 3. 3.]
[3. 3. 3. 3.]
[3. 3. 3. 3.]
[3. 3. 3. 3.]]
```
上述示例代码中,在`TensorAddNet`类的`construct`之前加装了`ms_function`装饰器,该装饰器会将`construct`方法编译成计算图,在给定输入之后,以图的形式下发执行,而上一示例代码中的`F.tensor_add`会直以普通的PyNative的方式执行。
需要说明的是,加装了`ms_function`装饰器的函数中,如果包含不需要进行参数训练的算子(如`pooling``tensor_add`等算子),则这些算子可以在被装饰的函数中直接调用,如下例所示。
**示例代码**
```python
import numpy as np
import mindspore.nn as nn
from mindspore import context, Tensor
import mindspore.ops.operations as P
from mindspore.common.api import ms_function
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
tensor_add = P.TensorAdd()
@ms_function
def tensor_add_fn(x, y):
res = tensor_add(x, y)
return res
x = Tensor(np.ones([4, 4]).astype(np.float32))
y = Tensor(np.ones([4, 4]).astype(np.float32))
z = tensor_add_fn(x, y)
print(z.asnumpy())
```
**输出**
```shell
[[2. 2. 2. 2.]
[2. 2. 2. 2.]
[2. 2. 2. 2.]
[2. 2. 2. 2.]]
```
如果被装饰的函数中包含了需要进行参数训练的算子(如`Convolution``BatchNorm`等算子),则这些算子必须在被装饰等函数之外完成实例化操作,如下例所示。
**示例代码**
```python
import numpy as np
import mindspore.nn as nn
from mindspore import context, Tensor
from mindspore.common.api import ms_function
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
conv_obj = nn.Conv2d(in_channels=3, out_channels=4, kernel_size=3, stride=2, padding=0)
@ms_function
def conv_fn(x):
res = conv_obj(x)
return res
input_data = np.random.randn(2, 3, 6, 6).astype(np.float32)
z = conv_fn(Tensor(input_data))
print(z.asnumpy())
```
**输出**
```shell
[[[[ 0.10377571 -0.0182163 -0.05221086]
[ 0.1428334 -0.01216263 0.03171652]
[-0.00673915 -0.01216291 0.02872104]]
[[ 0.02906547 -0.02333629 -0.0358406 ]
[ 0.03805163 -0.00589525 0.04790922]
[-0.01307234 -0.00916951 0.02396654]]
[[ 0.01477884 -0.06549098 -0.01571796]
[ 0.00526886 -0.09617482 0.04676902]
[-0.02132788 -0.04203424 0.04523344]]
[[ 0.04590619 -0.00251453 -0.00782715]
[ 0.06099087 -0.03445276 0.00022781]
[ 0.0563223 -0.04832596 -0.00948266]]]
[[[ 0.08444098 -0.05898955 -0.039262 ]
[ 0.08322686 -0.0074796 0.0411371 ]
[-0.02319113 0.02128408 -0.01493311]]
[[ 0.02473745 -0.02558945 -0.0337843 ]
[-0.03617039 -0.05027632 -0.04603915]
[ 0.03672804 0.00507637 -0.08433761]]
[[ 0.09628943 0.01895323 -0.02196114]
[ 0.04779419 -0.0871575 0.0055248 ]
[-0.04382382 -0.00511185 -0.01168541]]
[[ 0.0534859 0.02526264 0.04755395]
[-0.03438103 -0.05877855 0.06530266]
[ 0.0377498 -0.06117418 0.00546303]]]]
```
## 调试网络训练模型
PyNative模式下,还可以支持单独求梯度的操作。如下例所示,可通过`grad_all`求该函数或者网络所有的输入梯度。
**示例代码**
```python
from mindspore.ops import composite as C
import mindspore.context as context
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
def mul(x, y):
return x * y
def mainf(x, y):
return C.grad_all(mul)(x, y)
print(mainf(1,2))
```
**输出**
```python
(2, 1)
```
在进行网络训练时,求得梯度然后调用优化器对参数进行优化(暂不支持在反向计算梯度的过程中设置断点),然后再利用前向计算loss,从而实现在PyNative模式下进行网络训练。
**完整LeNet示例代码**
```python
import numpy as np
import mindspore.nn as nn
import mindspore.ops.operations as P
from mindspore.nn import Dense
from mindspore import context, Tensor, ParameterTuple
from mindspore.common.initializer import TruncatedNormal
from mindspore.ops import composite as C
from mindspore.common import dtype as mstype
from mindspore.nn.wrap.cell_wrapper import WithLossCell
from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
from mindspore.nn.optim import Momentum
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
def conv(in_channels, out_channels, kernel_size, stride=1, padding=0):
"""weight initial for conv layer"""
weight = weight_variable()
return nn.Conv2d(in_channels, out_channels,
kernel_size=kernel_size, stride=stride, padding=padding,
weight_init=weight, has_bias=False, pad_mode="valid")
def fc_with_initialize(input_channels, out_channels):
"""weight initial for fc layer"""
weight = weight_variable()
bias = weight_variable()
return nn.Dense(input_channels, out_channels, weight, bias)
def weight_variable():
"""weight initial"""
return TruncatedNormal(0.02)
class LeNet5(nn.Cell):
"""
Lenet network
Args:
num_class (int): Num classes. Default: 10.
Returns:
Tensor, output tensor
Examples:
>>> LeNet(num_class=10)
"""
def __init__(self, num_class=10):
super(LeNet5, self).__init__()
self.num_class = num_class
self.batch_size = 32
self.conv1 = conv(1, 6, 5)
self.conv2 = conv(6, 16, 5)
self.fc1 = fc_with_initialize(16 * 5 * 5, 120)
self.fc2 = fc_with_initialize(120, 84)
self.fc3 = fc_with_initialize(84, self.num_class)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
self.reshape = P.Reshape()
def construct(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.conv2(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.reshape(x, (self.batch_size, -1))
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
return x
class GradWrap(nn.Cell):
""" GradWrap definition """
def __init__(self, network):
super(GradWrap, self).__init__(auto_prefix=False)
self.network = network
self.weights = ParameterTuple(filter(lambda x: x.requires_grad, network.get_parameters()))
def construct(self, x, label):
weights = self.weights
return C.grad_by_list(self.network, weights)(x, label)
net = LeNet5()
optimizer = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.1, 0.9)
criterion = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
net_with_criterion = WithLossCell(net, criterion)
train_network = GradWrap(net_with_criterion)
train_network.set_train()
input_data = Tensor(np.ones([net.batch_size, 1, 32, 32]).astype(np.float32) * 0.01)
label = Tensor(np.ones([net.batch_size]).astype(np.int32))
output = net(Tensor(input_data))
loss_output = criterion(output, label)
grads = train_network(input_data, label)
success = optimizer(grads)
loss = loss_output.asnumpy()
print(loss)
```
**输出**
```python
2.3050091
```
上述执行方式中,可以在construct函数任意需要的地方设置断点,获取网络执行的中间结果,通过pdb的方式对网络进行调试。
\ No newline at end of file
# 模型参数的保存和加载
<!-- TOC -->
- [模型参数的保存和加载](#模型参数的保存和加载)
- [概述](#概述)
- [模型参数保存](#模型参数保存)
- [CheckPoint配置策略](#checkpoint配置策略)
- [模型参数加载](#模型参数加载)
- [用于推理验证](#用于推理验证)
- [用于再训练场景](#用于再训练场景)
<!-- /TOC -->
## 概述
在模型训练过程中,可以添加检查点(CheckPoint)用于保存模型的参数,以便进行推理及中断后再训练使用。
使用场景如下:
- 训练后推理场景
- 模型训练完毕后保存模型的参数,用于推理或预测操作。
- 训练过程中,通过实时验证精度,把精度最高的模型参数保存下来,用于预测操作。
- 再训练场景
- 进行长时间训练任务时,保存训练过程中的CheckPoint文件,防止任务异常退出后从初始状态开始训练。
- Fine Tune(微调):训练一个模型并保存参数,然后针对不同任务进行Fine Tune操作。
MindSpore的CheckPoint文件是一个二进制文件,存储了所有训练参数的值。采用了Google的Protocol Buffers机制,与开发语言、平台无关,具有良好的可扩展性。
CheckPoint的protocol格式定义在`mindspore/ccsrc/utils/checkpoint.proto`中。
以下通过一个示例来介绍MindSpore保存和加载的功能,网络选取ResNet-50,数据集为MNIST。
## 模型参数保存
在模型训练的过程中,使用callback机制传入回调函数`ModelCheckpoint`对象,可以保存模型参数,生成CheckPoint文件。
通过`CheckpointConfig`对象可以设置CheckPoint的保存策略。
保存的参数分为网络参数和优化器参数。
`ModelCheckpoint()`提供默认配置策略,方便用户快速上手。
具体用法如下:
```python
from mindspore.train.callback import ModelCheckpoint
ckpoint_cb = ModelCheckpoint()
model.train(epoch_num, dataset, callbacks=ckpoint_cb)
```
用户可以根据具体需求对CheckPoint策略进行配置。
具体用法如下:
```python
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig
config_ck = CheckpointConfig(save_checkpoint_steps=32, keep_checkpoint_max=10)
ckpoint_cb = ModelCheckpoint(prefix='resnet50', directory=None, config=config_ck)
model.train(epoch_num, dataset, callbacks=ckpoint_cb)
```
上述代码中,首先需要初始化一个`CheckpointConfig`类对象,用来设置保存策略。
`save_checkpoint_steps`表示每隔多少个step保存一次,`keep_checkpoint_max`表示最多保留CheckPoint文件的数量。
`prefix`表示生成CheckPoint文件的前缀名,`directory`表示存放文件的目录。
创建一个`ModelCheckpoint`对象把它传递给model.train方法,就可以在训练过程中使用CheckPoint功能了。
生成的CheckPoint文件如下:
> - resnet50-graph.meta # 编译后的计算图
> - resnet50-1_32.ckpt # CheckPoint文件后缀名为'.ckpt'
> - resnet50-2_32.ckpt # 文件的命名方式表示保存参数所在的epoch和step数
> - resnet50-3_32.ckpt # 表示保存的是第3个epoch的第32个step的模型参数
> - ...
如果用户使用相同的前缀名,运行多次训练脚本,可能会生成同名CheckPoint文件。
MindSpore为方便用户区分每次生成的文件,会在用户定义的前缀后添加"_"和数字加以区分。
例:`resnet50_3-2_32.ckpt` 表示运行第3次脚本生成的第2个epoch的第32个step的CheckPoint文件。
> 当保存的单个模型参数较大时(超过64M),会因为Protobuf自身对数据大小的限制,导致保存失败。这时可通过设置环境变量`PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python`解除限制。
### CheckPoint配置策略
MindSpore提供了两种保存CheckPoint策略: 迭代策略和时间策略,可以通过创建`CheckpointConfig`对象设置相应策略。
`CheckpointConfig`中共有四个参数可以设置:
- save_checkpoint_steps: 表示每隔多少个step保存一个CheckPoint文件,默认值为1。
- save_checkpoint_seconds:表示每隔多少秒保存一个CheckPoint文件,默认值为0。
- keep_checkpoint_max:表示最多保存多少个CheckPoint文件,默认值为5。
- keep_checkpoint_per_n_minutes:表示每隔多少分钟保留一个CheckPoint文件,默认值为0。
`save_checkpoint_steps``keep_checkpoint_max`为迭代策略,根据训练迭代的次数进行配置。
`save_checkpoint_seconds``keep_checkpoint_per_n_minutes`为时间策略,根据训练的时长进行配置。
两种策略不能同时使用,迭代策略优先级高于时间策略,当同时设置时,只有迭代策略可以生效。
当参数显示设置为`None`时,表示放弃该策略。
在迭代策略脚本正常结束的情况下,会默认保存最后一个step的CheckPoint文件。
## 模型参数加载
保存好CheckPoint文件后,就可以对参数进行加载。
### 用于推理验证
针对仅推理场景可以使用`load_checkpoint`把参数直接加载到网络中,以便进行后续的推理验证。
示例代码如下:
```python
resnet = ResNet50()
load_checkpoint("resnet50-2_32.ckpt", net=resnet)
dateset_eval = create_dataset(os.path.join(mnist_path, "test"), 32, 1) # define the test dataset
loss = CrossEntropyLoss()
model = Model(resnet, loss)
acc = model.eval(dataset_eval)
```
`load_checkpoint`方法会把参数文件中的网络参数加载到模型中。加载后,网络中的参数就是CheckPoint保存的。
`eval`方法会验证训练后模型的精度。
### 用于再训练场景
针对任务中断再训练及Fine Tune场景,可以加载网络参数和优化器参数到模型中。
示例代码如下:
```python
# return a parameter dict for model
param_dict = load_checkpoint("resnet50-2_32.ckpt")
resnet = ResNet50()
opt = Momentum()
# load the parameter into net
load_param_into_net(resnet, param_dict)
# load the parameter into operator
load_param_into_net(opt, param_dict)
loss = SoftmaxCrossEntropyWithLogits()
model = Model(resnet, loss, opt)
model.train(epoch, dataset)
```
`load_checkpoint`方法会返回一个参数字典,`load_param_into_net`会把参数字典中相应的参数加载到网络或优化器中。
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
"""resnet50_distributed_training"""
import os
import random
import argparse
import mindspore.nn as nn
import mindspore.common.dtype as mstype
import mindspore.ops.functional as F
import mindspore.dataset as ds
import mindspore.dataset.transforms.vision.c_transforms as vision
import mindspore.dataset.transforms.c_transforms as C
from mindspore.communication.management import init, get_rank, get_group_size
from mindspore import Tensor
from mindspore.ops import operations as P
from mindspore.nn.optim.momentum import Momentum
from mindspore.train.model import Model, ParallelMode
from mindspore import context
from mindspore.train.callback import LossMonitor
from resnet import resnet50
device_id = int(os.getenv('DEVICE_ID'))
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
context.set_context(enable_task_sink=True, device_id=device_id) # set task_sink and device_id
context.set_context(enable_hccl=True) # set enable_hccl
context.set_context(enable_loop_sink=False)
context.set_context(enable_mem_reuse=False)
init()
rank_id = get_rank()
rank_size = get_group_size()
def create_dataset(repeat_num=1, batch_size=32, rank_id=0, rank_size=1):
resize_height = 224
resize_width = 224
rescale = 1.0 / 255.0
shift = 0.0
# get rank_id and rank_size
rank_id = get_rank()
rank_size = get_group_size()
data_set = ds.Cifar10Dataset(data_path, num_shards=rank_size, shared_id=rank_id)
# define map operations
random_crop_op = vision.RandomCrop((32, 32), (4, 4, 4, 4))
random_horizontal_op = vision.RandomHorizontalFlip()
resize_op = vision.Resize((resize_height, resize_width))
rescale_op = vision.Rescale(rescale, shift)
normalize_op = vision.Normalize((0.4465, 0.4822, 0.4914), (0.2010, 0.1994, 0.2023))
changeswap_op = vision.HWC2CHW()
type_cast_op = C.TypeCast(mstype.int32)
c_trans = [random_crop_op, random_horizontal_op]
c_trans += [resize_op, rescale_op, normalize_op, changeswap_op]
# apply map operations on images
data_set = data_set.map(input_columns="label", operations=type_cast_op)
data_set = data_set.map(input_columns="image", operations=c_trans)
# apply repeat operations
data_set = data_set.repeat(repeat_num)
# apply shuffle operations
data_set = data_set.shuffle(buffer_size=10)
# apply batch operations
data_set = data_set.batch(batch_size=batch_size, drop_remainder=True)
return data_set
class SoftmaxCrossEntropyExpand(nn.Cell):
def __init__(self, sparse=False):
super(SoftmaxCrossEntropyExpand, self).__init__()
self.exp = P.Exp()
self.sum = P.ReduceSum(keep_dims=True)
self.onehot = P.OneHot()
self.on_value = Tensor(1.0, mstype.float32)
self.off_value = Tensor(0.0, mstype.float32)
self.div = P.Div()
self.log = P.Log()
self.sum_cross_entropy = P.ReduceSum(keep_dims=False)
self.mul = P.Mul()
self.mul2 = P.Mul()
self.mean = P.ReduceMean(keep_dims=False)
self.sparse = sparse
self.max = P.ReduceMax(keep_dims=True)
self.sub = P.Sub()
self.eps = Tensor(1e-24, mstype.float32)
def construct(self, logit, label):
logit_max = self.max(logit, -1)
exp = self.exp(self.sub(logit, logit_max))
exp_sum = self.sum(exp, -1)
softmax_result = self.div(exp, exp_sum)
if self.sparse:
label = self.onehot(label, F.shape(logit)[1], self.on_value, self.off_value)
softmax_result_log = self.log(softmax_result + self.eps)
loss = self.sum_cross_entropy((self.mul(softmax_result_log, label)), -1)
loss = self.mul2(F.scalar_to_array(-1.0), loss)
loss = self.mean(loss, -1)
return loss
def test_train_cifar(num_classes=10, epoch_size=10):
context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL, mirror_mean=True)
loss_cb = LossMonitor()
dataset = create_dataset(epoch_size)
net = resnet50(32, num_classes)
loss = SoftmaxCrossEntropyExpand(sparse=True)
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9)
model = Model(net, loss_fn=loss, optimizer=opt)
model.train(epoch_size, dataset, callbacks=[loss_cb], dataset_sink_mode=False)
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
"""Lenet Tutorial"""
import os
import urllib.request
from urllib.parse import urlparse
import gzip
import argparse
import mindspore.dataset as ds
import mindspore.nn as nn
from mindspore import context
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor
from mindspore.train import Model
import mindspore.ops.operations as P
from mindspore.common.initializer import TruncatedNormal
import mindspore.dataset.transforms.vision.c_transforms as CV
import mindspore.dataset.transforms.c_transforms as C
from mindspore.dataset.transforms.vision import Inter
from mindspore.nn.metrics import Accuracy
from mindspore.common import dtype as mstype
from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
def unzipfile(gzip_path):
"""unzip dataset file
Args:
gzip_path: dataset file path
"""
open_file = open(gzip_path.replace('.gz',''), 'wb')
gz_file = gzip.GzipFile(gzip_path)
open_file.write(gz_file.read())
gz_file.close()
def download_dataset():
"""Download the dataset from http://yann.lecun.com/exdb/mnist/."""
print("******Downloading the MNIST dataset******")
train_path = "./MNIST_Data/train/"
test_path = "./MNIST_Data/test/"
train_path_check = os.path.exists(train_path)
test_path_check = os.path.exists(test_path)
if train_path_check == False and test_path_check ==False:
os.makedirs(train_path)
os.makedirs(test_path)
train_url = {"http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz", "http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz"}
test_url = {"http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz", "http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz"}
for url in train_url:
url_parse = urlparse(url)
# split the file name from url
file_name = os.path.join(train_path,url_parse.path.split('/')[-1])
if not os.path.exists(file_name.replace('.gz','')):
file = urllib.request.urlretrieve(url, file_name)
unzipfile(file_name)
os.remove(file_name)
for url in test_url:
url_parse = urlparse(url)
# split the file name from url
file_name = os.path.join(test_path,url_parse.path.split('/')[-1])
if not os.path.exists(file_name.replace('.gz','')):
file = urllib.request.urlretrieve(url, file_name)
unzipfile(file_name)
os.remove(file_name)
def create_dataset(data_path, batch_size=32, repeat_size=1,
num_parallel_workers=1):
""" create dataset for train or test
Args:
data_path: Data path
batch_size: The number of data records in each group
repeat_size: The number of replicated data records
num_parallel_workers: The number of parallel workers
"""
# define dataset
mnist_ds = ds.MnistDataset(data_path)
# define operation parameters
resize_height, resize_width = 32, 32
rescale = 1.0 / 255.0
shift = 0.0
rescale_nml = 1 / 0.3081
shift_nml = -1 * 0.1307 / 0.3081
# define map operations
resize_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR) # Resize images to (32, 32)
rescale_nml_op = CV.Rescale(rescale_nml, shift_nml) # normalize images
rescale_op = CV.Rescale(rescale, shift) # rescale images
hwc2chw_op = CV.HWC2CHW() # change shape from (height, width, channel) to (channel, height, width) to fit network.
type_cast_op = C.TypeCast(mstype.int32) # change data type of label to int32 to fit network
# apply map operations on images
mnist_ds = mnist_ds.map(input_columns="label", operations=type_cast_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=resize_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=rescale_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=rescale_nml_op, num_parallel_workers=num_parallel_workers)
mnist_ds = mnist_ds.map(input_columns="image", operations=hwc2chw_op, num_parallel_workers=num_parallel_workers)
# apply DatasetOps
buffer_size = 10000
mnist_ds = mnist_ds.shuffle(buffer_size=buffer_size) # 10000 as in LeNet train script
mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True)
mnist_ds = mnist_ds.repeat(repeat_size)
return mnist_ds
def conv(in_channels, out_channels, kernel_size, stride=1, padding=0):
"""Conv layer weight initial."""
weight = weight_variable()
return nn.Conv2d(in_channels, out_channels,
kernel_size=kernel_size, stride=stride, padding=padding,
weight_init=weight, has_bias=False, pad_mode="valid")
def fc_with_initialize(input_channels, out_channels):
"""Fc layer weight initial."""
weight = weight_variable()
bias = weight_variable()
return nn.Dense(input_channels, out_channels, weight, bias)
def weight_variable():
"""Weight initial."""
return TruncatedNormal(0.02)
class LeNet5(nn.Cell):
"""Lenet network structure."""
# define the operator required
def __init__(self):
super(LeNet5, self).__init__()
self.batch_size = 32
self.conv1 = conv(1, 6, 5)
self.conv2 = conv(6, 16, 5)
self.fc1 = fc_with_initialize(16 * 5 * 5, 120)
self.fc2 = fc_with_initialize(120, 84)
self.fc3 = fc_with_initialize(84, 10)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
self.reshape = P.Reshape()
# use the preceding operators to construct networks
def construct(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.conv2(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.reshape(x, (self.batch_size, -1))
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
return x
def train_net(args, model, epoch_size, mnist_path, repeat_size, ckpoint_cb):
"""Define the training method."""
print("============== Starting Training ==============")
# load training dataset
ds_train = create_dataset(os.path.join(mnist_path, "train"), 32, repeat_size)
model.train(epoch_size, ds_train, callbacks=[ckpoint_cb, LossMonitor()], dataset_sink_mode=False)
def test_net(args, network, model, mnist_path):
"""Define the evaluation method."""
print("============== Starting Testing ==============")
# load the saved model for evaluation
param_dict = load_checkpoint("checkpoint_lenet-1_1875.ckpt")
# load parameter to the network
load_param_into_net(network, param_dict)
# load testing dataset
ds_eval = create_dataset(os.path.join(mnist_path, "test"))
acc = model.eval(ds_eval, dataset_sink_mode=False)
print("============== Accuracy:{} ==============".format(acc))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='MindSpore LeNet Example')
parser.add_argument('--device_target', type=str, default="Ascend", choices=['Ascend', 'GPU', 'CPU'],
help='device where the code will be implemented (default: Ascend)')
args = parser.parse_args()
context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target,
enable_mem_reuse=False)
# download mnist dataset
download_dataset()
# learning rate setting
lr = 0.01
momentum = 0.9
epoch_size = 1
mnist_path = "./MNIST_Data"
# define the loss function
net_loss = SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True, reduction='mean')
repeat_size = epoch_size
# create the network
network = LeNet5()
# define the optimizer
net_opt = nn.Momentum(network.trainable_params(), lr, momentum)
config_ck = CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10)
# save the network model and parameters for subsequence fine-tuning
ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet", config=config_ck)
# group layers into an object with training and evaluation features
model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
train_net(args, model, epoch_size, mnist_path, repeat_size, ckpoint_cb)
test_net(args, network, model, mnist_path)
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
"""
network config
"""
from easydict import EasyDict as edict
# LSTM CONFIG
lstm_cfg = edict({
'num_classes': 2,
'learning_rate': 0.1,
'momentum': 0.9,
'num_epochs': 1,
'batch_size': 64,
'embed_size': 300,
'num_hiddens': 100,
'num_layers': 2,
'bidirectional': True,
'save_checkpoint_steps': 390,
'keep_checkpoint_max': 10
})
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
"""
LSTM Tutorial
"""
import os
import shutil
import math
import argparse
import json
from itertools import chain
import numpy as np
from config import lstm_cfg as cfg
import mindspore.nn as nn
import mindspore.context as context
import mindspore.dataset as ds
from mindspore.ops import operations as P
from mindspore import Tensor
from mindspore.common.initializer import initializer
from mindspore.common.parameter import Parameter
from mindspore.mindrecord import FileWriter
from mindspore.train import Model
from mindspore.nn.metrics import Accuracy
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor
# Install gensim with 'pip install gensim'
import gensim
def encode_samples(tokenized_samples, word_to_idx):
""" encode word to index """
features = []
for sample in tokenized_samples:
feature = []
for token in sample:
if token in word_to_idx:
feature.append(word_to_idx[token])
else:
feature.append(0)
features.append(feature)
return features
def pad_samples(features, maxlen=500, pad=0):
""" pad all features to the same length """
padded_features = []
for feature in features:
if len(feature) >= maxlen:
padded_feature = feature[:maxlen]
else:
padded_feature = feature
while len(padded_feature) < maxlen:
padded_feature.append(pad)
padded_features.append(padded_feature)
return padded_features
def read_imdb(path, seg='train'):
""" read imdb dataset """
pos_or_neg = ['pos', 'neg']
data = []
for label in pos_or_neg:
files = os.listdir(os.path.join(path, seg, label))
for file in files:
with open(os.path.join(path, seg, label, file), 'r', encoding='utf8') as rf:
review = rf.read().replace('\n', '')
if label == 'pos':
data.append([review, 1])
elif label == 'neg':
data.append([review, 0])
return data
def tokenizer(text):
return [tok.lower() for tok in text.split(' ')]
def collect_weight(glove_path, vocab, word_to_idx, embed_size):
""" collect weight """
vocab_size = len(vocab)
wvmodel = gensim.models.KeyedVectors.load_word2vec_format(os.path.join(glove_path, 'glove.6B.300d.txt'),
binary=False, encoding='utf-8')
weight_np = np.zeros((vocab_size+1, embed_size)).astype(np.float32)
idx_to_word = {i+1: word for i, word in enumerate(vocab)}
idx_to_word[0] = '<unk>'
for i in range(len(wvmodel.index2word)):
try:
index = word_to_idx[wvmodel.index2word[i]]
except KeyError:
continue
weight_np[index, :] = wvmodel.get_vector(
idx_to_word[word_to_idx[wvmodel.index2word[i]]])
return weight_np
def preprocess(aclimdb_path, glove_path, embed_size):
""" preprocess the train and test data """
train_data = read_imdb(aclimdb_path, 'train')
test_data = read_imdb(aclimdb_path, 'test')
train_tokenized = []
test_tokenized = []
for review, _ in train_data:
train_tokenized.append(tokenizer(review))
for review, _ in test_data:
test_tokenized.append(tokenizer(review))
vocab = set(chain(*train_tokenized))
vocab_size = len(vocab)
print("vocab_size: ", vocab_size)
word_to_idx = {word: i+1 for i, word in enumerate(vocab)}
word_to_idx['<unk>'] = 0
train_features = np.array(pad_samples(encode_samples(train_tokenized, word_to_idx))).astype(np.int32)
train_labels = np.array([score for _, score in train_data]).astype(np.int32)
test_features = np.array(pad_samples(encode_samples(test_tokenized, word_to_idx))).astype(np.int32)
test_labels = np.array([score for _, score in test_data]).astype(np.int32)
weight_np = collect_weight(glove_path, vocab, word_to_idx, embed_size)
return train_features, train_labels, test_features, test_labels, weight_np, vocab_size
def get_imdb_data(labels_data, features_data):
data_list = []
for i, (label, feature) in enumerate(zip(labels_data, features_data)):
data_json = {"id": i,
"label": int(label),
"feature": feature.reshape(-1)}
data_list.append(data_json)
return data_list
def convert_to_mindrecord(embed_size, aclimdb_path, proprocess_path, glove_path):
""" convert imdb dataset to mindrecord """
num_shard = 4
train_features, train_labels, test_features, test_labels, weight_np, _ = \
preprocess(aclimdb_path, glove_path, embed_size)
np.savetxt(os.path.join(proprocess_path, 'weight.txt'), weight_np)
# write mindrecord
schema_json = {"id": {"type": "int32"},
"label": {"type": "int32"},
"feature": {"type": "int32", "shape":[-1]}}
writer = FileWriter(os.path.join(proprocess_path, 'aclImdb_train.mindrecord'), num_shard)
data = get_imdb_data(train_labels, train_features)
writer.add_schema(schema_json, "nlp_schema")
writer.add_index(["id", "label"])
writer.write_raw_data(data)
writer.commit()
writer = FileWriter(os.path.join(proprocess_path, 'aclImdb_test.mindrecord'), num_shard)
data = get_imdb_data(test_labels, test_features)
writer.add_schema(schema_json, "nlp_schema")
writer.add_index(["id", "label"])
writer.write_raw_data(data)
writer.commit()
def init_lstm_weight(
input_size,
hidden_size,
num_layers,
bidirectional,
has_bias=True):
"""Initialize lstm weight."""
num_directions = 1
if bidirectional:
num_directions = 2
weight_size = 0
gate_size = 4 * hidden_size
for layer in range(num_layers):
for _ in range(num_directions):
input_layer_size = input_size if layer == 0 else hidden_size * num_directions
weight_size += gate_size * input_layer_size
weight_size += gate_size * hidden_size
if has_bias:
weight_size += 2 * gate_size
stdv = 1 / math.sqrt(hidden_size)
w_np = np.random.uniform(-stdv, stdv, (weight_size,
1, 1)).astype(np.float32)
w = Parameter(
initializer(
Tensor(w_np), [
weight_size, 1, 1]), name='weight')
return w
def lstm_default_state(batch_size, hidden_size, num_layers, bidirectional):
"""init default input."""
num_directions = 1
if bidirectional:
num_directions = 2
h = Tensor(
np.zeros((num_layers * num_directions, batch_size, hidden_size)).astype(np.float32))
c = Tensor(
np.zeros((num_layers * num_directions, batch_size, hidden_size)).astype(np.float32))
return h, c
class SentimentNet(nn.Cell):
"""Sentiment network structure."""
def __init__(self,
vocab_size,
embed_size,
num_hiddens,
num_layers,
bidirectional,
num_classes,
weight,
batch_size):
super(SentimentNet, self).__init__()
self.embedding = nn.Embedding(vocab_size,
embed_size,
embedding_table=weight)
self.embedding.embedding_table.requires_grad = False
self.trans = P.Transpose()
self.perm = (1, 0, 2)
self.encoder = nn.LSTM(input_size=embed_size,
hidden_size=num_hiddens,
num_layers=num_layers,
has_bias=True,
bidirectional=bidirectional,
dropout=0.0)
w_init = init_lstm_weight(
embed_size,
num_hiddens,
num_layers,
bidirectional)
self.encoder.weight = w_init
self.h, self.c = lstm_default_state(batch_size, num_hiddens, num_layers, bidirectional)
self.concat = P.Concat(1)
if bidirectional:
self.decoder = nn.Dense(num_hiddens * 4, num_classes)
else:
self.decoder = nn.Dense(num_hiddens * 2, num_classes)
def construct(self, inputs):
# (64,500,300)
embeddings = self.embedding(inputs)
embeddings = self.trans(embeddings, self.perm)
output, _ = self.encoder(embeddings, (self.h, self.c))
# states[i] size(64,200) -> encoding.size(64,400)
encoding = self.concat((output[0], output[1]))
outputs = self.decoder(encoding)
return outputs
def create_dataset(base_path, batch_size, num_epochs, is_train):
"""Create dataset for training."""
columns_list = ["feature", "label"]
num_consumer = 4
if is_train:
path = os.path.join(base_path, 'aclImdb_train.mindrecord0')
else:
path = os.path.join(base_path, 'aclImdb_test.mindrecord0')
dtrain = ds.MindDataset(path, columns_list, num_consumer)
dtrain = dtrain.shuffle(buffer_size=dtrain.get_dataset_size())
dtrain = dtrain.batch(batch_size, drop_remainder=True)
dtrain = dtrain.repeat(count=num_epochs)
return dtrain
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='MindSpore LSTM Example')
parser.add_argument('--preprocess', type=str, default='false', choices=['true', 'false'],
help='Whether to perform data preprocessing')
parser.add_argument('--mode', type=str, default="train", choices=['train', 'test'],
help='implement phase, set to train or test')
# Download dataset from 'http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz' and extract to 'aclimdb_path'
parser.add_argument('--aclimdb_path', type=str, default="./aclImdb",
help='path where the dataset is store')
# Download glove from 'http://nlp.stanford.edu/data/glove.6B.zip' and extract to 'glove_path'
# Add a new line '400000 300' at the beginning of 'glove.6B.300d.txt' with '40000' for total words and '300' for vector length
parser.add_argument('--glove_path', type=str, default="./glove",
help='path where the glove is store')
parser.add_argument('--preprocess_path', type=str, default="./preprocess",
help='path where the pre-process data is store')
parser.add_argument('--ckpt_path', type=str, default="./ckpt", help='if mode is test, must provide\
path where the trained ckpt file')
args = parser.parse_args()
context.set_context(
mode=context.GRAPH_MODE,
save_graphs=False,
device_target="GPU")
if args.preprocess == 'true':
print("============== Starting Data Pre-processing ==============")
shutil.rmtree(args.preprocess_path)
os.mkdir(args.preprocess_path)
convert_to_mindrecord(cfg.embed_size, args.aclimdb_path, args.preprocess_path, args.glove_path)
embedding_table = np.loadtxt(os.path.join(args.preprocess_path, "weight.txt")).astype(np.float32)
network = SentimentNet(vocab_size=embedding_table.shape[0],
embed_size=cfg.embed_size,
num_hiddens=cfg.num_hiddens,
num_layers=cfg.num_layers,
bidirectional=cfg.bidirectional,
num_classes=cfg.num_classes,
weight=Tensor(embedding_table),
batch_size=cfg.batch_size)
loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
opt = nn.Momentum(network.trainable_params(), cfg.learning_rate, cfg.momentum)
loss_cb = LossMonitor()
model = Model(network, loss, opt, {'acc': Accuracy()})
if args.mode == 'train':
print("============== Starting Training ==============")
ds_train = create_dataset(args.preprocess_path, cfg.batch_size, cfg.num_epochs, True)
config_ck = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_steps,
keep_checkpoint_max=cfg.keep_checkpoint_max)
ckpoint_cb = ModelCheckpoint(prefix="lstm", directory=args.ckpt_path, config=config_ck)
model.train(cfg.num_epochs, ds_train, callbacks=[ckpoint_cb, loss_cb])
elif args.mode == 'test':
print("============== Starting Testing ==============")
ds_eval = create_dataset(args.preprocess_path, cfg.batch_size, 1, False)
param_dict = load_checkpoint(args.ckpt_path)
load_param_into_net(network, param_dict)
acc = model.eval(ds_eval)
print("============== Accuracy:{} ==============".format(acc))
else:
raise RuntimeError('mode should be train or test, rather than {}'.format(args.mode))
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
import sys
import time
import numpy as np
import pytest
from scipy.special import softmax
from mindspore import Model
from mindspore import Tensor
from mindspore import context
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindarmour.attacks.gradient_method import FastGradientSignMethod
from mindarmour.utils.logger import LogUtil
from mindarmour.evaluations.attack_evaluation import AttackEvaluate
from lenet5_net import LeNet5
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
sys.path.append("..")
from data_processing import generate_mnist_dataset
LOGGER = LogUtil.get_instance()
TAG = 'FGSM_Test'
@pytest.mark.level1
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_card
@pytest.mark.component_mindarmour
def test_fast_gradient_sign_method():
"""
FGSM-Attack test
"""
# upload trained network
ckpt_name = './trained_ckpt_file/checkpoint_lenet-10_1875.ckpt'
net = LeNet5()
load_dict = load_checkpoint(ckpt_name)
load_param_into_net(net, load_dict)
# get test data
data_list = "./MNIST_unzip/test"
batch_size = 32
ds = generate_mnist_dataset(data_list, batch_size, sparse=False)
# prediction accuracy before attack
model = Model(net)
batch_num = 3 # the number of batches of attacking samples
test_images = []
test_labels = []
predict_labels = []
i = 0
for data in ds.create_tuple_iterator():
i += 1
images = data[0].astype(np.float32)
labels = data[1]
test_images.append(images)
test_labels.append(labels)
pred_labels = np.argmax(model.predict(Tensor(images)).asnumpy(),
axis=1)
predict_labels.append(pred_labels)
if i >= batch_num:
break
predict_labels = np.concatenate(predict_labels)
true_labels = np.argmax(np.concatenate(test_labels), axis=1)
accuracy = np.mean(np.equal(predict_labels, true_labels))
LOGGER.info(TAG, "prediction accuracy before attacking is : %s", accuracy)
# attacking
attack = FastGradientSignMethod(net, eps=0.3)
start_time = time.clock()
adv_data = attack.batch_generate(np.concatenate(test_images),
np.concatenate(test_labels), batch_size=32)
stop_time = time.clock()
np.save('./adv_data', adv_data)
pred_logits_adv = model.predict(Tensor(adv_data)).asnumpy()
# rescale predict confidences into (0, 1).
pred_logits_adv = softmax(pred_logits_adv, axis=1)
pred_labels_adv = np.argmax(pred_logits_adv, axis=1)
accuracy_adv = np.mean(np.equal(pred_labels_adv, true_labels))
LOGGER.info(TAG, "prediction accuracy after attacking is : %s", accuracy_adv)
attack_evaluate = AttackEvaluate(np.concatenate(test_images).transpose(0, 2, 3, 1),
np.concatenate(test_labels),
adv_data.transpose(0, 2, 3, 1),
pred_logits_adv)
LOGGER.info(TAG, 'mis-classification rate of adversaries is : %s',
attack_evaluate.mis_classification_rate())
LOGGER.info(TAG, 'The average confidence of adversarial class is : %s',
attack_evaluate.avg_conf_adv_class())
LOGGER.info(TAG, 'The average confidence of true class is : %s',
attack_evaluate.avg_conf_true_class())
LOGGER.info(TAG, 'The average distance (l0, l2, linf) between original '
'samples and adversarial samples are: %s',
attack_evaluate.avg_lp_distance())
LOGGER.info(TAG, 'The average structural similarity between original '
'samples and adversarial samples are: %s',
attack_evaluate.avg_ssim())
LOGGER.info(TAG, 'The average costing time is %s',
(stop_time - start_time)/(batch_num*batch_size))
if __name__ == '__main__':
test_fast_gradient_sign_method()
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
"""defense example using nad"""
import sys
import logging
import numpy as np
import pytest
from mindspore import Tensor
from mindspore import context
from mindspore import nn
from mindspore.nn import SoftmaxCrossEntropyWithLogits
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindarmour.attacks import FastGradientSignMethod
from mindarmour.defenses import NaturalAdversarialDefense
from mindarmour.utils.logger import LogUtil
from lenet5_net import LeNet5
sys.path.append("..")
from data_processing import generate_mnist_dataset
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
LOGGER = LogUtil.get_instance()
TAG = 'Nad_Example'
@pytest.mark.level1
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_card
@pytest.mark.component_mindarmour
def test_nad_method():
"""
NAD-Defense test.
"""
# 1. load trained network
ckpt_name = './trained_ckpt_file/checkpoint_lenet-10_1875.ckpt'
net = LeNet5()
load_dict = load_checkpoint(ckpt_name)
load_param_into_net(net, load_dict)
loss = SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=False)
opt = nn.Momentum(net.trainable_params(), 0.01, 0.09)
nad = NaturalAdversarialDefense(net, loss_fn=loss, optimizer=opt,
bounds=(0.0, 1.0), eps=0.3)
# 2. get test data
data_list = "./MNIST_unzip/test"
batch_size = 32
ds_test = generate_mnist_dataset(data_list, batch_size=batch_size,
sparse=False)
inputs = []
labels = []
for data in ds_test.create_tuple_iterator():
inputs.append(data[0].astype(np.float32))
labels.append(data[1])
inputs = np.concatenate(inputs)
labels = np.concatenate(labels)
# 3. get accuracy of test data on original model
net.set_train(False)
acc_list = []
batchs = inputs.shape[0] // batch_size
for i in range(batchs):
batch_inputs = inputs[i*batch_size : (i + 1)*batch_size]
batch_labels = np.argmax(labels[i*batch_size : (i + 1)*batch_size], axis=1)
logits = net(Tensor(batch_inputs)).asnumpy()
label_pred = np.argmax(logits, axis=1)
acc_list.append(np.mean(batch_labels == label_pred))
LOGGER.debug(TAG, 'accuracy of TEST data on original model is : %s',
np.mean(acc_list))
# 4. get adv of test data
attack = FastGradientSignMethod(net, eps=0.3)
adv_data = attack.batch_generate(inputs, labels)
LOGGER.debug(TAG, 'adv_data.shape is : %s', adv_data.shape)
# 5. get accuracy of adv data on original model
net.set_train(False)
acc_list = []
batchs = adv_data.shape[0] // batch_size
for i in range(batchs):
batch_inputs = adv_data[i*batch_size : (i + 1)*batch_size]
batch_labels = np.argmax(labels[i*batch_size : (i + 1)*batch_size], axis=1)
logits = net(Tensor(batch_inputs)).asnumpy()
label_pred = np.argmax(logits, axis=1)
acc_list.append(np.mean(batch_labels == label_pred))
LOGGER.debug(TAG, 'accuracy of adv data on original model is : %s',
np.mean(acc_list))
# 6. defense
net.set_train()
nad.batch_defense(inputs, labels, batch_size=32, epochs=10)
# 7. get accuracy of test data on defensed model
net.set_train(False)
acc_list = []
batchs = inputs.shape[0] // batch_size
for i in range(batchs):
batch_inputs = inputs[i*batch_size : (i + 1)*batch_size]
batch_labels = np.argmax(labels[i*batch_size : (i + 1)*batch_size], axis=1)
logits = net(Tensor(batch_inputs)).asnumpy()
label_pred = np.argmax(logits, axis=1)
acc_list.append(np.mean(batch_labels == label_pred))
LOGGER.debug(TAG, 'accuracy of TEST data on defensed model is : %s',
np.mean(acc_list))
# 8. get accuracy of adv data on defensed model
acc_list = []
batchs = adv_data.shape[0] // batch_size
for i in range(batchs):
batch_inputs = adv_data[i*batch_size : (i + 1)*batch_size]
batch_labels = np.argmax(labels[i*batch_size : (i + 1)*batch_size], axis=1)
logits = net(Tensor(batch_inputs)).asnumpy()
label_pred = np.argmax(logits, axis=1)
acc_list.append(np.mean(batch_labels == label_pred))
LOGGER.debug(TAG, 'accuracy of adv data on defensed model is : %s',
np.mean(acc_list))
if __name__ == '__main__':
LOGGER.set_level(logging.DEBUG)
test_nad_method()
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
'''cifar_resnet50'''
import os
import random
import argparse
import mindspore.nn as nn
import mindspore.common.dtype as mstype
import mindspore.ops.functional as F
import mindspore.dataset as ds
import mindspore.dataset.transforms.vision.c_transforms as C
import mindspore.dataset.transforms.c_transforms as C2
from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
from mindspore.communication.management import init
from mindspore import Tensor
from mindspore.ops import operations as P
from mindspore.nn.optim.momentum import Momentum
from mindspore.train.model import Model, ParallelMode
from mindspore import context
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.parallel._auto_parallel_context import auto_parallel_context
from resnet import resnet50
random.seed(1)
parser = argparse.ArgumentParser(description='Image classification')
parser.add_argument('--run_distribute', type=bool, default=False, help='Run distribute.')
parser.add_argument('--device_num', type=int, default=1, help='Device num.')
parser.add_argument('--do_train', type=bool, default=True, help='Do train or not.')
parser.add_argument('--do_eval', type=bool, default=False, help='Do eval or not.')
parser.add_argument('--epoch_size', type=int, default=1, help='Epoch size.')
parser.add_argument('--batch_size', type=int, default=32, help='Batch size.')
parser.add_argument('--num_classes', type=int, default=10, help='Num classes.')
parser.add_argument('--checkpoint_path', type=str, default=None, help='CheckPoint file path.')
parser.add_argument('--dataset_path', type=str, default="/var/log/npu/datasets/cifar/cifar-10-batches-bin", help='Dataset path.')
args_opt = parser.parse_args()
device_id = int(os.getenv('DEVICE_ID'))
data_home = args_opt.dataset_path
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
context.set_context(enable_task_sink=True, device_id=device_id)
context.set_context(enable_loop_sink=False)
context.set_context(enable_mem_reuse=False)
def create_dataset(repeat_num=1, training=True):
"""
create data for next use such as training or infering
"""
cifar_ds = ds.Cifar10Dataset(data_home)
if args_opt.run_distribute:
rank_id = int(os.getenv('RANK_ID'))
rank_size = int(os.getenv('RANK_SIZE'))
cifar_ds = ds.Cifar10Dataset(data_home, num_shards=rank_size, shard_id=rank_id)
resize_height = 224
resize_width = 224
rescale = 1.0 / 255.0
shift = 0.0
# define map operations
random_crop_op = C.RandomCrop((32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT
random_horizontal_op = C.RandomHorizontalFlip()
resize_op = C.Resize((resize_height, resize_width)) # interpolation default BILINEAR
rescale_op = C.Rescale(rescale, shift)
normalize_op = C.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
changeswap_op = C.HWC2CHW()
type_cast_op = C2.TypeCast(mstype.int32)
c_trans = []
if training:
c_trans = [random_crop_op, random_horizontal_op]
c_trans += [resize_op, rescale_op, normalize_op,
changeswap_op]
# apply map operations on images
cifar_ds = cifar_ds.map(input_columns="label", operations=type_cast_op)
cifar_ds = cifar_ds.map(input_columns="image", operations=c_trans)
# apply repeat operations
cifar_ds = cifar_ds.repeat(repeat_num)
# apply shuffle operations
cifar_ds = cifar_ds.shuffle(buffer_size=10)
# apply batch operations
cifar_ds = cifar_ds.batch(batch_size=args_opt.batch_size, drop_remainder=True)
return cifar_ds
if __name__ == '__main__':
# in this way by judging the mark of args, users will decide which function to use
if args_opt.do_eval:
context.set_context(enable_hccl=False)
else:
if args_opt.run_distribute:
context.set_context(enable_hccl=True)
context.set_auto_parallel_context(device_num=args_opt.device_num, parallel_mode=ParallelMode.DATA_PARALLEL)
auto_parallel_context().set_all_reduce_fusion_split_indices([140])
init()
else:
context.set_context(enable_hccl=False)
epoch_size = args_opt.epoch_size
net = resnet50(args_opt.batch_size, args_opt.num_classes)
ls = SoftmaxCrossEntropyWithLogits(sparse=True, is_grad=False, reduction="mean")
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9)
model = Model(net, loss_fn=ls, optimizer=opt, metrics={'acc'})
# as for train, users could use model.train
if args_opt.do_train:
dataset = create_dataset(epoch_size)
batch_num = dataset.get_dataset_size()
config_ck = CheckpointConfig(save_checkpoint_steps=batch_num, keep_checkpoint_max=35)
ckpoint_cb = ModelCheckpoint(prefix="train_resnet_cifar10", directory="./", config=config_ck)
loss_cb = LossMonitor()
model.train(epoch_size, dataset, callbacks=[ckpoint_cb, loss_cb])
# as for evaluation, users could use model.eval
if args_opt.do_eval:
if args_opt.checkpoint_path:
param_dict = load_checkpoint(args_opt.checkpoint_path)
load_param_into_net(net, param_dict)
eval_dataset = create_dataset(1, training=False)
res = model.eval(eval_dataset)
print("result: ", res)
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
'''resnet'''
import numpy as np
import mindspore.nn as nn
from mindspore import Tensor
from mindspore.ops import operations as P
from mindspore.common.initializer import initializer
from mindspore.common import dtype as mstype
def weight_variable(shape):
"""weight_variable"""
return initializer('XavierUniform', shape=shape, dtype=mstype.float32)
def weight_variable_uniform(shape):
"""weight_variable_uniform"""
return initializer('Uniform', shape=shape, dtype=mstype.float32)
def weight_variable_0(shape):
"""weight_variable_0"""
zeros = np.zeros(shape).astype(np.float32)
return Tensor(zeros)
def weight_variable_1(shape):
"""weight_variable_1"""
ones = np.ones(shape).astype(np.float32)
return Tensor(ones)
def conv3x3(in_channels, out_channels, stride=1, padding=0):
"""3x3 convolution """
weight_shape = (out_channels, in_channels, 3, 3)
weight = weight_variable(weight_shape)
return nn.Conv2d(in_channels, out_channels,
kernel_size=3, stride=stride, padding=padding, weight_init=weight, has_bias=False, pad_mode="same")
def conv1x1(in_channels, out_channels, stride=1, padding=0):
"""1x1 convolution"""
weight_shape = (out_channels, in_channels, 1, 1)
weight = weight_variable(weight_shape)
return nn.Conv2d(in_channels, out_channels,
kernel_size=1, stride=stride, padding=padding, weight_init=weight, has_bias=False, pad_mode="same")
def conv7x7(in_channels, out_channels, stride=1, padding=0):
"""1x1 convolution"""
weight_shape = (out_channels, in_channels, 7, 7)
weight = weight_variable(weight_shape)
return nn.Conv2d(in_channels, out_channels,
kernel_size=7, stride=stride, padding=padding, weight_init=weight, has_bias=False, pad_mode="same")
def bn_with_initialize(out_channels):
"""bn_with_initialize"""
shape = (out_channels)
mean = weight_variable_0(shape)
var = weight_variable_1(shape)
beta = weight_variable_0(shape)
gamma = weight_variable_uniform(shape)
bn = nn.BatchNorm2d(out_channels, momentum=0.99, eps=0.00001, gamma_init=gamma,
beta_init=beta, moving_mean_init=mean, moving_var_init=var)
return bn
def bn_with_initialize_last(out_channels):
"""bn_with_initialize_last"""
shape = (out_channels)
mean = weight_variable_0(shape)
var = weight_variable_1(shape)
beta = weight_variable_0(shape)
gamma = weight_variable_uniform(shape)
bn = nn.BatchNorm2d(out_channels, momentum=0.99, eps=0.00001, gamma_init=gamma,
beta_init=beta, moving_mean_init=mean, moving_var_init=var)
return bn
def fc_with_initialize(input_channels, out_channels):
"""fc_with_initialize"""
weight_shape = (out_channels, input_channels)
weight = weight_variable(weight_shape)
bias_shape = (out_channels)
bias = weight_variable_uniform(bias_shape)
return nn.Dense(input_channels, out_channels, weight, bias)
class ResidualBlock(nn.Cell):
"""ResidualBlock"""
expansion = 4
def __init__(self,
in_channels,
out_channels,
stride=1):
"""init block"""
super(ResidualBlock, self).__init__()
out_chls = out_channels // self.expansion
self.conv1 = conv1x1(in_channels, out_chls, stride=stride, padding=0)
self.bn1 = bn_with_initialize(out_chls)
self.conv2 = conv3x3(out_chls, out_chls, stride=1, padding=0)
self.bn2 = bn_with_initialize(out_chls)
self.conv3 = conv1x1(out_chls, out_channels, stride=1, padding=0)
self.bn3 = bn_with_initialize_last(out_channels)
self.relu = P.ReLU()
self.add = P.TensorAdd()
def construct(self, x):
"""construct"""
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
out = self.add(out, identity)
out = self.relu(out)
return out
class ResidualBlockWithDown(nn.Cell):
"""ResidualBlockWithDown"""
expansion = 4
def __init__(self,
in_channels,
out_channels,
stride=1,
down_sample=False):
"""init block with down"""
super(ResidualBlockWithDown, self).__init__()
out_chls = out_channels // self.expansion
self.conv1 = conv1x1(in_channels, out_chls, stride=stride, padding=0)
self.bn1 = bn_with_initialize(out_chls)
self.conv2 = conv3x3(out_chls, out_chls, stride=1, padding=0)
self.bn2 = bn_with_initialize(out_chls)
self.conv3 = conv1x1(out_chls, out_channels, stride=1, padding=0)
self.bn3 = bn_with_initialize_last(out_channels)
self.relu = P.ReLU()
self.down_sample = down_sample
self.conv_down_sample = conv1x1(in_channels, out_channels, stride=stride, padding=0)
self.bn_down_sample = bn_with_initialize(out_channels)
self.add = P.TensorAdd()
def construct(self, x):
"""construct"""
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
identity = self.conv_down_sample(identity)
identity = self.bn_down_sample(identity)
out = self.add(out, identity)
out = self.relu(out)
return out
class MakeLayer0(nn.Cell):
"""MakeLayer0"""
def __init__(self, block, in_channels, out_channels, stride):
"""init"""
super(MakeLayer0, self).__init__()
self.a = ResidualBlockWithDown(in_channels, out_channels, stride=1, down_sample=True)
self.b = block(out_channels, out_channels, stride=stride)
self.c = block(out_channels, out_channels, stride=1)
def construct(self, x):
"""construct"""
x = self.a(x)
x = self.b(x)
x = self.c(x)
return x
class MakeLayer1(nn.Cell):
"""MakeLayer1"""
def __init__(self, block, in_channels, out_channels, stride):
"""init"""
super(MakeLayer1, self).__init__()
self.a = ResidualBlockWithDown(in_channels, out_channels, stride=stride, down_sample=True)
self.b = block(out_channels, out_channels, stride=1)
self.c = block(out_channels, out_channels, stride=1)
self.d = block(out_channels, out_channels, stride=1)
def construct(self, x):
"""construct"""
x = self.a(x)
x = self.b(x)
x = self.c(x)
x = self.d(x)
return x
class MakeLayer2(nn.Cell):
"""MakeLayer2"""
def __init__(self, block, in_channels, out_channels, stride):
"""init"""
super(MakeLayer2, self).__init__()
self.a = ResidualBlockWithDown(in_channels, out_channels, stride=stride, down_sample=True)
self.b = block(out_channels, out_channels, stride=1)
self.c = block(out_channels, out_channels, stride=1)
self.d = block(out_channels, out_channels, stride=1)
self.e = block(out_channels, out_channels, stride=1)
self.f = block(out_channels, out_channels, stride=1)
def construct(self, x):
"""construct"""
x = self.a(x)
x = self.b(x)
x = self.c(x)
x = self.d(x)
x = self.e(x)
x = self.f(x)
return x
class MakeLayer3(nn.Cell):
"""MakeLayer3"""
def __init__(self, block, in_channels, out_channels, stride):
"""init"""
super(MakeLayer3, self).__init__()
self.a = ResidualBlockWithDown(in_channels, out_channels, stride=stride, down_sample=True)
self.b = block(out_channels, out_channels, stride=1)
self.c = block(out_channels, out_channels, stride=1)
def construct(self, x):
"""construct"""
x = self.a(x)
x = self.b(x)
x = self.c(x)
return x
class ResNet(nn.Cell):
"""ResNet"""
def __init__(self, block, num_classes=100, batch_size=32):
"""init"""
super(ResNet, self).__init__()
self.batch_size = batch_size
self.num_classes = num_classes
self.conv1 = conv7x7(3, 64, stride=2, padding=0)
self.bn1 = bn_with_initialize(64)
self.relu = P.ReLU()
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="same")
self.layer1 = MakeLayer0(block, in_channels=64, out_channels=256, stride=1)
self.layer2 = MakeLayer1(block, in_channels=256, out_channels=512, stride=2)
self.layer3 = MakeLayer2(block, in_channels=512, out_channels=1024, stride=2)
self.layer4 = MakeLayer3(block, in_channels=1024, out_channels=2048, stride=2)
self.pool = P.ReduceMean(keep_dims=True)
self.squeeze = P.Squeeze(axis=(2, 3))
self.fc = fc_with_initialize(512 * block.expansion, num_classes)
def construct(self, x):
"""construct"""
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.pool(x, (2, 3))
x = self.squeeze(x)
x = self.fc(x)
return x
def resnet50(batch_size, num_classes):
"""create resnet50"""
return ResNet(ResidualBlock, num_classes, batch_size)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册