ValueError in training GAN Model

问题描述 投票:0回答:0

我在训练我的 GAN 模型时一次又一次地面临 ValueError 的问题。请帮助我。

import cv2
import numpy as np
import pandas as pd
import tensorflow as tf





# read the CSV file containing the labels
labels_df = pd.read_csv('labels.csv')

# define a function to preprocess and resize the images
def preprocess_image(image, target_size):
    # convert the image to grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # resize the image to the target size
    resized_image = cv2.resize(gray_image, target_size)
    
    # normalize the pixel values to be between 0 and 1
    normalized_image = resized_image / 255.0
    
    return normalized_image

# initialize lists to store the images and labels
images = []
labels = []

# define the target size of the resized images
target_size = (128, 128)

# iterate over the rows of the dataframe and read the corresponding image using OpenCV
for index, row in labels_df.iterrows():
    image_filename = row['image_filename']
    label = row['label']
    
    # read image using OpenCV
    image = cv2.imread(image_filename)
    
    # preprocess and resize the image
    preprocessed_image = preprocess_image(image, target_size)
    
    # save the preprocessed image and label in numpy arrays
    images.append(preprocessed_image)
    labels.append(label)

# convert the lists to numpy arrays
images = np.array(images)
labels = np.array(labels)

# save the numpy arrays to disk
np.save('preprocessed_images.npy', images)
np.save('labels.npy', labels)



# load the preprocessed images and labels from the numpy arrays
images = np.load('preprocessed_images.npy')
labels = np.load('labels.npy')

# define the input shape of the generator
generator_input_shape = 100

# define the generator model
generator = tf.keras.Sequential([
    tf.keras.layers.Dense(7 * 7 * 64, input_dim=generator_input_shape),
    tf.keras.layers.Reshape((7, 7, 64)),
    tf.keras.layers.Conv2DTranspose(64, kernel_size=4, strides=2, padding='same'),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.LeakyReLU(alpha=0.2),
    tf.keras.layers.Conv2DTranspose(64, kernel_size=4, strides=2, padding='same'),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.LeakyReLU(alpha=0.2),
    tf.keras.layers.Conv2D(1, kernel_size=7, activation='sigmoid', padding='same')
])

# define the discriminator model
discriminator = tf.keras.Sequential([
    tf.keras.layers.Conv2D(64, kernel_size=4, strides=2, input_shape=(28, 28, 1), padding='same'),
    tf.keras.layers.LeakyReLU(alpha=0.2),
    tf.keras.layers.Dropout(0.4),
    tf.keras.layers.Conv2D(128, kernel_size=4, strides=2, padding='same'),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.LeakyReLU(alpha=0.2),
    tf.keras.layers.Dropout(0.4),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

# define the batch size and number of epochs
batch_size = 12
epochs = 10

# define the labels for the real and fake images
real_labels = np.ones((batch_size, 1))
fake_labels = np.zeros((batch_size, 1))

# define the GAN model by combining the generator and discriminator
gan = tf.keras.Sequential([generator, discriminator])

# compile the GAN model
discriminator.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(lr=0.0002, beta_1=0.5))
gan.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(lr=0.0002, beta_1=0.5))

# iterate over the epochs
for epoch in range(epochs):
    # select a random batch of images
    idx = np.random.randint(0, images.shape[0], batch_size)
    real_images = images[idx].reshape(batch_size, 28, 28, 1) # corrected
    
    # generate a batch of fake images
    noise = np.random.normal(0, 1, (batch_size, generator_input_shape))
    fake_images = generator.predict(noise)

    # train the discriminator on real and fake images
    real_labels = np.ones((batch_size, 1))
    fake_labels = np.zeros((batch_size, 1))
    d_loss_real = discriminator.train_on_batch(real_images, real_labels)
    d_loss_fake = discriminator.train_on_batch(fake_images, fake_labels)

    # calculate the discriminator loss for this batch
    d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

    # train the generator to fool the discriminator
    noise = np.random.normal(0, 1, (batch_size, generator_input_shape))
    fool_labels = np.ones((batch_size, 1))
    g_loss = gan.train_on_batch(noise, fool_labels)

    # print the progress
    print(f"Epoch {epoch+1}/{epochs} | Discriminator loss: {d_loss[0]} | Generator loss: {g_loss}")


discriminator.summary()


generator.summary()



这是代码。

错误信息: ”------------------------------------------------ -------------------------- ValueError Traceback(最后一次调用) 单元格输入 [27],第 53 行 范围内的纪元为 50(纪元): 51 # 选择一批随机图像 52 idx = np.random.randint(0, 图像.shape[0], batch_size) ---> 53 real_images = images[idx].reshape(batch_size, 28, 28, 1) # 更正 55 # 生成一批假图像 56 噪声 = np.random.normal(0, 1, (batch_size, generator_input_shape))

ValueError:无法将大小为 1048576 的数组重塑为形状 (64,28,28,1)

生成器总结: “ 型号:“sequential_9”


层(类型)输出形状参数#

dense_6(密集)(无,3136)316736

reshape_3(重塑)(无、7、7、64)0

conv2d_transpose_6 (Conv2DT (None, 14, 14, 64) 65600
转置)

batch_normalization_9 (Batc (None, 14, 14, 64) 256
h归一化)

leaky_re_lu_12 (LeakyReLU) (无, 14, 14, 64) 0

conv2d_transpose_7 (Conv2DT (None, 28, 28, 64) 65600
转置)

batch_normalization_10(蝙蝠(无、28、28、64)256
ch归一化)

leaky_re_lu_13(LeakyReLU)(无、28、28、64)0

conv2d_9 (Conv2D) (无, 28, 28, 1) 3137

================================================= ================ 总参数:451,585 可训练参数:451,329 不可训练的参数:256 ______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

鉴别器总结:

“型号:”sequential_10“


层(类型)输出形状参数#

conv2d_10 (Conv2D) (无, 14, 14, 64) 1088

leaky_re_lu_14 (LeakyReLU) (无, 14, 14, 64) 0

dropout_6(辍学)(无,14、14、64)0

conv2d_11 (Conv2D) (无, 7, 7, 128) 131200

batch_normalization_11 (Bat (None, 7, 7, 128) 512
ch归一化)

leaky_re_lu_15 (LeakyReLU) (无, 7, 7, 128) 0

dropout_7 (Dropout) (无, 7, 7, 128) 0

flatten_3(展平)(无,6272)0

dense_7(密集)(无,1)6273

================================================= ================ 总参数:139,073 可训练参数:138,817 不可训练的参数:256 ______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

我正在尝试更改 batch_size 但不起作用。我有 128*128 的图像用于训练模型。

python tensorflow machine-learning deep-learning generative-adversarial-network
© www.soinside.com 2019 - 2024. All rights reserved.