TensorFlow Keras 使用 MobileNetV2 模型,输入小于 32x32

问题描述

我想使用大小小于 MobileNetV2 的没有权重的 32x32 模型。如果我尝试

model = tf.keras.applications.MobileNetV2(input_shape=(10,10,3),include_top=False,weights=None)

给出错误

ValueError: Input size must be at least 32x32; got `input_shape=(10,3)

我知道我不能使用所有层,因为模型中的分辨率降低太多,所以假设我想使用模型的前 30 层。

如何创建使用 MobileNetV2 的前 30 层且输入形状为 10x10x3 的模型?我不想手动创建 MobileNetV2 模型,但我想使用 tf.keras.applications.MobileNetV2 方法来加载它。

解决方法

The Post field is required 创建一个新模型,前 30 层作为输出(即多输出模型)。方法如下:

MobileNet

测试

base = tf.keras.applications.MobileNetV2(include_top=False,weights=None)
features_list = [layer.output for layer in base.layers[:30]]
model = keras.Model(inputs=base.input,outputs=features_list)

len(model.layers) # 30

根据您的评论。我们能做到这一点。如果您希望模型具有单个输出,即 MobileNetV2 模型第 30 层的输出,请执行以下操作。

img = np.random.random((1,10,3)).astype("float32")
model(img)

[<tf.Tensor: shape=(1,3),dtype=float32,numpy=
 array([[[[0.7529889,0.18826886,0.9792667 ],[0.52218866,0.36510527,0.4743469 ],...
...

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...