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 = 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 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 = self.content_img ############################### ## 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 = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step') # global step self.lr = 2.0 self.gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.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) ############################### ## 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 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 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'] = ... ############################### 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' ] 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 __init__(self, content_img, style_img, img_height, img_width): self.content_img = get_resized_image(content_img, img_width, img_height) self.style_img = get_resized_image(style_img, img_width, img_height) self.initial_img = init_random_image(self.content_img, img_width, img_height) self.img_height = img_height self.img_width = img_width #hyperparamters self.lr = 2.0 self.global_step = tf.get_variable('global_step', initializer=tf.constant(0), trainable=False) self.content_layer = 'conv4_2' self.style_layers = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'] self.style_layer_w = [0.5, 0.5, 0.5, 0.5, 0.5] # The alpha and beta hyperparameters that weight the total loss function self.content_w = 1e2 self.style_w = 1e-2
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_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 imgloader(dataq, batch_sz, imgs, tmpdir, seed, tid, patch_sz): qctr = 0 curidx = 0 # order for going through the images np.random.seed(seed) imgsord=np.random.permutation(len(imgs['filename'])) # sample this many grids per image num_grids = 4 gridctr = 0 # storage for the sampled batch perm = [] label = [] pats = [] # index within the current batch j = 0; # keep returning batches forever. Each iteration of this loop # samples one grid of patches from an image. while True: # if we've already sampled num_grids in this image, we sample a new one. if(gridctr==0): while True: try: im=ut.get_resized_image(imgsord[curidx % len(imgs['filename'])], imgs, {"gri_targpixels":random.randint(150000,450000)}) except: curidx = (curidx + 1) % (len(imgsord)) print("broken image id " + str(curidx)) continue; curidx = (curidx + 1) % (len(imgsord)); if(im.shape[0] > patch_sz[0] * 2 + gap + noise and im.shape[1] > patch_sz[1] * 2 + gap + noise): break gridctr = num_grids; # compute where the grid starts, and then comptue its size. gridstartx = random.randint(0, patch_sz[1] + gap - 1) gridstarty = random.randint(0, patch_sz[0] + gap - 1) gridszx = int((im.shape[1] + gap-gridstartx) / (patch_sz[1] + gap)) gridszy = int((im.shape[0] + gap-gridstarty) / (patch_sz[0] + gap)) # Whenever we sample and store a patch, we'll put its index in this # variable so it's easy to pair it up later. grid=np.zeros((gridszy, gridszx), int) # if we can't fit the current grid into the batch without going over # batch_sz, put the batch in the queue and reset. if(gridszx * gridszy + j >= batch_sz): pats=map(prep_image, pats) data=np.array(pats) qctr+=1 perm=(np.array(perm)) label=(np.array(label)) if tmpdir is None: dataq.put((np.ascontiguousarray(data), perm, label), timeout=600) else: fnm=tmpdir + str(tid) + '_' + str(qctr) + '.npy' np.save(fnm, data) dataq.put((fnm, perm, label), timeout=600) perm=[] label=[] pats=[] j=0 gridctr-=1; # for each location in the grid, sample a patch, search up and to the # left for patches that can be paired with it, and add them to the batch. for y in range(0,gridszy): for x in range(0,gridszx): (xpix, ypix)=sample_patch(x, y, gridstartx, gridstarty, patch_sz, gap, noise, im.shape) pats.append(np.copy( im[ypix:ypix + patch_sz[0], xpix:xpix + patch_sz[1], :]*255)); grid[y, x] = j; for pair in [(-1,-1), (0,-1), (1,-1), (-1,0)]: gridposx = pair[0] + x; gridposy = pair[1] + y; if(gridposx < 0 or gridposy < 0 or gridposx >= gridszx): continue; perm.append(np.array([j, grid[gridposy, gridposx]])); label.append(pos2lbl(pair)); perm.append(np.array([grid[gridposy, gridposx],j])) label.append(pos2lbl((-pair[0],-pair[1]))); j+=1;
# 搭建图的函数流程,调用上面的函数构成 # 定义输入变量,这里把输入也变为了一个变量节点,可以进行传播求导 with tf.variable_scope('input') as scope: #注意这里是变量,这个就有点类似于用变量表达Placeholder的感觉,因为后面都在给input_image赋值 input_image = tf.Variable(np.zeros([1, IMAGE_HEIGHT, IMAGE_WIDTH, 3]), dtype=tf.float32) #读取图像和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'] = _creat_loss( model, input_image, content_image, style_image) #设计优化函数 model['optimizer'] = tf.train.AdagradOptimizer(learning_rate).minimize( model['total_loss']) #设计summary用于TensorBoard展示 model['summary_op'] = _create_summary(model) # 运行图,得到最后生成的图片 # 构造一个噪声图像,这样比直接用白噪声快一点