зеркало из https://github.com/microsoft/MMdnn.git
Normalize line ending and fix absolute paths. (#855)
This commit is contained in:
Родитель
6e9b8a722a
Коммит
8836a43100
|
@ -19,7 +19,7 @@ Major features include:
|
|||
|
||||
- **Model Search & Visualization**
|
||||
|
||||
- We provide a [model collection](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/models/README.md) to help you find some popular models.
|
||||
- We provide a [model collection](mmdnn/models/README.md) to help you find some popular models.
|
||||
- We provide a <a href="#visualization">model visualizer</a> to display the network architecture more intuitively.
|
||||
|
||||
- **Model Deployment**
|
||||
|
|
|
@ -1,101 +1,101 @@
|
|||
# Tensorflow slim "Resnet V2" to PyTorch conversion example
|
||||
|
||||
Model: ["ResNet V2 152" for Imagenet](https://github.com/tensorflow/models/blob/master/research/slim/nets/resnet_v2.py)
|
||||
|
||||
Source: TensorFlow
|
||||
|
||||
Destination: PyTorch
|
||||
|
||||
---
|
||||
|
||||
## Prepare the TensorFlow model
|
||||
|
||||
You need to prepare your pre-trained TensorFlow model firstly. And there is a pre-trained model extractor for frameworks to help you. You can refer it to extract your TensorFlow model checkpoint files.
|
||||
|
||||
```bash
|
||||
$ mmdownload -f tensorflow -n resnet_v2_152
|
||||
|
||||
Downloading file [./resnet_v2_152_2017_04_14.tar.gz] from [http://download.tensorflow.org/models/resnet_v2_152_2017_04_14.tar.gz]
|
||||
100% [......................................................................] 675629399 / 675629399
|
||||
|
||||
Model saved in file: ./imagenet_resnet_v2_152.ckpt
|
||||
```
|
||||
|
||||
Then you got the TensorFlow checkpoint files for *ResNet V2 152* model which is in your current working directory, including *imagenet_resnet_v2_152.ckpt.meta* for architecture , *imagenet_resnet_v2_152.ckpt.data-00000-of-00001* and *imagenet_resnet_v2_152.ckpt.index* for weights.
|
||||
|
||||
## Find the output node of the model
|
||||
|
||||
TensorFlow original checkpoint files contain many operators (if you tried tensorboard to visualize the graph) which is not used in our toolkits. we should prune them with specifying the output node of your model.
|
||||
|
||||
```bash
|
||||
$ mmvismeta imagenet_resnet_v2_152.ckpt.meta ./log/
|
||||
.
|
||||
.
|
||||
.
|
||||
TensorBoard 1.5.1 at http://kit-station:6006 (Press CTRL+C to quit)
|
||||
```
|
||||
|
||||
Then you can open URL above to find the output node of your model,
|
||||
|
||||
![TensorBoard](https://github.com/Microsoft/MMdnn/blob/master/docs/tensorboard.png)
|
||||
|
||||
like the squeeze node named *MMdnn_Output* we set up in our tensorflow model extractor. Detail information is in [TensorFlow README](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/tensorflow/README.md)
|
||||
|
||||
## Convert TensorFlow Model to PyTorch
|
||||
|
||||
We provide two ways to convert models.
|
||||
|
||||
### **One-step Command**
|
||||
|
||||
Above MMdnn@0.1.4, we provide one command to achieve the conversion
|
||||
|
||||
```bash
|
||||
$ mmconvert -sf tensorflow -in imagenet_resnet_v2_152.ckpt.meta -iw imagenet_resnet_v2_152.ckpt --dstNode MMdnn_Output -df pytorch -om tf_to_pytorch_resnet_152.pth
|
||||
.
|
||||
.
|
||||
.
|
||||
PyTorch model file is saved as [tf_to_pytorch_resnet_152.pth], generated by [052eb72db9934edc90d8e1ffa48144d7.py] and [052eb72db9934edc90d8e1ffa48144d7.npy].
|
||||
```
|
||||
|
||||
Then you get the PyTorch original model *tf_to_pytorch_resnet_152.pth* converted from TensorFlow. **052eb72db9934edc90d8e1ffa48144d7.py** and **052eb72db9934edc90d8e1ffa48144d7.npy** are temporal files which will be removed automatically.
|
||||
|
||||
|
||||
### Step-by-step Command for debugging
|
||||
|
||||
#### Convert the pre-trained model files to intermediate representation
|
||||
|
||||
```bash
|
||||
$ mmtoir -f tensorflow -n imagenet_resnet_v2_152.ckpt.meta -w imagenet_resnet_v2_152.ckpt --dstNode MMdnn_Output -o converted
|
||||
|
||||
Parse file [imagenet_resnet_v2_152.ckpt.meta] with binary format successfully.
|
||||
Tensorflow model file [imagenet_resnet_v2_152.ckpt.meta] loaded successfully.
|
||||
Tensorflow checkpoint file [imagenet_resnet_v2_152.ckpt] loaded successfully. [816] variables loaded.
|
||||
IR network structure is saved as [converted.json].
|
||||
IR network structure is saved as [converted.pb].
|
||||
IR weights are saved as [converted.npy].
|
||||
```
|
||||
|
||||
Then you got the **intermediate representation** files *converted.json* for visualization, *converted.proto* and *converted.npy* for next steps.
|
||||
|
||||
#### Convert the IR files to PyTorch code
|
||||
|
||||
```bash
|
||||
$ mmtocode -f pytorch -n converted.pb -w converted.npy -d converted_pytorch.py -dw converted_pytorch.npy
|
||||
|
||||
Parse file [converted.pb] with binary format successfully.
|
||||
Target network code snippet is saved as [converted_pytorch.py].
|
||||
Target weights are saved as [converted_pytorch.npy].
|
||||
```
|
||||
|
||||
And you will get a filename *converted_pytorch.py*, which contains the **original PyTorch** codes to build the *ResNet V2 152* network and *converted_pytorch.npy* which is used to set weights in the network building process.
|
||||
|
||||
With the three steps, you have already converted the pre-trained TensorFlow *ResNet V2 152* models to PyTorch network building file *converted_pytorch.py* and weights file *converted_pytorch.npy*. You can use these two files to fine-tune training or inference.
|
||||
|
||||
#### Dump the original PyTorch model
|
||||
|
||||
```bash
|
||||
$ mmtomodel -f pytorch -in converted_pytorch.py -iw converted_pytorch.npy -o converted_pytorch.pth
|
||||
|
||||
PyTorch model file is saved as [converted_pytorch.pth], generated by [converted_pytorch.py] and [converted_pytorch.npy].
|
||||
```
|
||||
The file *converted_pytorch.pth* can be loaded by PyTorch directly.
|
||||
# Tensorflow slim "Resnet V2" to PyTorch conversion example
|
||||
|
||||
Model: ["ResNet V2 152" for Imagenet](https://github.com/tensorflow/models/blob/master/research/slim/nets/resnet_v2.py)
|
||||
|
||||
Source: TensorFlow
|
||||
|
||||
Destination: PyTorch
|
||||
|
||||
---
|
||||
|
||||
## Prepare the TensorFlow model
|
||||
|
||||
You need to prepare your pre-trained TensorFlow model firstly. And there is a pre-trained model extractor for frameworks to help you. You can refer it to extract your TensorFlow model checkpoint files.
|
||||
|
||||
```bash
|
||||
$ mmdownload -f tensorflow -n resnet_v2_152
|
||||
|
||||
Downloading file [./resnet_v2_152_2017_04_14.tar.gz] from [http://download.tensorflow.org/models/resnet_v2_152_2017_04_14.tar.gz]
|
||||
100% [......................................................................] 675629399 / 675629399
|
||||
|
||||
Model saved in file: ./imagenet_resnet_v2_152.ckpt
|
||||
```
|
||||
|
||||
Then you got the TensorFlow checkpoint files for *ResNet V2 152* model which is in your current working directory, including *imagenet_resnet_v2_152.ckpt.meta* for architecture , *imagenet_resnet_v2_152.ckpt.data-00000-of-00001* and *imagenet_resnet_v2_152.ckpt.index* for weights.
|
||||
|
||||
## Find the output node of the model
|
||||
|
||||
TensorFlow original checkpoint files contain many operators (if you tried tensorboard to visualize the graph) which is not used in our toolkits. we should prune them with specifying the output node of your model.
|
||||
|
||||
```bash
|
||||
$ mmvismeta imagenet_resnet_v2_152.ckpt.meta ./log/
|
||||
.
|
||||
.
|
||||
.
|
||||
TensorBoard 1.5.1 at http://kit-station:6006 (Press CTRL+C to quit)
|
||||
```
|
||||
|
||||
Then you can open URL above to find the output node of your model,
|
||||
|
||||
![TensorBoard](tensorboard.png)
|
||||
|
||||
like the squeeze node named *MMdnn_Output* we set up in our tensorflow model extractor. Detail information is in [TensorFlow README](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/tensorflow/README.md)
|
||||
|
||||
## Convert TensorFlow Model to PyTorch
|
||||
|
||||
We provide two ways to convert models.
|
||||
|
||||
### **One-step Command**
|
||||
|
||||
Above MMdnn@0.1.4, we provide one command to achieve the conversion
|
||||
|
||||
```bash
|
||||
$ mmconvert -sf tensorflow -in imagenet_resnet_v2_152.ckpt.meta -iw imagenet_resnet_v2_152.ckpt --dstNode MMdnn_Output -df pytorch -om tf_to_pytorch_resnet_152.pth
|
||||
.
|
||||
.
|
||||
.
|
||||
PyTorch model file is saved as [tf_to_pytorch_resnet_152.pth], generated by [052eb72db9934edc90d8e1ffa48144d7.py] and [052eb72db9934edc90d8e1ffa48144d7.npy].
|
||||
```
|
||||
|
||||
Then you get the PyTorch original model *tf_to_pytorch_resnet_152.pth* converted from TensorFlow. **052eb72db9934edc90d8e1ffa48144d7.py** and **052eb72db9934edc90d8e1ffa48144d7.npy** are temporal files which will be removed automatically.
|
||||
|
||||
|
||||
### Step-by-step Command for debugging
|
||||
|
||||
#### Convert the pre-trained model files to intermediate representation
|
||||
|
||||
```bash
|
||||
$ mmtoir -f tensorflow -n imagenet_resnet_v2_152.ckpt.meta -w imagenet_resnet_v2_152.ckpt --dstNode MMdnn_Output -o converted
|
||||
|
||||
Parse file [imagenet_resnet_v2_152.ckpt.meta] with binary format successfully.
|
||||
Tensorflow model file [imagenet_resnet_v2_152.ckpt.meta] loaded successfully.
|
||||
Tensorflow checkpoint file [imagenet_resnet_v2_152.ckpt] loaded successfully. [816] variables loaded.
|
||||
IR network structure is saved as [converted.json].
|
||||
IR network structure is saved as [converted.pb].
|
||||
IR weights are saved as [converted.npy].
|
||||
```
|
||||
|
||||
Then you got the **intermediate representation** files *converted.json* for visualization, *converted.proto* and *converted.npy* for next steps.
|
||||
|
||||
#### Convert the IR files to PyTorch code
|
||||
|
||||
```bash
|
||||
$ mmtocode -f pytorch -n converted.pb -w converted.npy -d converted_pytorch.py -dw converted_pytorch.npy
|
||||
|
||||
Parse file [converted.pb] with binary format successfully.
|
||||
Target network code snippet is saved as [converted_pytorch.py].
|
||||
Target weights are saved as [converted_pytorch.npy].
|
||||
```
|
||||
|
||||
And you will get a filename *converted_pytorch.py*, which contains the **original PyTorch** codes to build the *ResNet V2 152* network and *converted_pytorch.npy* which is used to set weights in the network building process.
|
||||
|
||||
With the three steps, you have already converted the pre-trained TensorFlow *ResNet V2 152* models to PyTorch network building file *converted_pytorch.py* and weights file *converted_pytorch.npy*. You can use these two files to fine-tune training or inference.
|
||||
|
||||
#### Dump the original PyTorch model
|
||||
|
||||
```bash
|
||||
$ mmtomodel -f pytorch -in converted_pytorch.py -iw converted_pytorch.npy -o converted_pytorch.pth
|
||||
|
||||
PyTorch model file is saved as [converted_pytorch.pth], generated by [converted_pytorch.py] and [converted_pytorch.npy].
|
||||
```
|
||||
The file *converted_pytorch.pth* can be loaded by PyTorch directly.
|
||||
|
|
|
@ -1,152 +1,152 @@
|
|||
# CNTK README
|
||||
|
||||
## CNTK pre-trained model
|
||||
|
||||
We tested some CNTK pre-trained models to others, get more detail from [this file](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/cntk/extractor.py)
|
||||
|
||||
| Models | Caffe | Keras | Tensorflow | CNTK | MXNet | PyTorch | CoreML | ONNX |
|
||||
| :----------: | :---: | :---: | :--------: | :--: | :---: | :-----: | :----: | :--: |
|
||||
| Inception_v3 | | | √ | √ | | √ | | √ |
|
||||
| ResNet 18 | √ | √ | √ | √ | √ | √ | | √ |
|
||||
| ResNet 152 | √ | √ | √ | √ | √ | √ | | √ |
|
||||
|
||||
**√** - Correctness tested
|
||||
|
||||
**o** - Some difference after conversion
|
||||
|
||||
**space** - not tested
|
||||
|
||||
---
|
||||
|
||||
# Usage
|
||||
|
||||
## Download CNTK pre-trained model
|
||||
|
||||
```bash
|
||||
$ mmdownload -f cntk
|
||||
|
||||
Support frameworks: ['resnet18', 'resnet50', 'resnet152', 'resnet101', 'inception_v3', 'Fast-RCNN_Pascal', 'alexnet', 'Fast-RCNN_grocery100']
|
||||
|
||||
$ mmdownload -f cntk -n resnet50 -o ./
|
||||
|
||||
Downloading file [./ResNet50_ImageNet_CNTK.model] from [https://www.cntk.ai/Models/CNTK_Pretrained/ResNet50_ImageNet_CNTK.model]
|
||||
progress: 100264.0 KB downloaded, 100%
|
||||
Selected GPU[0] GeForce GTX 980 Ti as the process wide default device.
|
||||
Cntk Model resnet50 saved as [./ResNet50_ImageNet_CNTK.model].
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## One-step conversion
|
||||
|
||||
Above MMdnn@0.1.4, we provide one command to achieve the conversion
|
||||
|
||||
```bash
|
||||
$ mmconvert -sf cntk -iw ResNet50_ImageNet_CNTK.model -df cntk -om cntk_resnet50.dnn --inputShape 3,224,224
|
||||
.
|
||||
.
|
||||
.
|
||||
CNTK model file is saved as [cntk_resnet50.dnn], generated by [f499918b3e7346a78dbaf02559231d53.py] and [f499918b3e7346a78dbaf02559231d53.npy].
|
||||
|
||||
```
|
||||
|
||||
Then you get the CNTK original model *cntk_resnet50.dnn* converted from CNTK. Temporal files are removed automatically.
|
||||
|
||||
---
|
||||
|
||||
## Step-by-step conversion (for debugging)
|
||||
|
||||
### Convert architecture from CNTK to IR (CNTK -> IR)
|
||||
|
||||
|
||||
You can use following bash command to convert the network model(architecture and weights) [*ResNet50_ImageNet_CNTK.model*] to IR architecture file [*resnet50_cntk.pb*], [*resnet50_cntk.json*] and IR weights file [*resnet50_cntk.npy*]
|
||||
|
||||
```bash
|
||||
$ mmtoir -f cntk -n ResNet50_ImageNet_CNTK.model -d resnet50_cntk
|
||||
.
|
||||
.
|
||||
.
|
||||
IR network structure is saved as [resnet50_cntk.json].
|
||||
IR network structure is saved as [resnet50_cntk.pb].
|
||||
IR weights are saved as [resnet50_cntk.npy].
|
||||
|
||||
```
|
||||
|
||||
### Convert models from IR to CNTK code snippet (IR -> CNTK)
|
||||
|
||||
You can use following bash command to convert the IR architecture file [*resnet50_cntk.pb*] and weights file [*resnet50_cntk.npy*] to CNTK Python code file[*cntk_resnet50.py*]
|
||||
|
||||
```bash
|
||||
$ mmtocode -f cntk -n resnet50_cntk.pb -w resnet50_cntk.npy -d cntk_resnet50.py
|
||||
|
||||
Parse file [resnet50_cntk.pb] with binary format successfully.
|
||||
Target network code snippet is saved as [cntk_resnet50.py].
|
||||
|
||||
```
|
||||
|
||||
### Generate CNTK model from code snippet file and weight file
|
||||
|
||||
You can use following bash command to generate CNTK model file [*cntk_resnet50.dnn*] from python code [*cntk_resnet50.py*] and weights file [*resnet50_cntk.npy*] for further usage.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion.examples.cntk.imagenet_test -n cntk_resnet50.py -w resnet50_cntk.npy --dump cntk_resnet50.dnn
|
||||
|
||||
CNTK model file is saved as [cntk_resnet50.dnn], generated by [cntk_resnet50.py] and [resnet50_cntk.npy].
|
||||
```
|
||||
|
||||
## Develop version
|
||||
|
||||
Ubuntu 16.04 with
|
||||
|
||||
- CNTK CPU 2.4
|
||||
|
||||
@ 2018/02/08
|
||||
|
||||
## Limitation
|
||||
|
||||
- Main dataflow in network is NHWC (channel last) format, but CNTK CNN-related operators take NCHW (channel first) format data. So we transpose the data format before and after each CNN-related operators (such as Convolution, Pooling, LRN, BN and so on). The data transpose sacrifices some computing performance. There is some methods to reduce the performance gap.
|
||||
|
||||
1. Like PyTorch and MXNet emitter, change the main dataflow format to NCHW (channel last) and tranpose the weights in IR-Code step.
|
||||
|
||||
1. Remove unnecessary transpose during building the network.
|
||||
|
||||
- Currently no RNN-related operations support
|
||||
|
||||
|
||||
## FAQ
|
||||
|
||||
### Retrain the converted CNTK model
|
||||
|
||||
If you want to retrain the converted model, you can change all layers from "Channel Last" to "Channel First" in the converted code file and then change the code in **def batch_normalization(input, name, epsilon, **kwargs):**
|
||||
|
||||
```python
|
||||
from
|
||||
|
||||
def batch_normalization(input, name, epsilon, **kwargs):
|
||||
mean = cntk.Parameter(init = __weights_dict[name]['mean'],
|
||||
name = name + "_mean")
|
||||
var = cntk.Parameter(init = __weights_dict[name]['var'],
|
||||
name = name + "_var")
|
||||
layer = (input - mean) / cntk.sqrt(var + epsilon)
|
||||
|
||||
......
|
||||
|
||||
to
|
||||
|
||||
def batch_normalization(input, name, epsilon, **kwargs):
|
||||
layer = cntk.layers.BatchNormalization( map_rank = 1, name=name )(input)
|
||||
mean = cntk.Parameter(init = __weights_dict[name]['mean'],
|
||||
name = name + "_mean")
|
||||
layer.aggregate_mean = mean
|
||||
var = cntk.Parameter(init = __weights_dict[name]['var'],
|
||||
name = name + "_var")
|
||||
layer.aggregate_variance = var
|
||||
layer.aggregate_count = 4096.0
|
||||
|
||||
......
|
||||
|
||||
```
|
||||
Thanks to [this issue](https://github.com/Microsoft/MMdnn/issues/396)
|
||||
|
||||
|
||||
# CNTK README
|
||||
|
||||
## CNTK pre-trained model
|
||||
|
||||
We tested some CNTK pre-trained models to others, get more detail from [this file](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/cntk/extractor.py)
|
||||
|
||||
| Models | Caffe | Keras | Tensorflow | CNTK | MXNet | PyTorch | CoreML | ONNX |
|
||||
| :----------: | :---: | :---: | :--------: | :--: | :---: | :-----: | :----: | :--: |
|
||||
| Inception_v3 | | | √ | √ | | √ | | √ |
|
||||
| ResNet 18 | √ | √ | √ | √ | √ | √ | | √ |
|
||||
| ResNet 152 | √ | √ | √ | √ | √ | √ | | √ |
|
||||
|
||||
**√** - Correctness tested
|
||||
|
||||
**o** - Some difference after conversion
|
||||
|
||||
**space** - not tested
|
||||
|
||||
---
|
||||
|
||||
# Usage
|
||||
|
||||
## Download CNTK pre-trained model
|
||||
|
||||
```bash
|
||||
$ mmdownload -f cntk
|
||||
|
||||
Support frameworks: ['resnet18', 'resnet50', 'resnet152', 'resnet101', 'inception_v3', 'Fast-RCNN_Pascal', 'alexnet', 'Fast-RCNN_grocery100']
|
||||
|
||||
$ mmdownload -f cntk -n resnet50 -o ./
|
||||
|
||||
Downloading file [./ResNet50_ImageNet_CNTK.model] from [https://www.cntk.ai/Models/CNTK_Pretrained/ResNet50_ImageNet_CNTK.model]
|
||||
progress: 100264.0 KB downloaded, 100%
|
||||
Selected GPU[0] GeForce GTX 980 Ti as the process wide default device.
|
||||
Cntk Model resnet50 saved as [./ResNet50_ImageNet_CNTK.model].
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## One-step conversion
|
||||
|
||||
Above MMdnn@0.1.4, we provide one command to achieve the conversion
|
||||
|
||||
```bash
|
||||
$ mmconvert -sf cntk -iw ResNet50_ImageNet_CNTK.model -df cntk -om cntk_resnet50.dnn --inputShape 3,224,224
|
||||
.
|
||||
.
|
||||
.
|
||||
CNTK model file is saved as [cntk_resnet50.dnn], generated by [f499918b3e7346a78dbaf02559231d53.py] and [f499918b3e7346a78dbaf02559231d53.npy].
|
||||
|
||||
```
|
||||
|
||||
Then you get the CNTK original model *cntk_resnet50.dnn* converted from CNTK. Temporal files are removed automatically.
|
||||
|
||||
---
|
||||
|
||||
## Step-by-step conversion (for debugging)
|
||||
|
||||
### Convert architecture from CNTK to IR (CNTK -> IR)
|
||||
|
||||
|
||||
You can use following bash command to convert the network model(architecture and weights) [*ResNet50_ImageNet_CNTK.model*] to IR architecture file [*resnet50_cntk.pb*], [*resnet50_cntk.json*] and IR weights file [*resnet50_cntk.npy*]
|
||||
|
||||
```bash
|
||||
$ mmtoir -f cntk -n ResNet50_ImageNet_CNTK.model -d resnet50_cntk
|
||||
.
|
||||
.
|
||||
.
|
||||
IR network structure is saved as [resnet50_cntk.json].
|
||||
IR network structure is saved as [resnet50_cntk.pb].
|
||||
IR weights are saved as [resnet50_cntk.npy].
|
||||
|
||||
```
|
||||
|
||||
### Convert models from IR to CNTK code snippet (IR -> CNTK)
|
||||
|
||||
You can use following bash command to convert the IR architecture file [*resnet50_cntk.pb*] and weights file [*resnet50_cntk.npy*] to CNTK Python code file[*cntk_resnet50.py*]
|
||||
|
||||
```bash
|
||||
$ mmtocode -f cntk -n resnet50_cntk.pb -w resnet50_cntk.npy -d cntk_resnet50.py
|
||||
|
||||
Parse file [resnet50_cntk.pb] with binary format successfully.
|
||||
Target network code snippet is saved as [cntk_resnet50.py].
|
||||
|
||||
```
|
||||
|
||||
### Generate CNTK model from code snippet file and weight file
|
||||
|
||||
You can use following bash command to generate CNTK model file [*cntk_resnet50.dnn*] from python code [*cntk_resnet50.py*] and weights file [*resnet50_cntk.npy*] for further usage.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion.examples.cntk.imagenet_test -n cntk_resnet50.py -w resnet50_cntk.npy --dump cntk_resnet50.dnn
|
||||
|
||||
CNTK model file is saved as [cntk_resnet50.dnn], generated by [cntk_resnet50.py] and [resnet50_cntk.npy].
|
||||
```
|
||||
|
||||
## Develop version
|
||||
|
||||
Ubuntu 16.04 with
|
||||
|
||||
- CNTK CPU 2.4
|
||||
|
||||
@ 2018/02/08
|
||||
|
||||
## Limitation
|
||||
|
||||
- Main dataflow in network is NHWC (channel last) format, but CNTK CNN-related operators take NCHW (channel first) format data. So we transpose the data format before and after each CNN-related operators (such as Convolution, Pooling, LRN, BN and so on). The data transpose sacrifices some computing performance. There is some methods to reduce the performance gap.
|
||||
|
||||
1. Like PyTorch and MXNet emitter, change the main dataflow format to NCHW (channel last) and tranpose the weights in IR-Code step.
|
||||
|
||||
1. Remove unnecessary transpose during building the network.
|
||||
|
||||
- Currently no RNN-related operations support
|
||||
|
||||
|
||||
## FAQ
|
||||
|
||||
### Retrain the converted CNTK model
|
||||
|
||||
If you want to retrain the converted model, you can change all layers from "Channel Last" to "Channel First" in the converted code file and then change the code in **def batch_normalization(input, name, epsilon, **kwargs):**
|
||||
|
||||
```python
|
||||
from
|
||||
|
||||
def batch_normalization(input, name, epsilon, **kwargs):
|
||||
mean = cntk.Parameter(init = __weights_dict[name]['mean'],
|
||||
name = name + "_mean")
|
||||
var = cntk.Parameter(init = __weights_dict[name]['var'],
|
||||
name = name + "_var")
|
||||
layer = (input - mean) / cntk.sqrt(var + epsilon)
|
||||
|
||||
......
|
||||
|
||||
to
|
||||
|
||||
def batch_normalization(input, name, epsilon, **kwargs):
|
||||
layer = cntk.layers.BatchNormalization( map_rank = 1, name=name )(input)
|
||||
mean = cntk.Parameter(init = __weights_dict[name]['mean'],
|
||||
name = name + "_mean")
|
||||
layer.aggregate_mean = mean
|
||||
var = cntk.Parameter(init = __weights_dict[name]['var'],
|
||||
name = name + "_var")
|
||||
layer.aggregate_variance = var
|
||||
layer.aggregate_count = 4096.0
|
||||
|
||||
......
|
||||
|
||||
```
|
||||
Thanks to [this issue](https://github.com/Microsoft/MMdnn/issues/396)
|
||||
|
||||
|
||||
|
|
|
@ -1,145 +1,145 @@
|
|||
# CoreML README
|
||||
|
||||
|
||||
We tested the [Awesome-CoreML-Models](https://github.com/likedan/Awesome-CoreML-Models) and the parser works. Any contribution is welcome.
|
||||
|
||||
Models | Caffe | CoreML | CNTK | Keras | MXNet | PyTorch | TensorFlow| Onnx
|
||||
:-----------------------:|:-----:|:------:|:----:|:-----:|:-----:|:-------:|:------:|:------:|
|
||||
alexnet | √ | √ | | √ | √ | √ | √ | √
|
||||
densenet201 | √ | √ | | √ | √ | √ | √ | √
|
||||
inception_v3 | √ | √ | | √ | | √ | √ | √
|
||||
vgg19 | √ | √ | | √ | √ | √ | √ | √
|
||||
vgg19_bn | √ | √ | | √ | √ | √ | √ | √
|
||||
resnet152 | √ | √ | | √ | √ | √ | √ | √
|
||||
|
||||
|
||||
**√** - Correctness tested
|
||||
|
||||
**o** - Some difference after conversion
|
||||
|
||||
**space** - not tested
|
||||
|
||||
---
|
||||
|
||||
## Convert to CoreML
|
||||
|
||||
We use a Keras "mobilenet" model to CoreML as an examples.
|
||||
|
||||
### Prepare your pre-trained model
|
||||
|
||||
In this example, we can use our [Keras pre-trained model extractor](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/keras/extract_model.py) to prepare mobilenet model.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion.examples.keras.extract_model -n mobilenet -i mmdnn/conversion/examples/data/seagull.jpg
|
||||
|
||||
Using TensorFlow backend.
|
||||
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.6/mobilenet_1_0_224_tf.h5
|
||||
17227776/17225924 [==============================] - 12s 1us/step
|
||||
17235968/17225924 [==============================] - 12s 1us/step
|
||||
Network structure is saved as [imagenet_mobilenet.json].
|
||||
Network weights are saved as [imagenet_mobilenet.h5].
|
||||
[(21, 0.84343129), (23, 0.10283408), (146, 0.039170805), (404, 0.0033809284), (144, 0.0026779801)]
|
||||
```
|
||||
|
||||
The Keras model architecture is saved as *imagenet_mobilenet.json*, weights are saved as *imagenet_mobilenet.h5*, and get the original model inference result for our example photo.
|
||||
|
||||
Then use keras -> IR parser to convert the original Keras to IR format.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion._script.convertToIR -f keras -d keras_mobilenet -n imagenet_mobilenet.json -w imagenet_mobilenet.h5
|
||||
|
||||
Using TensorFlow backend.
|
||||
Network file [imagenet_mobilenet.json] and [imagenet_mobilenet.h5] is loaded successfully.
|
||||
IR network structure is saved as [keras_mobilenet.json].
|
||||
IR network structure is saved as [keras_mobilenet.pb].
|
||||
IR weights are saved as [keras_mobilenet.npy].
|
||||
```
|
||||
|
||||
Then we got the IR format model.
|
||||
|
||||
### Slim Model Extractor
|
||||
|
||||
You can refer [Slim Model Extractor](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/coreml/extractor.py) to extract your own coreml model, which is a sample tool to extract both architecture and weights from slim pre-trained models.
|
||||
|
||||
Support frameworks: ['inception_v3', 'mobilenet', 'resnet50', 'tinyyolo', 'vgg16']
|
||||
|
||||
Example:
|
||||
|
||||
```bash
|
||||
$ mmdownload -f coreml -n mobilenet
|
||||
|
||||
Downloading file [./MobileNet.mlmodel] from [https://docs-assets.developer.apple.com/coreml/models/MobileNet.mlmodel]
|
||||
progress: 16736.0 KB downloaded, 100%
|
||||
Coreml model mobilenet is saved in [./]
|
||||
```
|
||||
|
||||
|
||||
## Convert model (including architecture and weights) from Coreml to IR
|
||||
|
||||
You can use following bash command to convert the checkpoint files to IR architecture file [*resnet152.pb*], [*resnet152.json*] and IR weights file [*resnet152.npy*]
|
||||
|
||||
```bash
|
||||
$ mmtoir -f coreml -d mobilenet -n MobileNet.mlmodel --dstNodeName MMdnn_Output
|
||||
|
||||
IR network structure is saved as [mobilenet.json].
|
||||
IR network structure is saved as [mobilenet.pb].
|
||||
IR weights are saved as [mobilenet.npy].
|
||||
```
|
||||
|
||||
### Convert to CoreML
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion._script.IRToModel -f coreml -in keras_mobilenet.pb -iw keras_mobilenet.npy -o keras_mobilenet.mlmodel --scale 0.00784313725490196 --redBias -1 --greenBias -1 --blueBias -1
|
||||
|
||||
Parse file [keras_mobilenet.pb] with binary format successfully.
|
||||
.
|
||||
.
|
||||
.
|
||||
input {
|
||||
name: "input_1"
|
||||
type {
|
||||
imageType {
|
||||
width: 224
|
||||
height: 224
|
||||
colorSpace: RGB
|
||||
}
|
||||
}
|
||||
}
|
||||
output {
|
||||
name: "reshape_2"
|
||||
type {
|
||||
multiArrayType {
|
||||
shape: 1000
|
||||
dataType: DOUBLE
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Then the converted CoreML model is saved as *keras_mobilenet.mlmodel*.
|
||||
|
||||
> [Note!] The argument *--scale 0.00784313725490196 --redBias -1 --greenBias -1 --blueBias -1* is Keras mobilenet preprocessing.
|
||||
|
||||
### Test converted model (Not necessary)
|
||||
|
||||
We implemented an sample code for image inference testing. You can refer the [code](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/coreml/imagenet_test.py) to implement **your own testing code**.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion.examples.coreml.imagenet_test -input input_1 -output reshape_2 --image mmdnn/conversion/examples/data/seagull.jpg -size 224 -n keras_mobilenet.mlmodel
|
||||
|
||||
Loading model [keras_mobilenet.mlmodel].
|
||||
Model loading success.
|
||||
[(21, 0.83917254209518433), (23, 0.10752557963132858), (146, 0.038640134036540985), (404, 0.0034028184600174427), (144, 0.0027129633817821741)]
|
||||
```
|
||||
|
||||
The inference result is slightly different from the original keras model. Currently we consider it is acceptable. Any further investigation is welcome.
|
||||
|
||||
## Develop version
|
||||
|
||||
macOS High Sierra 10.13.3 (17C205)
|
||||
|
||||
@ 2018/01/10
|
||||
|
||||
## Limitation
|
||||
|
||||
# CoreML README
|
||||
|
||||
|
||||
We tested the [Awesome-CoreML-Models](https://github.com/likedan/Awesome-CoreML-Models) and the parser works. Any contribution is welcome.
|
||||
|
||||
Models | Caffe | CoreML | CNTK | Keras | MXNet | PyTorch | TensorFlow| Onnx
|
||||
:-----------------------:|:-----:|:------:|:----:|:-----:|:-----:|:-------:|:------:|:------:|
|
||||
alexnet | √ | √ | | √ | √ | √ | √ | √
|
||||
densenet201 | √ | √ | | √ | √ | √ | √ | √
|
||||
inception_v3 | √ | √ | | √ | | √ | √ | √
|
||||
vgg19 | √ | √ | | √ | √ | √ | √ | √
|
||||
vgg19_bn | √ | √ | | √ | √ | √ | √ | √
|
||||
resnet152 | √ | √ | | √ | √ | √ | √ | √
|
||||
|
||||
|
||||
**√** - Correctness tested
|
||||
|
||||
**o** - Some difference after conversion
|
||||
|
||||
**space** - not tested
|
||||
|
||||
---
|
||||
|
||||
## Convert to CoreML
|
||||
|
||||
We use a Keras "mobilenet" model to CoreML as an examples.
|
||||
|
||||
### Prepare your pre-trained model
|
||||
|
||||
In this example, we can use our [Keras pre-trained model extractor](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/keras/extract_model.py) to prepare mobilenet model.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion.examples.keras.extract_model -n mobilenet -i mmdnn/conversion/examples/data/seagull.jpg
|
||||
|
||||
Using TensorFlow backend.
|
||||
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.6/mobilenet_1_0_224_tf.h5
|
||||
17227776/17225924 [==============================] - 12s 1us/step
|
||||
17235968/17225924 [==============================] - 12s 1us/step
|
||||
Network structure is saved as [imagenet_mobilenet.json].
|
||||
Network weights are saved as [imagenet_mobilenet.h5].
|
||||
[(21, 0.84343129), (23, 0.10283408), (146, 0.039170805), (404, 0.0033809284), (144, 0.0026779801)]
|
||||
```
|
||||
|
||||
The Keras model architecture is saved as *imagenet_mobilenet.json*, weights are saved as *imagenet_mobilenet.h5*, and get the original model inference result for our example photo.
|
||||
|
||||
Then use keras -> IR parser to convert the original Keras to IR format.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion._script.convertToIR -f keras -d keras_mobilenet -n imagenet_mobilenet.json -w imagenet_mobilenet.h5
|
||||
|
||||
Using TensorFlow backend.
|
||||
Network file [imagenet_mobilenet.json] and [imagenet_mobilenet.h5] is loaded successfully.
|
||||
IR network structure is saved as [keras_mobilenet.json].
|
||||
IR network structure is saved as [keras_mobilenet.pb].
|
||||
IR weights are saved as [keras_mobilenet.npy].
|
||||
```
|
||||
|
||||
Then we got the IR format model.
|
||||
|
||||
### Slim Model Extractor
|
||||
|
||||
You can refer [Slim Model Extractor](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/coreml/extractor.py) to extract your own coreml model, which is a sample tool to extract both architecture and weights from slim pre-trained models.
|
||||
|
||||
Support frameworks: ['inception_v3', 'mobilenet', 'resnet50', 'tinyyolo', 'vgg16']
|
||||
|
||||
Example:
|
||||
|
||||
```bash
|
||||
$ mmdownload -f coreml -n mobilenet
|
||||
|
||||
Downloading file [./MobileNet.mlmodel] from [https://docs-assets.developer.apple.com/coreml/models/MobileNet.mlmodel]
|
||||
progress: 16736.0 KB downloaded, 100%
|
||||
Coreml model mobilenet is saved in [./]
|
||||
```
|
||||
|
||||
|
||||
## Convert model (including architecture and weights) from Coreml to IR
|
||||
|
||||
You can use following bash command to convert the checkpoint files to IR architecture file [*resnet152.pb*], [*resnet152.json*] and IR weights file [*resnet152.npy*]
|
||||
|
||||
```bash
|
||||
$ mmtoir -f coreml -d mobilenet -n MobileNet.mlmodel --dstNodeName MMdnn_Output
|
||||
|
||||
IR network structure is saved as [mobilenet.json].
|
||||
IR network structure is saved as [mobilenet.pb].
|
||||
IR weights are saved as [mobilenet.npy].
|
||||
```
|
||||
|
||||
### Convert to CoreML
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion._script.IRToModel -f coreml -in keras_mobilenet.pb -iw keras_mobilenet.npy -o keras_mobilenet.mlmodel --scale 0.00784313725490196 --redBias -1 --greenBias -1 --blueBias -1
|
||||
|
||||
Parse file [keras_mobilenet.pb] with binary format successfully.
|
||||
.
|
||||
.
|
||||
.
|
||||
input {
|
||||
name: "input_1"
|
||||
type {
|
||||
imageType {
|
||||
width: 224
|
||||
height: 224
|
||||
colorSpace: RGB
|
||||
}
|
||||
}
|
||||
}
|
||||
output {
|
||||
name: "reshape_2"
|
||||
type {
|
||||
multiArrayType {
|
||||
shape: 1000
|
||||
dataType: DOUBLE
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Then the converted CoreML model is saved as *keras_mobilenet.mlmodel*.
|
||||
|
||||
> [Note!] The argument *--scale 0.00784313725490196 --redBias -1 --greenBias -1 --blueBias -1* is Keras mobilenet preprocessing.
|
||||
|
||||
### Test converted model (Not necessary)
|
||||
|
||||
We implemented an sample code for image inference testing. You can refer the [code](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/coreml/imagenet_test.py) to implement **your own testing code**.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion.examples.coreml.imagenet_test -input input_1 -output reshape_2 --image mmdnn/conversion/examples/data/seagull.jpg -size 224 -n keras_mobilenet.mlmodel
|
||||
|
||||
Loading model [keras_mobilenet.mlmodel].
|
||||
Model loading success.
|
||||
[(21, 0.83917254209518433), (23, 0.10752557963132858), (146, 0.038640134036540985), (404, 0.0034028184600174427), (144, 0.0027129633817821741)]
|
||||
```
|
||||
|
||||
The inference result is slightly different from the original keras model. Currently we consider it is acceptable. Any further investigation is welcome.
|
||||
|
||||
## Develop version
|
||||
|
||||
macOS High Sierra 10.13.3 (17C205)
|
||||
|
||||
@ 2018/01/10
|
||||
|
||||
## Limitation
|
||||
|
||||
- Currently no RNN-related operations support
|
|
@ -9,8 +9,8 @@ import tensorflow as tf
|
|||
from tensorflow.core.framework import graph_pb2
|
||||
import tfcoreml as tf_converter
|
||||
|
||||
TMP_MODEL_DIR = '/Users/kit/tmp/tfcoreml'
|
||||
TEST_IMAGE = '/Users/kit/github/MMdnn/mmdnn/conversion/examples/data/seagull.jpg'
|
||||
TMP_MODEL_DIR = 'tmp/tfcoreml'
|
||||
TEST_IMAGE = 'mmdnn/conversion/examples/data/seagull.jpg'
|
||||
|
||||
def _download_file(url):
|
||||
"""Download the file.
|
||||
|
|
|
@ -1,150 +1,150 @@
|
|||
# Keras README
|
||||
|
||||
|
||||
## Keras pre-trained model
|
||||
|
||||
We tested some Keras pre-trained models to others, get more detail from [this file](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/keras/extractor.py)
|
||||
|
||||
| Models | Caffe | Keras | Tensorflow | CNTK | MXNet | PyTorch | CoreML | ONNX |
|
||||
| :----------: | :---: | :---: | :--------: | :--: | :---: | :-----: | :----: | :--: |
|
||||
| Vgg16 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| Vgg19 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| inception_v3 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| resnet50 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| densenet | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| xception | | √ | √ | | | | √ | |
|
||||
| mobilenet | | √ | √ | | | | √ | √ |
|
||||
| nasnet | | √ | √ | | | | √ | |
|
||||
| yolo2 | | √ | | | | | | √ |
|
||||
|
||||
**√** - Correctness tested
|
||||
|
||||
**o** - Some difference after conversion
|
||||
|
||||
**space** - not tested
|
||||
|
||||
---
|
||||
|
||||
# Usage
|
||||
|
||||
## Download Keras pre-trained model
|
||||
|
||||
```bash
|
||||
$ mmdownload -f keras
|
||||
|
||||
Support frameworks: set(['resnet50', 'mobilenet', 'vgg19', 'vgg16', 'inception_v3', 'nasnet', 'inception_resnet_v2', 'xception', 'yolo2', 'densenet'])
|
||||
|
||||
$ mmdownload -f keras -n resnet50 -o ./
|
||||
|
||||
Keras model resnet50 is saved in [./imagenet_resnet50.h5]
|
||||
|
||||
```
|
||||
---
|
||||
|
||||
## One-step conversion
|
||||
|
||||
Above MMdnn@0.1.4, we provide one command to achieve the conversion
|
||||
|
||||
```bash
|
||||
$ mmconvert -sf keras -iw imagenet_resnet50.h5 -df cntk -om keras_resnet50.dnn
|
||||
.
|
||||
.
|
||||
.
|
||||
CNTK model file is saved as [keras_resnet50.dnn], generated by [8275ad5170f6441caa0b96a94d467b8e.py] and [8275ad5170f6441caa0b96a94d467b8e.npy].
|
||||
```
|
||||
|
||||
Then you get the CNTK original model *keras_resnet50.dnn* converted from Caffe. Temporal files are removed automatically.
|
||||
|
||||
---
|
||||
|
||||
## Step-by-step conversion (for debugging)
|
||||
|
||||
### Convert architecture from Keras to IR
|
||||
|
||||
You can use following bash command to convert the network architecture [*imagenet_inceptionv3.json*] to IR architecture file [*inception_v3.pb*], [*inception_v3.json*]. You can convert only network structure to IR for visualization or training in other frameworks.
|
||||
|
||||
```bash
|
||||
$ mmtoir -f keras -d inception_v3 -n imagenet_inceptionv3.json
|
||||
|
||||
Using TensorFlow backend.
|
||||
IR network structure is saved as [inception_v3.json].
|
||||
IR network structure is saved as [inception_v3.pb].
|
||||
Warning: weights are not loaded.
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Convert model (including architecture and weights) from Keras to IR
|
||||
|
||||
You can use following bash command to convert the network architecture [*imagenet_inceptionv3.json*] with weights [*imagenet_inceptionv3.h5*] to IR architecture file [*inception_v3.pb*], [*inception_v3.json*] and IR weights file [*inception_v3.npy*]
|
||||
|
||||
```bash
|
||||
$ mmtoir -f keras -d inception_v3 -n imagenet_inceptionv3.json -w imagenet_inceptionv3.h5
|
||||
|
||||
Using TensorFlow backend.
|
||||
.
|
||||
.
|
||||
.
|
||||
Network file [imagenet_inceptionv3.json] is loaded successfully.
|
||||
IR network structure is saved as [inception_v3.json].
|
||||
IR network structure is saved as [inception_v3.pb].
|
||||
IR weights are saved as [inception_v3.npy].
|
||||
```
|
||||
|
||||
### Convert models from IR to Keras code snippet
|
||||
|
||||
Since the generated Keras model code snippet can restore weights from IR weights file directly, you don't need to convert weights in this step.You can use following bash command to convert the IR architecture file [*inception_v3.pb*] to Keras Python code file[*keras_inception_v3.py*]
|
||||
|
||||
```bash
|
||||
$ mmtocode -f keras --IRModelPath inception_v3.pb --IRWeightPath inception_v3.npy --dstModelPath keras_inception_v3.py
|
||||
|
||||
Parse file [inception_v3.pb] with binary format successfully.
|
||||
Target network code snippet is saved as [keras_inception_v3.py].
|
||||
```
|
||||
|
||||
### Generate Keras model from code snippet file and weight file
|
||||
|
||||
After generating the keras code snippet, you can convert the Keras code snippet [*keras_inception_v3.py*] and IR weights file [*inception_v3.npy*] to Keras original model for further usage.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion.examples.keras.imagenet_test -n keras_inception_v3.py -w inception_v3.npy --dump keras_inception_v3.h5
|
||||
|
||||
Using TensorFlow backend.
|
||||
.
|
||||
.
|
||||
.
|
||||
Keras model file is saved as [keras_inception_v3.h5], generated by [keras_inception_v3.py] and [inception_v3.npy].
|
||||
```
|
||||
|
||||
## Support Operators
|
||||
|
||||
- Add
|
||||
- Concat
|
||||
- Relu
|
||||
- Softmax
|
||||
- Tanh
|
||||
- Sigmoid
|
||||
- Softplus
|
||||
- Softsign
|
||||
- HardSigmoid
|
||||
- Elu
|
||||
|
||||
## Develop version
|
||||
|
||||
Ubuntu 16.04 with
|
||||
|
||||
- Keras 2.1.3
|
||||
|
||||
- Tensorflow GPU 1.4.0
|
||||
|
||||
@ 11/21/2017
|
||||
|
||||
## Limitation
|
||||
|
||||
- Lambda layer no support
|
||||
|
||||
- *inception_resnet_v2* pre-trained model no support
|
||||
|
||||
- Currently no RNN related operations support
|
||||
|
||||
- Model in model is not supported yet. We can implement it if needed.
|
||||
# Keras README
|
||||
|
||||
|
||||
## Keras pre-trained model
|
||||
|
||||
We tested some Keras pre-trained models to others, get more detail from [this file](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/keras/extractor.py)
|
||||
|
||||
| Models | Caffe | Keras | Tensorflow | CNTK | MXNet | PyTorch | CoreML | ONNX |
|
||||
| :----------: | :---: | :---: | :--------: | :--: | :---: | :-----: | :----: | :--: |
|
||||
| Vgg16 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| Vgg19 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| inception_v3 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| resnet50 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| densenet | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| xception | | √ | √ | | | | √ | |
|
||||
| mobilenet | | √ | √ | | | | √ | √ |
|
||||
| nasnet | | √ | √ | | | | √ | |
|
||||
| yolo2 | | √ | | | | | | √ |
|
||||
|
||||
**√** - Correctness tested
|
||||
|
||||
**o** - Some difference after conversion
|
||||
|
||||
**space** - not tested
|
||||
|
||||
---
|
||||
|
||||
# Usage
|
||||
|
||||
## Download Keras pre-trained model
|
||||
|
||||
```bash
|
||||
$ mmdownload -f keras
|
||||
|
||||
Support frameworks: set(['resnet50', 'mobilenet', 'vgg19', 'vgg16', 'inception_v3', 'nasnet', 'inception_resnet_v2', 'xception', 'yolo2', 'densenet'])
|
||||
|
||||
$ mmdownload -f keras -n resnet50 -o ./
|
||||
|
||||
Keras model resnet50 is saved in [./imagenet_resnet50.h5]
|
||||
|
||||
```
|
||||
---
|
||||
|
||||
## One-step conversion
|
||||
|
||||
Above MMdnn@0.1.4, we provide one command to achieve the conversion
|
||||
|
||||
```bash
|
||||
$ mmconvert -sf keras -iw imagenet_resnet50.h5 -df cntk -om keras_resnet50.dnn
|
||||
.
|
||||
.
|
||||
.
|
||||
CNTK model file is saved as [keras_resnet50.dnn], generated by [8275ad5170f6441caa0b96a94d467b8e.py] and [8275ad5170f6441caa0b96a94d467b8e.npy].
|
||||
```
|
||||
|
||||
Then you get the CNTK original model *keras_resnet50.dnn* converted from Caffe. Temporal files are removed automatically.
|
||||
|
||||
---
|
||||
|
||||
## Step-by-step conversion (for debugging)
|
||||
|
||||
### Convert architecture from Keras to IR
|
||||
|
||||
You can use following bash command to convert the network architecture [*imagenet_inceptionv3.json*] to IR architecture file [*inception_v3.pb*], [*inception_v3.json*]. You can convert only network structure to IR for visualization or training in other frameworks.
|
||||
|
||||
```bash
|
||||
$ mmtoir -f keras -d inception_v3 -n imagenet_inceptionv3.json
|
||||
|
||||
Using TensorFlow backend.
|
||||
IR network structure is saved as [inception_v3.json].
|
||||
IR network structure is saved as [inception_v3.pb].
|
||||
Warning: weights are not loaded.
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Convert model (including architecture and weights) from Keras to IR
|
||||
|
||||
You can use following bash command to convert the network architecture [*imagenet_inceptionv3.json*] with weights [*imagenet_inceptionv3.h5*] to IR architecture file [*inception_v3.pb*], [*inception_v3.json*] and IR weights file [*inception_v3.npy*]
|
||||
|
||||
```bash
|
||||
$ mmtoir -f keras -d inception_v3 -n imagenet_inceptionv3.json -w imagenet_inceptionv3.h5
|
||||
|
||||
Using TensorFlow backend.
|
||||
.
|
||||
.
|
||||
.
|
||||
Network file [imagenet_inceptionv3.json] is loaded successfully.
|
||||
IR network structure is saved as [inception_v3.json].
|
||||
IR network structure is saved as [inception_v3.pb].
|
||||
IR weights are saved as [inception_v3.npy].
|
||||
```
|
||||
|
||||
### Convert models from IR to Keras code snippet
|
||||
|
||||
Since the generated Keras model code snippet can restore weights from IR weights file directly, you don't need to convert weights in this step.You can use following bash command to convert the IR architecture file [*inception_v3.pb*] to Keras Python code file[*keras_inception_v3.py*]
|
||||
|
||||
```bash
|
||||
$ mmtocode -f keras --IRModelPath inception_v3.pb --IRWeightPath inception_v3.npy --dstModelPath keras_inception_v3.py
|
||||
|
||||
Parse file [inception_v3.pb] with binary format successfully.
|
||||
Target network code snippet is saved as [keras_inception_v3.py].
|
||||
```
|
||||
|
||||
### Generate Keras model from code snippet file and weight file
|
||||
|
||||
After generating the keras code snippet, you can convert the Keras code snippet [*keras_inception_v3.py*] and IR weights file [*inception_v3.npy*] to Keras original model for further usage.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion.examples.keras.imagenet_test -n keras_inception_v3.py -w inception_v3.npy --dump keras_inception_v3.h5
|
||||
|
||||
Using TensorFlow backend.
|
||||
.
|
||||
.
|
||||
.
|
||||
Keras model file is saved as [keras_inception_v3.h5], generated by [keras_inception_v3.py] and [inception_v3.npy].
|
||||
```
|
||||
|
||||
## Support Operators
|
||||
|
||||
- Add
|
||||
- Concat
|
||||
- Relu
|
||||
- Softmax
|
||||
- Tanh
|
||||
- Sigmoid
|
||||
- Softplus
|
||||
- Softsign
|
||||
- HardSigmoid
|
||||
- Elu
|
||||
|
||||
## Develop version
|
||||
|
||||
Ubuntu 16.04 with
|
||||
|
||||
- Keras 2.1.3
|
||||
|
||||
- Tensorflow GPU 1.4.0
|
||||
|
||||
@ 11/21/2017
|
||||
|
||||
## Limitation
|
||||
|
||||
- Lambda layer no support
|
||||
|
||||
- *inception_resnet_v2* pre-trained model no support
|
||||
|
||||
- Currently no RNN related operations support
|
||||
|
||||
- Model in model is not supported yet. We can implement it if needed.
|
||||
|
|
|
@ -1,135 +1,135 @@
|
|||
# MXNet README
|
||||
|
||||
## MXNet pre-trained model
|
||||
|
||||
We tested some MXNet pre-trained models to others, get more detail from [this file](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/mxnet/extractor.py)
|
||||
|
||||
| Models | Caffe | Keras | Tensorflow | CNTK | MXNet | PyTorch | CoreML | ONNX |
|
||||
| :-----------: | :---: | :---: | :--------: | :--: | :---: | :-----: | :----: | :--: |
|
||||
| Vgg19 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| Inception_bn | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| ResNet 18 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| ResNet 152 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| ResNext 50 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| ResNext 101 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| squeezenet_v1 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
|
||||
**√** - Correctness tested
|
||||
|
||||
**o** - Some difference after conversion
|
||||
|
||||
**space** - not tested
|
||||
|
||||
---
|
||||
|
||||
# Usage
|
||||
|
||||
## Download MXNET pre-trained model
|
||||
|
||||
```bash
|
||||
$ mmdownload -f mxnet
|
||||
|
||||
Support frameworks : ['imagenet1k-resnet-152', 'vgg19', 'imagenet1k-resnet-101', 'imagenet1k-resnet-50', 'vgg16', 'imagenet1k-inception-bn', 'imagenet1k-resnext-101', 'imagenet11k-resnet-152', 'imagenet1k-resnext-50', 'imagenet1k-resnext-101-64x4d', 'imagenet1k-resnet-18', 'imagenet11k-place365ch-resnet-152', 'imagenet1k-resnet-34', 'squeezenet_v1.1', 'imagenet11k-place365ch-resnet-50', 'squeezenet_v1.0']
|
||||
|
||||
$ mmdownload -f mxnet -n imagenet1k-resnet-50 -o ./
|
||||
|
||||
Downloading file [./resnet-50-symbol.json] from [http://data.mxnet.io/models/imagenet/resnet/50-layers/resnet-50-symbol.json]
|
||||
progress: 80.0 KB downloaded, 100%
|
||||
Downloading file [./resnet-50-0000.params] from [http://data.mxnet.io/models/imagenet/resnet/50-layers/resnet-50-0000.params]
|
||||
progress: 100000.0 KB downloaded, 100%
|
||||
MXNet Model imagenet1k-resnet-50 saved as [./resnet-50-symbol.json] and [./resnet-50-0000.params].
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## One-step conversion
|
||||
|
||||
Above MMdnn@0.1.4, we provide one command to achieve the conversion
|
||||
|
||||
```bash
|
||||
$ mmconvert -sf mxnet -in resnet-50-symbol.json -iw resnet-50-0000.params -df cntk -om mxnet_resnet50.dnn --inputShape 3,224,224
|
||||
.
|
||||
.
|
||||
.
|
||||
CNTK model file is saved as [mxnet_resnet50.dnn], generated by [4c616299273a42e086b30c6c4d1c64c0.py] and [4c616299273a42e086b30c6c4d1c64c0.npy].
|
||||
|
||||
```
|
||||
|
||||
Then you get the CNTK original model *mxnet_resnet152.dnn* converted from MXNet. Temporal files are removed automatically.
|
||||
|
||||
---
|
||||
|
||||
## Step-by-step conversion (for debugging)
|
||||
|
||||
### Convert architecture from MXNET to IR (MXNET -> IR)
|
||||
|
||||
You can use following bash command to convert the network architecture [*mxnet/models/resnet-50-symbol.json*] to IR architecture file [*resnet50.pb*], [*resnet50.json*]. You can convert only network structure to IR for visualization or training in other frameworks.
|
||||
|
||||
```bash
|
||||
$ mmtoir -f mxnet -n mxnet/models/resnet-50-symbol.json -d resnet50 --inputShape 3,224,224
|
||||
.
|
||||
.
|
||||
.
|
||||
IR network structure is saved as [resnet50.json].
|
||||
IR network structure is saved as [resnet50.pb].
|
||||
Warning: weights are not loaded.
|
||||
```
|
||||
|
||||
### Convert models (including architecture and weights) from MXNet to IR (MXNET -> IR)
|
||||
|
||||
You can use following bash command to convert the network architecture [*mxnet/models/resnet-50-symbol.json*] with weights [*mxnet/models/resnet-50-0000.params*] to IR architecture file [*resnet50.pb*], [*resnet50.json*], [*resnet50.npy*].
|
||||
|
||||
> The input data shape is not in the architecture description of MXNet, we need to specify the data shape in conversion command.
|
||||
|
||||
```bash
|
||||
$ mmtoir -f mxnet -n mxnet/models/resnet-50-symbol.json -w mxnet/models/resnet-50-0000.params -d resnet50 --inputShape 3,224,224
|
||||
.
|
||||
.
|
||||
.
|
||||
IR network structure is saved as [resnet50.json].
|
||||
IR network structure is saved as [resnet50.pb].
|
||||
IR weights are saved as [resnet50.npy].
|
||||
```
|
||||
|
||||
### Convert models from IR to MXNet code snippet and weights (IR -> MXNet)
|
||||
|
||||
We need to generate both MXNet architecture code snippet and weights file to build the MXNet network.
|
||||
|
||||
> [Note!] Argument 'dw' is used to specify the converted MXNet model file name for next step use.
|
||||
|
||||
```bash
|
||||
$ mmtocode -f mxnet --IRModelPath inception_v3.pb --dstModelPath mxnet_inception_v3.py --IRWeightPath inception_v3.npy -dw mxnet_inception_v3-0000.params
|
||||
|
||||
Parse file [inception_v3.pb] with binary format successfully.
|
||||
Detect input layer [input_1] using infer batch size, set it as default value [1]
|
||||
Target network code snippet is saved as [mxnet_inception_v3.py].
|
||||
```
|
||||
|
||||
### Convert models from IR to MXNet checkpoint file
|
||||
|
||||
After generating the MXNet code snippet and weights, you can take a further step to generate an original MXNet checkpoint file.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion.examples.mxnet.imagenet_test -n mxnet_inception_v3 -w mxnet_inception_v3-0000.params --dump inception_v3
|
||||
.
|
||||
.
|
||||
.
|
||||
MXNet checkpoint file is saved as [inception_v3], generated by [mxnet_inception_v3.py] and [mxnet_inception_v3-0000.params].
|
||||
```
|
||||
|
||||
Then the output files *inception_v3-symbol.json* and *inception_v3-0000.params* can be loaded by MXNet directly.
|
||||
|
||||
---
|
||||
|
||||
## Develop version
|
||||
|
||||
Ubuntu 16.04 with
|
||||
|
||||
- MXNet 0.11.0
|
||||
|
||||
@ 11/22/2017
|
||||
|
||||
## Limitation
|
||||
|
||||
- Currently no RNN related operations support
|
||||
# MXNet README
|
||||
|
||||
## MXNet pre-trained model
|
||||
|
||||
We tested some MXNet pre-trained models to others, get more detail from [this file](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/mxnet/extractor.py)
|
||||
|
||||
| Models | Caffe | Keras | Tensorflow | CNTK | MXNet | PyTorch | CoreML | ONNX |
|
||||
| :-----------: | :---: | :---: | :--------: | :--: | :---: | :-----: | :----: | :--: |
|
||||
| Vgg19 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| Inception_bn | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| ResNet 18 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| ResNet 152 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| ResNext 50 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| ResNext 101 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
| squeezenet_v1 | √ | √ | √ | √ | √ | √ | √ | √ |
|
||||
|
||||
**√** - Correctness tested
|
||||
|
||||
**o** - Some difference after conversion
|
||||
|
||||
**space** - not tested
|
||||
|
||||
---
|
||||
|
||||
# Usage
|
||||
|
||||
## Download MXNET pre-trained model
|
||||
|
||||
```bash
|
||||
$ mmdownload -f mxnet
|
||||
|
||||
Support frameworks : ['imagenet1k-resnet-152', 'vgg19', 'imagenet1k-resnet-101', 'imagenet1k-resnet-50', 'vgg16', 'imagenet1k-inception-bn', 'imagenet1k-resnext-101', 'imagenet11k-resnet-152', 'imagenet1k-resnext-50', 'imagenet1k-resnext-101-64x4d', 'imagenet1k-resnet-18', 'imagenet11k-place365ch-resnet-152', 'imagenet1k-resnet-34', 'squeezenet_v1.1', 'imagenet11k-place365ch-resnet-50', 'squeezenet_v1.0']
|
||||
|
||||
$ mmdownload -f mxnet -n imagenet1k-resnet-50 -o ./
|
||||
|
||||
Downloading file [./resnet-50-symbol.json] from [http://data.mxnet.io/models/imagenet/resnet/50-layers/resnet-50-symbol.json]
|
||||
progress: 80.0 KB downloaded, 100%
|
||||
Downloading file [./resnet-50-0000.params] from [http://data.mxnet.io/models/imagenet/resnet/50-layers/resnet-50-0000.params]
|
||||
progress: 100000.0 KB downloaded, 100%
|
||||
MXNet Model imagenet1k-resnet-50 saved as [./resnet-50-symbol.json] and [./resnet-50-0000.params].
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## One-step conversion
|
||||
|
||||
Above MMdnn@0.1.4, we provide one command to achieve the conversion
|
||||
|
||||
```bash
|
||||
$ mmconvert -sf mxnet -in resnet-50-symbol.json -iw resnet-50-0000.params -df cntk -om mxnet_resnet50.dnn --inputShape 3,224,224
|
||||
.
|
||||
.
|
||||
.
|
||||
CNTK model file is saved as [mxnet_resnet50.dnn], generated by [4c616299273a42e086b30c6c4d1c64c0.py] and [4c616299273a42e086b30c6c4d1c64c0.npy].
|
||||
|
||||
```
|
||||
|
||||
Then you get the CNTK original model *mxnet_resnet152.dnn* converted from MXNet. Temporal files are removed automatically.
|
||||
|
||||
---
|
||||
|
||||
## Step-by-step conversion (for debugging)
|
||||
|
||||
### Convert architecture from MXNET to IR (MXNET -> IR)
|
||||
|
||||
You can use following bash command to convert the network architecture [*mxnet/models/resnet-50-symbol.json*] to IR architecture file [*resnet50.pb*], [*resnet50.json*]. You can convert only network structure to IR for visualization or training in other frameworks.
|
||||
|
||||
```bash
|
||||
$ mmtoir -f mxnet -n mxnet/models/resnet-50-symbol.json -d resnet50 --inputShape 3,224,224
|
||||
.
|
||||
.
|
||||
.
|
||||
IR network structure is saved as [resnet50.json].
|
||||
IR network structure is saved as [resnet50.pb].
|
||||
Warning: weights are not loaded.
|
||||
```
|
||||
|
||||
### Convert models (including architecture and weights) from MXNet to IR (MXNET -> IR)
|
||||
|
||||
You can use following bash command to convert the network architecture [*mxnet/models/resnet-50-symbol.json*] with weights [*mxnet/models/resnet-50-0000.params*] to IR architecture file [*resnet50.pb*], [*resnet50.json*], [*resnet50.npy*].
|
||||
|
||||
> The input data shape is not in the architecture description of MXNet, we need to specify the data shape in conversion command.
|
||||
|
||||
```bash
|
||||
$ mmtoir -f mxnet -n mxnet/models/resnet-50-symbol.json -w mxnet/models/resnet-50-0000.params -d resnet50 --inputShape 3,224,224
|
||||
.
|
||||
.
|
||||
.
|
||||
IR network structure is saved as [resnet50.json].
|
||||
IR network structure is saved as [resnet50.pb].
|
||||
IR weights are saved as [resnet50.npy].
|
||||
```
|
||||
|
||||
### Convert models from IR to MXNet code snippet and weights (IR -> MXNet)
|
||||
|
||||
We need to generate both MXNet architecture code snippet and weights file to build the MXNet network.
|
||||
|
||||
> [Note!] Argument 'dw' is used to specify the converted MXNet model file name for next step use.
|
||||
|
||||
```bash
|
||||
$ mmtocode -f mxnet --IRModelPath inception_v3.pb --dstModelPath mxnet_inception_v3.py --IRWeightPath inception_v3.npy -dw mxnet_inception_v3-0000.params
|
||||
|
||||
Parse file [inception_v3.pb] with binary format successfully.
|
||||
Detect input layer [input_1] using infer batch size, set it as default value [1]
|
||||
Target network code snippet is saved as [mxnet_inception_v3.py].
|
||||
```
|
||||
|
||||
### Convert models from IR to MXNet checkpoint file
|
||||
|
||||
After generating the MXNet code snippet and weights, you can take a further step to generate an original MXNet checkpoint file.
|
||||
|
||||
```bash
|
||||
$ python -m mmdnn.conversion.examples.mxnet.imagenet_test -n mxnet_inception_v3 -w mxnet_inception_v3-0000.params --dump inception_v3
|
||||
.
|
||||
.
|
||||
.
|
||||
MXNet checkpoint file is saved as [inception_v3], generated by [mxnet_inception_v3.py] and [mxnet_inception_v3-0000.params].
|
||||
```
|
||||
|
||||
Then the output files *inception_v3-symbol.json* and *inception_v3-0000.params* can be loaded by MXNet directly.
|
||||
|
||||
---
|
||||
|
||||
## Develop version
|
||||
|
||||
Ubuntu 16.04 with
|
||||
|
||||
- MXNet 0.11.0
|
||||
|
||||
@ 11/22/2017
|
||||
|
||||
## Limitation
|
||||
|
||||
- Currently no RNN related operations support
|
||||
|
|
|
@ -1,143 +1,143 @@
|
|||
# PyTorch README
|
||||
|
||||
Currently, we have already implemented both the the PyTorch -> IR part and the IR -> PyTorch part.
|
||||
|
||||
Models | Caffe | CoreML | CNTK | Keras | MXNet | PyTorch | TensorFlow| Onnx
|
||||
:-----------------------:|:-----:|:------:|:----:|:-----:|:-----:|:-------:|:------:|:------:|
|
||||
Vgg16 | √ | √ | | √ | √ | √ | √ | √
|
||||
Inception_v3 | √ | √ | | √ | √ | √ | √ | √
|
||||
ResNet 50 | √ | √ | | √ | √ | √ | √ | √
|
||||
MobileNet V1 | √ | √ | | √ | √ | √ | √ | √
|
||||
Tiny-yolo | | √ | | √ | √ | √ | √ | √
|
||||
|
||||
**√** - Correctness tested
|
||||
|
||||
**o** - Some difference after conversion
|
||||
|
||||
**space** - not tested
|
||||
|
||||
|
||||
The PyTorch parser is modified from branch [pytorch](https://github.com/Microsoft/MMdnn/tree/pytorch) , using jit CppOP to build the graph.
|
||||
|
||||
Any contribution is welcome.
|
||||
|
||||
## Extract PyTorch pre-trained models
|
||||
|
||||
You can refer [PyTorch model extractor](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/pytorch/extractor.py) to extract your pytorch models.
|
||||
|
||||
```bash
|
||||
$ mmdownload -f pytorch -h
|
||||
Support frameworks: ['alexnet', 'densenet121', 'densenet161', 'densenet169', 'densenet201', 'inception_v3', 'resnet101', 'resnet152', 'resnet18', 'resnet34', 'resnet50', 'vgg11', 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn', 'vgg19', 'vgg19_bn']
|
||||
|
||||
$ mmdownload -f pytorch -n resnet101 -o ./
|
||||
Downloading: "https://download.pytorch.org/models/resnet101-5d3b4d8f.pth" to /home/ruzhang/.torch/models/resnet101-5d3b4d8f.pth
|
||||
███████████████████| 102502400/102502400 [00:06<00:00, 15858546.50it/s]
|
||||
PyTorch pretrained model is saved as [./imagenet_resnet101.pth].
|
||||
|
||||
```
|
||||
|
||||
### Convert Pytorch pre-trained models to IR
|
||||
You can convert the whole pytorch model to IR structure. Please remember for the generality, we now only take the whole model `pth`, not just the state dict. To be more specific, it is save using `torch.save()` and `torch.load()` can load the whole model.
|
||||
|
||||
```bash
|
||||
$ mmtoir -f pytorch -d resnet101 --inputShape 3,224,224 -n imagenet_resnet101.pth
|
||||
```
|
||||
|
||||
Please bear in mind that always add `--inputShape` argparse. This thing is different from other framework because pytorch is a dynamic framework.
|
||||
|
||||
Then you will get
|
||||
```
|
||||
IR network structure is saved as [resnet101.json].
|
||||
IR network structure is saved as [resnet101.pb].
|
||||
IR weights are saved as [resnet101.npy].
|
||||
```
|
||||
|
||||
### Convert models from IR to PyTorch code snippet and weights
|
||||
|
||||
You can use following bash command to convert the IR architecture file [*inception_v3.pb*] and weights file [*inception_v3.npy*] to Caffe Python code file[*pytorch_inception_v3.py*] and IR weights file suit for caffe model[*pytorch_inception_v3.npy*]
|
||||
|
||||
> Note: We need to transform the IR weights to PyTorch suitable weights. Use argument *-dw* to specify the output weight file name.
|
||||
|
||||
```bash
|
||||
$ mmtocode -f pytorch -n inception_v3.pb --IRWeightPath inception_v3.npy --dstModelPath pytorch_inception_v3.py -dw pytorch_inception_v3.npy
|
||||
|
||||
Parse file [inception_v3.pb] with binary format successfully.
|
||||
Target network code snippet is saved as [pytorch_inception_v3.py].
|
||||
Target weights are saved as [pytorch_inception_v3.npy].
|
||||
```
|
||||
|
||||
### Generate PyTorch model from code snippet file and weight file
|
||||
|
||||
You can use following bash command to generate PyTorch model file [*pytorch_inception_v3.pth*] from python code [*pytorch_inception_v3.py*] and weights file [*pytorch_inception_v3.npy*] for further usage.
|
||||
|
||||
```bash
|
||||
$ mmtomodel -f pytorch -in pytorch_inception_v3.py -iw pytorch_inception_v3.npy -o pytorch_inception_v3.pth
|
||||
|
||||
PyTorch model file is saved as [pytorch_inception_v3.pth], generated by [pytorch_inception_v3.py] and [pytorch_inception_v3.npy]. Notice that you may need [pytorch_inception_v3.py] to load the model back.
|
||||
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
Detail scripts of *Tensorflow slim resnet_v1_101 model* to *PyTorch* conversion are in [issue 22](https://github.com/Microsoft/MMdnn/issues/22). You can refer it to implement your conversion.
|
||||
|
||||
## Develop version
|
||||
|
||||
Ubuntu 16.04 with
|
||||
|
||||
- PyTorch 0.4.0
|
||||
|
||||
@ 2018/04/25
|
||||
|
||||
## Links
|
||||
|
||||
- [pytorch to keras converter](https://github.com/nerox8664/pytorch2keras)
|
||||
|
||||
## Limitation
|
||||
|
||||
- The main dataflow in a pytorch network is converted from NHWC(channel last) to NCHW(channel first) format, but some operators (like Concat) with axis may not transform correctly. You may need to correct it manually.
|
||||
|
||||
- Currently, no RNN-related operations supported
|
||||
|
||||
## FAQ
|
||||
|
||||
- There are two types models saved in PyTorch. One is including architecture and weights, which is supported in the MMdnn now. The other one is only including the weights, which is not supported now.
|
||||
|
||||
```python
|
||||
only_weight_file = "./alexnet-owt-4df8aa71.pth" # Download from the model zoo
|
||||
architecture_weight_file = "imagenet_alexnet.pth" # Download using mmdownload()
|
||||
|
||||
m = torch.load(only_weight_file) # <class 'collections.OrderedDict'>
|
||||
m_1 = torch.load(architecture_weight_file) # <class 'torchvision.models.alexnet.AlexNet'> supported!
|
||||
|
||||
```
|
||||
- When you get the error "AttributeError: 'collections.OrderedDict' object has no attribute 'state_dict'" , it's because you use the model only include weights part. You need to save a new model with archietecture
|
||||
|
||||
```python
|
||||
torch.save(model, filename)
|
||||
```
|
||||
|
||||
- How to load the converted PyTorch model ?
|
||||
|
||||
```python
|
||||
|
||||
import torch
|
||||
import imp
|
||||
import numpy as np
|
||||
MainModel = imp.load_source('MainModel', "tf_pytorch_vgg19.py")
|
||||
|
||||
the_model = torch.load("tf_pytorch_vgg19.pth")
|
||||
the_model.eval()
|
||||
|
||||
x = np.random.random([224,224,3])
|
||||
x = np.transpose(x, (2, 0, 1))
|
||||
x = np.expand_dims(x, 0).copy()
|
||||
data = torch.from_numpy(x)
|
||||
data = torch.autograd.Variable(data, requires_grad = False).float()
|
||||
|
||||
predict = the_model(data)
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
# PyTorch README
|
||||
|
||||
Currently, we have already implemented both the the PyTorch -> IR part and the IR -> PyTorch part.
|
||||
|
||||
Models | Caffe | CoreML | CNTK | Keras | MXNet | PyTorch | TensorFlow| Onnx
|
||||
:-----------------------:|:-----:|:------:|:----:|:-----:|:-----:|:-------:|:------:|:------:|
|
||||
Vgg16 | √ | √ | | √ | √ | √ | √ | √
|
||||
Inception_v3 | √ | √ | | √ | √ | √ | √ | √
|
||||
ResNet 50 | √ | √ | | √ | √ | √ | √ | √
|
||||
MobileNet V1 | √ | √ | | √ | √ | √ | √ | √
|
||||
Tiny-yolo | | √ | | √ | √ | √ | √ | √
|
||||
|
||||
**√** - Correctness tested
|
||||
|
||||
**o** - Some difference after conversion
|
||||
|
||||
**space** - not tested
|
||||
|
||||
|
||||
The PyTorch parser is modified from branch [pytorch](https://github.com/Microsoft/MMdnn/tree/pytorch) , using jit CppOP to build the graph.
|
||||
|
||||
Any contribution is welcome.
|
||||
|
||||
## Extract PyTorch pre-trained models
|
||||
|
||||
You can refer [PyTorch model extractor](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/pytorch/extractor.py) to extract your pytorch models.
|
||||
|
||||
```bash
|
||||
$ mmdownload -f pytorch -h
|
||||
Support frameworks: ['alexnet', 'densenet121', 'densenet161', 'densenet169', 'densenet201', 'inception_v3', 'resnet101', 'resnet152', 'resnet18', 'resnet34', 'resnet50', 'vgg11', 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn', 'vgg19', 'vgg19_bn']
|
||||
|
||||
$ mmdownload -f pytorch -n resnet101 -o ./
|
||||
Downloading: "https://download.pytorch.org/models/resnet101-5d3b4d8f.pth" to /my/home/.torch/models/resnet101-5d3b4d8f.pth
|
||||
███████████████████| 102502400/102502400 [00:06<00:00, 15858546.50it/s]
|
||||
PyTorch pretrained model is saved as [./imagenet_resnet101.pth].
|
||||
|
||||
```
|
||||
|
||||
### Convert Pytorch pre-trained models to IR
|
||||
You can convert the whole pytorch model to IR structure. Please remember for the generality, we now only take the whole model `pth`, not just the state dict. To be more specific, it is save using `torch.save()` and `torch.load()` can load the whole model.
|
||||
|
||||
```bash
|
||||
$ mmtoir -f pytorch -d resnet101 --inputShape 3,224,224 -n imagenet_resnet101.pth
|
||||
```
|
||||
|
||||
Please bear in mind that always add `--inputShape` argparse. This thing is different from other framework because pytorch is a dynamic framework.
|
||||
|
||||
Then you will get
|
||||
```
|
||||
IR network structure is saved as [resnet101.json].
|
||||
IR network structure is saved as [resnet101.pb].
|
||||
IR weights are saved as [resnet101.npy].
|
||||
```
|
||||
|
||||
### Convert models from IR to PyTorch code snippet and weights
|
||||
|
||||
You can use following bash command to convert the IR architecture file [*inception_v3.pb*] and weights file [*inception_v3.npy*] to Caffe Python code file[*pytorch_inception_v3.py*] and IR weights file suit for caffe model[*pytorch_inception_v3.npy*]
|
||||
|
||||
> Note: We need to transform the IR weights to PyTorch suitable weights. Use argument *-dw* to specify the output weight file name.
|
||||
|
||||
```bash
|
||||
$ mmtocode -f pytorch -n inception_v3.pb --IRWeightPath inception_v3.npy --dstModelPath pytorch_inception_v3.py -dw pytorch_inception_v3.npy
|
||||
|
||||
Parse file [inception_v3.pb] with binary format successfully.
|
||||
Target network code snippet is saved as [pytorch_inception_v3.py].
|
||||
Target weights are saved as [pytorch_inception_v3.npy].
|
||||
```
|
||||
|
||||
### Generate PyTorch model from code snippet file and weight file
|
||||
|
||||
You can use following bash command to generate PyTorch model file [*pytorch_inception_v3.pth*] from python code [*pytorch_inception_v3.py*] and weights file [*pytorch_inception_v3.npy*] for further usage.
|
||||
|
||||
```bash
|
||||
$ mmtomodel -f pytorch -in pytorch_inception_v3.py -iw pytorch_inception_v3.npy -o pytorch_inception_v3.pth
|
||||
|
||||
PyTorch model file is saved as [pytorch_inception_v3.pth], generated by [pytorch_inception_v3.py] and [pytorch_inception_v3.npy]. Notice that you may need [pytorch_inception_v3.py] to load the model back.
|
||||
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
Detail scripts of *Tensorflow slim resnet_v1_101 model* to *PyTorch* conversion are in [issue 22](https://github.com/Microsoft/MMdnn/issues/22). You can refer it to implement your conversion.
|
||||
|
||||
## Develop version
|
||||
|
||||
Ubuntu 16.04 with
|
||||
|
||||
- PyTorch 0.4.0
|
||||
|
||||
@ 2018/04/25
|
||||
|
||||
## Links
|
||||
|
||||
- [pytorch to keras converter](https://github.com/nerox8664/pytorch2keras)
|
||||
|
||||
## Limitation
|
||||
|
||||
- The main dataflow in a pytorch network is converted from NHWC(channel last) to NCHW(channel first) format, but some operators (like Concat) with axis may not transform correctly. You may need to correct it manually.
|
||||
|
||||
- Currently, no RNN-related operations supported
|
||||
|
||||
## FAQ
|
||||
|
||||
- There are two types models saved in PyTorch. One is including architecture and weights, which is supported in the MMdnn now. The other one is only including the weights, which is not supported now.
|
||||
|
||||
```python
|
||||
only_weight_file = "./alexnet-owt-4df8aa71.pth" # Download from the model zoo
|
||||
architecture_weight_file = "imagenet_alexnet.pth" # Download using mmdownload()
|
||||
|
||||
m = torch.load(only_weight_file) # <class 'collections.OrderedDict'>
|
||||
m_1 = torch.load(architecture_weight_file) # <class 'torchvision.models.alexnet.AlexNet'> supported!
|
||||
|
||||
```
|
||||
- When you get the error "AttributeError: 'collections.OrderedDict' object has no attribute 'state_dict'" , it's because you use the model only include weights part. You need to save a new model with archietecture
|
||||
|
||||
```python
|
||||
torch.save(model, filename)
|
||||
```
|
||||
|
||||
- How to load the converted PyTorch model ?
|
||||
|
||||
```python
|
||||
|
||||
import torch
|
||||
import imp
|
||||
import numpy as np
|
||||
MainModel = imp.load_source('MainModel', "tf_pytorch_vgg19.py")
|
||||
|
||||
the_model = torch.load("tf_pytorch_vgg19.pth")
|
||||
the_model.eval()
|
||||
|
||||
x = np.random.random([224,224,3])
|
||||
x = np.transpose(x, (2, 0, 1))
|
||||
x = np.expand_dims(x, 0).copy()
|
||||
data = torch.from_numpy(x)
|
||||
data = torch.autograd.Variable(data, requires_grad = False).float()
|
||||
|
||||
predict = the_model(data)
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче