DCGAN实现

代码

  • dcgan.py

#!/usr/bin/env python
# -*- coding: utf-8 -*- import os
import math
import argparse
import cv2
import numpy as np
import tensorflow as tf # DataManager负责提供数据
class DataManager(object): def __init__(self, data_dir):
self.data_dir = data_dir
self.im_shape = (48, 48, 3)
self.im_list = self._get_im_names()
self.batch_size = 64
self.chunk_size = len(self.im_list) // self.batch_size def _get_im_names(self):
if not self.data_dir:
return np.asarray([])
im_list = np.asarray(os.listdir(self.data_dir))
np.random.shuffle(im_list)
return im_list def imread(self, im_name):
im = cv2.imread(os.path.join(self.data_dir, im_name))
im = cv2.resize(im, self.im_shape[:2])
im = (im.astype('float32') - 127.5) / 128.0
return im def imwrite(self, name, im):
im = (im * 128.0 + 127.5)
im = im.astype('uint8')
cv2.imwrite('./images/%s.jpg' % name, im) def next_batch(self):
start = 0
end = start + self.batch_size
for i in range(self.chunk_size):
name_list = self.im_list[start: end]
batch_im_list = np.asarray([self.imread(im_name) for im_name in name_list])
yield batch_im_list
start += self.batch_size
end += self.batch_size # 不使用任何其他框架(Keras, Slim), 神经网络中所有的操作都重新封装成一个方法
class DCGAN(object): def __init__(self, data_dir):
# 通过data_manager控制数据的输入与输出
self.data_manager = DataManager(data_dir)
self.batch_size = self.data_manager.batch_size
self.im_shape = self.data_manager.im_shape
self.chunk_size = self.data_manager.chunk_size # 噪声的长度
self.z_len = 100
self.learning_rate = 0.0002
self.epochs = 100
self.beta1 = 0.5
self.sample_size = 64 # 全连接层
def fc(self, ims, output_size, scope='fc'):
with tf.variable_scope(scope, reuse=False):
weights = tf.get_variable('weights', [ims.shape[1], output_size], tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02))
biases = tf.get_variable('biases', [1, output_size], initializer=tf.constant_initializer(0.0))
return tf.matmul(ims, weights) + biases # 批量均值化
def batch_norm(self, x, epsilon=1e-5, momentum=0.9, scope='batch_norm', is_training=True):
with tf.variable_scope(scope, reuse=False):
return tf.contrib.layers.batch_norm(x, epsilon=epsilon, decay=momentum, updates_collections=None, scale=True, is_training=is_training) # 卷积层
def conv2d(self, ims, output_dim, scope='conv2d'):
with tf.variable_scope(scope, reuse=False):
# 在Tensorflow中, SAME不是一般人理解的SAME, 在此框架中, 只要知道了输入的维度和stride的大小, 让输入的维度除以stride的大小就是卷积之后的维度
# 在卷积中, ksize的维度为[height, width, in_channels, out_channels], 注意: 与转置卷积不同
ksize = [5, 5, ims.shape[-1], output_dim]
strides = [1, 2, 2, 1]
weights = tf.get_variable('weights', ksize, tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02))
biases = tf.get_variable('biases', [1, 1, 1, output_dim], tf.float32, initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(ims, weights, strides=strides, padding='SAME') + biases
return conv # 转置卷积层
def deconv2d(self, ims, output_shape, scope='deconv2d'):
with tf.variable_scope(scope, reuse=False):
ksize = [5, 5, output_shape[-1], ims.shape[-1]]
strides = [1, 2, 2, 1] weights = tf.get_variable('weights', ksize, tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.02))
biases = tf.get_variable('biases', [1, 1, 1, output_shape[-1]], tf.float32, initializer=tf.constant_initializer(0.0)) deconv = tf.nn.conv2d_transpose(ims, weights, output_shape=output_shape, strides=strides) + biases
return deconv # leaky ReLu
def lrelu(self, x, alpha=0.2):
return tf.maximum(x, x * alpha) # 判别器, 比较简单, 就是传统的分类, 不过去掉了池化层, 添加了batch norm
def discriminator(self, ims, reuse=False):
with tf.variable_scope('discriminator', reuse=reuse):
net = self.conv2d(ims, 64, scope='d_conv_1')
net = self.lrelu(net) net = self.conv2d(net, 64 * 2, scope='d_conv_2')
net = self.batch_norm(net, scope='d_bn_2')
net = self.lrelu(net) net = self.conv2d(net, 64 * 4, scope='d_conv_3')
net = self.batch_norm(net, scope='d_bn_3')
net = self.lrelu(net) net = self.conv2d(net, 64 * 8, scope='d_conv_4')
net = self.batch_norm(net, scope='d_bn_4')
net = self.lrelu(net) net = self.fc(tf.reshape(net, [-1, net.shape[1] * net.shape[2] * net.shape[3]]), 1, scope='d_fc_5')
return tf.nn.sigmoid(net), net # 生成器, 就是一个解码器, 去掉了池化层, 添加了Bath norm, 左右的结果通过tanh输出
def generator(self, noise_z, is_training=True):
with tf.variable_scope('generator', reuse=False):
# 训练输入的图像为48x48, 反过来计算出各个网络层的图像维度
net = self.fc(noise_z, 3 * 3 * 64 * 8)
net = tf.reshape(net, [-1, 3, 3, 64 * 8])
net = self.batch_norm(net, scope='g_bn_1', is_training=is_training)
net = tf.nn.relu(net) net = self.deconv2d(net, [self.batch_size, 6, 6, 64 * 4], scope='g_conv_2')
net = self.batch_norm(net, scope='g_bn_2', is_training=is_training)
net = tf.nn.relu(net) net = self.deconv2d(net, [self.batch_size, 12, 12, 64 * 2], scope='g_conv_3')
net = self.batch_norm(net, scope='g_bn_3', is_training=is_training)
net = tf.nn.relu(net) net = self.deconv2d(net, [self.batch_size, 24, 24, 64], scope='g_conv_4')
net = self.batch_norm(net, scope='g_bn_4', is_training=is_training)
net = tf.nn.relu(net) net = self.deconv2d(net, [self.batch_size, self.im_shape[0], self.im_shape[1], 3], scope='g_conv_5') return tf.nn.tanh(net) def train(self):
real_ims = tf.placeholder(tf.float32, [self.batch_size, self.im_shape[0], self.im_shape[1], self.im_shape[2]], name='real_ims')
noise_z = tf.placeholder(tf.float32, [None, self.z_len], name='noise_z') # Loss functions
fake_ims = self.generator(noise_z)
real_prob, real_logits = self.discriminator(real_ims)
fake_prob, fake_logits = self.discriminator(fake_ims, reuse=True) real_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=real_logits, labels=tf.ones_like(real_logits)))
fake_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=fake_logits, labels=tf.zeros_like(fake_logits)))
g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=fake_logits, labels=tf.ones_like(fake_logits)))
d_loss = real_loss + fake_loss real_loss_sum = tf.summary.scalar('real_loss', real_loss)
fake_loss_sum = tf.summary.scalar('fake_loss', fake_loss)
g_loss_sum = tf.summary.scalar('g_loss', g_loss)
d_loss_sum = tf.summary.scalar('d_loss', d_loss) # Optimizer
train_vars = tf.trainable_variables()
d_vars = [var for var in train_vars if var.name.startswith('discriminator')]
g_vars = [var for var in train_vars if var.name.startswith('generator')] d_global_step = tf.Variable(0, name='d_global_step', trainable=False)
g_global_step = tf.Variable(0, name='d_global_step', trainable=False)
d_optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=self.beta1).minimize(d_loss, var_list=d_vars, global_step=d_global_step)
g_optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=self.beta1).minimize(g_loss, var_list=g_vars, global_step=g_global_step) saver = tf.train.Saver()
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
with tf.Session() as sess:
sess.run(init_op)
d_merged = tf.summary.merge([d_loss_sum, real_loss_sum, fake_loss_sum])
g_merged = tf.summary.merge([g_loss_sum])
writer = tf.summary.FileWriter('./logs', sess.graph)
ckpt = tf.train.get_checkpoint_state('./checkpoints')
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
print('Restore from model.ckpt')
else:
print('Checkpoint is not found!')
for epoch in range(self.epochs):
batches = self.data_manager.next_batch()
for batch in batches:
noises = np.random.uniform(-1, 1, size=(self.batch_size, self.z_len)).astype(np.float32)
_, d_summary, d_step = sess.run([d_optimizer, d_merged, d_global_step], feed_dict={real_ims: batch, noise_z: noises})
sess.run(g_optimizer, feed_dict={noise_z: noises})
_, g_summary, g_step = sess.run([g_optimizer, g_merged, g_global_step], feed_dict={noise_z: noises}) writer.add_summary(d_summary, d_step)
writer.add_summary(g_summary, g_step) loss_d, loss_real, loss_fake, loss_g = sess.run([d_loss, real_loss, fake_loss, g_loss], feed_dict={real_ims: batch, noise_z: noises}) print('Epoch: %s, Dis Step: %s, d_loss: %s, real_loss: %s, fake_loss: %s, Gen Step: %s, g_loss: %s' \
% (epoch, d_step, loss_d, loss_real, loss_fake, g_step, loss_g))
if g_step % 100 == 0:
saver.save(sess, './checkpoints/model.ckpt', global_step=g_step)
print('G Step %s Save model' % g_step) def gen(self):
noise_z = tf.placeholder(tf.float32, [None, self.z_len], name='noise_z')
sample_ims = self.generator(noise_z, is_training=False)
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
sample_noise = np.random.uniform(-1, 1, size=(self.sample_size, self.z_len))
ckpt = tf.train.get_checkpoint_state('./checkpoints')
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
samples = sess.run(sample_ims, feed_dict={noise_z: sample_noise})
for idx, sample in enumerate(samples):
self.data_manager.imwrite(idx, sample)
else:
print('Checkpoint is not found!')
return def data_load_test():
manager = DataManager()
batch = manager.next_batch()
im_list = next(batch)
for idx, im in enumerate(im_list):
manager.imwrite(idx, im) def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('--train', help='path to dataset')
parser.add_argument('--gen', help='path to store images')
args = parser.parse_args()
if args.train:
dcgan = DCGAN(args.train)
dcgan.train()
elif args.gen:
if args.gen == 'yes':
dcgan = DCGAN(None)
dcgan.gen()
else:
print('should be --gen yes')
else:
print('...') if __name__ == '__main__':
main()

