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

Cannot parse file b'/content/drive/MyDrive/MobileFaceNet/MobileFaceNet_TF-master/arch/pretrained_model/saved_model.pb': Error parsing message. #73

Open
lnghia opened this issue Dec 18, 2020 · 7 comments

Comments

@lnghia
Copy link

lnghia commented Dec 18, 2020

I am trying to load the pretrained_model into a keras object by using tf.keras.models.load_model(). I've already renamed the model to saved_model.pb . However, I met this error which said:

Cannot parse file b'/content/drive/MyDrive/MobileFaceNet/MobileFaceNet_TF-master/arch/pretrained_model/saved_model.pb': Error parsing message. #73

How can I load and use the model (I need to use .fit() and .predict() methods).
Thank you !
I am using tensorflow 2.3.0

@lnghia lnghia changed the title Node 'gradients/MobileFaceNet/Logits/LinearConv1x1/BatchNorm/cond/FusedBatchNorm_1_grad/FusedBatchNormGrad' has an _output_shapes attribute inconsistent with the GraphDef for output #3: Dimension 0 in both shapes must be equal, but are 0 and 192. Shapes are [0] and [192]. Cannot parse file b'/content/drive/MyDrive/MobileFaceNet/MobileFaceNet_TF-master/arch/pretrained_model/saved_model.pb': Error parsing message. Dec 18, 2020
@jmattioni
Copy link

It's not possible to load the saved model to a Keras model. Tensorflow 2 changes relative to TF 1 are significant and Keras layers differ from the slim layers used in the sirius model. To load the saved model you must use Tensorflow 1.x with code similar to what you see in the sirius project. Once the graph/ckpt is loaded as a graph you can write out an hdf5 weights file with the help of h5py. The weights file can be loaded into a Keras model. However, you have to code a Keras model definition from scratch to exactly match the sirius definition in order to use the weights file. There is no converter that I could find.

There are a number of Keras implementations of MobileFaceNet available, but they're not exact matches to the sirius model and would not be able to successfully load the weights file produced from the sirius ckpt. Probably the most sensible option is to create or find a Keras model of MobileFaceNet and then do the training all over again from the same dataset used by sirius. I didn't want to do this because of the potential time/expense required. So as an exercise I created the weights file from the sirius ckpt, created a matching Keras model, loaded the weights and then converted the Keras model to a tflite model to test it.

The resulting model works, but doesn't seem to have the same level of accuracy as the original sirius tflite model. I suspect this is due to subtle differences between slim layers and corresponding Keras layers. There's also a custom prelu layer used in the sirius model that I replaced with a standard Keras PreLU layer. I suppose any or all of those differences could be affecting model accuracy.

@alexandrekj
Copy link

hi @jmattioni, can you share this modified keras implementation and the weights file that you created?

@jmattioni
Copy link

@alexandrekj

It’s been quite a while since I worked with this model. I was able to find my latest Google Colab notebook with the recoded Keras model. Here’s a link to it:

https://colab.research.google.com/drive/13ma9gXnPryeciAD6QFYLS9tjkETTvnh_?usp=sharing

The blocks in the notebook have to be manually executed more or less in order. Sorry, there are comments but there’s no specific instructions. Blocks that start with a comment that contains “NOPE!” were NOT used in the final result. Don't run those.

I don’t seem to have the original weights for the MobileFaceNet model. But, I believe the original MobileFaceNet weights are out there and should load into the Keras model built by the above notebook.

In my application I refined the base MobileFaceNet model by further training it with cat face images. The Keras model was then converted to TFLite to run on an Android phone. The app was then used to recognize specific cats.

Hope this helps!

@jmattioni
Copy link

@alexandrekj

Just found what I believe to be the original MobileFaceNet weights.

mobilecatfacenet.zip

@alexandrekj
Copy link

Thanks a lot @jmattioni!

@alexandrekj
Copy link

@jmattioni sorry to bother you again but do you have some insight on how to convert the frozen model (.pb file) to compatible .h5 keras file? I've searched to do this in several different ways but no one worked and seems like you have done this succesfully.

@jmattioni
Copy link

@alexandrekj

I believe this is the original weights file:
mobilefacenet_pb_weights.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants