预期 dense_4 有 2 个维度,但得到了形状为 (1449, 480, 640, 1) 的数组

expected dense_4 to have 2 dimensions, but got array with shape (1449, 480, 640, 1)

我正在尝试使用 Keras 设计一个卷积网络来估计图像的深度。

我有形状为 (1449,480,640,3) 的 RGB 输入图像和形状为 (1449,480,640,1) 的灰度输出深度图 但最后当我想设计最后一层时,我卡住了。使用密集层

我有这个错误"expected dense_4 to have 2 dimensions, but got array with shape (1449, 480, 640, 1)"

according to doc Keras 密集层的输入数据 2D array of shape (batch_size, units) 我们必须将从卷积层接收到的输出维度更改为二维数组。

将我的 gt ndarray 从 4d 重塑为 2d 后,它也不起作用 gt=gt.reshape(222566400,2) 它告诉我这个错误 "expected dense_4 to have shape (4070,) but got array with shape (2,)"

我明白,4070 个密集的神经元到每个 480*640 的位置 我如何重塑输出数组以适应依赖于 num 的密集层。神经元? 请注意,我有 2 个密集层

enter image description here

我的代码:

import numpy as np
import h5py  # For .mat files
# data path
path_to_depth ='/content/drive/My Drive/DataSet/nyu_depth_v2_labeled.mat'

# read mat file
f = h5py.File(path_to_depth,'r')


pred = np.zeros((1449,480,640,3))
gt = np.zeros((1449,480,640,1))   

for i in range(len(f['images'])):
  # read 0-th image. original format is [3 x 640 x 480], uint8
  img = f['images'][i]

  # reshape
  img_ = np.empty([480, 640, 3])
  img_[:,:,0] = img[0,:,:].T
  img_[:,:,1] = img[1,:,:].T
  img_[:,:,2] = img[2,:,:].T


  # read corresponding depth (aligned to the image, in-painted) of size [640 x 480], float64
  depth = f['depths'][i]

  depth_ = np.empty([480, 640])
  depth_[:,:] = depth[:,:].T


  pred[i,:,:,:] = img_ 
  #print(pred.shape)#(1449,480,640,3)

  gt[i,:,:,0] = depth_ 
  #print(gt.shape)#(1449, 480, 640, 1)

# dimensions of our images.
img_width, img_height = 480, 640


gt=gt.reshape(222566400,2)
gt = gt.astype('float32')

from keras.preprocessing.image import ImageDataGenerator #import library to preprocess the dataset
from keras.models import Sequential #import keras models libraries
from keras.layers import Conv2D, MaxPooling2D ,BatchNormalization#import layers libraries
from keras.layers import Activation, Dropout, Flatten, Dense #import layers libraries
from sklearn.metrics import classification_report, confusion_matrix #import validation functions
import tensorflow as tf

#Training
model = Sequential() #model type initialization

#conv1
model.add(Conv2D(96, (11, 11),padding='VALID', strides=4,input_shape=(img_width, img_height, 3))) #input layer
model.add(Activation('relu'))

model.add(BatchNormalization(axis=1))

#pool1 
model.add(MaxPooling2D(pool_size=(3, 3),padding='VALID')) #Pooling Layer: reduces the matrices

#conv2
model.add(Conv2D(256, (5, 5),padding='SAME')) #input layer
model.add(Activation('relu'))
model.add(BatchNormalization(axis=1)) 

#conv3
model.add(Conv2D(384, (3, 3),padding='SAME')) #input layer
model.add(Activation('relu'))

#conv4
model.add(Conv2D(384, (3, 3),padding='SAME',strides=2)) #input layer
model.add(Activation('relu'))

#conv5
model.add(Conv2D(256, (3, 3),padding='SAME')) #input layer
model.add(Activation('relu'))

#pool2
model.add(MaxPooling2D(pool_size=(3, 3),padding='VALID')) #Pooling Layer: reduces the matrices

model.add(Flatten()) #this layer converts the 3D Layers to 1D Layer
model.add(Dense(4096,activation='sigmoid')) #densly connected NN Layers

model.add(Dropout(0.5)) #layer to prevent from overfitting


model.add(Dense (4070,activation='softmax')) #densly connected NN Layers

#Model configuration for training
model.compile(loss='binary_crossentropy', #A loss function calculates the error in prediction
              optimizer='rmsprop',        #The optimizer updates the weight parameters to minimize the loss function
              metrics=['accuracy'])       #A metric function is similar to a loss function, except that the results from evaluating a metric are not used when training the model.

model.fit(pred,gt,batch_size=9,epochs=161,verbose=1, validation_split=0.1) 

我猜你的架构有问题。如果我理解得很好,你想要的输出应该是大小 (1449,480,640,1)。

首先,你的最后一层激活是一个softmax,你的损失设置为'binary_crossentropy',这确实没有意义。此外,在激活 sigmoid 之前还有另一个 DENSE 层。这是有原因的吗?为什么你有两个 DENSE 连接在一起?

回到你的问题,你的这个架构并没有真正解决你的问题。您需要的是 A​​utoenocoder-ish 结构。为此,我建议您在展平卷积结果后,在 Conv 层之后向 UPSAMPLE 添加更多层,并以某种方式对其进行管理以达到 (1449,480,640,1) 的输出大小。因为你想要它灰度(我想你的意思是每个像素应该是 0 或 1),我建议使用 sigmoid 作为最后一层激活,然后使用二元交叉熵作为损失