我基本上遵循本指南,以构建使用TensorFlow后端构建卷积自动编码器。指南的主要区别在于我的数据是257x257灰度图像。以下代码:
TRAIN_FOLDER = 'data/OIRDS_gray/'
EPOCHS = 10
SHAPE = (257,257,1)
FILELIST = os.listdir(TRAIN_FOLDER)
def loadTrainData():
train_data = []
for fn in FILELIST:
img = misc.imread(TRAIN_FOLDER + fn)
img = np.reshape(img,(len(img[0,:]), len(img[:,0]), SHAPE[2]))
if img.shape != SHAPE:
print "image shape mismatch!"
print "Expected: "
print SHAPE
print "but got:"
print img.shape
sys.exit()
train_data.append (img)
train_data = np.array(train_data)
train_data = train_data.astype('float32')/ 255
return np.array(train_data)
def createModel():
input_img = Input(shape=SHAPE)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu',padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid',padding='same')(x)
return Model(input_img, decoded)
x_train = loadTrainData()
autoencoder = createModel()
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
print x_train.shape
autoencoder.summary()
# Run the network
autoencoder.fit(x_train, x_train,
epochs=EPOCHS,
batch_size=128,
shuffle=True)
给我一个错误: ValueError: Error when checking target: expected conv2d_7 to have shape (None, 260, 260, 1) but got array with shape (859, 257, 257, 1)
您可以看到,这不是Theano/Tensorflow后端昏暗订购的标准问题,而是其他问题。我检查了我的数据应该是print x_train.shape
:
(859, 257, 257, 1)
我还运行autoencoder.summary()
:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 257, 257, 1) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 257, 257, 16) 160
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 129, 129, 16) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 129, 129, 8) 1160
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 65, 65, 8) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 65, 65, 8) 584
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 33, 33, 8) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 33, 33, 8) 584
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 66, 66, 8) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 66, 66, 8) 584
_________________________________________________________________
up_sampling2d_2 (UpSampling2 (None, 132, 132, 8) 0
_________________________________________________________________
conv2d_6 (Conv2D) (None, 132, 132, 16) 1168
_________________________________________________________________
up_sampling2d_3 (UpSampling2 (None, 264, 264, 16) 0
_________________________________________________________________
conv2d_7 (Conv2D) (None, 264, 264, 1) 145
=================================================================
Total params: 4,385
Trainable params: 4,385
Non-trainable params: 0
_________________________________________________________________
现在,我不确定问题在哪里,但是看起来确实出现在Conv2D_6(Param#太高)上的问题。我确实知道CAE在原则上的工作是如何的,但是我对确切的技术细节尚不那么熟悉,我试图通过弄乱Deonvolution Padding(而不是使用有效)来解决此问题。我与DIM匹配的关闭是(None, 258, 258, 1)
。我通过盲目地尝试在反卷积方面的填充组合来实现这一目标,这并不是真正解决问题的明智方法...
在这一点上,我不知所措,任何帮助都将不胜感激
由于您的输入和输出数据相同,因此最终输出形状应与输入形状相同。
最后一个卷积层应具有形状(None, 257,257,1)
。
问题之所以发生,是因为您有一个奇数作为图像的尺寸(257)。
应用MaxPooling
时,它应将数字除以两个,因此它选择向上或向下舍入(它正在上升,请参阅129,来自257/2 = 128.5)
稍后,当您进行UpSampling
时,模型不知道当前尺寸已舍入,它只是将值加倍。序列发生的情况是在最终结果中添加了7个像素。
您可以尝试裁剪结果或填充输入。
我通常使用兼容大小的图像。如果您有3个MaxPooling
层,则您的尺寸应为2³的倍数。答案是264。
直接填充输入数据:
x_train = numpy.lib.pad(x_train,((0,0),(3,4),(3,4),(0,0)),mode='constant')
这将要求SHAPE=(264,264,1)
填充模型内:
import keras.backend as K
input_img = Input(shape=SHAPE)
x = Lambda(lambda x: K.spatial_2d_padding(x, padding=((3, 4), (3, 4))), output_shape=(264,264,1))(input_img)
裁剪结果:
在您不直接更改实际数据(numpy array)的任何情况下,这都是必需的。
decoded = Lambda(lambda x: x[:,3:-4,3:-4,:], output_shape=SHAPE)(x)