一键上妆的BeautyGAN
一、参考及运行版本
- 源作者:张宏伦
- 知乎连接:https://www.zhihu.com/people/zhang-hong-lun
- GitHub:https://github.com/Honlan/BeautyGAN
- 原运行环境:Python 3.6 和 tensorflow1.9
二、本文运行版本
- 现运行环境:Python 3. 7 和tensorflow2.0
- 需要配置的文件
- 模型:https://drive.google.com/drive/folders/1pgVqnF2-rnOxcUQ3SO4JwHUFTdiSe5t9
- 代码:https://github.com/Honlan/BeautyGAN
三、异常处理
1.异常
(1)AttributeError: module ‘tensorflow’ has no attribute ‘reset_default_graph’
解决方法
#import tensorflow as tf
import tensorflow.compat.v1 as tf
(2)raise RuntimeError('The Session graph is empty. Add operations to the 'RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
解决方法
- 参考:https://github.com/OlafenwaMoses/ImageAI/issues/400
The thing is
The tensorflow core r2.0 have enabled eager execution by default so doesn't need to write tf.compat.v1.Session() and use .run() function
If we want to use tf.compat.v1.Session() then we need to do thi
tf.compat.v1.disable_eager_execution() in the starting of algorithm. Now we can use tf.compat.v1.Session() and .run() function.
Tensorflow core r2.0 have enabled eager execution by default. so, without changing it
we just have to change our code
# sess = tf.Session()
with tf.compat.v1.Session() as ses:
# sess = tf.Session()
# sess.run(tf.global_variables_initializer())
# saver = tf.train.import_meta_graph(os.path.join('model', 'model.meta'))
# saver.restore(sess, tf.train.latest_checkpoint('model'))
# graph = tf.get_default_graph()
# X = graph.get_tensor_by_name('X:0')
# Y = graph.get_tensor_by_name('Y:0')
# Xs = graph.get_tensor_by_name('generator/xs:0')
# for i in range(len(makeups)):
# makeup = cv2.resize(imread(makeups[i]), (img_size, img_size))
# Y_img = np.expand_dims(preprocess(makeup), 0)
# Xs_ = sess.run(Xs, feed_dict={X: X_img, Y: Y_img})
# Xs_ = deprocess(Xs_)
# result[:img_size, (i + 1) * img_size: (i + 2) * img_size] = makeup / 255.
# result[img_size: 2 * img_size, (i + 1) * img_size: (i + 2) * img_size] = Xs_[0]
#
# imsave('result.jpg', result)
with tf.compat.v1.Session() as ses:
# ses = tf.compat.v1.Session()
ses.run(tf.global_variables_initializer())
saver = tf.train.import_meta_graph(os.path.join('model', 'model.meta'))
saver.restore(ses, tf.train.latest_checkpoint('model'))
graph = tf.get_default_graph()
X = graph.get_tensor_by_name('X:0')
Y = graph.get_tensor_by_name('Y:0')
Xs = graph.get_tensor_by_name('generator/xs:0')
for i in range(len(makeups)):
makeup = cv2.resize(imread(makeups[i]), (img_size, img_size))
Y_img = np.expand_dims(preprocess(makeup), 0)
Xs_ = ses.run(Xs, feed_dict={X: X_img, Y: Y_img})
Xs_ = deprocess(Xs_)
result[:img_size, (i + 1) * img_size: (i + 2) * img_size] = makeup / 255.
result[img_size: 2 * img_size, (i + 1) * img_size: (i + 2) * img_size] = Xs_[0]
imsave('result.jpg', result)
四、完整代码
1.代码
# -*- coding: utf-8 -*-
# import tensorflow as tf
import tensorflow.compat.v1 as tf
import numpy as np
import os
import glob
from imageio import imread, imsave
import cv2
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--no_makeup', type=str, default=os.path.join('imgs', 'no_makeup', 'myP.jpg'), help='path to the no_makeup image')
args = parser.parse_args()
def preprocess(img):
return (img / 255. - 0.5) * 2
def deprocess(img):
return (img + 1) / 2
batch_size = 1
img_size = 256
no_makeup = cv2.resize(imread(args.no_makeup), (img_size, img_size))
X_img = np.expand_dims(preprocess(no_makeup), 0)
makeups = glob.glob(os.path.join('imgs', 'makeup', '*.*'))
result = np.ones((2 * img_size, (len(makeups) + 1) * img_size, 3))
result[img_size: 2 * img_size, :img_size] = no_makeup / 255.
# tf.compat.v1.reset_default_graph()
# sess = tf.compat.v1.Session()
# sess = tf.Session()
# sess.run(tf.global_variables_initializer())
# saver = tf.train.import_meta_graph(os.path.join('model', 'model.meta'))
# saver.restore(sess, tf.train.latest_checkpoint('model'))
# graph = tf.get_default_graph()
# X = graph.get_tensor_by_name('X:0')
# Y = graph.get_tensor_by_name('Y:0')
# Xs = graph.get_tensor_by_name('generator/xs:0')
# for i in range(len(makeups)):
# makeup = cv2.resize(imread(makeups[i]), (img_size, img_size))
# Y_img = np.expand_dims(preprocess(makeup), 0)
# Xs_ = sess.run(Xs, feed_dict={X: X_img, Y: Y_img})
# Xs_ = deprocess(Xs_)
# result[:img_size, (i + 1) * img_size: (i + 2) * img_size] = makeup / 255.
# result[img_size: 2 * img_size, (i + 1) * img_size: (i + 2) * img_size] = Xs_[0]
#
# imsave('result.jpg', result)
with tf.compat.v1.Session() as ses:
# ses = tf.compat.v1.Session()
ses.run(tf.global_variables_initializer())
saver = tf.train.import_meta_graph(os.path.join('model', 'model.meta'))
saver.restore(ses, tf.train.latest_checkpoint('model'))
graph = tf.get_default_graph()
X = graph.get_tensor_by_name('X:0')
Y = graph.get_tensor_by_name('Y:0')
Xs = graph.get_tensor_by_name('generator/xs:0')
for i in range(len(makeups)):
makeup = cv2.resize(imread(makeups[i]), (img_size, img_size))
Y_img = np.expand_dims(preprocess(makeup), 0)
Xs_ = ses.run(Xs, feed_dict={X: X_img, Y: Y_img})
Xs_ = deprocess(Xs_)
result[:img_size, (i + 1) * img_size: (i + 2) * img_size] = makeup / 255.
result[img_size: 2 * img_size, (i + 1) * img_size: (i + 2) * img_size] = Xs_[0]
imsave('result.jpg', result)
2.运行结果