层的输入与预期输入的层不兼容

时间:2021-02-17 14:37:50

标签: python tensorflow machine-learning deep-learning generative-adversarial-network

我尝试制作 GAN 训练网络,尝试使用一些现有网络,但在每个网络上都遇到了相同的错误

 ValueError: Input 0 of layer sequential_17 is incompatible with the layer: expected axis -1 of input shape to have value 2 but received input with shape (None, 256, 256, 1)

我已经读过这是由于没有对我的数据进行批处理造成的,但我显然在 fit 函数中进行了批处理:

d_loss_real = discriminator.fit(x=ab, y=y_train_real,batch_size=20,epochs=2,verbose=1)

崩溃的模型是:

def discriminator():
        model = Sequential()
        model.add(Conv2D(32,(3,3), padding='same',strides=2,input_shape=d_image_shape))
        model.add(LeakyReLU(0.2))
        model.add(Dropout(0.25))
        
        model.add(Conv2D(64,(3,3),padding='same',strides=2))
        model.add(BatchNormalization())
        model.add(LeakyReLU(.2))
        model.add(Dropout(0.25))
        
        
        model.add(Conv2D(128,(3,3), padding='same', strides=2))
        model.add(BatchNormalization())
        model.add(LeakyReLU(0.2))
        model.add(Dropout(0.25))
        
        
        model.add(Conv2D(256,(3,3), padding='same',strides=2))
        model.add(BatchNormalization())
        model.add(LeakyReLU(0.2))
        model.add(Dropout(0.25))
        
        
        model.add(Flatten())
        model.add(Dense(1))
        model.add(Activation('sigmoid'))
        
        image = Input(shape=d_image_shape)
        validity = model(image)
        return Model(image,validity)

        image = Input(shape=d_image_shape)
        validity = model(image)
        return Model(image,validity)

ab 和 L 值为:

L = np.array([rgb_to_lab(image,l=True) for image in X_train])
AB = np.array([rgb_to_lab(image,ab=True) for image in X_train])

rgb_to_lab 函数:

def rgb_to_lab(img, l=False, ab=False):

    img = img / 255
    l = color.rgb2lab(img)[:,:,0]
    l = l / 50 - 1
    l = l[...,np.newaxis]

    ab = color.rgb2lab(img)[:,:,1:]
    ab = (ab + 128) / 255 * 2 - 1
    if l.all():
        return l
    else: return ab

def lab_to_rgb(img):

    new_img = np.zeros((256,256,3))
    for i in range(len(img)):
        for j in range(len(img[i])):
            pix = img[i,j]
            new_img[i,j] = [(pix[0] + 1) * 50,(pix[1] +1) / 2 * 255 - 128,(pix[2] +1) / 2 * 255 - 128]
    new_img = color.lab2rgb(new_img) * 255
    new_img = new_img.astype('uint8')
    return new_img

0 个答案:

没有答案