def run():
    parser = argparse.ArgumentParser(
        description='Neural artistic style. Generates an image by combining '
                    'the subject from one image and the style from another.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )

    # Added by Phil
    parser.add_argument('--src1', required=True, type=str,
                        help='First image to be compared.')
    parser.add_argument('--mat-path', required=True, type=str,
                        help='Path where the grams should be stored.')

    # Optionals
    parser.add_argument('--vgg19', default='imagenet-vgg-verydeep-19.mat',
                        type=str, help='VGG-19 .mat file.')
    parser.add_argument('--pool-method', default='avg', type=str,
                        choices=['max', 'avg'], help='Subsampling scheme.')
    parser.add_argument('--style-weights', nargs='*', type=weight_tuple,
                        default=[(0, 1), (2, 1), (4, 1), (8, 1), (12, 1)],
                        help='List of style weights (conv_idx,weight).')
    args = parser.parse_args()

    layers, img_mean = vgg19_net(args.vgg19, pool_method=args.pool_method)

    # Inputs
    pixel_mean = np.mean(img_mean, axis=(0, 1))
    style_img = imread(args.src1) - pixel_mean

    # Setup network
    style_weights = weight_array(args.style_weights)
    net = GramNet(layers, style_weights)

    # Compute style grams
    grams = net.compute_grams(to_bc01(style_img))

    # Store style grams
    filename = get_filename_without_extension(args.src1)
    for i, gram in enumerate(grams):
        if gram is not None:
            print(gram.__class__.__name__)
            print("Layer {}: Shape: {}".format(i, gram.shape))
            path = os.path.join(args.mat_path, "{}_{}x{}".format(filename, gram.shape[0], gram.shape[1]))
            scipy.io.savemat(path, dict(data=gram.__array__()))
def run():
    parser = argparse.ArgumentParser(
        description="Neural artistic style. Generates an image by combining "
        "the subject from one image and the style from another.",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument("--subject", required=True, type=str, help="Subject image.")
    parser.add_argument("--style", required=True, type=str, help="Style image.")
    parser.add_argument("--output", default="out.png", type=str, help="Output image.")
    parser.add_argument("--init", default=None, type=str, help="Initial image. Subject is chosen as default.")
    parser.add_argument(
        "--init-noise",
        default=0.0,
        type=float_range,
        help="Weight between [0, 1] to adjust the noise level " "in the initial image.",
    )
    parser.add_argument("--random-seed", default=None, type=int, help="Random state.")
    parser.add_argument("--animation", default="animation", type=str, help="Output animation directory.")
    parser.add_argument("--iterations", default=500, type=int, help="Number of iterations to run.")
    parser.add_argument("--learn-rate", default=2.0, type=float, help="Learning rate.")
    parser.add_argument("--smoothness", type=float, default=2e-7, help="Weight of smoothing scheme.")
    parser.add_argument(
        "--subject-weights",
        nargs="*",
        type=weight_tuple,
        default=[(9, 1)],
        help="List of subject weights (conv_idx,weight).",
    )
    parser.add_argument(
        "--style-weights",
        nargs="*",
        type=weight_tuple,
        default=[(0, 1), (2, 1), (4, 1), (8, 1), (12, 1)],
        help="List of style weights (conv_idx,weight).",
    )
    parser.add_argument("--subject-ratio", type=float, default=2e-2, help="Weight of subject relative to style.")
    parser.add_argument("--pool-method", default="avg", type=str, choices=["max", "avg"], help="Subsampling scheme.")
    parser.add_argument("--vgg19", default="imagenet-vgg-verydeep-19.mat", type=str, help="VGG-19 .mat file.")
    args = parser.parse_args()

    if args.random_seed is not None:
        np.random.seed(args.random_seed)

    layers, img_mean = vgg19_net(args.vgg19, pool_method=args.pool_method)

    # Inputs
    pixel_mean = np.mean(img_mean, axis=(0, 1))
    style_img = imread(args.style) - pixel_mean
    subject_img = imread(args.subject) - pixel_mean
    if args.init is None:
        init_img = subject_img
    else:
        init_img = imread(args.init) - pixel_mean
    noise = np.random.normal(size=init_img.shape, scale=np.std(init_img))
    init_img = init_img * (1 - args.init_noise) + noise * args.init_noise

    # Setup network
    subject_weights = weight_array(args.subject_weights) * args.subject_ratio
    style_weights = weight_array(args.style_weights)
    net = StyleNetwork(
        layers,
        to_bc01(init_img),
        to_bc01(subject_img),
        to_bc01(style_img),
        subject_weights,
        style_weights,
        args.smoothness,
    )

    # Repaint image
    def net_img():
        return to_rgb(net.image) + pixel_mean

    if not os.path.exists(args.animation):
        os.mkdir(args.animation)

    params = net._params
    learn_rule = dp.Adam(learn_rate=args.learn_rate)
    learn_rule_states = [learn_rule.init_state(p) for p in params]
    for i in range(args.iterations):
        imsave(os.path.join(args.animation, "%.4d.png" % i), net_img())
        cost = np.mean(net._update())
        for param, state in zip(params, learn_rule_states):
            learn_rule.step(param, state)
        print("Iteration: %i, cost: %.4f" % (i, cost))
    imsave(args.output, net_img())
def run():
    parser = argparse.ArgumentParser(
        description='Neural artistic style. Generates an image by combining '
                    'the subject from one image and the style from another.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument('--subject', required=True, type=str,
                        help='Subject image.')
    parser.add_argument('--style', required=True, type=str,
                        help='Style image.')
    parser.add_argument('--output', default='out.png', type=str,
                        help='Output image.')
    parser.add_argument('--init', default=None, type=str,
                        help='Initial image. Subject is chosen as default.')
    parser.add_argument('--init-noise', default=0.0, type=float_range,
                        help='Weight between [0, 1] to adjust the noise level '
                             'in the initial image.')
    parser.add_argument('--random-seed', default=None, type=int,
                        help='Random state.')
    parser.add_argument('--animation', default='animation', type=str,
                        help='Output animation directory.')
    parser.add_argument('--iterations', default=500, type=int,
                        help='Number of iterations to run.')
    parser.add_argument('--learn-rate', default=2.0, type=float,
                        help='Learning rate.')
    parser.add_argument('--smoothness', type=float, default=5e-8,
                        help='Weight of smoothing scheme.')
    parser.add_argument('--subject-weights', nargs='*', type=weight_tuple,
                        default=[(9, 1)],
                        help='List of subject weights (conv_idx,weight).')
    parser.add_argument('--style-weights', nargs='*', type=weight_tuple,
                        default=[(0, 1), (2, 1), (4, 1), (8, 1), (12, 1)],
                        help='List of style weights (conv_idx,weight).')
    parser.add_argument('--subject-ratio', type=float, default=2e-2,
                        help='Weight of subject relative to style.')
    parser.add_argument('--pool-method', default='avg', type=str,
                        choices=['max', 'avg'], help='Subsampling scheme.')
    parser.add_argument('--vgg19', default='imagenet-vgg-verydeep-19.mat',
                        type=str, help='VGG-19 .mat file.')
    args = parser.parse_args()

    if args.random_seed is not None:
        np.random.seed(args.random_seed)

    layers, img_mean = vgg19_net(args.vgg19, pool_method=args.pool_method)

    # Inputs
    pixel_mean = np.mean(img_mean, axis=(0, 1))
    style_img = imread(args.style) - pixel_mean
    subject_img = imread(args.subject) - pixel_mean
    if args.init is None:
        init_img = subject_img
    else:
        init_img = imread(args.init) - pixel_mean
    noise = np.random.normal(size=init_img.shape, scale=np.std(init_img)*1e-1)
    init_img = init_img * (1 - args.init_noise) + noise * args.init_noise

    # Setup network
    subject_weights = weight_array(args.subject_weights) * args.subject_ratio
    style_weights = weight_array(args.style_weights)
    net = StyleNetwork(layers, to_bc01(init_img), to_bc01(subject_img),
                       to_bc01(style_img), subject_weights, style_weights,
                       args.smoothness)

    # Repaint image
    def net_img():
        return to_rgb(net.image) + pixel_mean

    if not os.path.exists(args.animation):
        os.mkdir(args.animation)

    params = net._params
    learn_rule = dp.Adam(learn_rate=args.learn_rate)
    learn_rule_states = [learn_rule.init_state(p) for p in params]
    for i in range(args.iterations):
        imsave(os.path.join(args.animation, '%.4d.png' % i), net_img())
        cost = np.mean(net._update())
        for param, state in zip(params, learn_rule_states):
            learn_rule.step(param, state)
        print('Iteration: %i, cost: %.4f' % (i, cost))
    imsave(args.output, net_img())

    play_done_sound()
def run():
    parser = argparse.ArgumentParser(
        description='Neural artistic style. Generates an image by combining '
        'the subject from one image and the style from another.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--subject',
                        required=True,
                        type=str,
                        help='Subject image.')
    parser.add_argument('--style',
                        required=True,
                        type=str,
                        help='Style image.')
    parser.add_argument('--output',
                        default='out.png',
                        type=str,
                        help='Output image.')
    parser.add_argument('--init',
                        default=None,
                        type=str,
                        help='Initial image. Subject is chosen as default.')
    parser.add_argument('--init-noise',
                        default=0.0,
                        type=float_range,
                        help='Weight between [0, 1] to adjust the noise level '
                        'in the initial image.')
    parser.add_argument('--random-seed',
                        default=None,
                        type=int,
                        help='Random state.')
    parser.add_argument('--animation',
                        default='animation',
                        type=str,
                        help='Output animation directory.')
    parser.add_argument('--iterations',
                        default=500,
                        type=int,
                        help='Number of iterations to run.')
    parser.add_argument('--learn-rate',
                        default=2.0,
                        type=float,
                        help='Learning rate.')
    parser.add_argument('--smoothness',
                        type=float,
                        default=2e-7,
                        help='Weight of smoothing scheme.')
    parser.add_argument('--subject-weights',
                        nargs='*',
                        type=weight_tuple,
                        default=[(9, 1)],
                        help='List of subject weights (conv_idx,weight).')
    parser.add_argument('--style-weights',
                        nargs='*',
                        type=weight_tuple,
                        default=[(0, 1), (2, 1), (4, 1), (8, 1), (12, 1)],
                        help='List of style weights (conv_idx,weight).')
    parser.add_argument('--subject-ratio',
                        type=float,
                        default=2e-2,
                        help='Weight of subject relative to style.')
    parser.add_argument('--pool-method',
                        default='avg',
                        type=str,
                        choices=['max', 'avg'],
                        help='Subsampling scheme.')
    parser.add_argument('--vgg19',
                        default='imagenet-vgg-verydeep-19.mat',
                        type=str,
                        help='VGG-19 .mat file.')
    args = parser.parse_args()

    if args.random_seed is not None:
        np.random.seed(args.random_seed)

    layers, img_mean = vgg19_net(args.vgg19, pool_method=args.pool_method)

    # Inputs
    pixel_mean = np.mean(img_mean, axis=(0, 1))
    style_img = imread(args.style) - pixel_mean
    subject_img = imread(args.subject) - pixel_mean
    if args.init is None:
        init_img = subject_img
    else:
        init_img = imread(args.init) - pixel_mean
    noise = np.random.normal(size=init_img.shape, scale=np.std(init_img))
    init_img = init_img * (1 - args.init_noise) + noise * args.init_noise

    # Setup network
    subject_weights = weight_array(args.subject_weights) * args.subject_ratio
    style_weights = weight_array(args.style_weights)
    net = StyleNetwork(layers, to_bc01(init_img), to_bc01(subject_img),
                       to_bc01(style_img), subject_weights, style_weights,
                       args.smoothness)

    # Repaint image
    def net_img():
        return to_rgb(net.image) + pixel_mean

    if not os.path.exists(args.animation):
        os.mkdir(args.animation)

    params = net._params
    learn_rule = dp.Adam(learn_rate=args.learn_rate)
    learn_rule_states = [learn_rule.init_state(p) for p in params]
    for i in range(args.iterations):
        imsave(os.path.join(args.animation, '%.4d.png' % i), net_img())
        cost = np.mean(net._update())
        for param, state in zip(params, learn_rule_states):
            learn_rule.step(param, state)
        print('Iteration: %i, cost: %.4f' % (i, cost))
    imsave(args.output, net_img())
def process_image_pair(**kwargs):

    start = time.time()

    if kwargs['random_seed'] is not None:
        np.random.seed(kwargs['random_seed'])

    print('loading mat net...')
    layers, img_mean = vgg19_net(kwargs['vgg19'],
                                 pool_method='avg')  #kwargs['pool_method'])

    # Inputs
    pixel_mean = np.mean(img_mean, axis=(0, 1))
    style_img = imread(kwargs['style_path']) - pixel_mean
    subject_img = imread(kwargs['content_path']) - pixel_mean

    # if kwargs['init'] is None:
    #     init_img = subject_img
    # else:
    #     init_img = imread(kwargs['init']) - pixel_mean

    init_img = subject_img

    noise = np.random.normal(size=init_img.shape,
                             scale=np.std(init_img) * 1e-1)
    init_img = init_img * (1 -
                           kwargs['init_noise']) + noise * kwargs['init_noise']

    # Setup network
    print('building network...')

    #FIXME: Set defaults
    subject_weights = weight_array(
        kwargs['subject_weights']) * kwargs['subject_ratio']
    style_weights = weight_array(kwargs['style_weights'])

    net = StyleNetwork(layers, to_bc01(init_img), to_bc01(subject_img),
                       to_bc01(style_img), subject_weights, style_weights,
                       kwargs['smoothness'])

    # Repaint image
    def net_img():
        return to_rgb(net.image) + pixel_mean

    if kwargs['animation']:
        os.mkdir(kwargs['animation_dir'])

    params = net.params
    learn_rule = dp.Adam(learn_rate=kwargs['learn_rate'])
    learn_rule_states = [learn_rule.init_state(p) for p in params]

    for i in range(kwargs['iterations']):
        print('iteration %d', i)

        if kwargs['animation']:
            imsave(os.path.join(kwargs['animation_dir'], '%.4d.png' % i),
                   net_img())

        cost = np.mean(net.update())
        for param, state in zip(params, learn_rule_states):
            learn_rule.step(param, state)
        print('Iteration: %i, cost: %.4f' % (i, cost))

    imsave(kwargs['output_path'], net_img())

    print("Time Elapsed: %0.2f", time.time() - start)
def run():
    parser = argparse.ArgumentParser(
        description='Neural artistic style.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument('--subject', required=True, type=str,
                        help='subject image')
    parser.add_argument('--style', required=True, type=str,
                        help='style image')
    parser.add_argument('--output', default='out.png', type=str,
                        help='output image')
    parser.add_argument('--animation', default='animation', type=str,
                        help='output animation directory')
    parser.add_argument('--iterations', default=500, type=int,
                        help='Number of iterations')
    parser.add_argument('--learn-rate', default=5.0, type=float,
                        help='Learning rate')
    parser.add_argument('--subject-weights', nargs='*', type=weight_tuple,
                        default=[(9, 1)],
                        help='list of subject weights (conv_idx,weight)')
    parser.add_argument('--style-weights', nargs='*', type=weight_tuple,
                        default=[(0, 1), (2, 1), (4, 1), (8, 1), (12, 1)],
                        help='list of style weights (conv_idx,weight)')
    parser.add_argument('--subject-ratio', type=float, default=2e-2,
                        help='weight of subject relative to style')
    parser.add_argument('--vgg19', default='imagenet-vgg-verydeep-19.mat',
                        type=str, help='VGG-19 .mat file')
    args = parser.parse_args()

    layers, img_mean = vgg19_net(args.vgg19, pool_method='avg')

    # Inputs
    pixel_mean = np.mean(img_mean, axis=(0, 1))
    style_img = imread(args.style)
    subject_img = imread(args.subject)
    style_img -= pixel_mean
    subject_img -= pixel_mean

    # Setup network
    subject_weights = weight_array(args.subject_weights) * args.subject_ratio
    style_weights = weight_array(args.style_weights)
    net = StyleNetwork(layers, to_bc01(subject_img), to_bc01(style_img),
                       subject_weights, style_weights)

    # Repaint image
    def net_img():
        return to_rgb(net.image) + pixel_mean

    if not os.path.exists(args.animation):
        os.mkdir(args.animation)

    params = net._params
    learn_rule = dp.Adam(learn_rate=args.learn_rate)
    learn_rule_states = [learn_rule.init_state(p) for p in params]
    for i in range(args.iterations):
        imsave(os.path.join(args.animation, '%.4d.png' % i), net_img())
        cost = np.mean(net._update())
        for param, state in zip(params, learn_rule_states):
            learn_rule.step(param, state)
        print('Iteration: %i, cost: %.4f' % (i, cost))
    imsave(args.output, net_img())
Example #7
0
def run():
    parser = argparse.ArgumentParser(
        description='Neural artistic style.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--subject',
                        required=True,
                        type=str,
                        help='subject image')
    parser.add_argument('--style', required=True, type=str, help='style image')
    parser.add_argument('--output',
                        default='out.png',
                        type=str,
                        help='output image')
    parser.add_argument('--animation',
                        default='animation',
                        type=str,
                        help='output animation directory')
    parser.add_argument('--iterations',
                        default=500,
                        type=int,
                        help='Number of iterations')
    parser.add_argument('--learn-rate',
                        default=5.0,
                        type=float,
                        help='Learning rate')
    parser.add_argument('--subject-weights',
                        nargs='*',
                        type=weight_tuple,
                        default=[(9, 1)],
                        help='list of subject weights (conv_idx,weight)')
    parser.add_argument('--style-weights',
                        nargs='*',
                        type=weight_tuple,
                        default=[(0, 1), (2, 1), (4, 1), (8, 1), (12, 1)],
                        help='list of style weights (conv_idx,weight)')
    parser.add_argument('--subject-ratio',
                        type=float,
                        default=2e-2,
                        help='weight of subject relative to style')
    parser.add_argument('--vgg19',
                        default='imagenet-vgg-verydeep-19.mat',
                        type=str,
                        help='VGG-19 .mat file')
    args = parser.parse_args()

    layers, img_mean = vgg19_net(args.vgg19, pool_method='avg')

    # Inputs
    pixel_mean = np.mean(img_mean, axis=(0, 1))
    style_img = imread(args.style)
    subject_img = imread(args.subject)
    style_img -= pixel_mean
    subject_img -= pixel_mean

    # Setup network
    subject_weights = weight_array(args.subject_weights) * args.subject_ratio
    style_weights = weight_array(args.style_weights)
    net = StyleNetwork(layers, to_bc01(subject_img), to_bc01(style_img),
                       subject_weights, style_weights)

    # Repaint image
    def net_img():
        return to_rgb(net.image) + pixel_mean

    if not os.path.exists(args.animation):
        os.mkdir(args.animation)

    params = net._params
    learn_rule = dp.Adam(learn_rate=args.learn_rate)
    learn_rule_states = [learn_rule.init_state(p) for p in params]
    for i in range(args.iterations):
        imsave(os.path.join(args.animation, '%.4d.png' % i), net_img())
        cost = np.mean(net._update())
        for param, state in zip(params, learn_rule_states):
            learn_rule.step(param, state)
        print('Iteration: %i, cost: %.4f' % (i, cost))
    imsave(args.output, net_img())