【发布时间】:2019-12-10 04:06:31
【问题描述】:
我正在尝试设计一个卷积网络来使用 Keras 估计图像的深度。
我有形状为 (1449,480,640,3) 的 RGB 输入图像和形状为 (1449,480,640,1) 的灰度输出深度图 但最后当我想设计最后一层时,我被卡住了。使用密集层
我有这个错误“预计 dense_4 有 2 个维度,但得到了形状为 (1449, 480, 640, 1) 的数组”
根据 doc Keras 将输入数据输入到形状为 (batch_size, units) 的密集层 2D 数组 我们必须将从卷积层接收到的输出的维度更改为二维数组。
将我的 gt ndarray 从 4d 重塑为 2d 后,它也不起作用 gt=gt.reshape(222566400,2) 它向我显示了这个错误 “预计dense_4的形状为(4070,),但得到的数组形状为(2,)”
我知道,480*640 的每个位置都有 4070 个密集神经元 我如何重塑输出数组以适应依赖于 num 的密集层。神经元? 请注意,我有 2 个密集层一个接一个
我的代码:
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)
【问题讨论】:
标签: python tensorflow keras depth estimation