问题描述
我想使用大小小于 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 ],...
...