Ejemplo n.º 1
0
    def figure_3_d(content_image, style_image):
        content_image_yuv = rgb_to_yuv(content_image)
        content_luminance = content_image_yuv[:, :1].repeat(1, 3, 1, 1)
        content_chromaticity = content_image_yuv[:, 1:]

        style_luminance = rgb_to_grayscale(style_image, num_output_channels=3)

        print("Replicating Figure 3 (d)")
        output_luminance = paper.nst(
            content_luminance,
            style_luminance,
            impl_params=args.impl_params,
        )
        output_luminance = torch.mean(output_luminance, dim=1, keepdim=True)
        output_chromaticity = resize(content_chromaticity,
                                     output_luminance.size()[2:])
        output_image_yuv = torch.cat((output_luminance, output_chromaticity),
                                     dim=1)
        output_image = yuv_to_rgb(output_image_yuv)
        filename = make_output_filename(
            "gatys_et_al_2017",
            "fig_3",
            "d",
            impl_params=args.impl_params,
        )
        output_file = path.join(args.image_results_dir, filename)
        save_result(output_image, output_file)
Ejemplo n.º 2
0
    def figure_2_ef(
        label,
        content_image,
        content_building_guide,
        content_sky_guide,
        style_building_image,
        style_building_guide,
        style_sky_image,
        style_sky_guide,
    ):
        content_guides = {
            "building": content_building_guide,
            "sky": content_sky_guide
        }
        style_images_and_guides = {
            "building": (style_building_image, style_building_guide),
            "sky": (style_sky_image, style_sky_guide),
        }

        print(f"Replicating Figure 2 ({label})")
        output_image = paper.guided_nst(
            content_image,
            content_guides,
            style_images_and_guides,
            impl_params=args.impl_params,
        )
        filename = make_output_filename(
            "gatys_et_al_2017",
            "fig_2",
            label,
            impl_params=args.impl_params,
        )
        output_file = path.join(args.image_results_dir, filename)
        save_result(output_image, output_file)
Ejemplo n.º 3
0
def figure_3(args):
    images = paper.images()
    images.download(args.image_source_dir)

    hyper_parameters = paper.hyper_parameters(args.impl_params)

    content_image = images["neckarfront"].read(
        size=hyper_parameters.nst.image_size, device=args.device)
    style_image = images["composition_vii"].read(
        size=hyper_parameters.nst.image_size, device=args.device)

    style_layers = hyper_parameters.style_loss.layers
    layer_configs = [
        style_layers[:idx + 1] for idx in range(len(style_layers))
    ]

    score_weights = (1e5, 1e4, 1e3, 1e2)

    params = "implementation" if args.impl_params else "paper"
    for layers, score_weight in itertools.product(layer_configs,
                                                  score_weights):
        row_label = layers[-1]
        column_label = f"{1.0 / score_weight:.0e}"
        print(
            f"Replicating Figure 3 image in row {row_label} and column {column_label} "
            f"with {params} parameters.")
        hyper_parameters.style_loss.layers = layers
        if args.impl_params:
            hyper_parameters.style_loss.layer_weights = paper.compute_layer_weights(
                layers)
        hyper_parameters.style_loss.score_weight = score_weight

        output_image = paper.nst(
            content_image,
            style_image,
            impl_params=args.impl_params,
            hyper_parameters=hyper_parameters,
        )

        filename = utils.make_output_filename(
            "gatys_ecker_betghe_2016",
            "fig_3",
            row_label,
            column_label,
            impl_params=args.impl_params,
        )
        save_result(
            output_image,
            path.join(args.image_results_dir, filename),
        )
Ejemplo n.º 4
0
 def figure_3_c(content_image, style_image):
     print("Replicating Figure 3 (c)")
     output_image = paper.nst(
         content_image,
         style_image,
         impl_params=args.impl_params,
     )
     filename = make_output_filename(
         "gatys_et_al_2017",
         "fig_3",
         "c",
         impl_params=args.impl_params,
     )
     output_file = path.join(args.image_results_dir, filename)
     save_result(output_image, output_file)
Ejemplo n.º 5
0
    def figure_3_e(content_image, style_image, method="cholesky"):
        style_image = match_channelwise_statistics(style_image, content_image,
                                                   method)

        print("Replicating Figure 3 (e)")
        output_image = paper.nst(
            content_image,
            style_image,
            impl_params=args.impl_params,
        )
        filename = make_output_filename(
            "gatys_et_al_2017",
            "fig_3",
            "e",
            impl_params=args.impl_params,
        )
        output_file = path.join(args.image_results_dir, filename)
        save_result(output_image, output_file)
