Ejemplo n.º 1
0
def run(arg):
    content_image = open_and_resize_image('php/upload/' + arg.f + '.jpg',
                                          args.width, vgg)
    print 'loading content image completed'
    style_image = open_and_resize_image('php/' + arg.s, args.width, vgg)
    print 'loading style image completed'
    content_layers = args.content_layers.split(',')
    style_layers = args.style_layers.split(',')

    def on_epoch_done(epoch, x, losses):
        print epoch

    model = NeuralStyle(vgg,
                        optimizer,
                        args.content_weight,
                        args.style_weight,
                        args.tv_weight,
                        content_layers,
                        style_layers,
                        args.resolution_num,
                        args.gpu,
                        initial_image=args.initial_image,
                        keep_color=args.keep_color)
    out_image = model.fit(content_image, style_image, args.iter, on_epoch_done)
    out_image = cuda.to_cpu(out_image.data)
    image = vgg.postprocess(out_image[0],
                            output_type='RGB').clip(0, 255).astype(np.uint8)
    Image.fromarray(image).save('php/final/' + arg.f + '.png')
Ejemplo n.º 2
0
def generate(model, args):
    print(args)

    model_new = NeuralStyle()
    model_new.content_layers = [args['content_layer']]

    # print('[GENERATE] Ran with content image: "{}"'.format(args['content_image']))
    # print('[GENERATE] Ran with first style image: "{}"'.format(args['style_image_1']))
    # # print('[GENERATE] Ran with second style image: "{}"'.format(args['style_image_2']))
    # Generate a PIL or Numpy image based on the input caption, and return it
    output_image = model_new.run(args['content_image'], args['style_image_1'],
                                 args['original_colors'],
                                 args['max_iterations'], args['style_scale'],
                                 args['style_only'])
    return {'image': output_image}
def run(args):
    if args.out_dir != None:
        if not os.path.exists(args.out_dir):
            try:
                os.mkdir(args.out_dir)
            except:
                print 'cannot make directory {}'.format(args.out_dir)
                exit()
        elif not os.path.isdir(args.out_dir):
            print 'file path {} exists but is not directory'.format(args.out_dir)
            exit()
    if args.type == 'vgg19':
        vgg = VGG19()
    else:
        vgg = VGG()
    content_image = open_and_resize_image(args.content, args.width, vgg)
    print 'loading content image completed'
    style_image = open_and_resize_image(args.style, args.width, vgg)
    if args.match_color_histogram:
        style_image = util.match_color_histogram(style_image, content_image)
    if args.luminance_only:
        content_image, content_iq = util.split_bgr_to_yiq(content_image)
        style_image, style_iq = util.split_bgr_to_yiq(style_image)
        content_mean = np.mean(content_image, axis=(1,2,3), keepdims=True)
        content_std = np.std(content_image, axis=(1,2,3), keepdims=True)
        style_mean = np.mean(style_image, axis=(1,2,3), keepdims=True)
        style_std = np.std(style_image, axis=(1,2,3), keepdims=True)
        style_image = (style_image - style_mean) / style_std * content_std + content_mean
    print 'loading style image completed'
    serializers.load_hdf5(args.model, vgg)
    print 'loading neural network model completed'
    optimizer = LBFGS(args.lr)
    content_layers = args.content_layers.split(',')
    style_layers = args.style_layers.split(',')

    def on_epoch_done(epoch, x, losses):
        if (epoch + 1) % args.save_iter == 0:
            image = cuda.to_cpu(x.data)
            if args.luminance_only:
                image = util.join_yiq_to_bgr(image, content_iq)
            image = vgg.postprocess(image[0], output_type='RGB').clip(0, 255).astype(np.uint8)
            Image.fromarray(image).save(os.path.join(args.out_dir, 'out_{0:04d}.png'.format(epoch + 1)))
            print 'epoch {} done'.format(epoch + 1)
            print 'losses:'
            label_width = max(map(lambda (name, loss): len(name), losses))
            for name, loss in losses:
                print '  {0:{width}s}: {1:f}'.format(name, loss, width=label_width)

    if args.method == 'mrf':
        model = MRF(vgg, optimizer, args.content_weight, args.style_weight, args.tv_weight, content_layers, style_layers, args.resolution_num, args.gpu, initial_image=args.initial_image, keep_color=args.keep_color)
    else:
        model = NeuralStyle(vgg, optimizer, args.content_weight, args.style_weight, args.tv_weight, content_layers, style_layers, args.resolution_num, args.gpu, initial_image=args.initial_image, keep_color=args.keep_color)
    out_image = model.fit(content_image, style_image, args.iter, on_epoch_done)
    out_image = cuda.to_cpu(out_image.data)
    if args.luminance_only:
        out_image = util.join_yiq_to_bgr(out_image, content_iq)
    image = vgg.postprocess(out_image[0], output_type='RGB').clip(0, 255).astype(np.uint8)
    Image.fromarray(image).save(os.path.join(args.out_dir, 'out.png'))
