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

can't import pretrained mobilenet model #1564

Closed
kaishijeng opened this issue Jun 15, 2017 · 32 comments
Closed

can't import pretrained mobilenet model #1564

kaishijeng opened this issue Jun 15, 2017 · 32 comments
Assignees
Labels
stat:awaiting response Waiting on input from the contributor type:support

Comments

@kaishijeng
Copy link

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,

@yan7109
Copy link

yan7109 commented Jun 15, 2017

same issue on mac, tf version 1.2.0

@aselle
Copy link
Contributor

aselle commented Jun 15, 2017

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

@aselle aselle added stat:awaiting response Waiting on input from the contributor type:support labels Jun 15, 2017
@StanislawAntol
Copy link

StanislawAntol commented Jun 16, 2017

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.

@korrawat
Copy link
Contributor

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

@StanislawAntol
Copy link

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

@sguada sguada self-assigned this Jun 16, 2017
@sguada
Copy link
Member

sguada commented Jun 16, 2017

Looking into this.

@futurely
Copy link

futurely commented Jun 20, 2017

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.

@StanislawAntol
Copy link

StanislawAntol commented Jun 20, 2017

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

@sguada
Copy link
Member

sguada commented Jun 20, 2017

Take a look #1702

@sguada sguada closed this as completed Jun 20, 2017
@NickShargan
Copy link

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

@MilPat555
Copy link

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

@KeyKy
Copy link

KeyKy commented Jul 14, 2017

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

@moniqueexner
Copy link

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

@xweihithub
Copy link

i have the same porblem~Anyone fixed it?

@kmonachopoulos
Copy link

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

@StanislawAntol
Copy link

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

@kmonachopoulos
Copy link

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

@StanislawAntol
Copy link

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

@amirjamez
Copy link

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

@StanislawAntol
Copy link

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

@amirjamez
Copy link

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

@StanislawAntol
Copy link

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

@ra9hur
Copy link

ra9hur commented Oct 16, 2017

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

@StanislawAntol
Copy link

@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>

@ra9hur
Copy link

ra9hur commented Oct 17, 2017

@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

@StanislawAntol
Copy link

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

@ra9hur
Copy link

ra9hur commented Oct 17, 2017

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

@StanislawAntol
Copy link

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.

@ra9hur
Copy link

ra9hur commented Oct 17, 2017

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.

@kashifmin
Copy link

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 ?

@roycezjq
Copy link

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:)

@Burton2000
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting response Waiting on input from the contributor type:support
Projects
None yet
Development

No branches or pull requests