DCGAN实现的更多相关文章

  1. 学习笔记GAN003:GAN、DCGAN、CGAN、InfoGAN

    ​GAN应用集中在图像生成,NLP.Robt Learning也有拓展.类似于NLP中的Actor-Critic. https://arxiv.org/pdf/1610.01945.pdf . Gen ...

  2. 学习笔记GAN004:DCGAN main.py

    Scipy 高端科学计算:http://blog.chinaunix.net/uid-21633169-id-4437868.html import os #引用操作系统函数文件 import sci ...

  3. DCGAN 论文简单解读

    DCGAN的全称是Deep Convolution Generative Adversarial Networks(深度卷积生成对抗网络).是2014年Ian J.Goodfellow 的那篇开创性的 ...

  4. DCGAN 代码简单解读

    之前在DCGAN文章简单解读里说明了DCGAN的原理.本次来实现一个DCGAN,并在数据集上实际测试它的效果.本次的代码来自github开源代码DCGAN-tensorflow,感谢carpedm20 ...

  5. 【深度学习】--DCGAN从入门到实例应用

    一.前述 DCGAN就是Deep Concolutions应用到GAN上,但是和传统的卷积应用还有一些区别,最大的区别就是没有池化层.本文将详细分析卷积在GAN上的应用. 二.具体 1.DCGAN和传 ...

  6. 用Tensorflow实现DCGAN

    1. GAN简介 最近几年,深度神经网络在图像识别.语音识别以及自然语言处理方面的应用有了爆炸式的增长,并且都达到了极高的准确率,某些方面甚至超过了人类的表现.然而人类的能力远超出图像识别和语音识别的 ...

  7. Tensorflow:DCGAN生成手写数字

    参考地址:https://blog.csdn.net/miracle_ma/article/details/78305991 使用DCGAN(deep convolutional GAN):深度卷积G ...

  8. 通过DCGAN进行生成花朵

    环境是你要安装Keras和Tensorflow 先来个network.py,里面定义了生成器网络和鉴别器网络: # -*- coding: UTF-8 -*- """ D ...

  9. 学习笔记GAN002:DCGAN

    Ian J. Goodfellow 论文:https://arxiv.org/abs/1406.2661 两个网络:G(Generator),生成网络,接收随机噪声Z,通过噪声生成样本,G(z).D( ...

  10. talk is cheap, show me the code——dcgan,wgan,wgan-gp的tensorflow实现

    最近学习了生成对抗网络(GAN),基于几个经典GAN网络结构做了些小实验,包括dcgan,wgan,wgan-gp.坦率的说,wgan,wgan-gp论文的原理还是有点小复杂,我也没有完全看明白,因此 ...