Ejemplo n.º 6
0
def figure_2(args):
    images = paper.images()
    images.download(args.image_source_dir)

    hyper_parameters = paper.hyper_parameters()

    content_image = images["neckarfront"].read(
        size=hyper_parameters.nst.image_size, device=args.device)

    class StyleImage:
        def __init__(self, label, image, score_weight):
            self.label = label
            self.image = image.read(size=hyper_parameters.nst.image_size,
                                    device=args.device)
            self.score_weight = score_weight

    style_images = (
        StyleImage("B", images["shipwreck"], 1e3),
        StyleImage("C", images["starry_night"], 1e3),
        StyleImage("D", images["the_scream"], 1e3),
        StyleImage("E", images["femme_nue_assise"], 1e4),
        StyleImage("F", images["composition_vii"], 1e4),
    )

    for style_image in style_images:
        print(f"Replicating Figure 2 {style_image.label}")
        hyper_parameters.style_loss.score_weight = style_image.score_weight

        output_image = paper.nst(
            content_image,
            style_image.image,
            impl_params=args.impl_params,
            hyper_parameters=hyper_parameters,
        )
        filename = utils.make_output_filename(
            "gatys_ecker_betghe_2016",
            "fig_2",
            style_image.label,
            impl_params=args.impl_params,
        )
        save_result(
            output_image,
            path.join(args.image_results_dir, filename),
        )
Ejemplo n.º 7
0
def figure_6(args):
    images = paper.images()
    images.download(args.image_source_dir)
    positions = ("top", "bottom")
    image_pairs = (
        (images["blue_bottle"], images["self-portrait"]),
        (images["s"], images["composition_viii"]),
    )

    for position, image_pair in zip(positions, image_pairs):
        content_image = image_pair[0].read(device=args.device)
        style_image = image_pair[1].read(device=args.device)

        print(
            f"Replicating the {position} half of figure 6 "
            f"with {'implementation' if args.impl_params else 'paper'} parameters"
        )

        hyper_parameters = paper.hyper_parameters(impl_params=args.impl_params)
        if args.impl_params:
            # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/run_trans.lua#L66
            hyper_parameters.content_loss.layer = "relu4_2"
            hyper_parameters.target_transforms.num_scale_steps = 1
            hyper_parameters.target_transforms.num_rotate_steps = 1

        output_image = paper.nst(
            content_image,
            style_image,
            impl_params=args.impl_params,
            hyper_parameters=hyper_parameters,
        )
        filename = make_output_filename(
            "li_wand_2016",
            "fig_6",
            position,
            impl_params=args.impl_params,
        )
        output_file = path.join(args.image_results_dir, filename)
        print(f"Saving result to {output_file}")
        write_image(output_image, output_file)
        print("#" * int(os.environ.get("COLUMNS", "80")))
Ejemplo n.º 8
0
def training(args):
    contents = ("karya", "tiger", "neckarfront", "bird", "kitty")
    styles = (
        "candy",
        "the_scream",
        "jean_metzinger",
        "mosaic",
        "pleades",
        "starry",
        "turner",
    )

    dataset = paper.dataset(
        args.dataset_dir,
        impl_params=args.impl_params,
        instance_norm=args.instance_norm,
    )
    image_loader = paper.image_loader(
        dataset,
        impl_params=args.impl_params,
        instance_norm=args.instance_norm,
        pin_memory=str(args.device).startswith("cuda"),
    )

    images = paper.images()
    images.download(args.image_source_dir)

    for style in styles:
        style_image = images[style].read(device=args.device)

        transformer = paper.training(
            image_loader,
            style_image,
            impl_params=args.impl_params,
            instance_norm=args.instance_norm,
        )

        model_name = f"ulyanov_et_al_2016__{style}"
        if args.impl_params:
            model_name += "__impl_params"
        if args.instance_norm:
            model_name += "__instance_norm"
        utils.save_state_dict(transformer, model_name, root=args.model_dir)

        for content in contents:
            content_image = images[content].read(device=args.device)
            output_image = paper.stylization(
                content_image,
                transformer,
                impl_params=args.impl_params,
                instance_norm=args.instance_norm,
            )
            filename = utils.make_output_filename(
                "ulyanov_et_al_2016",
                style,
                content,
                impl_params=args.impl_params,
                instance_norm=args.instance_norm,
            )
            output_file = path.join(args.image_results_dir, filename)
            image.write_image(output_image, output_file)