Gin*_*noC 7 python python-3.x tensorflow tensorflow-datasets tensorflow2.0
系统信息: Python:3.6.9 Tensorflow:来自 pip 的 2.2.0 CPU 包
问题:
我从 tf-hub 获取了https://tfhub.dev/google/imagenet/resnet_v2_50/classification/4?tf-hub-format=compressed,然后在新目录中解压缩。
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
Run Code Online (Sandbox Code Playgroud)
https://storage.googleapis.com/tfhub-modules/google/imagenet/resnet_v2_50/feature_vector/4.tar.gz是来自https://tfhub.dev/google/imagenet/resnet_v2_50/的特征向量预训练模型tf-hub 中的feature_vector/4 。
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())
Run Code Online (Sandbox Code Playgroud)
给出这个输出:
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'
Run Code Online (Sandbox Code Playgroud)
这就是提到的错误。
为什么?
谢谢
小智 5
该模型是一个普通的 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6183 次 |
| 最近记录: |