看了这篇博客,决定实现以下DC-GAN:
博客地址:https://zhuanlan.zhihu.com/p/24767059
数据集地址也在这个博客中。
数据集jpg转pkl(joblib)代码:
import glob
import matplotlib.pyplot as plt
import cv2
import numpy as np
import joblib
imgs_dir_path = 'faces/'
pkl_file_path = 'faces_pkl/images.pkl'
imgs_dir = glob.glob(imgs_dir_path + '*.jpg')
imgs_cache = []
for i in range(len(imgs_dir)):
img = plt.imread(imgs_dir[i])
img = cv2.resize(img, (64,64), interpolation=cv2.INTER_CUBIC)
# plt.imshow(img)
# plt.show()
img = (img/255.0 - 0.5) * 2.0
imgs_cache.append(img)
end = 0
if i%1000 == 0:
print('we have already processed %d images'%(i))
imgs_pkl_data = np.array(imgs_cache, dtype=np.float64)
pkl_dict = {}
pkl_dict['data'] = imgs_pkl_data
with open(pkl_file_path, 'wb') as pkl_file:
joblib.dump(pkl_dict, pkl_file, protocol=4)
GAN实现代码:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
import joblib
import math
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
pkl_file_path = 'faces_pkl/images.pkl'
##discrimitive
def discrimitive(x, scope=None):
with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
conv1 = tf.layers.conv2d(inputs=x, kernel_size=5, strides=1, filters=32 ,activation=tf.nn.leaky_relu)
maxpool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=2, strides=2)
conv2 = tf.layers.conv2d(inputs=maxpool1, kernel_size=5, strides=1, filters=64,activation=tf.nn.leaky_relu)
maxpool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=2, strides=2)
h = maxpool2.get_shape()[1]
w = maxpool2.get_shape()[2]
c = maxpool2.get_shape()[3]
flatten = tf.reshape(maxpool2, shape=[-1,h*w*c])
fc1 = tf.layers.dense(inputs=flatten, units=1024, activation=tf.nn.leaky_relu)
logits = tf.layers.dense(inputs=fc1, units=1)
return logits
##generator
def generator(z, scope=None):
with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
fc1 = tf.layers.dense(inputs=z, units=1024, activation=tf.nn.relu)
bn1 = tf.layers.batch_normalization(inputs=fc1, training=True)
fc2 = tf.layers.dense(inputs=bn1, units=16 * 16 * 128, activation=tf.nn.relu)
bn2 = tf.layers.batch_normalization(inputs=fc2, training=True)
reshaped = tf.reshape(bn2, shape=[-1, 16, 16, 128])
conv_transpose1 = tf.layers.conv2d_transpose(inputs=reshaped, filters=64, kernel_size=4, strides=2,
activation=tf.nn.relu,
padding='same')
bn3 = tf.layers.batch_normalization(inputs=conv_transpose1, training=True)
conv_transpose2 = tf.layers.conv2d_transpose(inputs=bn3, filters=3, kernel_size=4, strides=2,
activation=tf.nn.tanh,
padding='same')
img = tf.reshape(conv_transpose2, shape=[-1, 64, 64, 3])
return img
dim = 784
batch_size = 100
iterations = 3000
learning_rate = 1e-3
beta1 = 0.5
##
x = tf.placeholder(dtype = tf.float32, shape=[None, 64, 64, 3])
z = tf.placeholder(dtype = tf.float32, shape=[None, 1024])
#####
labels_G = tf.placeholder(dtype=tf.float32, shape=[None, 1])
labels_D = tf.placeholder(dtype=tf.float32, shape=[None, 1])
logits_D = discrimitive(x, 'd')
logits_G1 = generator(z, 'g')
logits_G = discrimitive(logits_G1, 'd')
D_solver = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=beta1)
G_solver = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=beta1)
D_loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=labels_D, logits=logits_D)
G_loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=labels_G, logits=logits_G)
D_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'd')
G_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'g')
D_train_step = D_solver.minimize(D_loss, var_list=D_vars)
G_train_step = G_solver.minimize(G_loss, var_list=G_vars)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
##################################################
print('loading data')
with open(pkl_file_path, 'rb') as pkl_file:
pkl_dict = joblib.load(pkl_file)
imgs_pkl_data = pkl_dict['data']
imgs_num = imgs_pkl_data.shape[0]
batch_num = math.floor(imgs_num/batch_size)
################################################
for i in range(iterations):
zz = np.random.uniform(-1, 1, [batch_size, 1024])
feed_dict0 = {z: zz}
zzz = sess.run(logits_G1, feed_dict=feed_dict0)
batch_index = i % batch_num
x_this_batch = imgs_pkl_data[batch_index * batch_size:(batch_index + 1) * batch_size]
xx = x_this_batch
#discrimitive( 0 for real , 1 for fake)
labels1 = np.zeros([batch_size, 1])
labels2 = np.ones([batch_size, 1])
feed_dict1 = {x: xx, labels_D: labels1}
feed_dict2 = {x: zzz, labels_D: labels2}
sess.run(D_train_step, feed_dict=feed_dict1)
sess.run(D_train_step, feed_dict=feed_dict2)
#generator
feed_dict3 = {labels_G: labels1, z: zz}
sess.run(G_train_step, feed_dict=feed_dict3)
if i%20 == 0:
zzz = (zzz + 1.0)/2
zzz0 = zzz[0]
zzz1 = zzz[1]
zzz2 = zzz[2]
zzz3 = zzz[3]
plt.subplot(221)
plt.imshow(zzz0, cmap=plt.cm.gray)
plt.subplot(222)
plt.imshow(zzz1, cmap=plt.cm.gray)
plt.subplot(223)
plt.imshow(zzz2, cmap=plt.cm.gray)
plt.subplot(224)
plt.imshow(zzz3, cmap=plt.cm.gray)
plt.show()