Esempio n. 1
0
def style_transfer(subject, style, output, iterations):
    pool_method = ['max', 'avg']
    init_noise = 0.0
    default_subject_weights = [(9, 1)]
    default_style_weights = [(0, 1), (2, 1), (4, 1), (8, 1), (12, 1)]
    subject_ratio = 2e-2
    smoothness = 5e-8
    learn_rate = 2.0
    animation = 'animation'
    transfer_learning = 'imagenet-vgg-verydeep-19.mat'

    np.random.seed(None)

    layers, pixel_mean = vgg_net(transfer_learning, pool_method=pool_method[1])

    # Inputs
    style_img = imread(style) - pixel_mean
    subject_img = imread(subject) - 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 - init_noise) + noise * init_noise

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

    # Repaint image
    def net_img():
        return to_rgb(net.image) + pixel_mean
    if not os.path.exists(animation):
        os.mkdir(animation)

    params = net.params
    learn_rule = dp.Adam(learn_rate=learn_rate)
    learn_rule_states = [learn_rule.init_state(p) for p in params]
    print 'working on {0} -> {1}'.format(style, subject)
    for i in tqdm(range(iterations)):
        imsave(os.path.join(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: {0}/{1}, cost: {2:.4f}'.format(i, iterations, cost))
    imsave(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=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. 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('--scales', default=4, type=int,
                        help='Number of scales to use.')
    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('--network', default='imagenet-vgg-verydeep-19.mat',
                        type=str, help='Network in MatConvNet format).')
    parser.add_argument('--outer_it', default=8000, type=int, help='Outer iterations.')
    parser.add_argument('--inner_it', default=3, type=int, help='Inner iterations.')
    parser.add_argument('--patch_size', default=512, type=int, help='Patchsize.')
    args = parser.parse_args()

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

    layers, pixel_mean = vgg_net(args.network, pool_method=args.pool_method)

    # Inputs
    style_img = imread(args.style) - pixel_mean
    subject_img = imread(args.subject) - pixel_mean
    print "style_img", style_img.shape
    print "subject_img", subject_img.shape
    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)
   
    max_patch_size = np.array([args.patch_size, args.patch_size])
    inner_iterations = 10

    for outer_it in range(0,args.outer_it):
      scale = 1.0
      for sc in range(0, args.scales - 1): 
        if random.random() < 0.25: scale*=2.0
      print "OUTER_IT: ", outer_it, "SCALE:", scale

      effective_patch_size = max_patch_size * scale
      patch_size = (np.min([style_img.shape[0], subject_img.shape[0], init_img.shape[0], effective_patch_size[0]]),
                    np.min([style_img.shape[1], subject_img.shape[1], init_img.shape[1], effective_patch_size[1]]))

      rel_x = random.random()
      rel_y = random.random()
      x = int(math.floor(rel_x*(style_img.shape[0]-patch_size[0])))
      y = int(math.floor(rel_y*(style_img.shape[1]-patch_size[1])))
      style_patch = style_img[x:x+patch_size[0], y:y+patch_size[1], :]
      
      x = int(math.floor(rel_x*(subject_img.shape[0]-patch_size[0])))
      y = int(math.floor(rel_y*(subject_img.shape[1]-patch_size[1])))
      subject_patch = subject_img[x:x+patch_size[0], y:y+patch_size[1], :]
      
      x = int(math.floor(rel_x*(init_img.shape[0]-patch_size[0])))
      y = int(math.floor(rel_y*(init_img.shape[1]-patch_size[1])))
      init_patch = init_img[x:x+patch_size[0], y:y+patch_size[1], :]

      style_patch_scaled = nd.zoom(style_patch, (1.0 / scale, 1.0 / scale, 1), order=1) 
      subject_patch_scaled = nd.zoom(subject_patch, (1.0 / scale, 1.0 / scale, 1), order=1) 
      init_patch_scaled = nd.zoom(init_patch, (1.0 / scale, 1.0 / scale, 1), order=1) 
      
      net = StyleNetwork(layers, 
                         to_bc01(init_patch_scaled), 
                         to_bc01(subject_patch_scaled),
                         to_bc01(style_patch_scaled), 
                         subject_weights, style_weights,
                         args.smoothness)
      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.inner_it):
          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))
     
      result_patch_scaled = to_rgb(net.image) - init_patch_scaled 
      result_patch = nd.zoom(result_patch_scaled, (scale, scale, 1), order=1)
      
      border_cut = 10
      init_img[x+border_cut:x+result_patch.shape[0]-border_cut, 
               y+border_cut:y+result_patch.shape[1]-border_cut, :] += result_patch[border_cut:-border_cut, border_cut:-border_cut]
      imsave("%s.jpg"%args.output , init_img + pixel_mean)
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())
def style_image(args):
    if not args.progress_disabled:
        print_progress(0, 'Initializing...')

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

    layers, pixel_mean = vgg_net(args.network, pool_method=args.pool_method)

    # Inputs
    style_img = imread(args.style) - pixel_mean
    subject_img = imread(args.subject)

    if args.size is not None:
        # resize maintaining aspect ratio
        height, width = subject_img.shape[:2]
        new_width = args.size
        new_height = height * new_width / width
        subject_img = scipy.misc.imresize(subject_img, (new_height,  new_width))

    subject_img = subject_img - 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 args.animation_enabled and not os.path.exists(args.animation_directory):
        os.mkdir(args.animation_directory)

    if not args.progress_disabled:
        print_progress(0, 'Styling...')
        start_time = time.time()

    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):
        if not args.progress_disabled:
            seconds_passed = time.time() - start_time
            minutes_left = 0 if i == 0 else ((seconds_passed * args.iterations / i) - seconds_passed) / 60
            print_progress(float(i) / args.iterations,
                    'Iteration: %i/%i, Time left: %imin'
                    % (i + 1, args.iterations, minutes_left))
        if args.animation_enabled and i % args.animation_rate == 0:
            imsave(os.path.join(args.animation_directory, '%.4d.png' % i), net_img())
        net.update()
        for param, state in zip(params, learn_rule_states):
            learn_rule.step(param, state)

    imsave(args.output, net_img())

    if not args.progress_disabled:
        minutes_passed = (time.time() - start_time) / 60
        print_progress(1, 'Done! Took %imin.' % minutes_passed)
