我在keras中创建完全卷积网络。我有一个简单的代码
from datetime import datetime
import os
os.environ["THEANO_FLAGS"] = "exception_verbosity=high"
os.environ["KERAS_BACKEND"] = "theano"
import cv2
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D, Deconvolution2D, UpSampling2D
from keras import backend as K
K.set_image_dim_ordering('th')
from read_dataset import read_input, read_output
# in_arr = np.load('input.npy')
# out_arr = np.load('output.npy')
in_arr = read_input()
in_arr = in_arr.swapaxes(1, 3)
in_arr = in_arr.swapaxes(2, 3)
out_arr = read_output()
out_arr = out_arr.swapaxes(1, 3)
out_arr = out_arr.swapaxes(2, 3)
start = datetime.now()
input_shape = in_arr.shape
output_shape = out_arr.shape
# normalize and split train data
X_train = in_arr[:5]/255.0
Y_train = out_arr[:5]/255.0
X_test = in_arr[800:]
Y_test = out_arr[800:]
# create convolutional neural network model
model = Sequential()
model.add(Convolution2D(64, 3, 3, border_mode='same', activation='relu', input_shape=(3, 800, 1360)))
model.add(Convolution2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(UpSampling2D(size=(2, 2), dim_ordering='default'))
# model.add(Dense(10))
model.add(Convolution2D(1, 3, 3, border_mode='same'))
# model.add(Deconvolution2D(1, 3, 3, border_mode='same', activation='relu', output_shape=output_shape))
print('Input Shape', model.input_shape)
print('Output Shape', model.output_shape)
print(model.summary())
print('X_train shape', X_train.shape)
print('Y_train shape', Y_train.shape)
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, Y_train,
batch_size=8, nb_epoch=10, verbose=1)
print(datetime.now() - start)
我有模型摘要
Input Shape (None, 3, 800, 1360)
Output Shape (None, 1, 800, 1360)
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
convolution2d_1 (Convolution2D) (None, 64, 800, 1360) 1792 convolution2d_input_1[0][0]
____________________________________________________________________________________________________
convolution2d_2 (Convolution2D) (None, 64, 800, 1360) 36928 convolution2d_1[0][0]
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D) (None, 64, 400, 680) 0 convolution2d_2[0][0]
____________________________________________________________________________________________________
dropout_1 (Dropout) (None, 64, 400, 680) 0 maxpooling2d_1[0][0]
____________________________________________________________________________________________________
upsampling2d_1 (UpSampling2D) (None, 64, 800, 1360) 0 dropout_1[0][0]
____________________________________________________________________________________________________
convolution2d_3 (Convolution2D) (None, 1, 800, 1360) 577 upsampling2d_1[0][0]
====================================================================================================
Total params: 39,297
Trainable params: 39,297
Non-trainable params: 0
____________________________________________________________________________________________________
我有相应的训练样本量
X_train shape (5, 3, 800, 1360)
Y_train shape (5, 1, 800, 1360)
但我收到错误
ValueError: Error when checking model target: expected convolution2d_3 to have shape (None, 1, 800, 1) but got array with shape (5, 1, 800, 1360)
我无法理解为什么它会使convolution2d_3具有形状(None,1,800,1),如果在model.summary中它是(None,1,800,1360)。它有什么问题?请告诉我什么是错的。