本文介绍了ValueError:“连接"层要求输入具有匹配形状,但连接轴除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的项目构建一个Pix2Pix并收到错误消息:

I am attempting to build a Pix2Pix for my project and get the error:

ValueError: Concatenate层要求输入的形状与concat轴不同,但形状匹配.得到了输入形状:[[None,64,64,128),(None,63,63,128)]

ValueError: Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 64, 64, 128), (None, 63, 63, 128)]

生成器是一个U-net模型,我的输入高度x宽度x通道数是256,256,3(X_train)和256,256,1(Y_train).如果错误是由于预处理或模型本身引起的,我不是.任何帮助将不胜感激.

The generator is a U-net model and my inputs height x width x channels which is 256,256,3 (X_train) and 256, 256, 1 (Y_train). I'm not if the error is due to the preprocessing or the model itself. Any assistance would be much appreciated.

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)]

推荐答案

尝试定义图像数据格式

from keras import backend as K
K.set_image_data_format('channels_first')

我认为这应该可以解决您的问题.

I think this should resolve your problem.

这篇关于ValueError:“连接"层要求输入具有匹配形状,但连接轴除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 15:26