Ejemplo n.º 4
0
def main(_):
    neural_style = NeuralStyle(opts)
    loss_tensor = neural_style.loss
    content_loss_tensor = neural_style.content_loss
    style_loss_tensor = neural_style.style_loss
    total_variation_loss_tensor = neural_style.total_variation_loss

    global_step_tensor = tf.train.get_or_create_global_step()
    optimizer = tf.train.AdamOptimizer(learning_rate=opts.learning_rate,
                                       beta1=opts.adam_beta1,
                                       beta2=opts.adam_beta2,
                                       epsilon=opts.epsilon)
    train_op = optimizer.minimize(loss=loss_tensor,
                                  global_step=global_step_tensor)
    merged_summary = tf.summary.merge_all()
    summary_writer = tf.summary.FileWriter(opts.model_dir)
    saver = tf.train.Saver(sharded=True,
                           max_to_keep=3,
                           save_relative_paths=True)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        summary_writer.add_graph(sess.graph)
        tf.get_default_graph().finalize()

        start = time.time()
        for _ in xrange(opts.iters):
            (_, loss, content_loss, style_loss, total_variation_loss,
             global_step) = sess.run([
                 train_op, loss_tensor, content_loss_tensor, style_loss_tensor,
                 total_variation_loss_tensor, global_step_tensor
             ])
            print("iter {}, loss = {:.2f}, content_loss = {:.2f},"
                  " style_loss = {:.2f}, total_variation_loss = {:.2f},"
                  " elapsed {:.2f} s".format(global_step, loss, content_loss,
                                             style_loss, total_variation_loss,
                                             time.time() - start))
            start = time.time()

            if global_step % opts.save_output_steps == 0:
                # save output image
                img = sess.run([neural_style.output_image])
                img = np.squeeze(img, 0)
                output = 'output.' + str(global_step) + '.jpg'
                imsave(output, img)
                print("save output as {}.".format(output))

            if global_step % opts.save_summary_steps == 0:
                summary = sess.run(merged_summary)
                summary_writer.add_summary(summary, global_step)
                summary_writer.flush()

            if global_step % opts.save_checkpoints_steps == 0:
                save_checkpoint(sess, global_step, saver, opts.model_dir)

        # save final output image
        img = sess.run([neural_style.output_image])
        img = np.squeeze(img, 0)
        imsave(opts.output_image_path, img)

        summary_writer.close()
Ejemplo n.º 5
0
# inicializar o Adam optimizer
opt = tf.optimizers.Adam(learning_rate=0.01, beta_1=0.99, epsilon=1e-1)

# carregar as imagens de contexto e estilo the content and style images
print("\n\n[INFO] carregando as imagens de contexto e estilo...\n")
time.sleep(2)
content_image = load_image(content_image_path)
style_image = load_image(style_image_path)

# extrair os layers de acordo com o arquivo de configuração
content_layers = config.content_layers
style_layers = config.style_layers

# inicializar a arquitetura para extrair as features das imagens de contexto e estilo
extractor = NeuralStyle(style_layers, content_layers)
style_targets = extractor(style_image)["style"]
content_targets = extractor(content_image)["content"]

# iniciar a fase de treino
os.system('cls' if os.name == 'nt' else 'clear')
print("[INFO] treinando o modelo de transferência de estilo neural...\n\n")
image = tf.Variable(content_image)
step = 0

# instanciar uma Progress Bar
bar = progressbar.ProgressBar(max_value=config.epochs * config.steps_per_epoch)

# iterar ao longo das épocas
for epoch in range(config.epochs):
    # iterar ao longo dos steps
Ejemplo n.º 6
0
def setup():
    model = NeuralStyle()
    return model