Esempio n. 9
0
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('--scales',
                        default=4,
                        type=int,
                        help='Number of scales to use.')
    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('--network',
                        default='imagenet-vgg-verydeep-19.mat',
                        type=str,
                        help='Network in MatConvNet format).')
    parser.add_argument('--outer_it',
                        default=8000,
                        type=int,
                        help='Outer iterations.')
    parser.add_argument('--inner_it',
                        default=3,
                        type=int,
                        help='Inner iterations.')
    parser.add_argument('--patch_size',
                        default=512,
                        type=int,
                        help='Patchsize.')
    args = parser.parse_args()

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

    layers, pixel_mean = vgg_net(args.network, pool_method=args.pool_method)

    # Inputs
    style_img = imread(args.style) - pixel_mean
    subject_img = imread(args.subject) - pixel_mean
    print "style_img", style_img.shape
    print "subject_img", subject_img.shape
    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)

    max_patch_size = np.array([args.patch_size, args.patch_size])
    inner_iterations = 10

    for outer_it in range(0, args.outer_it):
        scale = 1.0
        for sc in range(0, args.scales - 1):
            if random.random() < 0.25: scale *= 2.0
        print "OUTER_IT: ", outer_it, "SCALE:", scale

        effective_patch_size = max_patch_size * scale
        patch_size = (np.min([
            style_img.shape[0], subject_img.shape[0], init_img.shape[0],
            effective_patch_size[0]
        ]),
                      np.min([
                          style_img.shape[1], subject_img.shape[1],
                          init_img.shape[1], effective_patch_size[1]
                      ]))

        rel_x = random.random()
        rel_y = random.random()
        x = int(math.floor(rel_x * (style_img.shape[0] - patch_size[0])))
        y = int(math.floor(rel_y * (style_img.shape[1] - patch_size[1])))
        style_patch = style_img[x:x + patch_size[0], y:y + patch_size[1], :]

        x = int(math.floor(rel_x * (subject_img.shape[0] - patch_size[0])))
        y = int(math.floor(rel_y * (subject_img.shape[1] - patch_size[1])))
        subject_patch = subject_img[x:x + patch_size[0],
                                    y:y + patch_size[1], :]

        x = int(math.floor(rel_x * (init_img.shape[0] - patch_size[0])))
        y = int(math.floor(rel_y * (init_img.shape[1] - patch_size[1])))
        init_patch = init_img[x:x + patch_size[0], y:y + patch_size[1], :]

        style_patch_scaled = nd.zoom(style_patch,
                                     (1.0 / scale, 1.0 / scale, 1),
                                     order=1)
        subject_patch_scaled = nd.zoom(subject_patch,
                                       (1.0 / scale, 1.0 / scale, 1),
                                       order=1)
        init_patch_scaled = nd.zoom(init_patch, (1.0 / scale, 1.0 / scale, 1),
                                    order=1)

        net = StyleNetwork(layers, to_bc01(init_patch_scaled),
                           to_bc01(subject_patch_scaled),
                           to_bc01(style_patch_scaled), subject_weights,
                           style_weights, args.smoothness)
        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.inner_it):
            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))

        result_patch_scaled = to_rgb(net.image) - init_patch_scaled
        result_patch = nd.zoom(result_patch_scaled, (scale, scale, 1), order=1)

        border_cut = 10
        init_img[x + border_cut:x + result_patch.shape[0] - border_cut,
                 y + border_cut:y + result_patch.shape[1] -
                 border_cut, :] += result_patch[border_cut:-border_cut,
                                                border_cut:-border_cut]
        imsave("%s.jpg" % args.output, init_img + pixel_mean)
Esempio n. 10
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())