models icon indicating copy to clipboard operation
models copied to clipboard

KeyError: 'IteratorGetDevice' when using official pre-trained resnet model

Open xqiangx1991 opened this issue 8 years ago • 27 comments

I use this code to import pre-trained model.

sess = tf.Session()
saver = tf.train.import_meta_graph("path/to/model/model.ckpt-62500.meta")
saver.restore(sess,"path/to/model/model.ckpt-62500")

but when i import it, it raise

KeyError: 'IteratorGetDevice'

and there are more key error when proceed.

some one asked the same question in this issue:https://github.com/tensorflow/tensorflow/issues/21024, it said "fixed after updated to tensorflow 1.9", but it does not work for me.

Does anyone else coming up with this error? or am i writing code wrong?

xqiangx1991 avatar Aug 02 '18 07:08 xqiangx1991

Thank you for your post. We noticed you have not filled out the following field in the issue template. Could you update them if they are relevant in your case, or leave them as N/A? Thanks. What is the top-level directory of the model you are using Have I written custom code OS Platform and Distribution TensorFlow installed from TensorFlow version Bazel version CUDA/cuDNN version GPU model and memory Exact command to reproduce

tensorflowbutler avatar Aug 02 '18 19:08 tensorflowbutler

@tensorflowbutler sorry for forgetting add fileds in issue template.

I used absolute path of the model, and just download and unzip the model under my project.

OS Platform: MacOS High Serria 10.13.6/Ubuntu 16.04 Tensorflow installed from pip Tensorflow version: 1.4.0 and 1.9.0 both tried Bazel version:N/A CUDA/cuDNN version: macos:N/A, Ubuntu:cuda 9.0 GPU memory: 10G and the command to reproduce is as follows:

sess = tf.Session()
saver = tf.train.import_meta_graph("path/to/model/model.ckpt-62500.meta")
saver.restore(sess,"path/to/model/model.ckpt-62500")

I tried a model generated by myself, and it works.

xqiangx1991 avatar Aug 03 '18 02:08 xqiangx1991

Let me say you that you're not alone with this issue. I've noticed the same error recently. I'm trying to use an official pretrained model (https://github.com/tensorflow/models/tree/master/official/resnet, ResNet-50 v2 on ImageNet) for further segmentation training purposes and get the same error.

tf.reset_default_graph()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True

with tf.Session(config = config) as sess:
        sess.run(tf.global_variables_initializer())
        saver = tf.train.import_meta_graph('./pretrained/imagenet-resnet50/model.ckpt-258931.meta', clear_devices = True)
        saver.restore(sess, tf.train.latest_checkpoint('./pretrained/imagenet-resnet50/'))

There's no difference in running tf.train.import_meta_graph() without the clear_devices flag regarding the printed error messages.


OS Platform: Windows 10 Education 1803 Tensorflow: Installed from pip (using Anaconda) Tensorflow version: 1.9.0 CUDA version: 9.0.176 cuDNN version: 7.1 GPU: NVIDIA GTX 1070, 8 GB Command to reproduce: see above

TheKeyLimePie avatar Aug 03 '18 12:08 TheKeyLimePie

Adding @mrry. I can also repo this my instance with latest TF nightly. Not sure why "IteratorGetDevice" is not available in the registed ops. Does it have anything to do with https://github.com/tensorflow/tensorflow/pull/18624?

qlzh727 avatar Aug 06 '18 18:08 qlzh727

It looks like the saved MetaGraphDef has some ops from tf.contrib in it, which haven't been loaded when you call tf.train.import_meta_graph().

Can you try adding the following statement before calling tf.train.import_meta_graph()?

_ = tf.contrib  # Force the `tf.contrib` kernels to be loaded.

mrry avatar Aug 06 '18 18:08 mrry

I added your suggested line of code just after sess.run(tf.global_variables_initializer()) but it didn't solve the error.

TheKeyLimePie avatar Aug 06 '18 19:08 TheKeyLimePie

Can you try adding it before the with tf.Session(...): line?

(Also, I don't think sess.run(tf.global_variables_initializer()) is doing anything, since the graph is empty at that point, so there are no variables, global or otherwise, to initialize.)

mrry avatar Aug 06 '18 20:08 mrry

Unfortunately, this didn't do the trick either and lead to the same error message. Calling a sub module like _ = tf.contrib.nn, on the other hand, revealed a new error message:

NotFoundError: Op type not registered 'NcclAllReduce' in binary running on [PC-NAME]. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) tf.contrib.resampler should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.

TheKeyLimePie avatar Aug 06 '18 20:08 TheKeyLimePie

Aha, thanks: now that you mention it, I realize that you need to reference a submodule of tf.contrib to trick the lazy module loader into running.

The NcclAllReduce op looks like it uses even lazier loading...

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/nccl/python/ops/nccl_ops.py#L267-L274

