- [PyTorch Model Scripts Conversion](#PyTorch-Model-Scripts-Conversion)
- [TensorFlow Model Scripts Conversion](#TensorFlow-Model-Scripts-Conversion)
- [Caution](#caution)
- [Unsupported situation of AST mode](#unsupported-situation-of-ast-mode)
- [Situation1](#situation1)
@@ -21,7 +25,7 @@
## Overview
MindConverter is a migration tool to transform the model scripts from PyTorch to Mindspore. Users can migrate their PyTorch models to Mindspore rapidly with minor changes according to the conversion report.
MindConverter is a migration tool to transform the model scripts from PyTorch or TensorFlow to Mindspore. Users can migrate their PyTorch or TensorFlow models to Mindspore rapidly with minor changes according to the conversion report.
## Installation
@@ -36,22 +40,31 @@ MindConverter currently only provides command-line interface. Here is the manual
--in_file IN_FILE Specify path for script file to use AST schema to do
script conversation.
--model_file MODEL_FILE
PyTorch .pth model file path to use graph based schema
to do script generation. When `--in_file` and
`--model_file` are both provided, use AST schema as
default.
PyTorch .pth or Tensorflow .pb model file path to use
graph based schema to do script generation. When
`--in_file` and `--model_file` are both provided, use
AST schema as default.
--shape SHAPE Optional, expected input tensor shape of
`--model_file`. It is required when use graph based
schema. Usage: --shape 3,244,244
`--model_file`. It's required when use graph based
schema. Usage: --shape 1,3,244,244
--input_node INPUT_NODE
Optional, input node(s) name of `--model_file`. It's
required when use Tensorflow model. Usage:
--input_node input_1:0,input_2:0
--output_node OUTPUT_NODE
Optional, output node(s) name of `--model_file`. It's
required when use Tensorflow model. Usage:
--output_node output_1:0,output_2:0
--output OUTPUT Optional, specify path for converted script file
directory. Default output directory is `output` folder
in the current working directory.
@@ -65,14 +78,16 @@ optional arguments:
```
**MindConverter provides two modes:**
### PyTorch Model Scripts Migration
**MindConverter provides two modes for PyTorch:**
1. **Abstract Syntax Tree (AST) based conversion**:Use the argument `--in_file` will enable the AST mode.
2. **Computational Graph basedconversion**:Use `--model_file` and `--shape` arguments will enable the Graph mode.
2. **Computational Graph basedconversion**:Use `--model_file` and `--shape` arguments will enable the Graph mode.
> The AST mode will be enabled, if both `--in_file` and `--model_file` are specified.
For the Grapa mode, `--shape` is mandatory.
For the Graph mode, `--shape` is mandatory.
For the AST mode, `--shape` is ignored.
@@ -82,9 +97,13 @@ Please note that your original PyTorch project is included in the module search
> Assume the project is located at `/home/user/project/model_training`, users can use this command to add the project to `PYTHONPATH` : `export PYTHONPATH=/home/user/project/model_training:$PYTHONPATH`
> MindConverter needs the original PyTourch scripts because of the reverse serialization.
> MindConverter needs the original PyTorch scripts because of the reverse serialization.
### TensorFlow Model Scripts Migration
**MindConverter provides computational graph based conversion for TensorFlow**: Transformation will be done given `--model_file`, `--shape`, `--input_node` and `--output_node`.
> AST mode is not supported for TensorFlow, only computational graph based mode is available.
## Scenario
@@ -93,29 +112,32 @@ MindConverter provides two modes for different migration demands.
1. Keep original scripts' structures, including variables, functions, and libraries.
2. Keep extra modifications as few as possible, or no modifications are required after conversion.
The AST mode is recommended for the first demand. It parses and analyzes PyTorch scripts, then replace them with the MindSpore AST to generate codes. Theoretically, The AST mode supports any model script. However, the conversion may differ due to the coding style of original scripts.
The AST mode is recommended for the first demand (AST mode is only supported for PyTorch). It parses and analyzes PyTorch scripts, then replace them with the MindSpore AST to generate codes. Theoretically, The AST mode supports any model script. However, the conversion may differ due to the coding style of original scripts.
For the second demand, the Graph mode is recommended. As the computational graph is a standard descriptive language, it is not affected by user's coding style. This mode may have more operators converted as long as these operators are supported by MindConverter.
Some typical image classification networks such as ResNet and VGG have been tested for the Graph mode. Note that:
> 1. Currently, the Graph mode does not support models with multiple inputs. Only models with a single input and single output are supported.
> 2. The Dropout operator will be lost after conversion because the inference mode is used to load the PyTorch model. Manually re-implement is necessary.
> 2. The Dropout operator will be lost after conversion because the inference mode is used to load the PyTorch or TensorFlow model. Manually re-implement is necessary.
> 3. The Graph-based mode will be continuously developed and optimized with further updates.
@@ -154,10 +176,12 @@ For non-transformed operators, suggestions are provided in the report. For insta
### Graph-Based Conversion
Assume the PyTorch model (.pth file) is located at `/home/user/model.pth`, with input shape (3, 224, 224) and the original PyTorch script is at `/home/user/project/model_training`. Output the transformed MindSpore script to `/home/user/output`, with the conversion report to `/home/user/output/report`. Use the following command:
#### PyTorch Model Scripts Conversion
Assume the PyTorch model (.pth file) is located at `/home/user/model.pth`, with input shape (1, 3, 224, 224) and the original PyTorch script is at `/home/user/project/model_training`. Output the transformed MindSpore script to `/home/user/output`, with the conversion report to `/home/user/output/report`. Use the following command:
The Graph mode has the same conversion report as the AST mode. However, the line number and column number refer to the transformed scripts since no original scripts are used in the process.
In addition, input and output Tensor shape of unconverted operators shows explicitly (`input_shape` and `output_shape`) as comments in converted scripts to help further manual modifications. Here is an example of the `Reshape` operator (Not supported in current version):
In addition, input and output Tensor shape of unconverted operators shows explicitly (`input_shape` and `output_shape`) as comments in converted scripts to help further manual modifications. <a name="manual_modify">Here is an example of the `Reshape` operator (Not supported in current version)</a>:
```python
class Classifier(nn.Cell):
@@ -211,9 +235,59 @@ class Classifier(nn.Cell):
> Note: `--output` and `--report` are optional. MindConverter creates an `output` folder under the current working directory, and outputs generated scripts and conversion reports to it.
#### TensorFlow Model Scripts Conversion
To use TensorFlow model script migration, you need to export TensorFlow model to Pb format first, and obtain the model input node and output node name. You can refer to the following methods to export and obtain the node name:
```python
import tensorflow as tf
from tensorflow.python.framework import graph_io
from tensorflow.keras.applications.inception_v3 import InceptionV3
After the above code is executed, the model will be saved to `/home/user/xxx/frozen_model.pb`. `INPUT_NODE` is input node name, and `OUTPUT_NODE` is output node's.
Suppose the input node name is `input_1:0`, output node name is `predictions/Softmax:0`, the input shape of model is `1,224,224,3`, the following command can be used to generate the script:
After executed,MindSpore script, and report file can be found in corresponding directory.
The format of conversion report generated by script generation scheme based on graph structure is the same as that of AST scheme. However, since the graph based scheme is a generative method, the original pytorch script is not referenced in the conversion process. Therefore, the code line and column numbers involved in the generated conversion report refer to the generated script.
In addition, for operators that are not converted successfully, the input and output shape of tensor of the node will be identified in the code_ shape`, `output_ For example, please refer to [PyTorch Model Scripts Conversion](#manual_modify).
## Caution
1. PyTorch is not an explicitly stated dependency library in MindInsight. The Graph conversion requires the consistent PyTorch version as the model is trained. (MindConverter recommends PyTorch 1.4.0 or 1.6.0)
1. PyTorch, TensorFlow, TF2ONNX is not an explicitly stated dependency library in MindInsight. The Graph conversion requires the consistent PyTorch or TensorFlow version as the model is trained. (MindConverter recommends PyTorch 1.4.0 or 1.6.0)
2. This script conversion tool relies on operators which supported by MindConverter and MindSpore. Unsupported operators may not successfully mapped to MindSpore operators. You can manually edit, or implement the mapping based on MindConverter, and contribute to our MindInsight repository. We appreciate your support for the MindSpore community.