Issue
I had Saved model using
tf.keras.experimental.export_saved_model(model, export_path)
This model have custom layers and loss function.
Loading model using
import tensorflow as tf
import tensorflow_hub as hub
import keras
class training_model:
def __init__(self):
path_bce="D:\\nsfw\\training_model\\models\\bce_20210120_153631"
path2="D:\\nsfw\\training_model\\models\\soft-f1_20210120_153631"
self.graph = tf.Graph()
with self.graph.as_default():
self.session = tf.Session()
with self.session.as_default() :
self.reloaded =tf.keras.experimental.load_from_saved_model(path2, custom_objects={'KerasLayer':hub.KerasLayer})
training_model=training_model()
img = keras.preprocessing.image.load_img(
"0drqz7883ox51.jpg", target_size=(224, 224)
)
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
with training_model.graph.as_default():
with training_model.session.as_default():
print(training_model.reloaded.predict(img_array,steps=1))
It is working fine if i remove Graph and Session but I want to serve this model with API.
Solution
You can try something like this.
with self.graph1.as_default():
self.face_graph = tf.compat.v1.GraphDef()
fid = tf.io.gfile.GFile(self.facenet_model, "rb")
serialized_graph = fid.read()
self.face_graph.ParseFromString(serialized_graph)
tf.import_graph_def(self.face_graph, name="")
self.facenet_sess = tf.compat.v1.Session(graph=self.graph1)
self.images_placeholder = self.graph1.get_tensor_by_name("input:0")
self.embeddings = self.graph1.get_tensor_by_name("embeddings:0")
Answered By - Neel Gajjar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.