关于gan的笔记

gan结构

这个过程大概可以理解为将图片压缩到特征再复现出来
理解
在这里插入图片描述

gan网络结构

网络主体为一个生成器和一个辨别器,一个将输入图像分辨真假,一个根据随机数生成假图片知道辨别器误分辨为真
在这里插入图片描述

gan训练过程

  1. 初始化生成器辨别器
  2. 固定G训练D
  3. 固定D训练G
    (循环2,3)

![在这里插入图片描述](https://img-blog.csdnimg.cn/20201026202028834.png#pic_center

代码解读

acgan训练过程

    def __init__(self):
        # 输入图像横向,纵向大小
        self.img_rows = 28
        self.img_cols = 28
        # 图像频道数(黑白图片为1)
        self.channels = 1
        # 规定输入部分形状
        self.img_shape = (self.img_rows, self.img_cols, self.channels)
        #输入前压缩大小
        self.num_classes = 10
        self.latent_dim = 100
        
        #优化器以及损失函数
        optimizer = Adam(0.0002, 0.5)
        losses = ['binary_crossentropy', 'sparse_categorical_crossentropy']

        # 建立描述器并编译
        self.discriminator = self.build_discriminator()
        self.discriminator.compile(loss=losses,
            optimizer=optimizer,
            metrics=['accuracy'])

        # 建立生成器
        self.generator = self.build_generator()

        # 生成器以随机生成的noise为输入,目标标签为输出,加入生成器生成图片
        noise = Input(shape=(self.latent_dim,))
        label = Input(shape=(1,))
        img = self.generator([noise, label])

        # 生成图片后为训练生成器,禁止辨别器的学习
        self.discriminator.trainable = False

        # 鉴别器将生成的图像作为输入,确定图像的有效性和标签
        valid, target_label = self.discriminator(img)

        # 组合模型并训练生成器欺骗鉴别器
        self.combined = Model([noise, label], [valid, target_label])
        self.combined.compile(loss=losses,
            optimizer=optimizer)

生成器建立

    def build_generator(self):
        # 生成器的建立
        model = Sequential()
        # 全连接层
        model.add(Dense(128 * 7 * 7, activation="relu", input_dim=self.latent_dim))
        model.add(Reshape((7, 7, 128)))
        model.add(BatchNormalization(momentum=0.8))
        # 上采样
        model.add(UpSampling2D())
        # 卷积
        model.add(Conv2D(128, kernel_size=3, padding="same"))
        # 激活
        model.add(Activation("relu"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(UpSampling2D())
        model.add(Conv2D(64, kernel_size=3, padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Conv2D(self.channels, kernel_size=3, padding='same'))
        model.add(Activation("tanh"))

        model.summary()

        noise = Input(shape=(self.latent_dim,))
        label = Input(shape=(1,), dtype='int32')
        label_embedding = Flatten()(Embedding(self.num_classes, self.latent_dim)(label))

        model_input = multiply([noise, label_embedding])
        img = model(model_input)

        return Model([noise, label], img)

辨别器建立

    def build_discriminator(self):
        # 建立描述器
        
        model = Sequential()        
        #卷积
        model.add(Conv2D(16, kernel_size=3, strides=2, input_shape=self.img_shape, padding="same"))
        # 激活
        model.add(LeakyReLU(alpha=0.2))
        # dropout
        model.add(Dropout(0.25))
        model.add(Conv2D(32, kernel_size=3, strides=2, padding="same"))
        # 填充
        model.add(ZeroPadding2D(padding=((0,1),(0,1))))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Conv2D(64, kernel_size=3, strides=2, padding="same"))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Conv2D(128, kernel_size=3, strides=1, padding="same"))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(0.25))

        model.add(Flatten())
        model.summary()

        img = Input(shape=self.img_shape)

        
        features = model(img)

        # 分开结果
        validity = Dense(1, activation="sigmoid")(features)
        label = Dense(self.num_classes, activation="softmax")(features)

        return Model(img, [validity, label])

训练

    def train(self, epochs, batch_size=128, sample_interval=50):

        # 提取测试集
        (X_train, y_train), (_, _) = mnist.load_data()

        # 规范化输入
        X_train = (X_train.astype(np.float32) - 127.5) / 127.5
        X_train = np.expand_dims(X_train, axis=3)
        y_train = y_train.reshape(-1, 1)

        # 建立vaild,fake矩阵
        valid = np.ones((batch_size, 1))
        fake = np.zeros((batch_size, 1))

        for epoch in range(epochs):

            # 训练辨别器

            # 随机选择训练集
            idx = np.random.randint(0, X_train.shape[0], batch_size)
            imgs = X_train[idx]

            # 生成噪声
            noise = np.random.normal(0, 1, (batch_size, self.latent_dim))

            # 提取图片标签
            sampled_labels = np.random.randint(0, 10, (batch_size, 1))

            # 生成图片
            gen_imgs = self.generator.predict([noise, sampled_labels])

            # 图片标签
            img_labels = y_train[idx]

            # 训练辨别器
            d_loss_real = self.discriminator.train_on_batch(imgs, [valid, img_labels])
            d_loss_fake = self.discriminator.train_on_batch(gen_imgs, [fake, sampled_labels])
            d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

            # 训练生成器

            g_loss = self.combined.train_on_batch([noise, sampled_labels], [valid, sampled_labels])

            # 打印结果
            print ("%d [D loss: %f, acc.: %.2f%%, op_acc: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[3], 100*d_loss[4], g_loss[0]))

            # 生成结果图
            if epoch % sample_interval == 0:
                self.save_model()
                self.sample_images(epoch)
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页