首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模型没有'shape‘属性- VGG16模型

模型没有'shape‘属性- VGG16模型
EN

Stack Overflow用户
提问于 2020-05-17 10:47:03
回答 1查看 306关注 0票数 0

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

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

代码语言:javascript
复制
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,并尝试将其拟合:

代码语言:javascript
复制
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上运行这段代码,我得到的错误是:

代码语言:javascript
复制
---------------------------------------------------------------------------
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'

有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-17 10:57:07

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

因此,您应该更改以下几行:

代码语言:javascript
复制
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的输入,那么您必须更改:

代码语言:javascript
复制
vgg16 = VGG16(input_tensor=input_tensor, weights='imagenet', include_top=False)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61846350

复制
相关文章

相似问题

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