【问题标题】:ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (32768, 1)ValueError:layersequential_1 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=2。收到的完整形状:(32768, 1)
【发布时间】:2021-08-12 00:30:45
【问题描述】:
import keras
from keras.models import Sequential
from keras.applications.vgg16 import VGG16
from keras.layers import Dense, InputLayer, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D, GlobalMaxPooling2D
from keras.preprocessing import image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm
from sklearn.model_selection import train_test_split
y = train['class']
images_train, images_test, y_train, y_test = train_test_split(images, y, random_state=42, 
test_size=0.2, stratify = y)

y_train = pd.get_dummies(y_train)
y_test = pd.get_dummies(y_test)

base_model = VGG16(weights='imagenet', include_top=False)

images_train = base_model.predict(images_train)
images_train.shape

images_test = base_model.predict(images_test)
images_test.shape

images_train = images_train.reshape(2321, 8*8*512)
images_test = images_test.reshape(581, 8*8*512)
max = images_train.max()
images_train = images_train/max
images_test = images_test/max

images_train.shape

model=Sequential()

model.add(Conv2D(input_shape=(10, 8, 8, 512),filters=4,kernel_size= 
(3,3),padding="same",activation="relu"))
model.add(Conv2D(filters=64,kernel_size=(3,3),padding="same",activation="relu"))

model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4, activation='softmax'))

from keras.callbacks import ModelCheckpoint
mcp_save = ModelCheckpoint('weight.hdf5', save_best_only=True, monitor='val_loss', mode='min')

model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])
import tensorflow as tf
train_data = tf.data.Dataset.from_tensor_slices((images_train, y_train))
valid_data = tf.data.Dataset.from_tensor_slices((images_test, y_test))
model.fit(train_data, epochs=40, validation_data=valid_data, callbacks=[mcp_save], 
batch_size=10)


      

ERROR:

我创建了一个由四种不同活动组成的数据集

  1. 擤鼻涕
  2. 跌倒
  3. 胸痛
  4. 呕吐 从左、右、前三个视图,总共有 180 个视频。然后我从每个活动视频中提取帧,所以我总共有 2902 帧。我正在尝试将这些帧输入 CNN 模型。作为学习者,我收到如上所示的错误,我无法解决此错误。请帮我解决这个错误。

【问题讨论】:

    标签: python pandas tensorflow machine-learning keras


    【解决方案1】:

    我无法从您的代码中读取那么多内容,但是您的输入形状对于网络来说是错误的。您尝试将 images_train 输入您的 VGG 网络。 VGG 网络需要一个四维 numpy 数组作为输入。它的形状为 (batch_size, channels, width, height) 或 (batch_size, width, height, channels),具体取决于您选择的是“channels_last”还是“channels_first”数据格式(我不知道在哪里设置这个,但是其中一个应该可以工作)。

    您对网络的输入没有这种形状,这就是您收到错误的原因。尝试运行 np.shape(images_train),然后您可以看到输入的形状。如果这不是四维的,那么您的输入对于 VGG 网络的形状不正确。

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 2021-03-16
      • 2021-01-16
      • 2022-12-10
      • 1970-01-01
      • 2020-12-24
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多