It looks like you might need another call to get this to run, e.g.

from tensorflow.contrib.nccl.python.ops import nccl_ops
nccl_ops._maybe_load_nccl_ops_so()

(Clearly it's a bug that you should have to do so much work to load a pretrained model. I'm not sure how this model was generated, but perhaps there is a way to generate it without the input pipeline and NCCL ops?)

mrry avatar Aug 06 '18 21:08 mrry

@mrry Unfortunately, even with all code above, new error message occurred.

tensorflow.python.framework.errors_impl.InvalidArgumentError: NodeDef missing attr 'output_types' from Op<name=FunctionBufferingResource; signature=string_arg:string, target_device:string -> resource:resource; attr=shared_name:string; attr=container:string; attr=f:func; attr=buffer_size:int; attr=output_types:list(type); is_stateful=true>; NodeDef: {{node FunctionBufferingResource}} = FunctionBufferingResource[_output_shapes=[<unknown>], buffer_size=1, container="", f=_prefetch_fn_0CSL7hFXlzE[], shared_name="", _device="/device:GPU:0"](IteratorToStringHandle, IteratorGetDevice)

During handling of the above exception, another exception occurred:

ValueError: NodeDef missing attr 'output_types' from Op<name=FunctionBufferingResource; signature=string_arg:string, target_device:string -> resource:resource; attr=shared_name:string; attr=container:string; attr=f:func; attr=buffer_size:int; attr=output_types:list(type); is_stateful=true>; NodeDef: {{node FunctionBufferingResource}} = FunctionBufferingResource[_output_shapes=[<unknown>], buffer_size=1, container="", f=_prefetch_fn_0CSL7hFXlzE[], shared_name="", _device="/device:GPU:0"](IteratorToStringHandle, IteratorGetDevice)

I am not sure if it is because i used macos cpu version of tensorflow. Later i will try tensorflow-gpu under ubuntu

xqiangx1991 avatar Aug 07 '18 06:08 xqiangx1991

This error reflects a mismatch between the version of TensorFlow used to save the model, and the version in which you're importing it. The attr mentioned in the error message was added in https://github.com/tensorflow/tensorflow/commit/6d1beebffd216c5cb2eda8a8ba3a59d605ed8fe1.

It seems clear that the model uses ops from tf.contrib, and there is no guarantee that tf.contrib ops will be compatible across versions. In particular, their signature may change, or they may be deleted and/or replaced with a different op. This suggests two workarounds:

  • Ensure that you're using the same version of TensorFlow for both saving and loading the model.
  • Ensure that the model you save has no tf.contrib ops in it. The Estimator.export_savedmodel() API may be useful here, because it can avoid inserting the input pipeline and optimizer ops, which seem to be what's causing problems here.

mrry avatar Aug 07 '18 14:08 mrry

@mrry It looks like the function nccl_ops._maybe_load_nccl_ops_so() will be available in the upcoming release, R1.10. Taking a closer look at the current release (R1.9), the resource_loader operation is executed right in the beginning of nccl_ops.py: https://github.com/tensorflow/tensorflow/blob/r1.9/tensorflow/contrib/nccl/python/ops/nccl_ops.py#L29-L30 So I simply added the from tensorflow.contrib.nccl.python.ops import nccl_ops to my code which should automatically execute the line mentioned above. Nevertheless, the error message remained the same.

TheKeyLimePie avatar Aug 07 '18 15:08 TheKeyLimePie

Thanks @mrry. I guess https://github.com/tensorflow/models/blob/master/official/resnet/imagenet_main.py#L184 might be the cause of this incompatibility. Since tf-model team own the official model code, we will update to use the latest tf.dataset code and regenerate the checkpoints.

qlzh727 avatar Aug 07 '18 15:08 qlzh727

@mrry Thanks for the tips!

I added import tensorflow.contrib to solve this for me on TF 1.8.0.

mfernezir avatar Aug 08 '18 11:08 mfernezir

Adding Taylor who is working on the model side.

qlzh727 avatar Aug 17 '18 17:08 qlzh727

Anyone know what is the version of tf for saving the official resnet model in the first place?

WonderAndMaps avatar Aug 21 '18 21:08 WonderAndMaps

I generated the checkpoints on 6/1/18 with a recent nightly build, which would put the version around 1.9.

robieta avatar Aug 21 '18 21:08 robieta

@mrry sorry to bother you ~ I have tried all your suggestions, however it doesn't work~ by the way , how can I know the version of online model ? The version of my tensorflow is 1.9.0, should I upgrade to 1.10.0?

HKDown2014 avatar Aug 24 '18 01:08 HKDown2014

@JoelTsui Have you solved this probelem ?

HKDown2014 avatar Aug 24 '18 01:08 HKDown2014

From previous comment, it seems that the checkpoints are generated with TF 1.8.

qlzh727 avatar Aug 24 '18 15:08 qlzh727