我下载了微小的 imagenet 数据集,它是 imagenet 数据集的一个子集,其图像大小为 64*64 像素。我想在像 alexnet 和 VGG 这样的原始图像网络上使用预训练模型,并将微小图像网络的图像作为网络的输入。这是真的还是假的? 您可能知道原始 imagenet 中图像的分辨率高于 tiny imagenet。它是否会导致推理任务出现问题? 感谢您的关注。
答案 0 :(得分:0)
通常,CNN 层可用于任何尺寸的图像。 CNN 层中权重的数量不取决于图像大小,而是取决于内核的数量和形状。因此,例如:
Conv2D(16, (3, 3), padding="same",input_shape=(None, None, 3))
总是 16(kernels) * 3 * 3 * 3(channels) + 16(biases) = 448 个权重。
唯一的问题是网络的头部通常是一组具有固定数量输入的密集层。因此,如果您只是 Flatten
Conv2D
和 Dense
层之间的网络,则图像的大小必须固定。但是,如果您放置例如 tf.keras.layers.GlobalAveragePooling2D
层,则图像的大小可能是可变的,因为该层产生的输出仅取决于内核的数量,而不取决于图像的大小。
如果您使用带有头的版本(include_top
参数):
base_model = tf.keras.applications.VGG16(weights = 'imagenet', include_top = True)
或
base_model = tf.keras.applications.MobileNet(weights = 'imagenet', include_top = True)
您可以通过 base_model.summary()
确认他们期望图像大小为 (224,224,3)。
但是,如果您像这样添加 include_top=False
:
base_model = tf.keras.applications.VGG16(weights = 'imagenet', include_top = False)
图像的预期 input_shape
是 (None, None, 3)
。对于大小为 (W, H, 3)
的图像的这种网络产生大小为 (W/S, H/S, K)
的输出,其中 K 是最后一层的内核数,S 是特定网络的收缩因子。例如对于 VGG16 网络 S=32 和 K=512,因此对于大小为 (224,224,3) 的图像输出大小为 (7,7,512) 而对于大小为 (512,512,3) 的图像输出为 (16,16,512) .这种输出有时称为“补丁”。
因此,如果您想构建使用一些预训练网络并对任意大小的图像进行分类的网络,您可以这样构建:
base_model = tf.keras.applications.ResNet50(weights = 'imagenet', include_top = False)
x = base_model.output
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dense(512, activation = 'relu')(x)
...
last_layer = tf.keras.layers.Dense(num_classes, activation = 'softmax')(x)
model = tf.keras.models.Model(inputs = base_model.input, outputs = last_layer)
这样的模型可以输入任意大小的图像,并为 num_classes
类生成概率向量。当然,在训练期间,您必须在一批中使用相同大小的图像,但随后您可以使用任何图像。