我正在尝试为我的项目构建一个Pix2Pix并收到错误:
ValueError: Concatenate
图层要求输入具有匹配的形状(concat轴除外)。得到输入形状:[(无,64,64,128),(无,63,63,128)]
生成器是U-net模型,我的输入高度x宽x通道为256,256,3(X_train)和256,256,1(Y_train)。如果错误是由预处理或模型本身引起的,我不会这样做。任何援助将不胜感激。
def build_generator(self):
"""U-Net Generator"""
def conv2d(layer_input, filters, f_size=3, bn=True):
"""Layers used during downsampling"""
d = Conv2D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input)
d = LeakyReLU(alpha=0.2)(d)
if bn:
d = BatchNormalization(momentum=0.8)(d)
return d
def deconv2d(layer_input, skip_input, filters, f_size=3, dropout_rate=0):
"""Layers used during upsampling"""
u = UpSampling2D(size=2)(layer_input)
u = Conv2D(filters, kernel_size=f_size, strides=1, padding='same', activation='relu')(u)
if dropout_rate:
u = Dropout(dropout_rate)(u)
u = BatchNormalization(momentum=0.8)(u)
u = Concatenate()([u, skip_input])
return u
# Image input
d0 = Input(shape=self.img_shape)
# Downsampling
d1 = conv2d(d0, self.gf, bn=False)
d2 = conv2d(d1, self.gf*2)
d3 = conv2d(d2, self.gf*4)
d4 = conv2d(d3, self.gf*8)
d5 = conv2d(d4, self.gf*8)
d6 = conv2d(d5, self.gf*8)
d7 = conv2d(d6, self.gf*8)
# Upsampling
u1 = deconv2d(d7, d6, self.gf*8)
u2 = deconv2d(u1, d5, self.gf*8)
u3 = deconv2d(u2, d4, self.gf*8)
u4 = deconv2d(u3, d3, self.gf*4)
u5 = deconv2d(u4, d2, self.gf*2)
u6 = deconv2d(u5, d1, self.gf)
u7 = UpSampling2D(size=2)(u6)
output_img = Conv2D(self.channels, kernel_size=3, strides=1, padding='same', activation='tanh')(u7)
return Model(d0, output_img)
以下跟踪错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-58-1a469bc54cde> in <module>()
----> 1 gan = Pix2Pix()
2 gan.train(epochs=30000, batch_size=1, save_interval=200)
<ipython-input-57-f9ff9b0e4228> in __init__(self)
48
49 # Build and compile the generator
---> 50 self.generator = self.build_generator()
51 self.generator.compile(loss='binary_crossentropy', optimizer=optimizer)
52
<ipython-input-57-f9ff9b0e4228> in build_generator(self)
107 u3 = deconv2d(u2, d4, self.gf*8)
108 u4 = deconv2d(u3, d3, self.gf*4)
--> 109 u5 = deconv2d(u4, d2, self.gf*2)
110 u6 = deconv2d(u5, d1, self.gf)
111
<ipython-input-57-f9ff9b0e4228> in deconv2d(layer_input, skip_input, filters, f_size, dropout_rate)
87 u = Dropout(dropout_rate)(u)
88 u = BatchNormalization(momentum=0.8)(u)
---> 89 u = Concatenate()([u, skip_input])
90 return u
91
/usr/local/lib/python3.5/site-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
569 self.build(input_shapes[0])
570 else:
--> 571 self.build(input_shapes)
572 self.built = True
573
/usr/local/lib/python3.5/site-packages/keras/layers/merge.py in build(self, input_shape)
275 'inputs with matching shapes '
276 'except for the concat axis. '
--> 277 'Got inputs shapes: %s' % (input_shape))
278
279 def call(self, inputs):
ValueError: `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 64, 64, 128), (None, 63, 63, 128)]
答案 0 :(得分:1)
尝试定义图像数据格式
from keras import backend as K
K.set_image_data_format('channels_first')
我认为这应该可以解决您的问题。