Keras - 输入数组应具有与目标数组相同的样本数-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

Keras - 输入数组应具有与目标数组相同的样本数

一码平川MACHEL 2019-01-17 16:21:59 2892

我有下面的代码运行生成对抗网络(GAN)374培训大小的图像32x32。

为什么我出现以下错误?

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(1281616)(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'))
机器学习/深度学习 数据挖掘 算法框架/工具 计算机视觉 Python
分享到
取消 提交回答
全部回答(1)
  • 一码平川MACHEL
    2019-07-17 23:25:46

    您的以下步骤导致此问题,

    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样本real_images。然后是外部循环,它正在为每个文件运行,因此外部循环正在收集20每个文件的7480样本,这些样本总共收集样本,您计划仅20在总计中收集样本。

    0 0
人工智能
使用钉钉扫一扫加入圈子
+ 订阅

了解行业+人工智能最先进的技术和实践,参与行业+人工智能实践项目

推荐文章
相似问题
推荐课程