def ffwd(data_in, paths_out, checkpoint_dir, device_t='/gpu:0', batch_size=4): # is_paths = type(data_in[0]) == str # if is_paths: # assert len(data_in) == len(paths_out) img_shape = get_img(data_in[0]).shape # else: # assert data_in.size[0] == len(paths_out) # img_shape = X[0].shape g = tf.Graph() batch_size = min(len(paths_out), batch_size) curr_num = 0 soft_config = tf.ConfigProto(allow_soft_placement=True) soft_config.gpu_options.allow_growth = True with g.as_default(), g.device(device_t), tf.Session( config=soft_config) as sess: batch_shape = (batch_size, ) + img_shape img_placeholder = tf.placeholder(tf.float32, shape=batch_shape, name='img_placeholder') preds = transform.net(img_placeholder) saver = tf.train.Saver() if os.path.isdir(checkpoint_dir): ckpt = tf.train.get_checkpoint_state(checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise Exception("No checkpoint found...") else: saver.restore(sess, checkpoint_dir) num_iters = int(len(paths_out) / batch_size) for i in range(num_iters): pos = i * batch_size curr_batch_out = paths_out[pos:pos + batch_size] # if is_paths: curr_batch_in = data_in[pos:pos + batch_size] X = np.zeros(batch_shape, dtype=np.float32) for j, path_in in enumerate(curr_batch_in): img = get_img(path_in) assert img.shape == img_shape X[j] = img # else: # X = data_in[pos:pos+batch_size] _preds = sess.run(preds, feed_dict={img_placeholder: X}) for j, path_out in enumerate(curr_batch_out): save_img(path_out, _preds[j]) remaining_in = data_in[num_iters * batch_size:] remaining_out = paths_out[num_iters * batch_size:] if len(remaining_in) > 0: ffwd(remaining_in, remaining_out, checkpoint_dir, device_t=device_t, batch_size=1)
def main(): parser = build_parser() options = parser.parse_args() check_opts(options) #check参数有没有问题 style_target = get_img(options.style) content_targets = list_files(options.train_path) #指定参数 kwargs = { "epochs": options.epochs, "print_iterations": options.checkpoint_iterations, "batch_size": options.batch_size, "checkpoint_dir": os.path.join(options.checkpoint_dir, 'fns.ckpt'), "learning_rate": options.learning_rate } args = [ content_targets, style_target, options.content_weight, options.style_weight, options.tv_weight, options.vgg_path ] #注意for循环, i = iterations for preds, losses, i, epoch in optimize(*args, **kwargs): #提出来单个 #need more than 3 values to unpack style_loss, content_loss, tv_loss, loss = losses print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss)) to_print = (style_loss, content_loss, tv_loss) print('style: %s, content: %s, tv: %s' % to_print)
def main(): parser = build_parser() options = parser.parse_args() check_opts(options) style_target = get_img(options.style) kwargs = { 'slow': options.slow, 'epochs': options.epochs, 'print_iterations': options.checkpoint_iterations, 'batch_size': options.batch_size, 'save_path': os.path.join(options.checkpoint_dir, 'fns.ckpt'), 'learning_rate': options.learning_rate } args = [ content_targets, style_target, options.content_weight, options.style_weight, options.tv_weight, options.vgg_path ] for preds, losses, i, epoch in optimize(*args, **kwargs): style_loss, content_loss, loss = losses print
def optimize(loss, content_image_paths, batch_size=4, learning_rate=1e-3, epochs=20, shoud_print=False, save_path='saver/fns.ckpt'): batch_shape = (batch_size,256,256,3) with tf.Session() as sess: optimizer = tf.train.AdadeltaOptimizer(learning_rate).minimize(loss) sess.run(tf.global_variables_initializer()) for epoch in xrange(epochs): num_examples = len(content_image_paths) iterations = 0 # the input should have been randomized while iterations * batch_size < num_examples: curr = iterations*batch_size step = curr + batch_size X_batch = np.zeros(batch_shape, dtype=np.float32) for j, path in enumerate(content_image_paths[curr: step]): X_batch[j] = utils.get_img(path, img_size=(256, 256, 3)).astype(np.float32) iterations += 1 optimizer.run(feed_dict={'X_content_images:0': X_batch}) saver = tf.train.Saver() saver.save(sess, save_path) if shoud_print: inferred_loss = sess.run([loss], feed_dict={'X_content_images:0': X_batch}) print inferred_loss
def main(): parser = build_parser() options = parser.parse_args() check_opts(options) content_img = get_img(options.content, (256, 256, 3)).astype(np.float32) content_img = np.reshape(content_img, (1, ) + content_img.shape) prediction = ffwd(content_img, options.style) save_img(options.output_path, prediction) print('Image saved to {}'.format(options.output_path))
def main(): parser = build_parser() options = parser.parse_args() check_opts(options) style_target = get_img(options.style) if not options.slow: content_targets = _get_files(options.train_path) elif options.test: content_targets = [options.test] kwargs = { "slow": options.slow, "epochs": options.epochs, "print_iterations": options.checkpoint_iterations, "batch_size": options.batch_size, "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'), "learning_rate": options.learning_rate, "checkpoint_restore": options.checkpoint_restore } if options.slow: if options.epochs < 10: kwargs['epochs'] = 1000 if options.learning_rate < 1: kwargs['learning_rate'] = 1e1 args = [ content_targets, style_target, options.content_weight, options.style_weight, options.tv_weight, options.vgg_path ] for preds, losses, i, epoch in optimize(*args, **kwargs): style_loss, content_loss, tv_loss, loss = losses print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss)) to_print = (style_loss, content_loss, tv_loss) print('style: %s, content:%s, tv: %s' % to_print) if options.test: assert options.test_dir is not False preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i) if not options.slow: ckpt_dir = os.path.dirname(options.checkpoint_dir) evaluate.ffwd_to_img(options.test, preds_path, options.checkpoint_dir) else: save_img(preds_path, img) ckpt_dir = options.checkpoint_dir cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir print("Training complete. For evaluation:\n `%s`" % cmd_text)
def main(): parser = build_parser() options = parser.parse_args() # 获取命令行参数 check_opts(options) # 参数检查 style_target = get_img(options.style) # 三通道风格图,大小不固定 if not options.slow: content_targets = _get_files(options.train_path) elif options.test: content_targets = [options.test] kwargs = { "slow": options.slow, "epochs": options.epochs, # 训练多少轮,所有的训练数据集都训练过一次称为一个epoch,即一轮 "print_iterations": options.checkpoint_iterations, "batch_size": options. batch_size, # 批处理。一组多少张,itertions=epoch/batch即迭代一轮的迭代次数, epochs*(epoch/batch)即为总迭代次数 "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'), "learning_rate": options.learning_rate } # !!! if options.slow: # !!! if options.epochs < 10: kwargs['epochs'] = 1000 if options.learning_rate < 1: kwargs['learning_rate'] = 1e1 args = [ content_targets, style_target, options.content_weight, options.style_weight, options.tv_weight, options.vgg_path ] # 待使用参数 for preds, losses, i, epoch in optimize(*args, **kwargs): # 迭代!!! style_loss, content_loss, tv_loss, loss = losses print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss)) to_print = (style_loss, content_loss, tv_loss) print('style: %s, content:%s, tv: %s' % to_print) if options.test: assert options.test_dir != False preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i) if not options.slow: ckpt_dir = os.path.dirname(options.checkpoint_dir) evaluate.ffwd_to_img(options.test, preds_path, options.checkpoint_dir) else: save_img( preds_path, preds.reshape((512, 512, 3)) ) ####131 错误save_img(preds_path,img),I found a fix for this. Replace img with preds.reshape((512,512,3)) ckpt_dir = options.checkpoint_dir cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir print("Training complete. For evaluation:\n `%s`" % cmd_text)
def ffwd_different_dimensions(self, in_path, out_path, checkpoint_dir, device_t=DEVICE, batch_size=4): in_path_of_shape = defaultdict(list) out_path_of_shape = defaultdict(list) for i in range(len(in_path)): in_image = in_path[i] out_image = out_path[i] shape = "%dx%dx%d" % get_img(in_image).shape in_path_of_shape[shape].append(in_image) out_path_of_shape[shape].append(out_image) for shape in in_path_of_shape: print('Processing images of shape %s' % shape) self.ffwd(in_path_of_shape[shape], out_path_of_shape[shape], checkpoint_dir, device_t, batch_size)
def optimize(content_targets, style_target, content_weight, style_weight, tv_weight, vgg_path, epochs=2, print_iterations=1000, batch_size=4, save_path='saver/fns.ckpt', slow=False, learning_rate=1e-3, debug=False, checkpoint_restore=False): if slow: batch_size = 1 mod = len(content_targets) % batch_size if mod > 0: print("Train set has been trimmed slightly..") content_targets = content_targets[:-mod] style_features = {} batch_shape = (batch_size, 256, 256, 3) style_shape = (1, ) + style_target.shape print(style_shape) # precompute style features with tf.Graph().as_default(), tf.device( '/cpu:0'), tf.compat.v1.Session() as sess: style_image = tf.compat.v1.placeholder(tf.float32, shape=style_shape, name='style_image') style_image_pre = src.vgg.preprocess(style_image) net = src.vgg.net(vgg_path, style_image_pre) style_pre = np.array([style_target]) for layer in STYLE_LAYERS: features = net[layer].eval(feed_dict={style_image: style_pre}) features = np.reshape(features, (-1, features.shape[3])) gram = np.matmul(features.T, features) / features.size style_features[layer] = gram with tf.Graph().as_default(), tf.compat.v1.Session() as sess: X_content = tf.compat.v1.placeholder(tf.float32, shape=batch_shape, name="X_content") X_pre = src.vgg.preprocess(X_content) # precompute content features content_features = {} content_net = src.vgg.net(vgg_path, X_pre) content_features[CONTENT_LAYER] = content_net[CONTENT_LAYER] if slow: preds = tf.Variable( tf.random.normal(X_content.get_shape()) * 0.256) preds_pre = preds else: preds = src.transform.net(X_content / 255.0) preds_pre = src.vgg.preprocess(preds) net = src.vgg.net(vgg_path, preds_pre) content_size = _tensor_size( content_features[CONTENT_LAYER]) * batch_size assert _tensor_size(content_features[CONTENT_LAYER]) == _tensor_size( net[CONTENT_LAYER]) content_loss = content_weight * ( 2 * tf.nn.l2_loss(net[CONTENT_LAYER] - content_features[CONTENT_LAYER]) / content_size) style_losses = [] for style_layer in STYLE_LAYERS: layer = net[style_layer] bs, height, width, filters = map(lambda i: i, layer.get_shape()) size = height * width * filters feats = tf.reshape(layer, (bs, height * width, filters)) feats_T = tf.transpose(a=feats, perm=[0, 2, 1]) grams = tf.matmul(feats_T, feats) / size style_gram = style_features[style_layer] style_losses.append(2 * tf.nn.l2_loss(grams - style_gram) / style_gram.size) style_loss = style_weight * functools.reduce(tf.add, style_losses) / batch_size # total variation denoising tv_y_size = _tensor_size(preds[:, 1:, :, :]) tv_x_size = _tensor_size(preds[:, :, 1:, :]) y_tv = tf.nn.l2_loss(preds[:, 1:, :, :] - preds[:, :batch_shape[1] - 1, :, :]) x_tv = tf.nn.l2_loss(preds[:, :, 1:, :] - preds[:, :, :batch_shape[2] - 1, :]) tv_loss = tv_weight * 2 * (x_tv / tv_x_size + y_tv / tv_y_size) / batch_size loss = content_loss + style_loss + tv_loss # overall loss train_step = tf.compat.v1.train.AdamOptimizer(learning_rate).minimize( loss) sess.run(tf.compat.v1.global_variables_initializer()) # If there is an existing checkpoint, load it to continue optimizing saver = tf.compat.v1.train.Saver() try: if checkpoint_restore: if os.path.isdir(save_path): ckpt = tf.train.get_checkpoint_state(save_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise Exception("No checkpoint found...") else: saver.restore(sess, save_path) except: print("Starting optimization from scratch") import random uid = random.randint(1, 100) print("UID: %s" % uid) for epoch in range(epochs): num_examples = len(content_targets) iterations = 0 while iterations * batch_size < num_examples: start_time = time.time() curr = iterations * batch_size step = curr + batch_size X_batch = np.zeros(batch_shape, dtype=np.float32) for j, img_p in enumerate(content_targets[curr:step]): X_batch[j] = get_img(img_p, (256, 256, 3)).astype(np.float32) iterations += 1 assert X_batch.shape[0] == batch_size feed_dict = {X_content: X_batch} train_step.run(feed_dict=feed_dict) end_time = time.time() delta_time = end_time - start_time if debug: print("UID: %s, batch time: %s" % (uid, delta_time)) is_print_iter = int(iterations) % print_iterations == 0 if slow: is_print_iter = epoch % print_iterations == 0 is_last = epoch == epochs - 1 and iterations * batch_size >= num_examples should_print = is_print_iter or is_last if should_print: to_get = [style_loss, content_loss, tv_loss, loss, preds] test_feed_dict = {X_content: X_batch} tup = sess.run(to_get, feed_dict=test_feed_dict) _style_loss, _content_loss, _tv_loss, _loss, _preds = tup losses = (_style_loss, _content_loss, _tv_loss, _loss) if slow: _preds = src.vgg.unprocess(_preds) else: res = saver.save(sess, save_path) yield (_preds, losses, iterations, epoch)
import tensorflow as tf import src.utils as utils import src.loss_calculation as loss_calculation import src.train as train import os style_image_path = 'data/style/wave.jpg' content_image_path = 'data/content_images/' vgg_model_path = 'data/imagenet-vgg-verydeep-19.mat' content_weight = 7.5e0 style_weight = 1e2 tv_weight = 2e2 style_image = utils.get_img(style_image_path) def _get_files(img_dir): files = utils.list_files(img_dir) return [os.path.join(img_dir, x) for x in files] saver_path = 'saver/' try: os.mkdir(saver_path) except Exception: pass flags = tf.app.flags FLAGS = flags.FLAGS flags.DEFINE_bool('log_device_placement', False, 'checkout device') flags.DEFINE_bool('should_print', True, 'whether we should print loss during training')
with g.as_default(), g.device("cpu:0"), tf.Session(config=soft_config) as sess: batch_shape = (1,) + image_as_array.shape batch = np.zeros(batch_shape, dtype=np.float32) batch[0,:,:,:] = image_as_array img_placeholder = tf.placeholder(tf.float32, shape=batch_shape, name='img_placeholder') preds = transform.net(img_placeholder) saver = tf.train.Saver() if os.path.isdir(checkpoint_dir): ckpt = tf.train.get_checkpoint_state(checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise Exception("No checkpoint found...") else: saver.restore(sess, checkpoint_dir) _preds = sess.run(preds, feed_dict={img_placeholder: batch}) return _preds[0] if __name__=="__main__": imagepath = "./examples/content/stata.jpg" checkpoint_dir = r"C:\Users\adity_000\Desktop\checkpoints" image = get_img(imagepath) import matplotlib.pyplot as plt res = get_output(image, checkpoint_dir) img = np.clip(res, 0, 255).astype(np.uint8) plt.imshow(img) plt.show()
# TensorFlow SavedModel, ready to serve. g = tf.Graph() soft_config = tf.ConfigProto(allow_soft_placement=True) soft_config.gpu_options.allow_growth = True with g.as_default(), tf.Session(config=soft_config) as sess: img_placeholder = tf.placeholder(tf.float32, shape=(1, 256, 256, 3), name='img_placeholder') preds = transform.net(img_placeholder) saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state('ckpt') saver.restore(sess, ckpt.model_checkpoint_path) # Load image (can use any image, just need one arbitrary run of model) img = get_img('images/input/input_italy.jpg', (256, 256, 3)) X = np.zeros((1, 256, 256, 3), dtype=np.float32) X[0] = img # run _preds = sess.run(preds, feed_dict={img_placeholder: X}) # If you want to freeze your graph instead of outputting a tensorflow # SavedModel, uncomment code and comment out all code below # frozen_graph_def = tf.graph_util.convert_variables_to_constants( # sess, # sess.graph_def, # ['add_37']) # # # Save the frozen graph # with open('output_graph.pb', 'wb') as f:
def _get_files(img_dir): files = utils.list_files(img_dir) return files with tf.Session() as sess: batch_shape = (batch_size, 256, 256, 3) X_content_images = tf.placeholder(tf.float32, shape=batch_shape, name='X_content_images') preds = style_transfer_conv_net.net(X_content_images / 255.) saver = tf.train.Saver() saver.restore(sess, model_checkpoint_path) content_images = _get_files(content_image_path) num_images = 50 num_iter = int(num_images / batch_size) for i in xrange(num_iter): selected_files = content_images[i * batch_size:(i + 1) * batch_size] X = np.zeros(batch_shape, dtype=np.float32) output_image_paths = [] index = 0 for index, selected_file in enumerate(selected_files): full_image_path = os.path.join(content_image_path, selected_file) output_image_paths.append( os.path.join(generated_image_path, selected_file)) X[index] = utils.get_img(full_image_path, img_size=(256, 256, 3)) generated_images = sess.run(preds, feed_dict={'X_content_images:0': X}) for index, output_image_path in enumerate(output_image_paths): utils.save_img(output_image_path, generated_images[index])
def main(): parser = build_parser() options = parser.parse_args() check_opts(options) if not options.slow: content_targets = _get_files(options.train_path) elif options.test: content_targets = [options.test] style_files = [] if os.path.isfile(options.style): style_files.extend(options.style) else: style_files = _get_files(options.style) for style_file in style_files: print("-------------Started to train2014 model for style '%s'" % os.path.basename(style_file)) style_target = get_img(style_file) checkpoint_dir = "checkpoint_" + os.path.splitext( os.path.basename(style_file))[0] if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) test_dir = "test_" + os.path.splitext(os.path.basename(style_file))[0] if options.test: if not os.path.exists(test_dir): os.makedirs(test_dir) kwargs = { "slow": options.slow, "epochs": options.epochs, "print_iterations": options.checkpoint_iterations, "batch_size": options.batch_size, "save_path": checkpoint_dir, "learning_rate": options.learning_rate } if options.slow: if options.epochs < 10: kwargs['epochs'] = 1000 if options.learning_rate < 1: kwargs['learning_rate'] = 1e1 args = [ content_targets, style_target, options.content_weight, options.style_weight, options.tv_weight, options.vgg_path ] for preds, losses, i, epoch in optimize(*args, **kwargs): style_loss, content_loss, tv_loss, loss = losses print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss)) to_print = (style_loss, content_loss, tv_loss) print('style: %s, content:%s, tv: %s' % to_print) if options.test: # assert options.test_dir != False preds_path = '%s/%s_%s.png' % (test_dir, epoch, i) if not options.slow: ckpt_dir = os.path.dirname(checkpoint_dir) evaluate.ffwd_to_img(options.test, preds_path, checkpoint_dir) else: save_img(preds_path, preds) ckpt_dir = checkpoint_dir cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir print("Training complete. For evaluation:\n `%s`" % cmd_text)
def main(): parser = build_parser() options = parser.parse_args() check_opts(options) # Load style and content images style_target = get_img(options.style) style_seg = get_img(options.style_seg) if not options.slow: content_targets = _get_files(options.train_path) elif options.test: content_targets = [options.test] kwargs = { "slow": options.slow, "epochs": options.epochs, "print_iterations": options.checkpoint_iterations, "batch_size": options.batch_size, "save_path": os.path.join(options.checkpoint_dir, 'fns.ckpt'), "learning_rate": options.learning_rate } if options.slow: if options.epochs < 10: kwargs['epochs'] = 1000 if options.learning_rate < 1: kwargs['learning_rate'] = 1e1 args = [ content_targets, # Batch image paths style_target, # Reference style *image* style_seg, # Reference style segmentation map *image* options.content_weight, options.style_weight, options.tv_weight, options.photo_weight, options.vgg_path, options.deeplab_path, options. resized_dir, # Batch image resized folder (intermediate from deeplab) options. seg_dir, # Batch image segmentation folder (intermediate from deeplab) options.matting_dir ] for preds, losses, i, epoch in optimize(*args, **kwargs): style_loss, content_loss, tv_loss, photo_loss, loss = losses print('Epoch %d, Iteration: %d, Loss: %s' % (epoch, i, loss)) to_print = (style_loss, content_loss, tv_loss, photo_loss) print('style: %s, content:%s, tv: %s, photo: %s' % to_print) if options.test: assert options.test_dir != False preds_path = '%s/%s_%s.png' % (options.test_dir, epoch, i) if not options.slow: ckpt_dir = os.path.dirname(options.checkpoint_dir) evaluate.ffwd_to_img(options.test, preds_path, options.checkpoint_dir) else: save_img(preds_path, img) ckpt_dir = options.checkpoint_dir cmd_text = 'python evaluate.py --checkpoint %s ...' % ckpt_dir print("Training complete. For evaluation:\n `%s`" % cmd_text)
import src.utils as utils import os content_image_path = 'data/content_images/' resized_content_image_path = 'data/resized_content_images/' def _get_files(img_dir): files = utils.list_files(img_dir) return files content_image_paths = _get_files(content_image_path) for filename in content_image_paths: fullpath = os.path.join(content_image_path, filename) print fullpath image = utils.get_img(fullpath, img_size=(256, 256, 3)) output_path = os.path.join(resized_content_image_path, filename) print output_path utils.save_img(output_path, image)
def optimize(content_targets, style_target, content_weight, style_weight, tv_weight, vgg_path, epochs=2, print_iterations=1000, batch_size=4, save_path='saver/fns.ckpt', slow=False, learning_rate=1e-3, debug=False): #!!!只进行两次前向? if slow: batch_size = 1 mod = len( content_targets) % batch_size #训练图像一次四张batch_size=4,训练集图片数量应为4的倍数 if mod > 0: print("Train set has been trimmed slightly..") #训练集被轻微修剪 content_targets = content_targets[:-mod] #去掉余数 style_features = {} #!!! ''' .shape=(HWC) style_shape=(1,图片垂直尺寸,图片水平尺寸,图片通道数) ''' batch_shape = (batch_size, 256, 256, 3) style_shape = (1, ) + style_target.shape #BHWC print("所训练的style image属性(图片垂直尺寸/图片水平尺寸/图片通道数):" + style_target.shape) # precompute style features预计算的风格特征 with tf.Graph().as_default(), tf.device( '/cpu:0'), tf.Session() as sess: #!!!cpu:0??? ''' tf.placeholder(): dtype:数据类型。常用的是tf.float32,tf.float64等数值类型 shape:数据形状。默认是None,就是一维值,也可以是多维:(batch_size,图片垂直尺寸/图片水平尺寸/图片通道数) name:名称 ''' style_image = tf.placeholder(tf.float32, shape=style_shape, name='style_image') #!!!session style_image_pre = vgg.preprocess(style_image) #1.将风格图标准化!!!图像-均值 net = vgg.net(vgg_path, style_image_pre) #2.风格图标准化后并进入vgg style_pre = np.array([style_target]) #风格图的矩阵形式 ''' 取出vgg中过程的特征图,即不同阶段被卷积后的特征图 ''' for layer in STYLE_LAYERS: #取特定层 features = net[layer].eval(feed_dict={ style_image: style_pre }) #喂style_pre给session即给style_image赋值为style_pre,拿到特征图 features = np.reshape(features, (-1, features.shape[3])) #!!! gram = np.matmul(features.T, features) / features.size #计算gram值!!!!!!!!!!!!! style_features[layer] = gram ''' 取出vgg中过程的内容图,即relu4_2 ''' with tf.Graph().as_default(), tf.Session() as sess: X_content = tf.placeholder(tf.float32, shape=batch_shape, name="X_content") #一次四张内容图 X_pre = vgg.preprocess(X_content) #标准化 # precompute content features content_features = {} content_net = vgg.net(vgg_path, X_pre) content_features[CONTENT_LAYER] = content_net[ CONTENT_LAYER] #relu4_2 batch=4 ''' content_features:不经过生成网络的内容图 preds_pre---net:经过生成网络的内容图,即中间图 ''' if slow: preds = tf.Variable( tf.random_normal(X_content.get_shape()) * 0.256) preds_pre = preds else: preds = transform.net(X_content / 255.0) #归一化,float化,经过生产网络残差网络,也是batch=4 preds_pre = vgg.preprocess(preds) #再经过vgg net = vgg.net(vgg_path, preds_pre) content_size = _tensor_size( content_features[CONTENT_LAYER]) * batch_size assert _tensor_size(content_features[CONTENT_LAYER]) == _tensor_size( net[CONTENT_LAYER]) ''' Loss(Content)内容损失函数 ''' content_loss = content_weight * ( 2 * tf.nn.l2_loss(net[CONTENT_LAYER] - content_features[CONTENT_LAYER]) / content_size) ''' Loss(Style)风格损失函数 grams:经过生成网络的内容图,即中间图进行Gram style_gram:上面算过的不经过生成网络的特征图gram ''' style_losses = [] for style_layer in STYLE_LAYERS: layer = net[style_layer] bs, height, width, filters = map(lambda i: i.value, layer.get_shape()) size = height * width * filters feats = tf.reshape(layer, (bs, height * width, filters)) feats_T = tf.transpose(feats, perm=[0, 2, 1]) grams = tf.matmul(feats_T, feats) / size style_gram = style_features[style_layer] style_losses.append(2 * tf.nn.l2_loss(grams - style_gram) / style_gram.size) style_loss = style_weight * functools.reduce( tf.add, style_losses) / batch_size #Loss(Style)风格损失函数 # total variation denoising tv_y_size = _tensor_size(preds[:, 1:, :, :]) tv_x_size = _tensor_size(preds[:, :, 1:, :]) y_tv = tf.nn.l2_loss(preds[:, 1:, :, :] - preds[:, :batch_shape[1] - 1, :, :]) x_tv = tf.nn.l2_loss(preds[:, :, 1:, :] - preds[:, :, :batch_shape[2] - 1, :]) tv_loss = tv_weight * 2 * (x_tv / tv_x_size + y_tv / tv_y_size) / batch_size #去噪loss值 ''' 总的loss ''' loss = content_loss + style_loss + tv_loss # overall loss ''' 梯度下降 ''' train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss) sess.run(tf.global_variables_initializer()) import random uid = random.randint(1, 100) print("UID: %s" % uid) for epoch in range(epochs): # num_examples = len(content_targets) iterations = 0 while iterations * batch_size < num_examples: #即一轮迭代 start_time = time.time() curr = iterations * batch_size step = curr + batch_size X_batch = np.zeros(batch_shape, dtype=np.float32) for j, img_p in enumerate( content_targets[curr:step]): #每batch_size个即4个一组 X_batch[j] = get_img(img_p, (256, 256, 3)).astype(np.float32) iterations += 1 assert X_batch.shape[0] == batch_size feed_dict = {X_content: X_batch} train_step.run(feed_dict=feed_dict) end_time = time.time() delta_time = end_time - start_time if debug: print("UID: %s, batch time: %s" % (uid, delta_time)) is_print_iter = int(iterations) % print_iterations == 0 if slow: is_print_iter = epoch % print_iterations == 0 ''' 判断到了设置的print_iterations轮数 和判断是做完最后一轮迭代 进行过程打印:should_print = is_print_iter or is_last ''' is_last = epoch == epochs - 1 and iterations * batch_size >= num_examples should_print = is_print_iter or is_last #打印 if should_print: to_get = [style_loss, content_loss, tv_loss, loss, preds] test_feed_dict = {X_content: X_batch} tup = sess.run(to_get, feed_dict=test_feed_dict) _style_loss, _content_loss, _tv_loss, _loss, _preds = tup losses = (_style_loss, _content_loss, _tv_loss, _loss) if slow: _preds = vgg.unprocess(_preds) else: saver = tf.train.Saver() res = saver.save(sess, save_path) #保存迭代打印内容 yield (_preds, losses, iterations, epoch) #返回值
def ffwd(data_in, paths_out, checkpoint_dir, device_t='/gpu:0', batch_size=4): start_ffwd = time.time() assert len(paths_out) > 0 is_paths = type(data_in[0]) == str if is_paths: assert len(data_in) == len(paths_out) img_shape = get_img(data_in[0]).shape else: assert data_in.size[0] == len(paths_out) img_shape = X[0].shape g = tf.Graph() batch_size = min(len(paths_out), batch_size) soft_config = tf.compat.v1.ConfigProto(allow_soft_placement=True) soft_config.gpu_options.allow_growth = True with g.as_default(), g.device(device_t), \ tf.compat.v1.Session(config=soft_config) as sess: batch_shape = (batch_size, ) + img_shape img_placeholder = tf.compat.v1.placeholder(tf.float32, shape=batch_shape, name='img_placeholder') preds = transform.net(img_placeholder) saver = tf.compat.v1.train.Saver() if os.path.isdir(checkpoint_dir): ckpt = tf.train.get_checkpoint_state(checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise Exception("No checkpoint found...") else: saver.restore(sess, checkpoint_dir) num_iters = int(len(paths_out) / batch_size) for i in range(num_iters): pos = i * batch_size curr_batch_out = paths_out[pos:pos + batch_size] if is_paths: curr_batch_in = data_in[pos:pos + batch_size] X = np.zeros(batch_shape, dtype=np.float32) for j, path_in in enumerate(curr_batch_in): img = get_img(path_in) assert img.shape == img_shape, \ 'Images have different dimensions. ' + \ 'Resize images or use --allow-different-dimensions.' X[j] = img else: X = data_in[pos:pos + batch_size] _preds = sess.run(preds, feed_dict={img_placeholder: X}) for j, path_out in enumerate(curr_batch_out): save_img(path_out, _preds[j]) remaining_in = data_in[num_iters * batch_size:] remaining_out = paths_out[num_iters * batch_size:] if len(remaining_in) > 0: ffwd(remaining_in, remaining_out, checkpoint_dir, device_t=device_t, batch_size=1) time_needed = time.time() - start_ffwd print("ffwd function worked {:.4} seconds, file={} shape={}".format( time_needed, 0, img_shape))
def ffwd(data_in, paths_out, checkpoint_dir, device_t='/gpu:0', batch_size=4): assert len(paths_out) > 0 is_paths = type(data_in[0]) == str if is_paths: assert len(data_in) == len(paths_out) img_shape = get_img(data_in[0]).shape else: assert data_in.size[0] == len(paths_out) img_shape = X[0].shape g = tf.Graph() batch_size = min(len(paths_out), batch_size) curr_num = 0 soft_config = tf.ConfigProto(allow_soft_placement=True) soft_config.gpu_options.allow_growth = True with g.as_default(), g.device(device_t), tf.Session( config=soft_config) as sess: batch_shape = (batch_size, ) + img_shape img_placeholder = tf.placeholder(tf.float32, shape=batch_shape, name='img_placeholder') preds = transform.net(img_placeholder) saver = tf.train.Saver() if os.path.isdir(checkpoint_dir): ckpt = tf.train.get_checkpoint_state(checkpoint_dir) if ckpt: saver.restore(sess, ckpt) else: os.makedirs("fst_checkpoints", exist_ok=True) ckpt = os.path.dirname("fst_checkpoints") print(ckpt, "variable ckpt status") print("...model checkpoints directory created...") else: saver.restore(sess, checkpoint_dir) num_iters = int(len(paths_out) / batch_size) for i in range(num_iters): pos = i * batch_size curr_batch_out = paths_out[pos:pos + batch_size] if is_paths: curr_batch_in = data_in[pos:pos + batch_size] X = np.zeros(batch_shape, dtype=np.float32) for j, path_in in enumerate(curr_batch_in): img = get_img(path_in) assert img.shape == img_shape, \ 'Images have different dimensions. ' + \ 'Resize images or use --allow-different-dimensions.' X[j] = img else: X = data_in[pos:pos + batch_size] # to fix error 'tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized # value Variable_47' sess.run(tf.compat.v1.global_variables_initializer()) sess.run(tf.local_variables_initializer()) _preds = sess.run(preds, feed_dict={img_placeholder: X}) for j, path_out in enumerate(curr_batch_out): save_img(path_out, _preds[j]) remaining_in = data_in[num_iters * batch_size:] remaining_out = paths_out[num_iters * batch_size:] if len(remaining_in) > 0: ffwd(remaining_in, remaining_out, checkpoint_dir, device_t=device_t, batch_size=1)