首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用VGG16模型提取特性并将它们用作另一个模型(例如resnet、vit等)的输入?

如何使用VGG16模型提取特性并将它们用作另一个模型(例如resnet、vit等)的输入?
EN

Stack Overflow用户
提问于 2022-03-02 14:45:06
回答 1查看 1.6K关注 0票数 1

我在深度学习和图像分类方面有点新。我想使用VGG16从图像中提取特征,并将它们作为vit模型的输入。以下是我的代码:

代码语言:javascript
复制
from tensorflow.keras.applications.vgg16 import VGG16
vgg_model = VGG16(include_top=False, weights = 'imagenet', input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))

for layer in vgg_model.layers:
    layer.trainable = False

from vit_keras import vit
vit_model = vit.vit_b16(
        image_size = IMAGE_SIZE,
        activation = 'sigmoid',
        pretrained = True,
        include_top = False,
        pretrained_top = False,
        classes = 2)

model = tf.keras.Sequential([
        vgg_model,
        vit_model,
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(512, activation = tfa.activations.gelu),
        tf.keras.layers.Dense(256, activation = tfa.activations.gelu),
        tf.keras.layers.Dense(64, activation = tfa.activations.gelu),
        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.Dense(1, 'sigmoid')
    ],
    name = 'vision_transformer')

model.summary()

但是,我得到了以下错误:

ValueError:层嵌入的输入0与层不兼容:输入形状的预期轴-1为值3,但接收到的输入为形状(无,8,8,512)

我假设这个错误发生在VGG16和vit的合并中。如何纠正这种情况下的错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-02 15:57:39

您不能将VGG16模型的输出提供给vit_model,因为这两个模型都期望输入形状(224, 224, 3)或您定义的某个形状。问题是VGG16模型具有输出形状(8, 8, 512)。您可以尝试重采样/重塑/调整输出以适应预期的形状,但我不推荐它。相反,只需向两个模型提供相同的输入,然后将它们的结果连接起来。下面是一个有用的例子:

代码语言:javascript
复制
import tensorflow as tf
import tensorflow_addons as tfa
from vit_keras import vit

IMAGE_SIZE = 224
vgg_model = tf.keras.applications.vgg16.VGG16(include_top=False, weights = 'imagenet', input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
for layer in vgg_model.layers:
    layer.trainable = False

vit_model = vit.vit_b16(
        image_size = IMAGE_SIZE,
        activation = 'sigmoid',
        pretrained = True,
        include_top = False,
        pretrained_top = False,
        classes = 2)

inputs = tf.keras.layers.Input((IMAGE_SIZE, IMAGE_SIZE, 3))
vgg_output = tf.keras.layers.Flatten()(vgg_model(inputs))
vit_output = vit_model(inputs)
x = tf.keras.layers.Concatenate(axis=-1)([vgg_output, vit_output])
x = tf.keras.layers.Dense(512, activation = tfa.activations.gelu)(x)
x = tf.keras.layers.Dense(256, activation = tfa.activations.gelu)(x)
x = tf.keras.layers.Dense(64, activation = tfa.activations.gelu)(x)
x = tf.keras.layers.BatchNormalization()(x)
outputs = tf.keras.layers.Dense(1, 'sigmoid')(x)
model = tf.keras.Model(inputs, outputs)
print(model.summary())
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71324609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档