Disclaimer: All belongs to the world proletariat all https://blog.csdn.net/qq_41776781/article/details/86651799
the main function of the entry:
# 程序的入口 main函数
if __name__ == '__main__':
# 程序开始首先指定使用那块GPU 不使用该语句的时候使用所有的GPU
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
# 这个是用来将原来的celeba转化成多大
global width, high, batch_size, z_dim
width = 64
high = 64
batch_size = 128
z_dim = 100
# 定义一个输出生成图像的文件夹
sample_dir = 'Result/Celeba_ACGAN_DCGAN/'
if not os.path.exists(sample_dir):
os.makedirs(sample_dir)
else:
print("*********输出文件夹已经存在是否继续执行**************")
train()
Train () function to load the training data and generates image celeba
def train():
# 将celeba图像转化numpy的形式
image = get_image()
z_samples = load_sample()
loss_d_list = []
loss_g_list = []
# 定义真实图像的占位符
real_image = tf.placeholder(dtype=tf.float32, shape=[batch_size, width, high, 3], name='X')
noise = tf.placeholder(dtype=tf.float32, shape=[batch_size, z_dim], name='noise')
is_training = tf.placeholder(dtype=tf.bool, name='is_training')
fake_image = generator(noise)
d_real = discriminator(real_image)
d_fake = discriminator(fake_image, reuse=True)
# loss
loss_d = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=d_real, labels=tf.ones_like(d_real))) + tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=d_fake, labels=tf.zeros_like(d_fake)))
loss_g = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=d_fake, labels=tf.ones_like(d_fake)))
vars_g = [var for var in tf.trainable_variables() if var.name.startswith('generator')]
vars_d = [var for var in tf.trainable_variables() if var.name.startswith('discriminator')]
optim_d = tf.train.AdamOptimizer(learning_rate=0.0002, beta1=0.5).minimize(loss_d, var_list=vars_d)
optim_g = tf.train.AdamOptimizer(learning_rate=0.0002, beta1=0.5).minimize(loss_g, var_list=vars_g)
# 定义sess.run开始会话和允许GPU的自动分配
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
sess.run(tf.global_variables_initializer())
for i in range(50001):
image_batch = get_random_batch(image)
n = np.random.uniform(-1.0, 1.0, [batch_size, z_dim]).astype(np.float32)
# 定义生成器和识别器的优化器 这里面选用的是AdamOptimizer
_, d_ls = sess.run([optim_d, loss_d], feed_dict={real_image: image_batch, noise: n, is_training: True})
_, g_ls = sess.run([optim_g, loss_g], feed_dict={noise: n, is_training: True})
# 保存训练的loss数据
loss_d_list.append(d_ls)
loss_g_list.append(g_ls)
print("", i, d_ls, g_ls)
if i == 0:
# 目的是检测生成图像是正确
print("**************显示真实图像**************")
image_batch = (image_batch + 1)/ 2
real_imgs = [real_img[:, :, :] for real_img in image_batch[0:64]]
image_batch = imsave_image(real_imgs)
plt.axis('off')
imsave(os.path.join(sample_dir, 'real_%5d.jpg' % i), image_batch)
if i % 100 == 0:
gen_imgs = sess.run(fake_image, feed_dict={noise: z_samples, is_training: False})
gen_imgs = (gen_imgs + 1) / 2
# 2019 1 14生成的图像是8 * 8的
imgs = [img[:, :, :] for img in gen_imgs[19:83]]
gen_imgs = imsave_image(imgs)
plt.axis('off')
imsave(os.path.join(sample_dir, 'sample_%5d.jpg' % i), gen_imgs)