Exemple #1
0
def main():
    filepath = lambda p: os.path.abspath(
        os.path.realpath(os.path.expanduser(p)))
    parser = argparse.ArgumentParser(
        prog='run.py',
        description=
        'Style Transfer script applying style of one image to another one')
    parser.add_argument('--style_image',
                        '-si',
                        type=filepath,
                        metavar='STYLE_IMAGE',
                        required=True,
                        help='Path to image which style will be extracted')
    parser.add_argument('--content_image',
                        '-ci',
                        type=filepath,
                        metavar='CONTENT_IMAGE',
                        required=True,
                        help='Path to image which content will be extracted')
    parser.add_argument(
        '--output_folder',
        '-of',
        type=filepath,
        metavar='OUTPUT_PATH',
        required=True,
        help='Folder path where the output image will be stored')
    parser.add_argument('--epochs',
                        '-e',
                        type=int,
                        metavar='N_EPOCHS',
                        default=1,
                        help='Number of optimization steps')
    parser.add_argument(
        '--white_noise',
        '-wn',
        action='store_true',
        help=
        'If set to True, initial image is white noise and not the content image'
    )
    args = parser.parse_args()

    style_image = Processing.load_image(args.style_image)
    content_image = Processing.load_image(args.content_image)
    assert style_image.size() == content_image.size(
    ), "Style and content images must be the same sizes"

    if args.white_noise:
        LOGGER.debug('Preparing input image as white noise')
        input_image = torch.randn(content_image.data.size(),
                                  device=Utils.DEVICE)
    else:
        LOGGER.debug('Preparing input image as the content image')
        input_image = content_image.clone()

    final_image = input_image.clone()
    LOGGER.info('Starting the Style Transfer')
    run_style_transfer(final_image,
                       content_image,
                       style_image,
                       epochs=args.epochs)
    LOGGER.info('Ended the Style Transfer')

    os.makedirs(args.output_folder, exist_ok=True)
    show_image(input_image,
               'Input image',
               save_path=os.path.join(args.output_folder, 'input_image'))
    show_image(content_image,
               'Content image',
               save_path=os.path.join(args.output_folder, 'content_image'))
    show_image(style_image,
               'Style image',
               save_path=os.path.join(args.output_folder, 'style_image'))
    show_image(final_image,
               'Final image',
               save_path=os.path.join(args.output_folder, 'final_image'))