InvalidArgumentError:无法挤压dim [2],预期尺寸为1,得到5

时间:2020-05-13 17:56:38

标签: python tensorflow keras

我正在使用5类视频分类,并在Google Colab平台中使用TimeDistributed CNN模型。火车数据集包含8个视频,每个视频包含75帧。验证数据集包含2个视频,每个视频包含75帧。我使用的批量大小为64。所以,我总共要处理10个视频。我使用 Adam 优化器和分类交叉熵损失来编译模型。拟合模型后与数据集,我得到以下错误:

InvalidArgumentError:无法挤压dim [2],预期尺寸为1,为5

这是我开发的代码:

from tensorflow.keras.optimizers import Adam

train_generator = datagen.flow_from_directory(
        '/content/drive/My Drive/New dataset/train',
        target_size=(64, 64),
        batch_size=batch_size,
        class_mode='categorical',  # this means our generator will only yield batches of data, no labels
        shuffle=False,
        classes=['class_a','class_b','class_c','class_d','class_e'])

validation_generator = datagen.flow_from_directory(
        '/content/drive/My Drive/New dataset/validation',
        target_size=(64, 64),
        batch_size=batch_size,
        class_mode='categorical',  # this means our generator will only yield batches of data, no labels
        shuffle=False,
        classes=['class_a','class_b','class_c','class_d','class_e'])

model = tf.keras.models.Sequential([

    tf.keras.layers.TimeDistributed(Conv2D(64, (3,3), padding='same', activation='relu'),input_shape=(75,64, 64, 3)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(5, activation='softmax')
])



model.compile(loss='categorical_crossentropy',
              optimizer=Adam(lr=0.0001),
              metrics=['accuracy'])

history = model.fit_generator(
      train_generator,
      validation_data=validation_generator, 
      validation_steps=150//64, 
      shuffle=False,    
      steps_per_epoch=8,  
      epochs=5,
      verbose=1)

我得到的错误:

WARNING:tensorflow:From <ipython-input-11-a17136d736b1>:8: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
Please use Model.fit, which supports generators.
Epoch 1/5

---------------------------------------------------------------------------

InvalidArgumentError                      Traceback (most recent call last)

<ipython-input-11-a17136d736b1> in <module>()
      6       steps_per_epoch=8,
      7       epochs=5,
----> 8       verbose=1)

------------------------------------------^10 frames^----------------------------------------------

/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  Can not squeeze dim[2], expected a dimension of 1, got 5
     [[node categorical_crossentropy/remove_squeezable_dimensions/Squeeze (defined at <ipython-input-11-a17136d736b1>:8) ]] [Op:__inference_train_function_607]

Function call stack:
train_function

谁能告诉我代码中有什么问题以及如何解决?

0 个答案:

没有答案