Adaptando mi código de TF1 a TF2.6 tengo problemas. Estoy tratando de agregar algunas capas personalizadas a un resnet de inicio, guardar el modelo y luego cargarlo y ejecutarlo.
from tensorflow.keras.layers import Dense from tensorflow.keras.models import Model from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2 from tensorflow.keras.layers import Dense, GlobalAveragePooling2D import tensorflow as tf import numpy as np from PIL import Image export_path = "./save_test" # Get model without top and add two layers base_model = InceptionResNetV2(weights='imagenet', input_tensor=None, include_top=False) out = base_model.output out = GlobalAveragePooling2D()(out) predictions = Dense(7, activation='softmax', name="output")(out) # Make new model using inputs from base model and custom outputs model = Model(inputs=base_model.input, outputs=[predictions]) # save model tf.saved_model.save(model, export_path) # load model and run with tf.compat.v1.Session(graph=tf.Graph()) as sess: tf.compat.v1.saved_model.loader.load(sess, ['serve'], export_path) graph = tf.compat.v1.get_default_graph() img = Image.new('RGB', (299, 299)) x = tf.keras.preprocessing.image.img_to_array(img) x = np.expand_dims(x, axis=0) x = x[..., :3] x /= 255.0 x = (x - 0.5) * 2.0 y_pred = sess.run('output/Softmax:0', feed_dict={'serving_default_input_1:0': x}) Error: KeyError: "The name 'output/Softmax:0' refers to a Tensor which does not exist. The operation, 'output/Softmax', does not exist in the graph."
Lo que no entiendo: predictions.name es 'output/Softmax:0' , pero graph.get_tensor_by_name('output/Softmax:0') me dice que no existe.
Nota: Soy consciente de que puedo guardar y cargar con tf.keras.models.save y tf.keras.models.load_model de TF2 y luego ejecutar el modelo con model(x) . Sin embargo, en mi aplicación tengo varios modelos en la memoria y descubrí que la inferencia lleva mucho más tiempo que en mi código TF1 usando el objeto de session . Por lo tanto, me gustaría usar el enfoque TF1 con el objeto de session en modo de compatibilidad.
¿Cómo puedo controlar los nombres de entrada/salida al guardar? ¿Qué me estoy perdiendo?