tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.43k stars 1.92k forks source link

Missing weights in converted model #5309

Closed ibnyusuf closed 3 years ago

ibnyusuf commented 3 years ago

I am attempting to convert a Keras model to a Tensorflow js model using the converter tool. The resulting model is missing weight meta data in the weightManifestsection of the model.json file.

Model generation and saving code:

import os
#from face_recognition import FaceRecognition
import keras_vggface
from mtcnn.mtcnn import MTCNN
from PIL import Image
from keras_vggface.utils import preprocess_input
from matplotlib import pyplot
from numpy import asarray
from keras_vggface.vggface import VGGFace
from scipy.spatial.distance import cosine
from tensorflow.keras.models import load_model

def get_embeddings(filenames):
    # extract faces
    faces = [extract_face(f) for f in filenames]
    # convert into an array of samples
    samples = asarray(faces, 'float32')
    # prepare the face for the model, e.g. center pixels
    samples = preprocess_input(samples, version=2)
    # create a vggface model
    model = VGGFace(model='resnet50', include_top=False, input_shape=(224, 224, 3), pooling='avg')
    # perform prediction
    yhat = model.predict(samples)
    return yhat, model

def is_match(known_embedding, candidate_embedding, thresh=0.5):
    # calculate distance between embeddings
    score = cosine(known_embedding, candidate_embedding)
    if score <= thresh:
        print('>face is a Match' % (score, thresh))
    else:
        print('>face is not a match' % (score, thresh))

if __name__ == '__main__':
    filenames = ["test1.jpg", "test2.jpg", "test3.jpg", "test4.jpg"]
    embeddings, model = get_embeddings(filenames);
    ahmed_id = embeddings[1];
    is_match(embeddings[0], embeddings[1])
    is_match(embeddings[2], embeddings[3])
    is_match(embeddings[1], embeddings[2])
    model.save("model", include_optimizer=True)

Model conversion code:

 tensorflowjs_converter --input_format=keras_saved_model --metadata= --quantize_uint8=* C:\Users\user\Documents\Face_Recognition_Tensorflow\model .

Error that I see when attempting to load into React Native app:

[Unhandled promise rejection: Error: 212 of 265 weights are not set: conv1/7x7_s2/bn/gamma,conv1/7x7_s2/bn/beta, ...

If I open the modeel.json file the meta data in the weightsManifest section is missing for these layers. I am not sure if it is an issue with how I saved the model or with the conversion process. However, if I read the saved model back into Python it works fine which makes me suspect the conversion process. Any ideas? Thanks!

rthadur commented 3 years ago

This is something to do with the model conversion , @pyu10055 do you know if keras_vggface is supported in tfjs ?

rthadur commented 3 years ago

can you try to convert using below command tensorflowjs_converter \ --input_format=tf_saved_model \ --output_format=tfjs_graph_model \ --signature_name=serving_default \ --saved_model_tags=serve \ /mobilenet/saved_model \ /mobilenet/web_model ans try to load the model using tf.loadGraphModel ?

ibnyusuf commented 3 years ago

Thanks a lot, that works! Could you explain what was causing the error?

rthadur commented 3 years ago

I can not comment with out knowing what steps you have used to convert model and load.

ibnyusuf commented 3 years ago

I used

tensorflowjs_converter --input_format=keras_saved_model --metadata= --quantize_uint8=* C:\Users\user\Documents\Face_Recognition_Tensorflow\model .

to convert the model and

tf.loadLayersModel

to load the model.

The command above to convert the model was produced by the wizard.

rthadur commented 3 years ago

if it is from wizard , i have no idea , in general all the saved models are converted using the commands I provided. @pyu10055 is this a bug in wizard or is it expected ?

google-ml-butler[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you.