【问题标题】:Model has no attribute 'shape' - VGG16 Model模型没有属性“形状” - VGG16 模型
【发布时间】:2020-09-02 21:06:33
【问题描述】:

我正在尝试使用 VGG16 模型对 RAVDESS video_song 数据集进行分类。为此,我从每个视频中每秒提取 3 帧。然后,我使用 InceptionV3 从这些帧中提取特征,将其保存到 csv 文件中。现在,我正在尝试训练一个模型来根据给定的输入预测情绪。

我正在使用train_test_split 将我的数据拆分为随机训练和测试子集:

p_x_train, p_x_test, p_y_train, p_y_test = train_test_split(deep_features_csv, emotion_classes_csv, test_size=.3, random_state=42, stratify=emotion_classes_csv)

x_train = preprocess_input(p_x_train.values)
x_test = preprocess_input(p_x_test.values)
y_train = preprocess_input(p_y_train.values)
y_test = preprocess_input(p_y_test.values)

在此之后,我构建了我的模型,在本例中是 VGG16,并尝试拟合它:

emotions = { 0: "neutral", 1: "calm", 2: "happy", 3: "sad", 4: "angry", 5: "fearful" }

num_classes = len(emotions)

input_tensor = Input(shape=x_train[0].shape, name='input_tensor')

vgg16 = VGG16(weights='imagenet', include_top=False)
vgg16.trainable = False

x = tf.keras.layers.Flatten(name='flatten')(vgg16)
x = tf.keras.layers.Dense(512, activation='relu', name='fc1')(vgg16)
x = tf.keras.layers.Dense(512, activation='relu', name='fc2')(x)
x = tf.keras.layers.Dense(10, activation='softmax', name='predictions')(x)
new_model = tf.keras.models.Model(inputs=vgg16.input, outputs=x)
new_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

hist_vgg16 = new_model.fit(x_train, y_train,
    batch_size = 32,
    epochs = 50,
    verbose = 1,
    validation_data = (x_test, y_test)
)

x_train[0] 的形状是(2048,)

我在 (google colab)[colab.research.google.com] 上运行此代码,这是我得到的错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-30-311ade600318> in <module>()
      8 vgg16.trainable = False
      9 
---> 10 x = tf.keras.layers.Flatten(name='flatten')(vgg16)
     11 x = tf.keras.layers.Dense(512, activation='relu', name='fc1')(vgg16)
     12 x = tf.keras.layers.Dense(512, activation='relu', name='fc2')(x)

2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    164         spec.min_ndim is not None or
    165         spec.max_ndim is not None):
--> 166       if x.shape.ndims is None:
    167         raise ValueError('Input ' + str(input_index) + ' of layer ' +
    168                          layer_name + ' is incompatible with the layer: '

AttributeError: 'Model' object has no attribute 'shape'

有人可以帮我吗?

【问题讨论】:

    标签: python google-colaboratory pattern-recognition conv-neural-network


    【解决方案1】:

    问题在于,在错误行中,您将 VGG16 模型作为输入引入,而您想要引入最后一层的输出,对吗?

    因此,您应该更改下一行:

    x = tf.keras.layers.Flatten(name='flatten')(vgg16.output) 
    x = tf.keras.layers.Dense(512, activation='relu', name='fc1')(x) #I suppose the input of this layer, is the output of Flatten
    

    另外一件事,你的 input_tensor 好像没用过,是不是我错了? 它应该是您的 vgg16 的输入还是您想要一个多输入模型?

    如果你的input_tensor是VGG16的输入,那么你就得改一下:

    vgg16 = VGG16(input_tensor=input_tensor, weights='imagenet', include_top=False)
    

    【讨论】:

    • 是的,输入张量是VGG16的输入。我根据您的建议更改了代码,但在定义 VGG16 的行中出现此错误:Input 0 of layer block1_conv1 is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 2048]
    • 这是因为 VGG 需要一个三维数组(例如:图像)。您的输入只有 1 个维度。
    • 如果答案有帮助,请接受答案并点赞
    • 我正试图弄清楚如何重塑我的数据以便对 vgg16 有用。 x_train[0] 是使用 inceptionv3 从 1 个视频帧中提取的 2048 个特征的向量,我没有用作 vgg16 输入的图像。
    • 那么,为什么要使用vgg?
    猜你喜欢
    • 1970-01-01
    • 2021-03-10
    • 2016-01-10
    • 2012-04-26
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多