我是Keras的新手,我正在尝试使用图像(64x64,1通道)对顺序和功能分类进行比较,这是我的模型(顺序):
x_pos_train = x_pos[int(x_pos.shape[0]* .20):] #shape(20,1,64,64)
x_pos_test = x_pos[:int(x_pos.shape[0]* .20)] #shape(5,1,64,64)
x_pos_noisy = x_pos_train + 0.05 * np.random.normal(loc=0., scale=1., size=x_pos_train.shape) #(20, 1, 64, 64)
#-----------------------------------------------------------------
from keras.models import Sequential
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, UpSampling2D
from keras import backend as K #image dim ordering in conv2d won't work
K.set_image_dim_ordering('th')
seqmodel = Sequential()
seqmodel.add(Conv2D(32, (3,3), activation='relu', padding='same', input_shape=(1,64,64)))
seqmodel.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
seqmodel.add(Conv2D(32, (3,3), activation='relu'))
seqmodel.add(UpSampling2D((2, 2)))
seqmodel.add(Conv2D(1, (3,3), activation='sigmoid'))
seqmodel.compile(optimizer='adadelta', loss='binary_crossentropy')
seqmodel.fit(x_pos_noisy,
x_pos_train,
epochs=10,
batch_size=32,
shuffle=True,
validation_split=.20)
x_pos是为keras(通道,行,列)重新整形的图像数组
来源位于:How to denoise images with neural networks。
问题是,当我运行它时,它会在seqmodel.fit处抛出错误:
ValueError: Error when checking target: expected conv2d_3 to have shape (1,58,58), but got array with shape (1,64,64)
由于原始代码不起作用,我已做了一些更改以满足我的需求。
修改1:
我知道目标(x_pos_train)与输出层不同,但我想知道为什么会发生这种情况,因为代码应该有效。
答案 0 :(得分:0)
由于你的第三个卷积层需要一个大小(1,64,64)的数组,你必须在层中使用“相同”填充,这会缩小输入的大小(在这种情况下是第二个和第三个卷积层)到限制他们改变输入数组的大小。 所以,代码看起来像这样:
seqmodel = Sequential()
seqmodel.add(Conv2D(32, (3,3), activation='relu', padding='same', input_shape=(1,64,64)))
seqmodel.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
seqmodel.add(Conv2D(32, (3,3), activation='relu', padding='same'))
seqmodel.add(UpSampling2D((2, 2)))
seqmodel.add(Conv2D(1, (3,3), activation='sigmoid', padding='same'))