gpt4 book ai didi

python - TF2.2 : Loading a Saved Model from tensorflow_hub failed with `AttributeError: ' _UserObject' object has no attribute 'summary' `

转载 作者:行者123 更新时间:2023-12-04 17:27:33 26 4
gpt4 key购买 nike

系统信息:
python :3.6.9
Tensorflow:来自 pip 的 2.2.0 CPU 包

问题:

我收到了 https://tfhub.dev/google/imagenet/resnet_v2_50/classification/4?tf-hub-format=compressed来自 tf-hub,然后在新目录中解压缩。

wget https://storage.googleapis.com/tfhub-modules/google/imagenet/resnet_v2_50/feature_vector/4.tar.gz
mkdir test_pb
mv 4.tar.gz test_pb
cd test_pb
tar -xvf 4.tar.gz
rm 4.tar.gz
cd ..
./test.py

https://storage.googleapis.com/tfhub-modules/google/imagenet/resnet_v2_50/feature_vector/4.tar.gz是来自 https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/4 的特征向量预训练模型在 tf-hub 中。
test.py是 Python 脚本,以下是独立代码:
#!/usr/bin/env python3
from __future__ import absolute_import, division, print_function, unicode_literals

import os
import tensorflow as tf

print(tf.__version__)

resnet50v2_save_path = os.path.join('.', "./test_pb/")

loaded1 = tf.keras.models.load_model(resnet50v2_save_path)
print("Load done")

print("Signatures: ", loaded1.signatures)

print("Type: ", type(loaded1))

print(loaded1.summary())

给出这个输出:
2.2.0
2020-06-12 20:29:07.677555: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 1995455000 Hz
2020-06-12 20:29:07.678219: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x59eb130 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-06-12 20:29:07.678241: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
Load done
Signatures: _SignatureMap({})
Type: <class 'tensorflow.python.saved_model.load.Loader._recreate_base_user_object.<locals>._UserObject'>
Traceback (most recent call last):
File "./test.py", line 18, in <module>
print(loaded1.summary())
AttributeError: '_UserObject' object has no attribute 'summary'

这就是提到的错误。

为什么?

谢谢

最佳答案

该模型是一个普通的 SavedModel 并且不能直接加载到 keras 模型中,因为它缺少 keras_metadata.pb文件。你必须像普通的 SavedModel 一样加载它并将它包装在一个 keras 模型中,如下所示:

!wget https://storage.googleapis.com/tfhub-modules/google/imagenet/resnet_v2_50/feature_vector/4.tar.gz
!mkdir saved_model
!tar -xvf 4.tar.gz -C saved_model

import tensorflow as tf
import tensorflow_hub as hub

m = tf.keras.Sequential([hub.KerasLayer("saved_model", trainable=True),
tf.keras.layers.Dense(10, activation='softmax')
])
m.build([None, 224, 224, 3])
m.summary()

# Model: "sequential"
# _________________________________________________________________
# Layer (type) Output Shape Param #
# =================================================================
# keras_layer (KerasLayer) (None, 2048) 23564800
# _________________________________________________________________
# dense (Dense) (None, 10) 20490
# =================================================================
# Total params: 23,585,290
# Trainable params: 23,539,850
# Non-trainable params: 45,440

关于python - TF2.2 : Loading a Saved Model from tensorflow_hub failed with `AttributeError: ' _UserObject' object has no attribute 'summary' `,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62350538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com