简体   繁体   中英

Error whle trying to convert keras model to tf saved_model. - AttributeError: 'list' object has no attribute 'dtype'

I have to serve keras model in tensorflow serving. For that, I am trying to convert keras model to tf saved_model format using this script. https://gist.github.com/himanshurawlani/8a017b2a893060d1282d13ea4a8ddefc

from tensorflow.keras.layers import Input
from tensorflow.keras import backend as K
import networks.generator as gen
import tensorflow as tf
K.set_learning_phase(0)

encoder = gen.encoder(3, 224)
encoder.load_weights(f"weights/encoder.h5")
encoder_path = 'model/encoder/1'

print(encoder.outputs)

with tf.keras.backend.get_session() as sess:
tf.saved_model.simple_save(
    sess,
    encoder_path,
    inputs = {'input_image': encoder.input},
    outputs = {t.name: t for t in encoder.outputs}
)

But I am getting AttributeError: 'list' object has no attribute 'dtype' in ---> 18 outputs = {t.name: t for t in encoder.outputs}

Output of print(encoder.outputs) :

[<tf.Tensor 'activation_4/Relu:0' shape=(?, 14, 14, 512) dtype=float32>, <tf.Tensor 'lambda_5/resize/ResizeBilinear:0' shape=(?, 112, 112, 3) dtype=float32>, <tf.Tensor 'lambda_6/resize/ResizeBilinear:0' shape=(?, 56, 56, 3) dtype=float32>, <tf.Tensor 'input_3:0' shape=(?, 224, 224, 3) dtype=float32>]

For reproducing errors-

Network module - https://github.com/shaoanlu/fewshot-face-translation-GAN/tree/master/networks Encoder Weights - https://drive.google.com/uc?id=1DUMmZGTGKMyEYSKy-w34IDHawVF24rIs

The example you tried to follow, has an issue. As we can read in the tf.compat.v1.saved_model.simple_save documentation :

outputs: dict mapping string output names to tensors. These are added to the SignatureDef as the outputs.

So you tried to pass a dict value as a generator object, which has been converted a list:

[<tf.Tensor 'activation_4/Relu:0' shape=(?, 14, 14, 512) dtype=float32>, <tf.Tensor 'lambda_5/resize/ResizeBilinear:0' shape=(?, 112, 112, 3) dtype=float32>, <tf.Tensor 'lambda_6/resize/ResizeBilinear:0' shape=(?, 56, 56, 3) dtype=float32>, <tf.Tensor 'input_3:0' shape=(?, 224, 224, 3) dtype=float32>]

to outputs parameter, which expects a tensor.

An simplified example to a generator object you tried to pass:

print(type(x for x in [1,2,3]))

out:

<class 'generator'>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM