要在您的系统上重新创建此问题的提问,请找到源代码和数据集here

What I am trying?
I am trying to create a simple GAN (Generative Adversarial N/w) where I am trying to recolor Black and White images using a few ImageNet images.


What Process am I following?
I have take a few Dog images, which are stored in folder ./ImageNet/dogs/ directory. Using Python code I have created 2 more steps where I convert

  1. 将狗狗图像转换为244 x 244分辨率并保存为./ImageNet/dogs_lowres/
  2. 将低分辨率图像转换为灰度并保存为./ImageNet/dogs_bnw/
  3. 将低分辨率的BNW图像送入GaN模型,生成彩色图像.

Where am I Stuck?
I am stuck at understanding how the Image dimensions / shape are used. I am getting the error as such:

ValueError: `logits` and `labels` must have the same shape, received ((32, 28, 28, 3) vs (32, 224, 224)).

下面是生成器和鉴别器的代码:

# GAN model for recoloring black and white images
generator = Sequential()
generator.add(Dense(7 * 7 * 128, input_dim=100))
generator.add(Reshape((7, 7, 128)))
generator.add(Conv2DTranspose(64, kernel_size=5, strides=2, padding='same'))
generator.add(Conv2DTranspose(32, kernel_size=5, strides=2, padding='same'))
generator.add(Conv2DTranspose(3, kernel_size=5, activation='sigmoid', padding='same'))

# Discriminator model
discriminator = Sequential()
discriminator.add(Flatten(input_shape=(224, 224, 3)))
discriminator.add(Dense(1, activation='sigmoid'))

# Compile the generator model
optimizer = Adam(learning_rate=0.0002, beta_1=0.5)
generator.compile(loss='binary_crossentropy', optimizer=optimizer)

# Train the GAN to recolor images
epochs = 10000
batch_size = 32

训练循环如下:

for epoch in range(epochs):
    idx = np.random.randint(0, bw_images.shape[0], batch_size)
    real_images = bw_images[idx]

    noise = np.random.normal(0, 1, (batch_size, 100))
    generated_images = generator.predict(noise)

    # noise_rs = noise.reshape(-1, 1)
    g_loss = generator.train_on_batch(noise, real_images)

    if epoch % 100 == 0:
        print(f"Epoch: {epoch}, Generator Loss: {g_loss}")

Where is the Error? I get error on line:
g_loss = generator.train_on_batch(noise, real_images)

当我判断shape个噪波和Real_Image对象时,我得到的是:

real_images.shape
(32, 224, 224)
noise.shape
(32, 100)

任何帮助/建议都很感激.

推荐答案

generator输出[32 28 28 3],而它得到的目标形状为[32 224 224].目标有两个不同之处:它是灰度而不是彩色的,而且维度更大.

我假设提供给生成器的目标应该是彩色的,而不是灰度的.您可以使用以下命令加载彩色图像并调整其大小:

def load_images_color(directory):
    images = []
    for filename in os.listdir(directory):
        img_path = os.path.join(directory, filename)
        img = cv2.imread(img_path)
        img = cv2.resize(img, (224, 224))  # Resize images to 224x224
        img = img.astype('float32') / 255.0  # Normalize pixel values
        images.append(img)
    return np.array(images)

# Load colour images
cl_images = load_images_color('./ImageNet/dogs')

...

for epoch in range(epochs):
    ...
        
    cl_real = cl_images[idx]
    #Resize colour images to match generator output shape
    cl_real_small = []
    for im in cl_real:
        cl_real_small.append( cv2.resize(im, (28, 28)) )
    cl_real_small = np.array(cl_real_small)
    
    ...

    g_loss = generator.train_on_batch(noise, cl_real_small)

Python相关问答推荐

错误:找不到TensorFlow / Cygwin的匹配分布

Python-Polars:如何用两个值的平均值填充NA?

单击Cookie横幅错误并在Selenium中启用搜索栏

预期LP_c_Short实例而不是_ctyles.PyCStructType

已安装' owiener ' Python模块,但在导入过程中始终没有名为owiener的模块

将numpy数组与空数组相加

两极:如何分割一个大 pyramid 并并行保存每个

使用Python C API重新启动Python解释器

使用imap-tools时错误,其邮箱地址包含域名中的非默认字符

指示组内的rejected_time是否在creation_timestamp后5分钟内

使用LineConnection动画1D数据

在Python中对分层父/子列表进行排序

更改matplotlib彩色条的字体并勾选标签?

pandas DataFrame GroupBy.diff函数的意外输出

使用新的类型语法正确注释ParamSecdecorator (3.12)

在Python Attrs包中,如何在field_Transformer函数中添加字段?

如何使用根据其他值相似的列从列表中获取的中间值填充空NaN数据

在极性中创建条件累积和

Scrapy和Great Expectations(great_expectations)—不合作

如果初始groupby找不到满足掩码条件的第一行,我如何更改groupby列,以找到它?