microsoft / MMdnn

MMdnn is a set of tools to help users inter-operate among different deep learning frameworks. E.g. model conversion and visualization. Convert models between Caffe, Keras, MXNet, Tensorflow, CNTK, PyTorch Onnx and CoreML.
MIT License
5.8k stars 965 forks source link

Convert ResNet101 from TensorFlow to PyTorch #22

Closed ahkarami closed 6 years ago

ahkarami commented 6 years ago

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 commented 6 years ago

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
  2. 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
  2. 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 commented 6 years ago

PS: the readme of PyTorch is created.

ahkarami commented 6 years ago

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

ahkarami commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

DW1HH commented 6 years ago

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

namizzz commented 6 years ago

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 commented 6 years ago

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

namizzz commented 6 years ago

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 commented 6 years ago

Thank you for your reply@namizzz

DW1HH commented 6 years ago

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.

z6833 commented 5 years ago

I have a tensorflow models produced by tensorflow1.14 , and there is no .ckpt files . There are model.data-00000-of-00001, model.index and model.meta instead . How can I transform them to .pt/.pkl files ? Could you help me ? URL for models https://github.com/StanfordSNR/indigo/tree/a9b2060d39e4da2e8987e893e3eca2a6c7cd0ab9/dagger/model

Yong-DAI commented 4 years ago

I have completed the above steps and got a vgg16.pth file, however, when I used that in pytorch. I got the following errors:

File "", line 1, in runfile('E:/doc_dy/pytorch_deephash-master/train.py', wdir='E:/doc_dy/pytorch_deephash-master')

File "C:\Users\chddy\Anaconda3\envs\pytorch_gpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "C:\Users\chddy\Anaconda3\envs\pytorch_gpu\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "E:/doc_dy/pytorch_deephash-master/train.py", line 8, in from net_b import AlexNetPlusLatent

File "E:\doc_dy\pytorch_deephash-master\net_b.py", line 6, in alexnet_model = models.vgg16(pretrained=True) ### alexnet

File "C:\Users\chddy\Anaconda3\envs\pytorch_gpu\lib\site-packages\torchvision\models\vgg.py", line 144, in vgg16 return _vgg('vgg16', 'D', False, pretrained, progress, **kwargs)

File "C:\Users\chddy\Anaconda3\envs\pytorch_gpu\lib\site-packages\torchvision\models\vgg.py", line 92, in _vgg progress=progress)

File "C:\Users\chddy\Anaconda3\envs\pytorch_gpu\lib\site-packages\torch\hub.py", line 434, in load_state_dict_from_url return torch.load(cached_file, map_location=map_location)

File "C:\Users\chddy\Anaconda3\envs\pytorch_gpu\lib\site-packages\torch\serialization.py", line 387, in load return _load(f, map_location, pickle_module, **pickle_load_args)

File "C:\Users\chddy\Anaconda3\envs\pytorch_gpu\lib\site-packages\torch\serialization.py", line 574, in _load result = unpickler.load()

ModuleNotFoundError: No module named 'MainModel'

Yong-DAI commented 4 years ago

I transfer the pretrained vgg16.ckpt in tensorflow to be used in pytorch. Because the pretrained vgg16.pth in pytorch is not trained enough.

arya-none commented 4 years ago

Dear @kitstar Would you please kindly help me to find whats the problems as below? when i tried to extract resnet50 tf mode. The sincerity anticipates your reply,thanks! [root@localhost model]# python3 -m mmdnn.conversion.examples.tensorflow.extract_model -n resnet50 -ckpt model.ckpt-450500 WARNING:tensorflow: The TensorFlow contrib module will not be included in TensorFlow 2.0. For more information, please see:

WARNING:tensorflow:From /usr/local/python3.7.5/lib/python3.7/site-packages/mmdnn/conversion/examples/tensorflow/extract_model.py:23: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From /usr/local/python3.7.5/lib/python3.7/site-packages/tensorflow_core/contrib/layers/python/layers/layers.py:1057: Layer.apply (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version. Instructions for updating: Please use layer.__call__ method instead. WARNING:tensorflow:From /usr/local/python3.7.5/lib/python3.7/site-packages/mmdnn/conversion/examples/tensorflow/extract_model.py:79: The name tf.global_variables_initializer is deprecated. Please use tf.compat.v1.global_variables_initializer instead.

WARNING:tensorflow:From /usr/local/python3.7.5/lib/python3.7/site-packages/mmdnn/conversion/examples/tensorflow/extract_model.py:81: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

2020-08-11 16:31:03.102027: W tensorflow/core/platform/profile_utils/cpu_utils.cc:98] Failed to find bogomips in /proc/cpuinfo; cannot determine CPU frequency 2020-08-11 16:31:03.104051: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x26ad6f30 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2020-08-11 16:31:03.104152: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version WARNING:tensorflow:From /usr/local/python3.7.5/lib/python3.7/site-packages/mmdnn/conversion/examples/tensorflow/extract_model.py:82: The name tf.summary.FileWriter is deprecated. Please use tf.compat.v1.summary.FileWriter instead.

WARNING:tensorflow:From /usr/local/python3.7.5/lib/python3.7/site-packages/mmdnn/conversion/examples/tensorflow/extract_model.py:85: The name tf.train.Saver is deprecated. Please use tf.compat.v1.train.Saver instead.

2020-08-11 16:31:14.165516: W tensorflow/core/framework/op_kernel.cc:1651] OP_REQUIRES failed at save_restore_v2_ops.cc:184 : Not found: Key resnet_v2_50/block1/unit_1/bottleneck_v2/conv1/BatchNorm/beta not found in checkpoint Traceback (most recent call last): File "/usr/local/python3.7.5/lib/python3.7/site-packages/tensorflow_core/python/client/session.py", line 1365, in _do_call return fn(*args) File "/usr/local/python3.7.5/lib/python3.7/site-packages/tensorflow_core/python/client/session.py", line 1350, in _run_fn target_list, run_metadata) File "/usr/local/python3.7.5/lib/python3.7/site-packages/tensorflow_core/python/client/session.py", line 1443, in _call_tf_sessionrun run_metadata) tensorflow.python.framework.errors_impl.NotFoundError: Key resnet_v2_50/block1/unit_1/bottleneck_v2/conv1/BatchNorm/beta not found in checkpoint [[{{node save/RestoreV2}}]]

YIRuriZhongtian commented 3 years ago

我有一个由tensorflow1.14生成的tensorflow模型,并且没有.ckpt文件。有model.data-00000-of-00001,model.index和model.meta代替。如何将它们转换为.pt / .pkl文件?你可以帮帮我吗 ?模型的网址https://github.com/StanfordSNR/indigo/tree/a9b2060d39e4da2e8987e893e3eca2a6c7cd0ab9/dagger/model

hello,have you solved this problem? i have same question

Rushikeshkoli15 commented 2 years ago

hello there, i am getting an error while converting my tf model to IR AssertionError: Squeeze is not in graph Could anyone help plz?

jsjy1 commented 2 years ago

我有一个由tensorflow1.14生成的tensorflow模型,并且没有.ckpt文件。有model.data-00000-of-00001,model.index和model.meta代替。如何将它们转换为.pt / .pkl文件?你可以帮帮我吗 ?模型的网址https://github.com/StanfordSNR/indigo/tree/a9b2060d39e4da2e8987e893e3eca2a6c7cd0ab9/dagger/model

hello,have you solved this problem? i have same question

hello,have you solved this problem? i have same question