TensorFlow Hub 模型复用

TensorFlow Hub 模型复用

https://tf.wiki/zh/appendix/tfhub.html

老师,我在本地安装了 tensorflow-hub0.8.0 版本的包,然后运行示例程序,结果报错:AttributeError:module ‘tensorflow_hub’ has no attribute ‘load’ 这个是怎么回事呀?

具体哪一步?是否在翻墙环境?

关于 hub 的模型复用,实际因为网络原因我们不能直接下载,可以有两种解决方案:

  1. 配置代理,详见 https://github.com/tensorflow/hub/issues/146
  2. 下载到本地【还是要用代理下就是了:)
    hub 的默认下载位置是 /tmp/tfhub_modules,然后直接从本地读取,当然你也可以自定义下载地址

您好,关于用hub进行风格迁移的例子,有个小疑问:在输出的时候,为什么使用tf.constant() ?有什么好处吗?我自己测试时,不使用tf.constant() 也能得到结果。与使用的结果是一样的


ValueError Traceback (most recent call last)
Cell In[23], line 7
4 num_classes = 10
6 # 使用 hub.KerasLayer 组件待训练模型
----> 7 new_model = tf.keras.Sequential([
8 hub.KerasLayer(“Google | inception_v3 | Kaggle class=“ansi-yellow-bg” style=“color:rgb(175,0,0)”>”, output_shape=[2048], trainable=False),
9 tf.keras.layers.Dense(num_classes, activation=‘softmax’)
10 ])
11 new_model.build([None, 299, 299, 3])
13 # 输出模型结构

File /opt/conda/lib/python3.10/site-packages/keras/src/models/sequential.py:73, in Sequential.init(self, layers, trainable, name)
71 if layers:
72 for layer in layers:
—> 73 self.add(layer, rebuild=False)
74 self._maybe_rebuild()

File /opt/conda/lib/python3.10/site-packages/keras/src/models/sequential.py:95, in Sequential.add(self, layer, rebuild)
93 layer = origin_layer
94 if not isinstance(layer, Layer):
—> 95 raise ValueError(
96 “Only instances of keras.Layer can be "
97 f"added to a Sequential model. Received: {layer} "
98 f”(of type {type(layer)})"
99 )
100 if not self._is_layer_name_unique(layer):
101 raise ValueError(
102 "All layers added to a Sequential model "
103 f"should have unique names. Name ‘{layer.name}’ is already "
104 "the name of a layer in this model. Update the name argument "
105 “to pass a unique name.”
106 )

ValueError: Only instances of keras.Layer can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x7bb404c406d0> (of type <class ‘tensorflow_hub.keras_layer.KerasLayer’>)