Skip to content

Commit

Permalink
refine README for 0.1.4 version.
Browse files Browse the repository at this point in the history
  • Loading branch information
kitstar committed Mar 22, 2018
1 parent c6ec848 commit 352e7fc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/keras2cntk.md
Expand Up @@ -52,7 +52,7 @@ $ mmconvert -sf keras -iw imagenet_inception_v3.h5 -df cntk -om keras_to_cntk_in
CNTK model file is saved as [keras_to_cntk_inception_v3.dnn], generated by [2c33f7f278cb46be992f50226fcfdb5d.py] and [2c33f7f278cb46be992f50226fcfdb5d.npy].
```

Then you get the CNTK original model *keras_to_cntk_inception_v3.dnn* converted from Keras.
Then you get the CNTK original model *keras_to_cntk_inception_v3.dnn* converted from Keras. **2c33f7f278cb46be992f50226fcfdb5d.py** and **2c33f7f278cb46be992f50226fcfdb5d.npy** are temporal files which will be removed automatically.

### **Step-by-step Command (for debugging)**

Expand Down
41 changes: 32 additions & 9 deletions docs/tf2pytorch.md
Expand Up @@ -6,12 +6,14 @@ Source: TensorFlow

Destination: PyTorch

0. Prepare the TensorFlow model
---

## 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
$ python -m mmdnn.conversion._script.extractModel -f tensorflow -n resnet_v2_152
$ 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
Expand All @@ -21,12 +23,12 @@ Model saved in file: ./imagenet_resnet_v2_152.ckpt

The 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.

1. Find the output node of the model
## 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
$ python -m mmdnn.conversion.examples.tensorflow.vis_meta imagenet_resnet_v2_152.ckpt.meta ./log/
$ mmvismeta imagenet_resnet_v2_152.ckpt.meta ./log/
.
.
.
Expand All @@ -39,10 +41,31 @@ The you can open URL above to find the output node of your model,

like the squeeze node named *MMdnn_Output* we setup in our tensorflow model extractor. Detail information is in [TensorFlow README](https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/tensorflow/README.md)

2. Convert the pre-trained model files to intermediate representation
## 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
$ python -m mmdnn.conversion._script.convertToIR -f tensorflow -n imagenet_resnet_v2_152.ckpt.meta -w imagenet_resnet_v2_152.ckpt --dstNode MMdnn_Output -o converted
$ 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.
Expand All @@ -54,10 +77,10 @@ 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.

3. Convert the IR files to PyTorch code
#### Convert the IR files to PyTorch code

```bash
$ python -m mmdnn.conversion._script.IRToCode -f pytorch -n converted.pb -w converted.npy -d converted_pytorch.py -dw converted_pytorch.npy
$ 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].
Expand All @@ -68,7 +91,7 @@ And you will get a filename *converted_pytorch.py*, which contains the **origina

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.

4. Dump the original PyTorch model
#### Dump the original PyTorch model

```bash
$ python -m mmdnn.conversion.examples.pytorch.imagenet_test -n converted_pytorch.py -w converted_pytorch.npy --dump converted_pytorch.pth
Expand Down
4 changes: 2 additions & 2 deletions mmdnn/conversion/examples/tensorflow/extractor.py
Expand Up @@ -155,11 +155,11 @@ def handle_checkpoint(cls, architecture, path):

init = tf.global_variables_initializer()
with tf.Session() as sess:
writer = tf.summary.FileWriter('./graphs', sess.graph)
# tf.train.export_meta_graph("kit.meta", as_text=True)
# writer = tf.summary.FileWriter('./graphs', sess.graph)
writer.close()
sess.run(init)
saver = tf.train.Saver()
tf.train.export_meta_graph("kit.meta", as_text=True)
saver.restore(sess, path + cls.architecture_map[architecture]['filename'])
save_path = saver.save(sess, path + "imagenet_{}.ckpt".format(architecture))
print("Model saved in file: %s" % save_path)
Expand Down
5 changes: 2 additions & 3 deletions mmdnn/conversion/tensorflow/README.md
Expand Up @@ -19,7 +19,7 @@ Support frameworks: ['resnet_v1_152', 'inception_v3', 'resnet_v2_50', 'resnet_v2
Example:

```bash
$ python -m mmdnn.conversion._script.extractModel -f tensorflow -n resnet_v2_152
$ 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
Expand All @@ -30,7 +30,6 @@ Model saved in file: ./imagenet_resnet_v2_152.ckpt
Then you can see files *imagenet_resnet_v2_152.ckpt.data-00000-of-00001*, *imagenet_resnet_v2_152.ckpt.index* and *imagenet_resnet_v2_152.ckpt.meta*, which can be handled by Tensorflow parser.


Mainly extract code like:

```python
Expand All @@ -48,7 +47,7 @@ with slim.arg_scope(...):
When you prepared your checkpoint, you can find the output node name from the graph by Tensorboard.

```bash
$ python -m mmdnn.conversion.examples.tensorflow.vis_meta imagenet_resnet_v2_152.ckpt.meta ./logs/
$ mmvismeta imagenet_resnet_v2_152.ckpt.meta ./logs/

TensorBoard 0.4.0rc3 at http://kit-station:6006 (Press CTRL+C to quit)
```
Expand Down

0 comments on commit 352e7fc

Please sign in to comment.