def cartoonize(load_folder, save_folder, model_path): input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) saver.restore(sess, tf.train.latest_checkpoint(model_path)) name_list = os.listdir(load_folder) for name in tqdm(name_list): try: load_path = os.path.join(load_folder, name) save_path = os.path.join(save_folder, name) image = cv2.imread(load_path) image = resize_crop(image) batch_image = image.astype(np.float32) / 127.5 - 1 batch_image = np.expand_dims(batch_image, axis=0) output = sess.run(final_out, feed_dict={input_photo: batch_image}) output = (np.squeeze(output) + 1) * 127.5 output = np.clip(output, 0, 255).astype(np.uint8) cv2.imwrite(save_path, output) except: print('cartoonize {} failed'.format(load_path))
def load_model(): ss = time.time() model_path = 'saved_models' input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars) print("loading model...............") config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) saver.restore(sess, tf.train.latest_checkpoint(model_path)) print("model loaded.................") print("model loading time ", str(time.time() - ss)) return sess, input_photo, final_out
def load_model(self, weights_dir, gpu): try: tf.disable_eager_execution() except: None tf.reset_default_graph() self.input_photo = tf.placeholder(tf.float32, [1, None, None, 3], name='input_image') network_out = network.unet_generator(self.input_photo) self.final_out = guided_filter.guided_filter(self.input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars) if gpu: gpu_options = tf.GPUOptions(allow_growth=True) device_count = {'GPU':1} else: gpu_options = None device_count = {'GPU':0} config = tf.ConfigProto(gpu_options=gpu_options, device_count=device_count) self.sess = tf.Session(config=config) self.sess.run(tf.global_variables_initializer()) saver.restore(self.sess, tf.train.latest_checkpoint(weights_dir))
def cartoonize(image_name): # print(img_file.name) model_path = "saved_models" input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) saver.restore(sess, tf.train.latest_checkpoint(model_path)) try: image = cv2.imread("test_images/"+image_name) image = resize_crop(image) batch_image = image.astype(np.float32)/127.5 - 1 batch_image = np.expand_dims(batch_image, axis=0) output = sess.run(final_out, feed_dict={input_photo: batch_image}) output = (np.squeeze(output)+1)*127.5 output = np.clip(output, 0, 255).astype(np.uint8) cv2.imwrite("cartoonized_images/"+image_name, output) return True except Exception as e: print(e) return False
def worker(input_q, output_q): input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) model_path = "/opt/project/cartoonize-inference/saved_models/" sess.run(tf.global_variables_initializer()) saver.restore(sess, tf.train.latest_checkpoint(model_path)) while True: try: frame = input_q.get() image = resize_crop(frame[1]) batch_image = image.astype(np.float32) / 127.5 - 1 batch_image = np.expand_dims(batch_image, axis=0) output = sess.run(final_out, feed_dict={input_photo: batch_image}) output = (np.squeeze(output) + 1) * 127.5 output = np.clip(output, 0, 255).astype(np.uint8) output_q.put((frame[0], output)) except: print('cartoonize failed')
def cartoonize(name, load_folder, save_folder, sess): global_sess = initialize_model(MODEL_PATH) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars) global_sess.run(tf.global_variables_initializer()) saver.restore(global_sess, tf.train.latest_checkpoint(model_path)) input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) # name_list = os.listdir(load_folder) inference_times = [ ] start_time = time.time() try: load_path = os.path.join(load_folder, name) save_path = os.path.join(save_folder, name) image = cv2.imread(load_path) image = resize_crop(image) batch_image = image.astype(np.float32)/127.5 - 1 batch_image = np.expand_dims(batch_image, axis=0) output = sess.run(final_out, feed_dict={input_photo: batch_image}) output = (np.squeeze(output)+1)*127.5 output = np.clip(output, 0, 255).astype(np.uint8) cv2.imwrite(save_path, output) except: print('cartoonize {} failed'.format(load_path)) print(f"The inference time is {time.time() - start_time} seconds")
def cartoonize(load_folder, save_folder, model_path): input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) saver.restore(sess, tf.train.latest_checkpoint(model_path)) name_list = os.listdir(load_folder) for name in tqdm(name_list): try: load_path = os.path.join(load_folder, name) save_path = os.path.join(save_folder, name) image = cv2.imread(load_path) image = cv2.resize_crop(image) batch_image = image.astype(np.float32)/127.5 - 1#The purpose of doing so is to normalize and scale the pixel data. #The activation function of the output layer of the generator is tanh, which returns a value between -1 and 1. To scale that to 0 and 255 (which are the values you expect for an image), we have to multiply it by 127.5 (so that -1 becomes -127.5, and 1 becomes 127.5), and then add 127.5 (so that -127.5 becomes 0, and 127.5 becomes 255). We then have to do the inverse of this when feeding an image into the discriminator (which will expect a value between -1 and 1). batch_image = np.expand_dims(batch_image, axis=0) #The expand_dims() function is used to expand the shape of an array. Insert a new axis that will appear at the axis position in the expanded array shape. output = sess.run(final_out, feed_dict={input_photo: batch_image}) output = (np.squeeze(output)+1)*127.5 output = np.clip(output, 0, 255).astype(np.uint8) cv2.imwrite(save_path, output) except: print('cartoonize {} failed'.format(load_path))
def cartoonize_photo(photo, model_path=MODEL_PATH): input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) saver.restore(sess, tf.train.latest_checkpoint(model_path)) image = resize_crop(photo) batch_image = image.astype(np.float32) / 127.5 - 1 batch_image = np.expand_dims(batch_image, axis=0) output = sess.run(final_out, feed_dict={input_photo: batch_image}) output = (np.squeeze(output) + 1) * 127.5 output = np.clip(output, 0, 255).astype(np.uint8) return output
def cartoonize(img_name, load_folder, save_folder, model_path): input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if "generator" in var.name] saver = tf.train.Saver(var_list=gene_vars) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) saver.restore(sess, tf.train.latest_checkpoint(model_path)) name_list = os.listdir(load_folder) load_path = os.path.join(load_folder, img_name) print(load_path) save_path = os.path.join(save_folder, img_name) print(save_path) image = cv2.imread(load_path) image = resize_crop(image) batch_image = image.astype(np.float32) / 127.5 - 1 batch_image = np.expand_dims(batch_image, axis=0) output = sess.run(final_out, feed_dict={input_photo: batch_image}) output = (np.squeeze(output) + 1) * 127.5 output = np.clip(output, 0, 255).astype(np.uint8) # adding watermark to image # logo = cv2.imread('images/pyimage.png') # (wH, wW) = logo.shape[:2] # (h, w) = output.shape[:2] # # output = np.dstack([output, np.ones((h, w), dtype="uint8") * 255]) # # overlay = np.zeros((h, w, 3), dtype="uint8") # center_y = int(h / 2) # center_x = int(w / 2) # top_y = center_y - int(wH / 2) # left_x = center_x - int(wW / 2) # bottom_y = top_y + h # right_x = left_x + w # roi = output[top_y: bottom_y, left_x: right_x] # # overlay[h - wH - 10:h - 10, w - wW - 10:w - 10] = logo # # print(overlay.shape) # water_output = output.copy() # print(water_output.shape) # print(roi.shape) # result = cv2.addWeighted(roi, 0.25, water_output, 1.0, 0) # img[top_y: bottom_y, left_x: right_x] = result cv2.imwrite(save_path, output)
def cartoonize(load_folder, save_folder, model_path): input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if "generator" in var.name] saver = tf.train.Saver(var_list=gene_vars) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) saver.restore(sess, tf.train.latest_checkpoint(model_path)) video_file = Path(load_folder).is_file() # check if it is a video if video_file: name_list = read_video(load_folder) metadata = get_video_meta_data(load_folder) total = metadata["nframes"] fps = metadata["fps"] duration = metadata["duration"] # print(total, metadata, fps * duration) if total == float("inf"): total = int(fps * duration) else: name_list = os.listdir(load_folder) total = len(name_list) for i, name in enumerate(tqdm(name_list, total=total)): # try: if video_file: image = name image = image[..., ::-1] save_path = os.path.join(save_folder, f"frame{i:05d}.png") else: load_path = os.path.join(load_folder, name) save_path = os.path.join(save_folder, name) image = cv2.imread(load_path) image = resize_crop(image) batch_image = image.astype(np.float32) / 127.5 - 1 batch_image = np.expand_dims(batch_image, axis=0) output = sess.run(final_out, feed_dict={input_photo: batch_image}) output = (np.squeeze(output) + 1) * 127.5 output = np.clip(output, 0, 255).astype(np.uint8) cv2.imwrite(save_path, output)
def cartoonize(load_folder, save_folder, model_path): input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) saver.restore(sess, tf.train.latest_checkpoint(model_path)) name_list = os.listdir(load_folder) my_list = name_list def divide_chunks(l, n): for i in range(0, len(l), n): yield l[i:i + n] n = 11 name_list_chunks = list(divide_chunks(my_list, n)) total_iteration = len(name_list_chunks) print(f"Total image is {len(name_list)}") print(f"Total iteration is {total_iteration}") count = 0 for i in name_list_chunks: print(f"Iteration {count}") for name in tqdm(i): try: # print(f"Image name : {name}") load_path = os.path.join(load_folder, name) save_path = os.path.join(save_folder, name) image = cv2.imread(load_path) image = resize_crop(image) batch_image = image.astype(np.float32) / 127.5 - 1 batch_image = np.expand_dims(batch_image, axis=0) output = sess.run(final_out, feed_dict={input_photo: batch_image}) output = (np.squeeze(output) + 1) * 127.5 output = np.clip(output, 0, 255).astype(np.uint8) cv2.imwrite(save_path, output) except: print('cartoonize {} failed'.format(load_path)) count += 1
def cartoonize_realtime(load_folder, save_folder, model_path): input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) saver.restore(sess, tf.train.latest_checkpoint(model_path)) return sess, final_out, input_photo
def __init__(self): basedir = os.path.abspath(os.path.dirname(__file__)) model_path = os.path.join(basedir, "..", "model", "saved_models") self.input_photo = tf.placeholder(tf.float32, [1, None, None, 3]) network_out = network.unet_generator(self.input_photo) self.final_out = guided_filter.guided_filter(self.input_photo, network_out, r=1, eps=5e-3) all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars) config = tf.ConfigProto() config.gpu_options.allow_growth = True self.session = tf.Session() self.session.run(tf.global_variables_initializer()) saver.restore(self.session, tf.train.latest_checkpoint(model_path))
def train(args): input_photo = tf.placeholder( tf.float32, [args.batch_size, args.patch_size, args.patch_size, 3]) input_superpixel = tf.placeholder( tf.float32, [args.batch_size, args.patch_size, args.patch_size, 3]) input_cartoon = tf.placeholder( tf.float32, [args.batch_size, args.patch_size, args.patch_size, 3]) # output=>fake picture output = network.unet_generator(input_photo) # output = guided_filter(input_photo, output, r=1) blur_fake = guided_filter(output, output, r=5, eps=2e-1) blur_cartoon = guided_filter(input_cartoon, input_cartoon, r=5, eps=2e-1) gray_fake, gray_cartoon = utils.color_shift(output, input_cartoon) d_loss_gray, g_loss_gray = loss.lsgan_loss(network.disc_sn, gray_cartoon, gray_fake, scale=1, patch=True, name='disc_gray') d_loss_blur, g_loss_blur = loss.lsgan_loss(network.disc_sn, blur_cartoon, blur_fake, scale=1, patch=True, name='disc_blur') vgg_model = loss.Vgg19('vgg19_no_fc.npy') vgg_photo = vgg_model.build_conv4_4(input_photo) vgg_output = vgg_model.build_conv4_4(output) vgg_superpixel = vgg_model.build_conv4_4(input_superpixel) h, w, c = vgg_photo.get_shape().as_list()[1:] photo_loss = tf.reduce_mean( tf.losses.absolute_difference(vgg_photo, vgg_output)) / (h * w * c) superpixel_loss = tf.reduce_mean(tf.losses.absolute_difference\ (vgg_superpixel, vgg_output))/(h*w*c) recon_loss = photo_loss + superpixel_loss tv_loss = loss.total_variation_loss(output) g_loss_total = 1e4 * tv_loss + 1e-1 * g_loss_blur + g_loss_gray + 2e2 * recon_loss d_loss_total = d_loss_blur + d_loss_gray all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'gene' in var.name] disc_vars = [var for var in all_vars if 'disc' in var.name] tf.summary.scalar('tv_loss', tv_loss) tf.summary.scalar('photo_loss', photo_loss) tf.summary.scalar('superpixel_loss', superpixel_loss) tf.summary.scalar('recon_loss', recon_loss) tf.summary.scalar('d_loss_gray', d_loss_gray) tf.summary.scalar('g_loss_gray', g_loss_gray) tf.summary.scalar('d_loss_blur', d_loss_blur) tf.summary.scalar('g_loss_blur', g_loss_blur) tf.summary.scalar('d_loss_total', d_loss_total) tf.summary.scalar('g_loss_total', g_loss_total) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): g_optim = tf.train.AdamOptimizer(args.adv_train_lr, beta1=0.5, beta2=0.99)\ .minimize(g_loss_total, var_list=gene_vars) d_optim = tf.train.AdamOptimizer(args.adv_train_lr, beta1=0.5, beta2=0.99)\ .minimize(d_loss_total, var_list=disc_vars) ''' config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) ''' gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=args.gpu_fraction) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) train_writer = tf.summary.FileWriter(args.save_dir + '/train_log') summary_op = tf.summary.merge_all() saver = tf.train.Saver(var_list=gene_vars, max_to_keep=20) with tf.device('/device:GPU:0'): sess.run(tf.global_variables_initializer()) saver.restore(sess, tf.train.latest_checkpoint('pretrain/saved_models')) face_photo_dir = 'dataset/photo_face' face_photo_list = utils.load_image_list(face_photo_dir) scenery_photo_dir = 'dataset/photo_scenery' scenery_photo_list = utils.load_image_list(scenery_photo_dir) face_cartoon_dir = 'dataset/cartoon_face' face_cartoon_list = utils.load_image_list(face_cartoon_dir) scenery_cartoon_dir = 'dataset/cartoon_scenery' scenery_cartoon_list = utils.load_image_list(scenery_cartoon_dir) for total_iter in tqdm(range(args.total_iter)): if np.mod(total_iter, 5) == 0: photo_batch = utils.next_batch(face_photo_list, args.batch_size) cartoon_batch = utils.next_batch(face_cartoon_list, args.batch_size) else: photo_batch = utils.next_batch(scenery_photo_list, args.batch_size) cartoon_batch = utils.next_batch(scenery_cartoon_list, args.batch_size) inter_out = sess.run(output, feed_dict={ input_photo: photo_batch, input_superpixel: photo_batch, input_cartoon: cartoon_batch }) ''' adaptive coloring has to be applied with the clip_by_value in the last layer of generator network, which is not very stable. to stabiliy reproduce our results, please use power=1.0 and comment the clip_by_value function in the network.py first If this works, then try to use adaptive color with clip_by_value. ''' if args.use_enhance: superpixel_batch = utils.selective_adacolor(inter_out, power=1.2) else: superpixel_batch = utils.simple_superpixel(inter_out, seg_num=200) _, g_loss, r_loss = sess.run( [g_optim, g_loss_total, recon_loss], feed_dict={ input_photo: photo_batch, input_superpixel: superpixel_batch, input_cartoon: cartoon_batch }) _, d_loss, train_info = sess.run( [d_optim, d_loss_total, summary_op], feed_dict={ input_photo: photo_batch, input_superpixel: superpixel_batch, input_cartoon: cartoon_batch }) train_writer.add_summary(train_info, total_iter) if np.mod(total_iter + 1, 50) == 0: print('Iter: {}, d_loss: {}, g_loss: {}, recon_loss: {}'.\ format(total_iter, d_loss, g_loss, r_loss)) if np.mod(total_iter + 1, 500) == 0: saver.save(sess, args.save_dir + '/saved_models/model', write_meta_graph=False, global_step=total_iter) photo_face = utils.next_batch(face_photo_list, args.batch_size) cartoon_face = utils.next_batch(face_cartoon_list, args.batch_size) photo_scenery = utils.next_batch(scenery_photo_list, args.batch_size) cartoon_scenery = utils.next_batch(scenery_cartoon_list, args.batch_size) result_face = sess.run(output, feed_dict={ input_photo: photo_face, input_superpixel: photo_face, input_cartoon: cartoon_face }) result_scenery = sess.run(output, feed_dict={ input_photo: photo_scenery, input_superpixel: photo_scenery, input_cartoon: cartoon_scenery }) utils.write_batch_image( result_face, args.save_dir + '/images', str(total_iter) + '_face_result.jpg', 4) utils.write_batch_image( photo_face, args.save_dir + '/images', str(total_iter) + '_face_photo.jpg', 4) utils.write_batch_image( result_scenery, args.save_dir + '/images', str(total_iter) + '_scenery_result.jpg', 4) utils.write_batch_image( photo_scenery, args.save_dir + '/images', str(total_iter) + '_scenery_photo.jpg', 4)
tf.disable_eager_execution() model_path = './White-box-Cartoonization/test_code/saved_models' tf.reset_default_graph() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: #Create placeholder for the input input_photo = tf.placeholder(tf.float32, [1, None, None, 3], name='input_photo') #Run the input placeholder through the generator, and then apply a filter to process the generator output network_out = network.unet_generator(input_photo) final_out = guided_filter.guided_filter(input_photo, network_out, r=1, eps=5e-3) final_out = tf.identity( final_out, name='final_output') #Create an identity filtering layer创建一个身份过滤层 #The above process is basically needed to construct the computation graph for the current session # 基本上需要上述过程来构造当前会话的计算图 #Get the genrator variables and restore the pre-trained chechpoints in the current session all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'generator' in var.name] saver = tf.train.Saver(var_list=gene_vars)
def train(args): input_photo = tf.placeholder( tf.float32, [args.batch_size, args.patch_size, args.patch_size, 3]) output = network.unet_generator(input_photo) recon_loss = tf.reduce_mean( tf.losses.absolute_difference(input_photo, output)) tf.summary.scalar('recon_loss', recon_loss) summary_op = tf.summary.merge_all() all_vars = tf.trainable_variables() gene_vars = [var for var in all_vars if 'gene' in var.name] update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): optim = tf.train.AdamOptimizer(args.adv_train_lr, beta1=0.5, beta2=0.99)\ .minimize(recon_loss, var_list=gene_vars) ''' config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) ''' gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=args.gpu_fraction) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) saver = tf.train.Saver(var_list=gene_vars, max_to_keep=20) summary_writer = tf.summary.FileWriter(args.save_dir + 'save_models/model', tf.get_default_graph()) with tf.device('/device:GPU:0'): sess.run(tf.global_variables_initializer()) face_photo_dir = 'dataset/photo_face' face_photo_list = utils.load_image_list(face_photo_dir) scenery_photo_dir = 'dataset/photo_scenery' scenery_photo_list = utils.load_image_list(scenery_photo_dir) for total_iter in tqdm(range(args.total_iter)): if np.mod(total_iter, 5) == 0: photo_batch = utils.next_batch(face_photo_list, args.batch_size) else: photo_batch = utils.next_batch(scenery_photo_list, args.batch_size) _, r_loss, summary_str = sess.run( [optim, recon_loss, summary_op], feed_dict={input_photo: photo_batch}) summary_writer.add_summary(summary_str, global_step=total_iter) if np.mod(total_iter + 1, 50) == 0: print('pretrain, iter: {}, recon_loss: {}'.format( total_iter, r_loss)) if np.mod(total_iter + 1, 500) == 0: saver.save(sess, args.save_dir + 'save_models/model', write_meta_graph=False, global_step=total_iter) photo_face = utils.next_batch(face_photo_list, args.batch_size) photo_scenery = utils.next_batch(scenery_photo_list, args.batch_size) result_face = sess.run(output, feed_dict={input_photo: photo_face}) result_scenery = sess.run( output, feed_dict={input_photo: photo_scenery}) utils.write_batch_image( result_face, args.save_dir + '/images', str(total_iter) + '_face_result.jpg', 4) utils.write_batch_image( photo_face, args.save_dir + '/images', str(total_iter) + '_face_photo.jpg', 4) utils.write_batch_image( result_scenery, args.save_dir + '/images', str(total_iter) + '_scenery_result.jpg', 4) utils.write_batch_image( photo_scenery, args.save_dir + '/images', str(total_iter) + '_scenery_photo.jpg', 4)