随机推荐

  1. Kubernetes容器化工具Kind实践部署Kubernetes v1.18.x 版本, 发布WordPress和MySQL

    Kind 介绍 Kind是Kubernetes In Docker的缩写,顾名思义是使用Docker容器作为Node并将Kubernetes部署至其中的一个工具.官方文档中也把Kind作为一种本地集群 ...

  2. XML解析---利用XStream解析xml数据及反构造Java对象

    XStream 是一个轻量级的.简单易用的开放源代码 Java库,用于将 Java 对象序列化为 XML 或者再转换回来.而且XStream还能将java对象转成其它格式,比如JSon. 需要用到的包 ...

  3. static关键字有何魔法?竟让Spring Boot搞出那么多静态内部类

    生命太短暂,不要去做一些根本没有人想要的东西.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习 ...

  4. Go Pentester - TCP Scanner

    Simple Port Scanner with Golang Use Go‘s net package: net.Dial(network, address string) package main ...

  5. 架构师都该懂的 CAP 定理

    面对可能出现的网络延迟,不可预估的请求流量等情况,设计一个分布式系统,我们通常围绕系统高可用,数据一致性的目标去规划和实现,想要完全实现这个目标,却并非易事.由此,分布式系统领域诞生了一个基本定理,即 ...

  6. 【java面试】- 集合篇

    Java 集合概览 从下图可以看出,在Java中除了以Map结尾的类之外, 其他类都实现了Collection接口.并且,以Map结尾的类都实现了Map接口 List.Set.Map三者的区别 Lis ...

  7. Java实现一个简单的文件上传案例

    Java实现一个简单的文件上传案例 实现流程: 1.客户端从硬盘读取文件数据到程序中 2.客户端输出流,写出文件到服务端 3.服务端输出流,读取文件数据到服务端中 4.输出流,写出文件数据到服务器硬盘 ...

  8. SQL查询基本用法

    -- 单列查询 select 编号 from employees -- 多列查询 select 编号,姓名 from employees -- 查询所有列 select * from employee ...

  9. 篮球30S定时器设计

    一.设计介绍 本设计采用74LS192作为计数器,74LS192具有同步加减计数功能,可以通过引脚电平对它设置达到清零重置的目的,可以达到对计数器清零的功能,使用两片级联74LS192分别显示十位和个 ...

  10. 1-Numpy的通用函数(ufunc)

    一.numpy“通用函数”(ufunc)包括以下几种: 元素级函数(一元函数):对数组中的每个元素进行运算 数组级函数:统计函数,像聚合函数(例如:求和.求平均) 矩阵运算 随机生成函数 常用一元通用 ...