Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert ResNet101 from TensorFlow to PyTorch #22

Closed
ahkarami opened this Issue Dec 6, 2017 · 12 comments

Comments

Projects
None yet
4 participants
@ahkarami
Copy link

ahkarami commented Dec 6, 2017

Dear @kitstar,
I want to convert a ResNet V1 101 model (from TF-Slim) to PyTorch. Would you please kindly help me to do that?
Just as another suggestion, I think it would be great if you create a README.md file for PyTorch conversion section.

@kitstar

This comment has been minimized.

Copy link
Contributor

kitstar commented Dec 6, 2017

Hi @ahkarami ,

Tested with follow steps and some code addition.

  1. Download resnet_v1_101 data
$ wget http://download.tensorflow.org/models/resnet_v1_101_2016_08_28.tar.gz
$ tar -xvf resnet_v1_101_2016_08_28.tar.gz
  1. Extract tf model files
    Refer the tensorflow model extractor to implement your own model extract code.

I did it for you and you can refer this commit

python -m mmdnn.conversion.examples.tensorflow.extract_model -n resnet_v1_101 -ckpt resnet_v1_101.ckpt
  1. Convert tf to IR
python -m mmdnn.conversion._script.convertToIR -f tensorflow -d kit_imagenet -n imagenet_resnet_v1_101.ckpt.meta --dstNodeName Squeeze -w imagenet_resnet_v1_101.ckpt
  1. Convert IR to Pytorch
    Please use argument -dw to extract the weight file for pytorch model saver
python -m mmdnn.conversion._script.IRToCode -f pytorch --IRModelPath kit_imagenet.pb --dstModelPath kit_imagenet.py --IRWeightPath kit_imagenet.npy -dw kit_pytorch.npy

Currently you can use the kit_imagenet.py and kit_pytorch.npy to inference. And you can dump the original PyTorch model by next step.

  1. Dump the PyTorch model
$ python -m mmdnn.conversion.examples.pytorch.imagenet_test --dump resnet.pth -n kit_imagenet.py -w kit_pytorch.npy

PyTorch model file is saved as [resnet.pth], generated by [kit_imagenet.py] and [kit_pytorch.npy].

Done.

@kitstar

This comment has been minimized.

Copy link
Contributor

kitstar commented Dec 6, 2017

PS: the readme of PyTorch is created.

@kitstar kitstar closed this Dec 7, 2017

@ahkarami

This comment has been minimized.

Copy link
Author

ahkarami commented Dec 7, 2017

Dear @kitstar,
Thank you very much for your complete answer. You are incredible!! 🥇

@ahkarami

This comment has been minimized.

Copy link
Author

ahkarami commented Dec 18, 2017

Dear @kitstar,
Thanks a lot for your complete and clear answer. However, unfortunately because I was so busy last 2 weeks, I couldn't test your answer. I Tested it today. At first I have downloaded the resnet_v1_101 data as you have mentioned. But, at the second step:
python -m mmdnn.conversion.examples.tensorflow.extract_model -n resnet_v1_101 -ckpt resnet_v1_101.ckpt
unfortunately I got this error:

Traceback (most recent call last):
  File "/home/karami/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/karami/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/karami/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/examples/tensorflow/extract_model.py", line 36, in <module>
    'resnet50'      : resnet_v2.resnet_arg_scope,
AttributeError: module 'tensorflow.contrib.slim.python.slim.nets.resnet_v2' has no attribute 'resnet_arg_scope'

Would you please help me to address this error?

@kitstar

This comment has been minimized.

Copy link
Contributor

kitstar commented Dec 19, 2017

Hi, what is your tf version? I tested it with tf 1.3 and 1.4 and you can grep your tf package with "resnet_arg_scope" to find the namespace.

@ahkarami

This comment has been minimized.

Copy link
Author

ahkarami commented Dec 19, 2017

Thank you. currently TF 1.1.0 has been installed on my system. I will upgrade it and test the conversion code again.

@DW1HH

This comment has been minimized.

Copy link

DW1HH commented Aug 27, 2018

@kitstar HI,What does it mean-> 'vgg19' : [(21, 0.37522122), (144, 0.28500062), (23, 0.099720284), (134, 0.036305398), (22, 0.033559237)],

@namizzz

This comment has been minimized.

Copy link
Member

namizzz commented Aug 27, 2018

Hi @DW1HH , previously, the truth table is the results of imagenet image for test_truth function to test the accuracy of conversion. But we use tests/test_conversion_imagenet.py to check the accuracy.

@DW1HH

This comment has been minimized.

Copy link

DW1HH commented Aug 27, 2018

@namizzz i want to transform mobilenet_v2_1.4 model from tensorflow to pytorch,but i have no idea for that.

@namizzz

This comment has been minimized.

Copy link
Member

namizzz commented Aug 27, 2018

Hi @DW1HH , refer to this:
mmconvert -sf tensorflow -iw resnet50v1_frozen.pb --inNodeName "input_tensor" --inputShape 224,224,3 --dstNodeName "softmax_tensor" -df pytorch -dw pytorch.npy -om resnet50v1.pth

@DW1HH

This comment has been minimized.

Copy link

DW1HH commented Aug 27, 2018

Thank you for your reply@namizzz

@DW1HH

This comment has been minimized.

Copy link

DW1HH commented Aug 28, 2018

I am sorry to bother you. @namizzz When the script finishes running,i get two files, mobilenet.pth and mobilenet.py ,but i can not load the mobilenet.pth , it's type-> <type 'long'>,not a dict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.