Ejemplo n.º 1
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)
Ejemplo n.º 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'] = ...
    ###############################
    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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)