CNN输出的Keras错误

时间:2018-03-10 15:16:32

标签: keras autoencoder

尝试在Keras中创建CNN模型以创建去噪自动编码器时出错。我的Keras后端是TensorFlow。

我的输入数据是一个numpy数组。 numpy数组取自灰度图像。我使用sklearn train_test_split拆分它。我已调整数据大小并在输出层出错。

/opt/anaconda/anaconda3/lib/python3.6/site-
packages/keras/engine/training.py in _standardize_input_data(data, 
names, shapes, check_batch_axis, exception_prefix)
151                             ' to have shape ' + str(shapes[i]) +
152                             ' but got array with shape ' +
--> 153                             str(array.shape))
154     return arrays
155 

ValueError: Error when checking target: expected conv2d_transpose_36 
to have shape (None, 279, 559, 1) but got array with shape (129, 258, 
540, 1)

train_images = os.listdir('data/train')
test_images = os.listdir('data/test')
clean_images = os.listdir('data/train_cleaned')

def set_common_size(img_link, std_size = (258, 540)):
    '''Function will take in the argument of a link to an image and return the image 
    with the standard size.'''
    img = Image.open(img_link)
    img = image.img_to_array(img)
    img = np.resize(img, std_size)
    return img / 255

train_data = []
test_data = []
cleaned_data = []
for img in train_images:
img_file = set_common_size('data/train/' + img)
train_data.append(img_file)
for img in test_images:
    img_file = set_common_size('data/test/' + img)
    test_data.append(img_file)
for img in clean_images:
    img_file = set_common_size('data/train_cleaned/' + img)
    cleaned_data.append(img_file)
train_data = np.asarray(train_data)
test_data = np.asarray(test_data)
cleaned_data = np.asarray(cleaned_data)

x_train, x_test, y_train, y_test = train_test_split(train_data, cleaned_data, test_size=0.1, random_state=42)

input_shape = x_train[0].shape
input_layer = Input(input_shape)

#Layer 1 
layer1 = Conv2D(64, 3, activation='relu', padding='same')(input_layer)
layer1 = MaxPooling2D(2)(layer1)
#Layer 2
layer2 = Conv2D(128, 3, activation='relu', padding='same')(layer1)
layer2 = MaxPooling2D(2)(layer2)
#Layer 3 
layer3 = Conv2D(256, 3, activation='relu', padding='same')(layer2)
layer3 = MaxPooling2D(2)(layer3)
#Bottleneck
encoded = layer3
#Layer1
uplayer1 = UpSampling2D(2)(layer3)
uplayer1 = Conv2DTranspose(256, 2, activation='relu')(uplayer1)
#Layer2
uplayer2 = UpSampling2D(2)(uplayer1)
uplayer2 = Conv2DTranspose(128, 5, activation='relu')(uplayer2)
#Layer3
uplayer3 = UpSampling2D(2)(uplayer2)
uplayer3 = Conv2DTranspose(64, 10, activation='relu')(uplayer3)
output = Conv2DTranspose(1, 3, activation='sigmoid')(uplayer3)

model = Model(input=input_layer, output=output)
print(model.summary())

model.compile(optimizer='adadelta', loss='binary_crossentropy')

model.fit(
        x_train, y_train, 
        epochs=100,
        shuffle=True,
        validation_data=(x_test, y_test)
        )

以下是模型摘要结果:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 258, 540, 1)       0         
_________________________________________________________________
conv2d_60 (Conv2D)           (None, 258, 540, 64)      640       
_________________________________________________________________    
max_pooling2d_37 (MaxPooling (None, 129, 270, 64)      0         
_________________________________________________________________
conv2d_61 (Conv2D)           (None, 129, 270, 128)     73856     
_________________________________________________________________
max_pooling2d_38 (MaxPooling (None, 64, 135, 128)      0         
_________________________________________________________________
conv2d_62 (Conv2D)           (None, 64, 135, 256)      295168    
_________________________________________________________________
max_pooling2d_39 (MaxPooling (None, 32, 67, 256)       0         
_________________________________________________________________
up_sampling2d_37 (UpSampling (None, 64, 134, 256)      0         
_________________________________________________________________
conv2d_transpose_29 (Conv2DT (None, 65, 135, 256)      262400    
_________________________________________________________________
up_sampling2d_38 (UpSampling (None, 130, 270, 256)     0         
_________________________________________________________________
conv2d_transpose_30 (Conv2DT (None, 134, 274, 128)     819328    
_________________________________________________________________
up_sampling2d_39 (UpSampling (None, 268, 548, 128)     0         
___________________________________________________________    
_________________________________________________________________
conv2d_transpose_32 (Conv2DT (None, 279, 559, 1)       577       
=============================================================______
conv2d_transpose_31 (Conv2DT (None, 277, 557, 64)      819264====
Trainable params: 2,271,233
Non-   params: 0

1 个答案:

答案 0 :(得分:0)

之间存在不匹配:

  • 您的模型创建的输出的形状:(279,559,1) (显示在摘要行conv2d_transpose_32 (Conv2DT (None, 279, 559, 1)中,它实际上是最后一层,但是你的控制台输出看起来有些混乱)

  • 您期望的输出形状:(258,540,1)(您输入的y_train中条目的形状)

使用模型摘要查看偏离预期形状的位置,并使用正确的padding值来获得预期的输出形状。