我有下面的代码,可在374
大小为32x32
的训练图像上运行创生对抗网络(GAN)。
为什么会有以下错误?
ValueError: Input arrays should have the same number of samples as target arrays. Found 7500 input samples and 40 target samples.
在以下语句中出现:
discriminator_loss = discriminator.train_on_batch(combined_images,labels)
import keras
from keras import layers
import numpy as np
import cv2
import os
from keras.preprocessing import image
latent_dimension = 32
height = 32
width = 32
channels = 3
iterations = 100000
batch_size = 20
real_images = []
# paths to the training and results directories
train_directory = '/training'
results_directory = '/results'
# GAN generator
generator_input = keras.Input(shape=(latent_dimension,))
# transform the input into a 16x16 128-channel feature map
x = layers.Dense(128*16*16)(generator_input)
x = layers.LeakyReLU()(x)
x = layers.Reshape((16,16,128))(x)
x = layers.Conv2D(256,5,padding='same')(x)
x = layers.LeakyReLU()(x)
# upsample to 32x32
x = layers.Conv2DTranspose(256,4,strides=2,padding='same')(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(256,5,padding='same')(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(256,5,padding='same')(x)
x = layers.LeakyReLU()(x)
# a 32x32 1-channel feature map is generated (i.e. shape of image)
x = layers.Conv2D(channels,7,activation='tanh',padding='same')(x)
# instantiae the generator model, which maps the input of shape (latent dimension) into an image of shape (32,32,1)
generator = keras.models.Model(generator_input,x)
generator.summary()
# GAN discriminator
discriminator_input = layers.Input(shape=(height,width,channels))
x = layers.Conv2D(128,3)(discriminator_input)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(128,4,strides=2)(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(128,4,strides=2)(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(128,4,strides=2)(x)
x = layers.LeakyReLU()(x)
x = layers.Flatten()(x)
# dropout layer
x = layers.Dropout(0.4)(x)
# classification layer
x = layers.Dense(1,activation='sigmoid')(x)
# instantiate the discriminator model, and turn a (32,32,1) input
# into a binary classification decision (fake or real)
discriminator = keras.models.Model(discriminator_input,x)
discriminator.summary()
discriminator_optimizer = keras.optimizers.RMSprop(
lr=0.0008,
clipvalue=1.0,
decay=1e-8)
discriminator.compile(optimizer=discriminator_optimizer, loss='binary_crossentropy')
# adversarial network
discriminator.trainable = False
gan_input = keras.Input(shape=(latent_dimension,))
gan_output = discriminator(generator(gan_input))
gan = keras.models.Model(gan_input,gan_output)
gan_optimizer = keras.optimizers.RMSprop(
lr=0.0004,
clipvalue=1.0,
decay=1e-8)
gan.compile(optimizer=gan_optimizer,loss='binary_crossentropy')
start = 0
for step in range(iterations):
# sample random points in the latent space
random_latent_vectors = np.random.normal(size=(batch_size,latent_dimension))
# decode the random latent vectors into fake images
generated_images = generator.predict(random_latent_vectors)
stop = start + batch_size
i = start
for root, dirs, files in os.walk(train_directory):
for file in files:
for i in range(stop-start):
img = cv2.imread(root + '/' + file)
real_images.append(img)
i = i+1
combined_images = np.concatenate([generated_images,real_images])
# assemble labels and discrminate between real and fake images
labels = np.concatenate([np.ones((batch_size,1)),np.zeros(batch_size,1)])
# add random noise to the labels
labels = labels + 0.05 * np.random.random(labels.shape)
# train the discriminator
discriminator_loss = discriminator.train_on_batch(combined_images,labels)
random_latent_vectors = np.random.normal(size=(batch_size,latent_dimension))
# assemble labels that classify the images as "real", which is not true
misleading_targets = np.zeros((batch_size,1))
# train the generator via the GAN model, where the discriminator weights are frozen
adversarial_loss = gan.train_on_batch(random_latent_vectors,misleading_targets)
start = start + batch_size
if start > len(train_directory)-batch_size:
start = 0
# save the model weights
if step % 100 == 0:
gan.save_weights('gan.h5')
print'discriminator loss: '
print discriminator_loss
print 'adversarial loss: '
print adversarial_loss
img = image.array_to_img(generated_images[0] * 255.)
img.save(os.path.join(results_directory,'generated_melanoma_image' + str(step) + '.png'))
img = image.array_to_img(real_images[0] * 255.)
img.save(os.path.join(results_directory,'real_melanoma_image' + str(step) + '.png'))
谢谢。
答案 0 :(得分:2)
您导致此问题的后续步骤
i = start
for root, dirs, files in os.walk(train_directory):
for file in files:
for i in range(stop-start):
img = cv2.imread(root + '/' + file)
real_images.append(img)
i = i+1
您正尝试收集20
的{{1}}个样本,这是通过内部循环完成的。然后有一个外部循环,每个文件都在运行,因此,外部循环正在为每个文件收集real_images
个样本,总共收集20
个样本,而您计划仅收集{{1} }。