def main(): with tf.variable_scope('input') as scope: # use variable instead of placeholder because we're training the intial image to make it # look like both the content image and the style image input_image = tf.Variable(np.zeros([1, IMAGE_HEIGHT, IMAGE_WIDTH, 3]), dtype=tf.float32) utils.download(VGG_DOWNLOAD_LINK, VGG_MODEL, EXPECTED_BYTES) model = vgg_model.load_vgg(VGG_MODEL, input_image) model['global_step'] = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') content_image = utils.get_resized_image(CONTENT_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) content_image = content_image - MEAN_PIXELS style_image = utils.get_resized_image(STYLE_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) style_image = style_image - MEAN_PIXELS model['content_loss'], model['style_loss'], model['total_loss'] = _create_losses(model, input_image, content_image, style_image) ############################### ## TO DO: create optimizer model['optimizer'] = tf.train.AdamOptimizer(LR).minimize(model['total_loss'], global_step=model['global_step']) ############################### model['summary_op'] = _create_summary(model) initial_image = utils.generate_noise_image(content_image, IMAGE_HEIGHT, IMAGE_WIDTH, NOISE_RATIO) train(model, input_image, initial_image)
def __init__(self, content_img, style_img, img_width, img_height): """ img_width and img_height are the dimensions we expect from the generated image. We will resize input content image and input style image to match this dimension. Feel free to alter any hyperparameter here and see how it affects your training. """ self.img_width = img_width self.img_height = img_height self.content_img = utils.get_resized_image(content_img, img_width, img_height) self.style_img = utils.get_resized_image(style_img, img_width, img_height) self.initial_img = utils.generate_noise_image(self.content_img, img_width, img_height) ############################### ## TO DO ## create global step (gstep) and hyperparameters for the model self.content_layer = "conv4_2" self.style_layers = [ "conv1_1", "conv2_1", "conv3_1", "conv4_1", "conv5_1" ] # content_w, style_w: corresponding weights for content loss and style loss self.content_w = 0.01 self.style_w = 1 # style_layer_w: weights for different style layers. deep layers have more weights self.style_layer_w = [0.5, 1.0, 1.5, 3.0, 4.0] self.gstep = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step") # global step self.lr = 2.0
def __init__(self, content_img, style_img, img_width, img_height): self.content_name = str(content_img.split("/")[-1].split(".")[0]) self.style_name = str(style_img.split("/")[-1].split(".")[0]) self.img_width = img_width self.img_height = img_height self.content_img = utils.get_resized_image(content_img, img_width, img_height) self.style_img = utils.get_resized_image(style_img, img_width, img_height) self.initial_img = utils.generate_noise_image(self.content_img, img_width, img_height) self.content_layer = "conv4_2" self.style_layers = [ "conv1_1", "conv2_1", "conv3_1", "conv4_1", "conv5_1" ] # 定义content loss和style loss的权重 self.content_w = 0.001 self.style_w = 1 self.style_layer_w = [0.5, 1.0, 1.5, 3.0, 4.0] self.gstep = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step") # global step self.lr = 2.0 utils.safe_mkdir("outputs/%s_%s" % (self.content_name, self.style_name))
def __init__(self, content_img, style_img, img_width, img_height): ''' img_width and img_height are the dimensions we expect from the generated image. We will resize input content image and input style image to match this dimension. Feel free to alter any hyperparameter here and see how it affects your training. ''' self.img_width = img_width self.img_height = img_height self.content_img = utils.get_resized_image(content_img, img_width, img_height) self.style_img = utils.get_resized_image(style_img, img_width, img_height) self.initial_img = utils.generate_noise_image(self.content_img, img_width, img_height) ############################### ## TO DO ## create global step (gstep) and hyperparameters for the model self.content_layer = 'conv4_2' self.style_layers = [ 'conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1' ] # content_w, style_w: corresponding weights for content loss and style loss self.content_w = None self.style_w = None # style_layer_w: weights for different style layers. deep layers have more weights self.style_layer_w = [0.5, 1.0, 1.5, 3.0, 4.0] self.gstep = None # global step self.lr = None
def main(): with tf.variable_scope('input') as scope: # use variable instead of placeholder because we're training the intial image to make it # look like both the content image and the style image input_image = tf.Variable(np.zeros([1, IMAGE_HEIGHT, IMAGE_WIDTH, 3]), dtype=tf.float32) utils.download(VGG_DOWNLOAD_LINK, VGG_MODEL, EXPECTED_BYTES) model = vgg_model.load_vgg(VGG_MODEL, input_image) model['global_step'] = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') content_image = utils.get_resized_image(CONTENT_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) content_image = content_image - MEAN_PIXELS style_image = utils.get_resized_image(STYLE_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) style_image = style_image - MEAN_PIXELS model['content_loss'], model['style_loss'], model[ 'total_loss'] = _create_losses(model, input_image, content_image, style_image) ############################### ## TO DO: create optimizer model['optimizer'] = tf.train.AdamOptimizer(learning_rate).minimize( model['total_loss'], global_step=model['global_step']) ############################### model['summary_op'] = _create_summary(model) initial_image = utils.generate_noise_image(content_image, IMAGE_HEIGHT, IMAGE_WIDTH, NOISE_RATIO) train(model, input_image, initial_image)
def train(): config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True sess = tf.Session(config=config) _, content_image = load_image(FLAGS.content_image, FLAGS.image_height, FLAGS.image_width) gen_image = generate_noise_image(content_image, 0.75, FLAGS.image_height, FLAGS.image_width, FLAGS.color_channels) input_image_list = np.reshape(gen_image, ((1, ) + gen_image.shape)) model = load_vgg_model(FLAGS.vgg_model_path, FLAGS.image_height, FLAGS.image_width, FLAGS.color_channels) sess.run(model['input'].assign(input_image_list)) train_step, total_loss = train_one_step(sess, model, FLAGS.image_height, FLAGS.image_width) sess.run(tf.global_variables_initializer()) for iter in range(FLAGS.iteration): sess.run(train_step) if iter % 50 == 0: mixed_image = sess.run(model['input']) print('Iteration {}: loss = {:e}'.format(iter, sess.run(total_loss))) if not os.path.exists(FLAGS.output): os.mkdir(FLAGS.output) filename = 'output/{}.png'.format(iter) save_image(filename, mixed_image[0])
def training(STYLE_IMAGE, CONTENT_IMAGE, LR, ITERS, NOISE_RATIO, model_load, trainable_signal): with tf.variable_scope('input') as scope: input_image = tf.Variable(np.zeros([1, IMAGE_HEIGHT, IMAGE_WIDTH, 3]), dtype=tf.float32) utils.download(VGG_DOWNLOAD_LINK, VGG_MODEL, EXPECTED_BYTES) model = vgg_model.load_vgg(VGG_MODEL, input_image) model['global_step'] = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') content_image = utils.get_resized_image(CONTENT_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) content_image = content_image - MEAN_PIXELS style_image = utils.get_resized_image(STYLE_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) style_image = style_image - MEAN_PIXELS model['content_loss'], model['style_loss'], model[ 'total_loss'] = _create_losses(model, input_image, content_image, style_image) model['optimizer'] = tf.train.AdamOptimizer(learning_rate=LR).minimize( model['total_loss'], global_step=model['global_step']) model['summary_op'] = _create_summary(model) initial_image = utils.generate_noise_image(content_image, IMAGE_HEIGHT, IMAGE_WIDTH, NOISE_RATIO) train(model, input_image, initial_image, ITERS, model_load, trainable_signal)
def __init__(self, content_img, style_img, img_width, img_height): """ 初始化 :param content_img: 待转换风格的图片(保留内容的图片) :param style_img: 风格图片(保留风格的图片) :param img_width: 图片的width :param img_height: 图片的height """ # 获取基本信息 self.content_name = str(content_img.split("/")[-1].split(".")[0]) self.style_name = str(style_img.split("/")[-1].split(".")[0]) self.img_width = img_width self.img_height = img_height # 规范化图片的像素尺寸 self.content_img = utils.get_resized_image(content_img, img_width, img_height) self.style_img = utils.get_resized_image(style_img, img_width, img_height) self.initial_img = utils.generate_noise_image(self.content_img, img_width, img_height) # 定义提取特征的层 self.content_layer = "conv4_2" self.style_layers = ["conv1_1", "conv2_1", "conv3_1", "conv4_1", "conv5_1"] # 定义content loss和style loss的权重 self.content_w = 0.001 self.style_w = 1 # 不同style layers的权重,层数越深权重越大 self.style_layer_w = [0.5, 1.0, 1.5, 3.0, 4.0] # global step和学习率 self.gstep = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step") # global step self.lr = 2.0 utils.safe_mkdir("outputs/%s_%s" % (self.content_name, self.style_name))
def generate_image(self): with tf.Session() as sess: # Now, we initialize the "generated" image as a noisy image created from the content_image. # By initializing the pixels of the generated image to be mostly noise but still slightly # correlated with the content image, this will help the content of the "generated" image # more rapidly match the content of the "content" image. noise_image = generate_noise_image(self.content_image) # Assign the content image to be the input of the VGG model. sess.run(self.model['input'].assign(self.content_image)) # Select the output tensor of layer conv4_2 out = self.model['conv4_2'] # Set a_C to be the hidden layer activation from the layer we have selected a_C = sess.run(out) a_G = out # Compute the content cost content_cost = compute_content_cost(a_C, a_G) # Assign the input of the model to be the "style" image sess.run(self.model['input'].assign(self.style_image)) # Compute the style cost style_cost = compute_style_cost(sess, self.model, STYLE_LAYERS) generated_image = self._train_iters(sess, (content_cost, style_cost), noise_image) tf.reset_default_graph() self._save_image(generated_image) return generated_image
def __init__(self, content_img, style_img, img_width, img_height): ''' img_width and img_height are the dimensions we expect from the generated image. We will resize input content image and input style image to match this dimension. Feel free to alter any hyperparameter here and see how it affects your training. ''' self.img_width = img_width self.img_height = img_height self.content_img = utils.get_resized_image(content_img, img_width, img_height) self.style_img = utils.get_resized_image(style_img, img_width, img_height) self.initial_img = utils.generate_noise_image(self.content_img, img_width, img_height) ############################### ## TO DO ## create global step (gstep) and hyperparameters for the model self.content_layer = 'conv4_2' self.style_layers = [ 'conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1' ] self.content_w = 0.1 self.style_w = 5 self.style_layer_w = [0.5, 1.0, 1.5, 3.0, 4.0] self.gstep = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') self.lr = 2.0
def main(): with tf.variable_scope('input') as scope: input_image = tf.Variable(np.zeros([1, IMAGE_HEIGHT, IMAGE_WIDTH, 3]), dtype=tf.float32) utils.download(VGG_DOWNLOAD_LINK, VGG_MODEL, EXPECTED_BYTES) utils.make_dir(CHECKPOINT_DIR) # utils.make_dir('outputs') utils.make_dir(OUT_DIR) model = vgg_model.load_vgg(VGG_MODEL, input_image) model['global_step'] = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') # because PIL is column major so need to change place of width and height content_image = utils.get_resized_image(CONTENT_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) content_image = content_image - MEAN_PIXELS style_image = utils.get_resized_image(STYLE_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) style_image = style_image - MEAN_PIXELS model['content_loss'], model['style_loss'], model[ 'total_loss'] = _create_losses(model, input_image, content_image, style_image) model['optimizer'] = tf.train.AdamOptimizer(LR).minimize( model['total_loss'], global_step=model['global_step']) model['summary_op'] = _create_summary(model) initial_image = utils.generate_noise_image(content_image, IMAGE_HEIGHT, IMAGE_WIDTH, NOISE_RATIO) train(model, input_image, initial_image)
def __init__(self, content_img, style_img, img_width, img_height): """ 初始化 :param content_img: 待转换风格的图片(保留内容的图片) :param style_img: 风格图片(保留风格的图片) :param img_width: 图片的width :param img_height: 图片的height """ # 获取基本信息 self.content_name = str(content_img.split("/")[-1].split(".")[0]) self.style_name = str(style_img.split("/")[-1].split(".")[0]) self.img_width = img_width self.img_height = img_height # 规范化图片的像素尺寸 self.content_img = utils.get_resized_image(content_img, img_width, img_height) self.style_img = utils.get_resized_image(style_img, img_width, img_height) self.initial_img = utils.generate_noise_image(self.content_img, img_width, img_height) # 定义提取特征的层 self.content_layer = "conv4_2" self.style_layers = [ "conv1_1", "conv2_1", "conv3_1", "conv4_1", "conv5_1" ] # 定义content loss和style loss的权重 self.content_w = 0.001 self.style_w = 1 # 不同style layers的权重,层数越深权重越大 self.style_layer_w = [0.5, 1.0, 1.5, 3.0, 4.0] # global step和学习率 self.gstep = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step") # global step self.lr = 2.0 utils.safe_mkdir("outputs/%s_%s" % (self.content_name, self.style_name))
def __init__(self, content_img, style_img, img_width, img_height): ''' img_width and img_height are the dimensions we expect from the generated image. We will resize input content image and input style image to match this dimension. Feel free to alter any hyperparameter here and see how it affects your training. ''' self.img_width = img_width self.img_height = img_height self.content_img = utils.get_resized_image(content_img, img_width, img_height) self.style_img = utils.get_resized_image(style_img, img_width, img_height) self.initial_img = utils.generate_noise_image(self.content_img, img_width, img_height) self.content_layer = 'conv4_2' self.style_layers = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'] self.content_w = 0.01 self.style_w = 1 self.style_layer_w = [0.5, 1.0, 1.5, 3.0, 4.0] self.gstep = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') self.lr = 2.0
def _image_processing(self): """ prepare images before starting to train """ logger.debug("setting up tf input pipeline ...") # image processing content_image = get_resized_image(self.params.content_image, self.params.image_height, self.params.image_width) self.content_image = content_image - MEAN_PIXELS style_image = get_resized_image(self.params.style_image, self.params.image_height, self.params.image_width) self.style_image = style_image - MEAN_PIXELS # this non tf variable will hold the final generated image self.initial_image = generate_noise_image(self.content_image, \ self.params.image_height, self.params.image_width, \ self.params.noise_ratio)
def main(): _setting_parameters() with tf.device('/cpu:0'): #Change to '/gpu:0' if having a GPU with tf.variable_scope('input') as scope: # use variable instead of placeholder because we're training the initial image to make it # look like both the content image and the style image input_image = tf.Variable(np.zeros( [1, IMAGE_HEIGHT, IMAGE_WIDTH, 3]), dtype=tf.float32) #Downloads the pretrained model utils.download(VGG_DOWNLOAD_LINK, VGG_MODEL, EXPECTED_BYTES) model = vgg_model.load_vgg(VGG_MODEL, input_image) model['global_step'] = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') content_image = utils.get_resized_image(CONTENT_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) content_image = content_image - MEAN_PIXELS style_image = utils.get_resized_image(STYLE_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) style_image = style_image - MEAN_PIXELS model['content_loss'], model['style_loss'], model[ 'total_loss'] = _create_losses(model, input_image, content_image, style_image) #Optimizer looking to reduce the total_loss of the model #TensorFlow optimizers: https://www.tensorflow.org/api_docs/python/tf/train model['optimizer'] = tf.train.AdamOptimizer(LR).minimize( model['total_loss'], global_step=model['global_step']) model['summary_op'] = _create_summary(model) initial_image = utils.generate_noise_image(content_image, IMAGE_HEIGHT, IMAGE_WIDTH, NOISE_RATIO) train(model, input_image, initial_image) #Showing the image one it is finished image_path = 'outputs/%d.png' % (ITERS - 1) utils.show_image(image_path)
def __init__(self, content_img, style_img, img_width, img_height): self.img_width = img_width self.img_height = img_height self.content_img = utils.get_resized_image(content_img, img_width, img_height) self.style_img = utils.get_resized_image(style_img, img_width, img_height) self.initial_img = utils.generate_noise_image(self.content_img, img_width, img_height) self.content_layer = 'conv4_2' self.style_layers = [ 'conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1' ] self.content_w = 0.001 self.style_w = 0.01 self.style_layer_w = [0.5, 1.0, 1.5, 3.0, 4.0] self.gstep = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') self.lr = 0.1
def style_transfer(cfg): with tf.variable_scope('input') as scope: # use variable instead of placeholder because we're training the intial image to make it # look like both the content image and the style image input_image = tf.Variable(np.zeros( [1, cfg.image_height, cfg.image_width, 3]), dtype=tf.float32) utils.download(VGG_DOWNLOAD_LINK, VGG_MODEL, EXPECTED_BYTES) utils.make_dir(cfg.checkpoints) utils.make_dir(cfg.outputs) model = vgg_model.load_vgg(VGG_MODEL, input_image) model['global_step'] = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') content_image = utils.get_resized_image(cfg.content_image, cfg.image_height, cfg.image_width) content_image = content_image - MEAN_PIXELS style_image = utils.get_resized_image(cfg.style_image, cfg.image_height, cfg.image_width) style_image = style_image - MEAN_PIXELS model['content_loss'], model['style_loss'], model[ 'total_loss'] = _create_losses(model, input_image, content_image, style_image, cfg) logging.info('model created') ############################### ## TO DO: create optimizer model['optimizer'] = tf.train.AdamOptimizer(cfg.lr).minimize( model['total_loss'], global_step=model['global_step']) ############################### model['summary_op'] = _create_summary(model) initial_image = utils.generate_noise_image(content_image, cfg.image_height, cfg.image_width, cfg.noise_ratio) train(model, input_image, initial_image, cfg)
def __init__(self, content_image, style_image, width, height, channel, content_w, style_w, training_steps, logging_steps=1): self.img_width = width self.img_height = height self.img_channel = channel self.input_image = None self.content = content_image.split('/')[-1][:-4] self.style = style_image.split('/')[-1][:-4] self.content_img = utils.image_resize(content_image, self.img_width, self.img_height, save=False) self.style_img = utils.image_resize(style_image, self.img_width, self.img_height, save=False) self.initial_img = utils.generate_noise_image(self.content_img, self.img_width, self.img_height) self.content_layer = ['conv4_2'] self.style_layers = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'] self.content_w = content_w self.style_w = style_w self.content_layer_w = [1.0] self.style_layer_w = [0.5, 1.0, 1.5, 3.0, 4.0] # [0.2] * 5 self.vgg19 = None # VGG19 model self.content_loss = 0. self.style_loss = 0. self.total_loss = 0. # Hyper-Parameters self.train_steps = training_steps self.logging_steps = logging_steps self.g_step = tf.Variable(0, trainable=False, name='global_steps') self.opt = None self.summary = None self.lr = 1. self.build()
def __init__(self, content_img, style_img, img_width, img_height): ''' img_width and img_height are the dimensions we expect from the generated image. We will resize input content image and input style image to match this dimension. Feel free to alter any hyperparameter here and see how it affects your training. ''' self.img_width = img_width self.img_height = img_height self.content_img = utils.get_resized_image(content_img, img_width, img_height) self.style_img = utils.get_resized_image(style_img, img_width, img_height) self.initial_img = utils.generate_noise_image(self.content_img, img_width, img_height) ############################### ## TO DO ## create global step (gstep) and hyperparameters for the model self.content_layer = 'conv4_2' self.style_layers = [ 'conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1' ] # content_w, style_w: corresponding weights for content loss and style loss self.content_w = 1 self.style_w = 20 # style_layer_w: weights for different style layers. deep layers have more weights self.style_layer_w = [0.5, 1.0, 1.5, 3.0, 4.0] self.gstep = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') starter_learning_rate = 0.1 self.lr = tf.train.exponential_decay(starter_learning_rate, self.gstep, 100000, 0.96, staircase=True)
def main(): """ main function to run the style transfer. """ parser = argparse.ArgumentParser() parser.add_argument("-s", "--style", type=str, help="style image") parser.add_argument("-c", "--content", type=str, help="content image") parser.add_argument("--height", type=int, default=255, help="image height") parser.add_argument("--width", type=int, default=333, help="image width") parser.add_argument("-nr", "--noise_ratio", type=int, default=0.5, help="noise ratio when combing images") args = parser.parse_args() CONTENT_IMAGE = args.content STYLE_IMAGE = args.style IMAGE_HEIGHT = args.height IMAGE_WIDTH = args.width NR = args.noise_ratio with tf.variable_scope("input"): # Define input image as variable, so we can directly # modify it to get the output image input_image = tf.Variable(np.zeros([1, IMAGE_HEIGHT, IMAGE_WIDTH, 3]), dtype=np.float32) # Download the model and make new directories utils.download_model(VGG_DOWNLOAD_LINK, VGG_MODEL, EXPECTED_BYTES) utils.make_dir("./checkpoints") utils.make_dir("./graphs") utils.make_dir("./outputs") # Initialize the model with given input image vgg = vgg_model.VGG_loader(file_path=VGG_MODEL) model = vgg.load_image(input_image) # Initialize the global step model["global_step"] = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step") content_image = utils.get_resized_image(CONTENT_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) content_image -= MEAN_PIXELS style_image = utils.get_resized_image(STYLE_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH) style_image -= MEAN_PIXELS model["content_loss"], model["style_loss"], model["total_loss"] = ( _create_losses(model, input_image, content_image, style_image)) # Define the optimizer model["optimizer"] = (tf.train.AdamOptimizer(LR).minimize( model["total_loss"])) # Define the summary op model["summary_op"] = _create_summary(model) # Generate initial image initial_image = (utils.generate_noise_image(content_image, IMAGE_HEIGHT, IMAGE_WIDTH, NR)) # Finally, run the optimizer to get the style transferred image train(model, input_image, initial_image)
def transfer(num_iterations=200): # Reset the graph tf.reset_default_graph() # Start interactive session sess = tf.InteractiveSession() # Initialize a noisy image by adding random noise to the content_image generated_image = generate_noise_image(content_image) # load VGG19 model model = load_vgg_model("imagenet-vgg-verydeep-19.mat") # Assign the content image to be the input of the VGG model. sess.run(model['input'].assign(content_image)) # Select the output tensor of layer conv4_2 out = model['conv4_2'] # Set a_C to be the hidden layer activation from the layer we have selected a_C = sess.run(out) # Set a_G to be the hidden layer activation from same layer. Here, a_G references model['conv4_2'] # and isn't evaluated yet. Later in the code, we'll assign the image G as the model input, so that # when we run the session, this will be the activations drawn from the appropriate layer, with G as input. a_G = out # Compute the content cost J_content = compute_content_cost(a_C, a_G) # Assign the input of the model to be the "style" image sess.run(model['input'].assign(style_image)) # Compute the style cost J_style = compute_style_cost(sess, model) J = total_cost(J_content, J_style, alpha, beta) # 10,40 # define optimizer (1 line) optimizer = tf.train.AdamOptimizer(2.0) # define train_step (1 line) train_step = optimizer.minimize(J) # Initialize global variables (you need to run the session on the initializer) sess.run(tf.global_variables_initializer()) # Run the noisy input image (initial generated image) through the model. Use assign(). sess.run(model["input"].assign(generated_image)) for i in range(num_iterations): # Run the session on the train_step to minimize the total cost sess.run(train_step) # Compute the generated image by running the session on the current model['input'] generated_image = sess.run(model["input"]) # Print every 20 iteration. if i % 20 == 0: Jt, Jc, Js = sess.run([J, J_content, J_style]) print("Iteration " + str(i) + " :") print("total cost = " + str(Jt)) print("content cost = " + str(Jc)) print("style cost = " + str(Js)) # save current generated image in the "/output" directory save_image("images/" + str(i) + ".png", generated_image) # save last generated image save_image("images/output.png", generated_image) return generated_image
tf.reset_default_graph() with tf.Session() as test: np.random.seed(3) J_content = np.random.randn() J_style = np.random.randn() J = total_cost(J_content, J_style) print(J) # 35.34667... tf.reset_default_graph() sess = tf.InteractiveSession() content_image = reshape_and_normalize_image(content_image) style_image = reshape_and_normalize_image(style_image) generated_image = generate_noise_image(content_image) plt.imshow(generated_image[0]) # plt.show() vgg = tf.keras.applications.VGG19( input_tensor=tf.convert_to_tensor(content_image, dtype='float32')) out = vgg.layers[4].output a_C = sess.run(out) a_G = out J_content = compute_content_cost(a_C, a_G) vgg = tf.keras.applications.VGG19( input_tensor=tf.convert_to_tensor(style_image, dtype='float32')) J_style = compute_total_style_cost(vgg, STYLE_LAYERS, sess)