0

main() 的内容更新生成器时'''noise = np.array(size = [batch_size, batch_images], low = -1.0, high = 1.0)''' 因为我使用的是np.array,所以我得到一个错误以及我应该为这部分分配什么

在标准 GAN 期间,我在更新生成器时使用了以下内容,但我更改为上面代码的地方没有训练 ''' noise = np.random.uniform (size = [batch_size, batch_images], low = -1.0, high = 1.0) '''

我们正在使用自动编码器制作 GAN。最初它基本上是让生成器使用随机数生成图像,但在我的 GAN 中,我们学习使用输出与输入相同的生成器(如 Autoencoder)与鉴别器竞争,我认为我认为生成器具有与在我们学习时输入图像。虽然我是刚开始深度学习的初学者,我可以实现标准的GAN,但是我不能实现上面提到的GAN

我认为main()中正在学习的部分的代码有问题,但是如果有懂的人请给我教授

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Reshape
from keras.layers.core import Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import UpSampling2D
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.core import Flatten,Dropout
from keras.optimizers import Adam
import numpy as np
from PIL import Image
import os
import glob
import random

n_colors = 3

def generator_model():
    model = Sequential()
    #encode
    model.add(Conv2D(64, (5, 5),input_shape=(64, 64, n_colors),activation='relu', padding='same'))
    model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))

    model.add(Conv2D(32, (5, 5),activation='relu', padding='same'))
    model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))

    model.add(Conv2D(16, (5, 5),activation='relu', padding='same'))
    model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
    #decode
    model.add(Conv2D(16,(5,5),activation='relu',padding='same'))
    model.add(UpSampling2D(size=(2, 2)))

    model.add(Conv2D(32,(5,5),activation='relu',padding='same'))
    model.add(UpSampling2D(size=(2, 2)))

    model.add(Conv2D(64,(5,5),activation='relu'))
    model.add(UpSampling2D(size=(2, 2)))

    model.add(Conv2D(n_colors,(5,5),activation='sigmoid', border_mode='same'))
    return model

def discriminator_model():
model = Sequential()

model.add(Conv2D(64(5,5),
input_shape(64,64,n_colors),padding='same'))
model.add(Activation('tanh'))
model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(128, (5, 5)))
    model.add(Activation('tanh'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Flatten())

    model.add(Dense(1024))
    model.add(Activation('tanh'))

    model.add(Dense(1))
    model.add(Activation('sigmoid'))
    return model

def generator_containing_discriminator(generator, discriminator):
    model = Sequential()
    model.add(generator)
    model.add(discriminator)
    return model

def image_batch(batch_size):
    files = glob.glob("./in_images/**/*.jpg", recursive=True)
    files = random.sample(files, batch_size)
    # print(files)
    res = []
    for path in files:
        img = Image.open(path)
        img = img.resize((64, 64))
        arr = np.array(img)
        arr = (arr - 127.5) / 127.5
        arr.resize((64, 64, n_colors))
        res.append(arr)
    return np.array(res)

def combine_images(generated_images, cols=5, rows=5):
    shape = generated_images.shape
    h = shape[1]
    w = shape[2]
    image = np.zeros((rows * h,  cols * w, n_colors))
    for index, img in enumerate(generated_images):
        if index >= cols * rows:
            break
        i = index // cols
        j = index % cols
        image[i*h:(i+1)*h, j*w:(j+1)*w, :] = img[:, :, :]
    image = image * 127.5 + 127.5
    image = Image.fromarray(image.astype(np.uint8))
    return image

def set_trainable(model, trainable):
    model.trainable = trainable
    for layer in model.layers:
        layer.trainable = trainable

def main():
    batch_size = 55

    discriminator = discriminator_model()
    generator = generator_model()

    discriminator_on_generator = generator_containing_discriminator(generator, discriminator)

    set_trainable(discriminator, False)
    discriminator_on_generator.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5))

    print(generator.summary())
    print(discriminator_on_generator.summary())

    set_trainable(discriminator, True)
    discriminator.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5))

    print(discriminator.summary())

    for i in range(30 * 1000):
        batch_images = image_batch(batch_size)

       #generator update
        #noise = np.random.uniform(size=[batch_size, batch_images], low=-1.0, high=1.0)
        noise = np.array(size=[batch_size, batch_images], low=-1.0, high=1.0)

        generated_images = generator.predict(image_batch)

        # discriminator update
        X = np.concatenate((batch_images, generated_images))
        # Learn so that the label of the training data becomes 1 and the 
        # label of the generated image becomes 0
        y = [1] * batch_size + [0] * batch_size
        d_loss = discriminator.train_on_batch(X, y)

        # generator update
        noise = np.array(size=[batch_size, batch_images], low=-1.0, high=1.0)

        # When I put the generated image in the discriminator
        # Learn so that the output becomes close to 1 
        #(the probability of being identified 
        # as a training image increases)
        g_loss = discriminator_on_generator.train_on_batch(noise, [1] * batch_size)

        if i % 100 == 0:
            print("step %d d_loss, g_loss : %g %g" % (i, d_loss, g_loss))
            image = combine_images(generated_images)
            os.system('mkdir -p ./generate_images')
            image.save("./gen_images/gen%05d.jpg" % i)
            generator.save_weights('generator.h5', True)
            discriminator.save_weights('discriminator.h5', True)

main()

学习正常开始,生成了某种图像,应该显示如下 step 0 d_loss, g_loss : 0.675611 0.594226

但是,出现以下错误

Traceback (most recent call last):
  File "keras_dcgan_copy.py", line 213, in <module>
    main()
  File "keras_dcgan_copy.py", line 188, in main
    noise = np.array(size=[batch_size, batch_images], low=-1.0, high=1.0)
TypeError: Required argument 'object' (pos 1) not found
4

1 回答 1

1

问题在于以下行。噪声应该是随机分布的numpy数组。

noise = np.array(size=[batch_size, batch_images], low=-1.0, high=1.0) 

您可以将上面的行更改为以下内容。而且batch_images也不是大小。这是一个 numpy 图像数组。因此batch_images,您应该在此处给出生成器输出的形状,而不是 。例如,如果生成器输出(64,64,3)彩色图像,则必须用它代替 batch_images。

noise = np.random.uniform(size=[batch_size, gen_out_xshape, gen_out_yshape, no_of_channels], low=-1.0, high=1.0) 

请注意,no_of_channels如果图像是灰度格式,则不需要。

于 2019-02-20T14:03:13.663 回答