简体   繁体   中英

Model has no attribute 'shape' - VGG16 Model

I'm trying to use VGG16 model to classify the RAVDESS video_song dataset. To do this, i have extracted 3 frames per second from each video. Then, i use InceptionV3 to extract features from this frames, saving it to a csv file. Now, i'm trying to train a model to predict emotions based on a given input.

I'm using train_test_split to split my data into random train and test subsets:

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)

After this, i build my model, which, in this case is VGG16, and try to fit it:

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)
)

The shape of x_train[0] is (2048,) .

I'm running this code on (google colab)[colab.research.google.com], and this is the error i got:

---------------------------------------------------------------------------
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'

Can someone help me here?

The problem is that in error line, you are introducing your VGG16 model as input, and what you want is to introduce the output of last layer, right?

So, you should change the next lines:

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

One other thing, your input_tensor seems to be not used, am i wrong? Should it be the input of your vgg16 or you want a multi-input model?

If your input_tensor is the input of VGG16, so you have to change:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM