在将图层添加到Keras mod后无法导入冻结的图形

2024-04-25 12:52:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从.h5文件加载一个经过训练的Keras模型,然后在它周围包上两个TensorFlow层并保存为ProtoBuf。保存操作正常,但是当我导入graph def时,我得到了错误:

ValueError: Input 0 of node batch_normalization_24_1/cond/ReadVariableOp/Switch_1 was passed float from batch_normalization_24/gamma_1:0 incompatible with expected resource.

以下是该错误的最小可重复性示例:

import tensorflow as tf
import keras
import keras.backend.tensorflow_backend as K
import base64


def bitstring_to_float32_tensor(input_bytes, image_size):
    """ Transforms image bitstring to float32 tensor.

        Args:
            input_bytes: A bitstring representative of an input image.
            image_size: The input image size (e.g., 512).

        Returns:
            A batched float32 tensor representative of the input image.
    """

    input_bytes = tf.reshape(input_bytes, [])

    # Transforms bitstring to uint8 tensor
    input_tensor = tf.image.decode_png(input_bytes, channels=3)

    # Converts to float32 tensor
    input_tensor = tf.image.convert_image_dtype(input_tensor,
                                            dtype=tf.float32)
    input_tensor = input_tensor / 127.5 - 1.0

    # Ensures tensor has correct shape
    input_tensor = tf.reshape(input_tensor, [image_size, image_size, 3])

    # Expands the single tensor into a batch of 1
    input_tensor = tf.expand_dims(input_tensor, 0)
    return input_tensor


def float32_tensor_to_bitstring(output_tensor):
    """ Transforms float32 tensor to list of image bitstrings.

        Args:
            output_tensor: A float32 tensor representative of
                an inferred image.

        Returns:
            output_node_names: A list containing the name of the output
                node in the graph.
    """

    # Converts to uint8 tensor
    output_tensor = (output_tensor + 1.0) / 2.0
    output_tensor = tf.image.convert_image_dtype(output_tensor, tf.uint8)
    output_tensor = tf.squeeze(output_tensor)

    # Transforms uint8 tensor to bitstring
    output_bytes = tf.image.encode_png(output_tensor)

    output_bytes = tf.identity(output_bytes, name="output_bytes")

    # Adds output node name to list
    output_node_names = ["output_bytes"]

    # Returns output list and image boolean
    return output_node_names


# Sets model phase to inference
K.set_learning_phase(0)

# Loads model from hdf5 file
model = tf.keras.models.load_model("model.h5")

# Instantiates placeholder for image bitstring
input_bytes = tf.placeholder(tf.string,
                             shape=[],
                             name="input_bytes")

# Converts image bitstring to float32 tensor
input_tensor = bitstring_to_float32_tensor(input_bytes, image_size=512)

# Performs inference on tensor, returning a float32 tensor
output_tensor = model.call(input_tensor)

# Converts float32 tensor to image bitstring
output_node_names = float32_tensor_to_bitstring(output_tensor)

# Starts a TensorFlow session
with K.get_session() as sess:
    # Initializes variables
    sess.run(tf.global_variables_initializer())

    # Exports graph to ProtoBuf
    output_graph_def = tf.graph_util.convert_variables_to_constants(
        sess, sess.graph.as_graph_def(), output_node_names)

    # Saves ProtoBuf to disk
    tf.train.write_graph(output_graph_def,
                         "./",
                         "test.pb",
                         as_text=False)

# Reads data from ProtoBuf
with tf.gfile.GFile("test.pb", "rb") as protobuf_file:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(protobuf_file.read())

# Sets tensor names to extract from graph
tensor_names = ["input_bytes:0", "output_bytes:0"]

# Imports graph and returns extracted tensors
io_tensors = tf.import_graph_def(graph_def,
                                 name="",
                                 return_elements=tensor_names)

Tensor2.2版本和Tensor2.GPFlow。在


Tags: oftoimagenodeinputoutputbytesnames
2条回答

我没有仅仅包装TensorFlow层,而是将它们封装在Keras Lambda layers中以与导入的模型兼容,然后构建了一个新的Keras模型。最后,我将它保存为SavedModel而不是ProtoBuf,它仍然适用于TensorFlow服务。在

工作代码如下:

import tensorflow as tf
from keras.engine.input_layer import Input
from keras.models import Model, load_model
from keras.layers import Lambda
import keras.backend.tensorflow_backend as K
import base64


def bitstring_to_float32_tensor(input_bytes):
    """ Transforms image bitstring to float32 tensor.

        Args:
            input_bytes: A bitstring representative of an input image.

        Returns:
            A batched float32 tensor representative of the input image.
    """

    input_bytes = tf.reshape(input_bytes, [])
    input_bytes = tf.cast(input_bytes, tf.string)

    # Transforms bitstring to uint8 tensor
    input_tensor = tf.image.decode_png(input_bytes, channels=3)

    # Converts to float32 tensor
    input_tensor = tf.image.convert_image_dtype(input_tensor,
                                            dtype=tf.float32)
    input_tensor = input_tensor / 127.5 - 1.0

    # Ensures tensor has correct shape
    input_tensor = tf.reshape(input_tensor, [512, 512, 3])

    # Expands the single tensor into a batch of 1
    input_tensor = tf.expand_dims(input_tensor, 0)
    return input_tensor


def float32_tensor_to_bitstring(output_tensor):
    """ Transforms float32 tensor to list of image bitstrings.

        Args:
            output_tensor: A float32 tensor representative of
                an inferred image.

        Returns:
            output_node_names: A list containing the name of the output
                node in the graph.
    """
    # Converts to uint8 tensor
    output_tensor = (output_tensor + 1.0) / 2.0
    output_tensor = tf.image.convert_image_dtype(output_tensor, tf.uint8)
    output_tensor = tf.squeeze(output_tensor)

    # Transforms uint8 tensor to bitstring
    output_bytes = tf.image.encode_png(output_tensor)

    output_bytes = tf.identity(output_bytes, name="output_bytes")

    # Expands the single tensor into a batch of 1
    output_bytes = tf.expand_dims(output_bytes, 0)

    # Returns output tensor
    return output_bytes


# Sets model phase to inference
K.set_learning_phase(0)

# Loads model from hdf5 file
model = load_model("model.h5")

# Instantiates placeholder for image bitstring
input_bytes = Input(shape=[], dtype=tf.string)

# Converts image bitstring to float32 tensor
input_tensor = Lambda(bitstring_to_float32_tensor)(input_bytes)

# Performs inference on tensor, returning a float32 tensor
output_tensor = sat_net(input_tensor)

# Converts float32 tensor to image bitstring
output_bytes = Lambda(float32_tensor_to_bitstring)(output_tensor)

# Builds new Model
sat_net = Model(input_bytes, output_bytes)
sat_net.summary()

# Creates signature for prediction
signature_definition = tf.saved_model.signature_def_utils.predict_signature_def(
    {"input_bytes": sat_net.input},
    {"output_bytes": sat_net.output})

# Instantiates a SavedModelBuilder
builder = tf.saved_model.builder.SavedModelBuilder("serve/1")

with tf.Session() as sess:
    # Initializes model and variables
    sess.run(tf.global_variables_initializer())

    # Adds meta-information
    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            tf.saved_model.signature_constants.
            DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_definition
        })

    # Saves the model
    builder.save()

我刚刚成功地处理了几乎相同的问题。 正如my answer中所指出的,这个问题可能与另一个问题有关

keras.backend.set_learning_phase(0)

应在模型加载前放置。在

^{pr2}$

相关问题 更多 >

    热门问题