Models: can't import pretrained mobilenet model

Created on 15 Jun 2017  路  32Comments  路  Source: tensorflow/models

I download mobilenet_v1_1.0_224_2017_06_14.tar.gz and untar it and then use the following code to import it.

import tensorflow as tf
from tensorflow.contrib import layers
with tf.Session() as sess:
tf.contrib
saver = tf.train.import_meta_graph('./tmp/mobilenet_v1_1.0_224.ckpt.meta')

However, I got the following error. Any idea why this error happens.

Traceback (most recent call last):
File "./restore.py", line 9, in
saver = tf.train.import_meta_graph('./tmp/mobilenet_v1_1.0_224.ckpt.meta')
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1683, in import_meta_graph
**kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/meta_graph.py", line 499, in import_scoped_meta
producer_op_list=producer_op_list)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/importer.py", line 280, in import_graph_def
raise ValueError('No op named %s in defined operations.' % node.op)
ValueError: No op named SSTableReaderV2 in defined operations.

My tensorflow version: 1.1.0-rc1

Thanks,

awaiting response support

Most helpful comment

I'm also getting this on Ubuntu, TF 1.2.0.

As a work-around, I also tried to use the slim library definitions and restore the variables. I have been able to save (and test) frozen models for the 4 1.0 files, but on 0.75/0.50/0.25 models, I get errors (see this gist for all output) due to dimension mismatches, e.g.:

W tensorflow/core/framework/op_kernel.cc:1158] Invalid argument: Assign requires shapes of both tensors to match. lhs shape= [256] rhs shape= [64]
     [[Node: save/Assign_92 = Assign[T=DT_FLOAT, _class=["loc:@MobilenetV1/Conv2d_5_pointwise/BatchNorm/moving_mean"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/gpu:0"](MobilenetV1/Conv2d_5_pointwise/BatchNorm/moving_mean, save/RestoreV2_92/_167)]]

The code that I used can be found at this gist (tested with Python 3 + TF 1.2.0). You just need to edit it to have the models_slim_dir variable to have the path to the models git repo.

EDIT: Nevermind, it looks like I was not passing in the depth_multiplier variable (I overlooked it, as there is no mechanism for it using nets_factory). I have updated the gist and it seems to work.

All 32 comments

same issue on mac, tf version 1.2.0

@korrawat , do you have any thoughts on what is causing this?

I'm also getting this on Ubuntu, TF 1.2.0.

As a work-around, I also tried to use the slim library definitions and restore the variables. I have been able to save (and test) frozen models for the 4 1.0 files, but on 0.75/0.50/0.25 models, I get errors (see this gist for all output) due to dimension mismatches, e.g.:

W tensorflow/core/framework/op_kernel.cc:1158] Invalid argument: Assign requires shapes of both tensors to match. lhs shape= [256] rhs shape= [64]
     [[Node: save/Assign_92 = Assign[T=DT_FLOAT, _class=["loc:@MobilenetV1/Conv2d_5_pointwise/BatchNorm/moving_mean"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/gpu:0"](MobilenetV1/Conv2d_5_pointwise/BatchNorm/moving_mean, save/RestoreV2_92/_167)]]

The code that I used can be found at this gist (tested with Python 3 + TF 1.2.0). You just need to edit it to have the models_slim_dir variable to have the path to the models git repo.

EDIT: Nevermind, it looks like I was not passing in the depth_multiplier variable (I overlooked it, as there is no mechanism for it using nets_factory). I have updated the gist and it seems to work.

@aselle, I got the same error on Ubuntu, TF 1.2.0. I'm not really sure why the checkpoint files don't work either.

@korrawat , Is the current slim MobileNet code the same code that was used to create those checkpoints? Otherwise, I don't understand what could be the issue with my restoring code. If you have a chance to look at that, I'd appreciate it.

Looking into this.

To get rid of the SSTableReaderV2 op that doesn't exist in the open source TensorFlow, build a graph with logits, end_points = mobilenet_v1.mobilenet_v1(tf.placeholder(dtype=tf.float32, shape=(1, 224, 224, 3), name='image_tensor'), 1001) instead of importing from the checkpoint meta graph def with saver = tf.train.import_meta_graph('mobilenet_v1_1.0_224.ckpt.meta'). A complete example is in object_detection/exporter.py.

@futurely , Have you tried this approach with the non-1.0 MobileNets? I get errors due to incompatible dimensions (see my previous post for gist link).

EDIT: Nevermind, it looks like I was not passing in the depth_multiplier variable (I overlooked it, as there is no mechanism for it using nets_factory). I have updated the code and it seems to work.

Take a look #1702

After generating *.pb files by gist from @StanislawAntol and placing it to *.ckpt files I was able to load weight.

@NickShargan I have generated the *.pb file, what do you mean by placing it to the *.ckpt files and how did you do so?

Hi, I have the same error in ubuntu16.04, tensorflow1.2, have your guys fixed your problem?

Hi, I'am getting the same error on win10, tf 1.2. Anyone got a solution?

i have the same porblem~Anyone fixed it?

@StanislawAntol can you be more precise on the process you follow ?

@kmonachopoulos, I'm not sure how much more precise I can be than the gist posted above. You just need to edit it to have the models git repo and then run it.

Sorry, it was just a path error I missed, it is working, thanks

@kmonachopoulos, no worries. I guess I technically updated the code and hadn't editet the comment to reflect this. It's fixed now to be clearer.

@StanislawAntol Thanks for the gist code. So what are we supposed to do after generating the _.pb_ files?

@amirjamez, It contains the model so you can load it, feed in input (i.e., images), and get classification results, e.g., something like this C++ code or this Python code.

@StanislawAntol, Yes, but I would like to see the weights related to each layer, and more importantly, the final layer's weights. Can we do that using the _.pb_ files? If so, could you kindly provide a MWE?
Second Question: Did anyone figure out why tf.train_import did not work? If there is no way to restore the pre-train weights, I don't understand the reason we have those .meta, .data files at all. Thanks.

@amirjamez, GitHub Issues is not the appropriate place for those kind of questions. Places like StackOverflow are more appropriate (as stated in the README).

I haven't touched TensorFlow in a while, but I'm sure you can do it. This post might be helpful, though you might have to be aware that the frozen graph (e.g., the output of my gist) has all the weights as constants, not trainable variables.

@StanislawAntol , freeze_mobilenet.py as in gist would not work. The models git repo structure has changed with the latest version in https://github.com/tensorflow/models.git.

Can you please help to provide newer freeze_mobilenet.py version that is compatible with the newer repo structure.

@ra9hur , All that was needed is to provide the correct path to the slim directory. I have updated the gist so it accepts a command-line argument for the tensorflow/models directory (to which it appends research/slim to), so now you can run it via:

python freeze_mobilenet.py <path to tensorflow/models cloned directory>

@StanislawAntol , Thanks for changes to the script. Was able to generate .pb with the following changes.

  1. freeze_mobilenet.py
    Line 74: Missing './MobileNet/Labels.json' file
    MobileNet folder is not available in the latest models git repo. This could be git cloned from: https://github.com/Zehaos/MobileNet

Locally, after git cloning models and MobileNet repos, the structure would be:
./models/research/slim/...
./MobileNet/...

  1. freeze_mobilenet.py
    from models.research.slim.nets import mobilenet_v1 # line 44: path to nets
    from models.research.slim.datasets import imagenet # line 45: path to datasets

  2. ./models/research/slim/datasets/imagenet.py
    from datasets import dataset_utils # line 39: comment this line

@ra9hur, you shouldn't have needed to do anything but provide the path (e.g., /home/ra9hur/tf/models) at the command line. It should have created a MobileNet for you and not have needed any other changes.

@StanislawAntol , initially did try that, but was getting error for missing 'Labels.json' file. I was trying multiple options and must have overlooked something. Thanks again.

Are you sure you were running the newest gist code? I just copied it into a new directory, installed TF1.3 into a new virtual environment (running Python3), cloned the models repository, and ran it without any issue.

OK, to declutter a bit. Of the 3 errors,

  1. Older gist code (yesterday)- Changed path to ./model/research/slim. But then got an error for missing 'Labels.json' file. Then, I copied MobileNet from git. I could not further proceed for a different issue which I am not able to recollect now. Then, requested you for the newer gist script.
  2. Newer gist code (today)- Had left MobileNet folder as is. Got 2 & 3 errors (list above) on the newer code. But those are minor issues.

May be, if I had not copied MobileNet manually, the newer script would have created this folder for me. Hope, that clarifies.

EDIT: @StanislawAntol , had to create frozen weights again with spatial_squeeze=False. Tried running the script while removing 'MobileNet' folder. To confirm, new script creates 'MobileNet' folder, copies 'Labels.json' file into the folder and works as-is.

I am facing the same issue and have been trying to fix it since hours. I don't have a fast internet access right now and hence would like to know if anyone has a solution without having to download tensorflow/models ?

I had a similar problem with tensorflow 1.2, and when i update tensorflow to version 1.4, the problem was solved. Hope it helps:)

Same error with vanilla TF 1.4 but the gist posted above worked perfectly for me so problem solved thanks.

Was this page helpful?
0 / 5 - 0 ratings