Ejemplo n.º 1
0
def evaluate(args):
    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               size=args.content_size,
                                               keep_asp=True)
    content_image = content_image.unsqueeze(0)
    style = utils.tensor_load_rgbimage(args.style_image, size=args.style_size)
    style = style.unsqueeze(0)
    style = utils.preprocess_batch(style)

    vgg = Vgg16()
    utils.init_vgg16(args.vgg_model_dir)
    vgg.load_state_dict(
        torch.load(os.path.join(args.vgg_model_dir, "vgg16.weight")))

    style_model = HangSNetV1()
    style_model.load_state_dict(torch.load(args.model))

    if args.cuda:
        style_model.cuda()
        vgg.cuda()
        content_image = content_image.cuda()
        style = style.cuda()

    style_v = Variable(style, volatile=True)
    utils.subtract_imagenet_mean_batch(style_v)
    features_style = vgg(style_v)
    gram_style = [utils.gram_matrix(y) for y in features_style]

    content_image = Variable(utils.preprocess_batch(content_image))
    target = Variable(gram_style[2].data, requires_grad=False)
    style_model.setTarget(target)

    output = style_model(content_image)
    utils.tensor_save_bgrimage(output.data[0], args.output_image, args.cuda)
Ejemplo n.º 2
0
def evaluate(args):
    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               size=args.content_size,
                                               keep_asp=True)
    content_image = content_image.unsqueeze(0)
    style = utils.tensor_load_rgbimage(args.style_image, size=args.style_size)
    style = style.unsqueeze(0)
    style = utils.preprocess_batch(style)

    style_model = Net(ngf=args.ngf)
    style_model.load_state_dict(torch.load(args.model), False)

    if args.cuda:
        style_model.cuda()
        content_image = content_image.cuda()
        style = style.cuda()

    style_v = Variable(style)

    content_image = Variable(utils.preprocess_batch(content_image))
    style_model.setTarget(style_v)

    output = style_model(content_image)
    #output = utils.color_match(output, style_v)
    utils.tensor_save_bgrimage(output.data[0], args.output_image, args.cuda)
def evaluate(my_content_image, my_content_size, my_style_image,my_style_size, my_ngf, my_cuda, my_output_image, my_model):
    content_image = utils.tensor_load_rgbimage(my_content_image, size=my_content_size, keep_asp=True)
    content_image = content_image.unsqueeze(0)
    style = utils.tensor_load_rgbimage(my_style_image, size=my_style_size)
    style = style.unsqueeze(0)    
    style = utils.preprocess_batch(style)

    style_model = Net(ngf=my_ngf)
    model_dict = torch.load(my_model)
    model_dict_clone = model_dict.copy()
    for key, value in model_dict_clone.items():
        if key.endswith(('running_mean', 'running_var')):
            del model_dict[key]
    style_model.load_state_dict(model_dict, False)

    if my_cuda:
        style_model.cuda()
        content_image = content_image.cuda()
        style = style.cuda()

    style_v = Variable(style)

    content_image = Variable(utils.preprocess_batch(content_image))
    style_model.setTarget(style_v)

    output = style_model(content_image)
    #output = utils.color_match(output, style_v)
    utils.tensor_save_bgrimage(output.data[0], my_output_image, my_cuda)
    return utils.tensor_return_bgrimage(output.data[0], my_cuda)
Ejemplo n.º 4
0
def optimize(args):
    """    Gatys et al. CVPR 2017
    ref: Image Style Transfer Using Convolutional Neural Networks
    """
    # load the content and style target
    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               size=args.content_size,
                                               keep_asp=True)
    content_image = content_image.unsqueeze(0)
    content_image = Variable(utils.preprocess_batch(content_image),
                             requires_grad=False)
    content_image = utils.subtract_imagenet_mean_batch(content_image)
    style_image = utils.tensor_load_rgbimage(args.style_image,
                                             size=args.style_size)
    style_image = style_image.unsqueeze(0)
    style_image = Variable(utils.preprocess_batch(style_image),
                           requires_grad=False)
    style_image = utils.subtract_imagenet_mean_batch(style_image)

    # load the pre-trained vgg-16 and extract features
    vgg = Vgg16()
    utils.init_vgg16(args.vgg_model_dir)
    vgg.load_state_dict(
        torch.load(os.path.join(args.vgg_model_dir, "vgg16.weight")))
    if args.cuda:
        content_image = content_image.cuda()
        style_image = style_image.cuda()
        vgg.cuda()
    features_content = vgg(content_image)
    f_xc_c = Variable(features_content[1].data, requires_grad=False)
    features_style = vgg(style_image)
    gram_style = [utils.gram_matrix(y) for y in features_style]
    # init optimizer
    output = Variable(content_image.data, requires_grad=True)
    optimizer = Adam([output], lr=args.lr)
    mse_loss = torch.nn.MSELoss()
    # optimizing the images
    for e in range(args.iters):
        utils.imagenet_clamp_batch(output, 0, 255)
        optimizer.zero_grad()
        features_y = vgg(output)
        content_loss = args.content_weight * mse_loss(features_y[1], f_xc_c)

        style_loss = 0.
        for m in range(len(features_y)):
            gram_y = utils.gram_matrix(features_y[m])
            gram_s = Variable(gram_style[m].data, requires_grad=False)
            style_loss += args.style_weight * mse_loss(gram_y, gram_s)

        total_loss = content_loss + style_loss

        if (e + 1) % args.log_interval == 0:
            print(total_loss.data.cpu().numpy()[0])
        total_loss.backward()

        optimizer.step()
    # save the image
    output = utils.add_imagenet_mean_batch(output)
    utils.tensor_save_bgrimage(output.data[0], args.output_image, args.cuda)
Ejemplo n.º 5
0
def train_ofb(args):
    train_dataset = dataset.DAVISDataset(args.dataset, use_flow=True)
    train_loader = DataLoader(train_dataset, batch_size=1)

    transformer = transformer_net.TransformerNet(args.pad_type)
    transformer.train()
    optimizer = torch.optim.Adam(transformer.parameters(), args.lr)
    mse_loss = torch.nn.MSELoss()

    vgg = Vgg16()
    vgg.load_state_dict(
        torch.load(os.path.join(args.vgg_model_dir, "vgg16.weight")))
    vgg.eval()

    if args.cuda:
        transformer.cuda()
    vgg.cuda()
    mse_loss.cuda()

    style = utils.tensor_load_resize(args.style_image, args.style_size)
    style = style.unsqueeze(0)
    print("=> Style image size: " + str(style.size()))
    print("=> Pixel OFB loss weight: %f" % args.time_strength)

    style = utils.preprocess_batch(style)
    if args.cuda: style = style.cuda()
    style = utils.subtract_imagenet_mean_batch(style)
    features_style = vgg(style)
    gram_style = [utils.gram_matrix(y).detach() for y in features_style]

    train_loader.dataset.reset()
    transformer.train()
    transformer.cuda()
    agg_content_loss = agg_style_loss = agg_pixelofb_loss = 0.
    iters = 0
    anormaly = False
    elapsed_time = 0
    for batch_id, (x, flow, conf) in enumerate(tqdm(train_loader)):
        x, flow, conf = x[0], flow[0], conf[0]
        iters += 1

        optimizer.zero_grad()
        x = utils.preprocess_batch(x)  # (N, 3, 256, 256)
        if args.cuda:
            x = x.cuda()
            flow = flow.cuda()
            conf = conf.cuda()
        y = transformer(x)  # (N, 3, 256, 256)

        begin_time = time.time()
        warped_y, warped_y_mask = warp(y[1:], flow)
        warped_y = warped_y.detach()
        warped_y_mask *= conf
        pixel_ofb_loss = args.time_strength * weighted_mse(
            y[:-1], warped_y, warped_y_mask)
        pixel_ofb_loss.backward()
        elapsed_time += time.time() - begin_time
        if batch_id > 1000: break
    print(elapsed_time / float(batch_id + 1))
Ejemplo n.º 6
0
def style_transfer_thumbnail(thumb, model, save_path, save=True):
    thumb = thumb.unsqueeze(0)
    thumb = utils.preprocess_batch(thumb)
    init_thumbnail_instance_norm(model, collection=True)
    stylized_thumb = model.forward(thumb)
    if save:
        save_image(stylized_thumb, save_path)
Ejemplo n.º 7
0
def stylize(args):
    #content_image = utils.tensor_load_rgbimage(args.content_image, scale = args.content_scale)
    #content_image = content_image.unsqueeze(0)
    content_image = None
    if args.srcnn:
        content_image = utils.tensor_load_rgbimage(args.content_image,
                                                   scale=args.upsample)
    else:
        content_image = utils.tensor_load_rgbimage(args.content_image)
    content_image.unsqueeze_(0)
    if args.cuda:
        content_image = content_image.cuda()
    content_image = Variable(utils.preprocess_batch(content_image),
                             volatile=True)

    style_model = None
    if args.srcnn:
        style_model = SRCNN()
    else:
        style_model = TransformerNet(args.arch)
    ##style_model = TransformerNet()
    style_model.load_state_dict(torch.load(args.model))

    if args.cuda:
        style_model.cuda()

    output = style_model(content_image)
    utils.tensor_save_bgrimage(output.data[0], args.output_image, args.cuda)
Ejemplo n.º 8
0
def evaluate():
    if mx.context.num_gpus() > 0:
        ctx = mx.gpu()
    else:
        ctx = mx.cpu(0)

    # loading configs
    args = Options().parse()
    cfg = Configs(args.config_path)
    # set logging level
    logging.basicConfig(level=logging.INFO)

    # images
    content_image = tensor_load_rgbimage(cfg.content_image,
                                         ctx,
                                         size=cfg.val_img_size,
                                         keep_asp=True)
    style_image = tensor_load_rgbimage(cfg.style_image,
                                       ctx,
                                       size=cfg.val_style_size)
    style_image = preprocess_batch(style_image)
    # model
    style_model = Net(ngf=cfg.ngf)
    style_model.collect_params().load(cfg.val_model, ctx=ctx)
    # forward
    output = style_model(content_image, style_image)
    # save img
    tensor_save_bgrimage(output[0], cfg.output_img)
    logging.info("Save img to {}".format(cfg.output_img))
Ejemplo n.º 9
0
def predict(args):
    style_model = Net(ngf=args.ngf)
    model_dict = torch.load(args.model)
    model_dict_clone = model_dict.copy()
    for key, value in model_dict_clone.items():
        if key.endswith(('running_mean', 'running_var')):
            del model_dict[key]
    style_model.load_state_dict(model_dict, False)
    style_model.eval()
    if args.cuda:
        style_model.cuda()
    
    img = utils.tensor_load_rgbimage(args.image, (args.w, args.h))
    img = img.unsqueeze(0).float()
    if args.cuda:
        img = img.cuda()
    img = Variable(img)
    
    style = utils.tensor_load_rgbimage(args.style_image, args.style_size)
    style = style.unsqueeze(0)
    style = utils.preprocess_batch(style)
    if args.cuda:
        style = style.cuda()
    style = Variable(style, requires_grad=False)
    style_model.set_target(style)
    
    img = style_model(img)
    
    utils.tensor_save_rgbimage(img, args.out_image, args.cuda)
Ejemplo n.º 10
0
def stylize(args):
    img = None
    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               scale=args.content_scale)
    content_image = content_image.unsqueeze(0)
    style_model = TransformerNet()
    style_model.load_state_dict(torch.load(args.model))
    cam = cv2.VideoCapture(0)
    for x in range(0, 150):
        ret_val, img13 = cam.read()
        content_image = utils.tensor_load_rgbimage_cam(
            img13, scale=args.content_scale)
        content_image = content_image.unsqueeze(0)
        if args.cuda:
            content_image = content_image.cuda()
        content_image2 = Variable(utils.preprocess_batch(content_image),
                                  volatile=True)

        if args.cuda:
            style_model.cuda()

        output = style_model(content_image2)

        im = utils.tensor_ret_bgrimage(output.data[0], args.output_image,
                                       args.cuda)
        if img is None:
            img = pl.imshow(im)
        else:
            img.set_data(im)
        pl.pause(.1)
        pl.draw()
Ejemplo n.º 11
0
def stylize_onnx_caffe2(args):
    """
    Read ONNX model and run it using Caffe2
    """

    assert not args.export_onnx

    # TODO: change tools to run without PyTorch
    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               scale=args.content_scale)
    content_image = content_image.unsqueeze(0)
    content_image = utils.preprocess_batch(content_image).numpy()

    import onnx
    import onnx_caffe2.backend

    model = onnx.load(args.model)

    prepared_backend = onnx_caffe2.backend.prepare(
        model, device='CUDA' if args.cuda else 'CPU')
    if args.half:
        for op in prepared_backend.predict_net.op:
            op.engine = 'CUDNN'
        content_image = content_image.astype(np.float16)
    inp = {model.graph.input[0].name: content_image}
    c2_out = prepared_backend.run(inp)[0]

    if args.half:
        c2_out = c2_out.astype(np.float32)
    output = torch.from_numpy(c2_out)

    utils.tensor_save_rgbimage(output[0], args.output_image, args.cuda)
Ejemplo n.º 12
0
def fast_evaluate(args, basedir, contents, idx=0):
    # basedir to save the data
    style_model = Net(ngf=args.ngf)
    style_model.load_state_dict(torch.load(args.model), False)
    style_model.eval()
    if args.cuda:
        style_model.cuda()

    style_loader = StyleLoader(args.style_folder,
                               args.style_size,
                               cuda=args.cuda)

    for content_image in contents:
        idx += 1
        content_image = utils.tensor_load_rgbimage(content_image,
                                                   size=args.content_size,
                                                   keep_asp=True).unsqueeze(0)
        if args.cuda:
            content_image = content_image.cuda()
        content_image = Variable(utils.preprocess_batch(content_image))

        for isx in range(style_loader.size()):
            style_v = Variable(style_loader.get(isx).data)
            style_model.setTarget(style_v)
            output = style_model(content_image)
            filename = os.path.join(basedir, "{}_{}.png".format(idx, isx + 1))
            utils.tensor_save_bgrimage(output.data[0], filename, args.cuda)
            print(filename)
Ejemplo n.º 13
0
def main():
    args = Options().parse()
    style_model = Net(ngf=args.ngf)
    model_dict = torch.load(args.model)
    model_dict_clone = model_dict.copy()
    for key, value in model_dict_clone.items():
        if key.endswith(('running_mean', 'running_var')):
            del model_dict[key]
    style_model.load_state_dict(model_dict, False)

    style_loaders = utils.StyleLoader(args.style_folder, args.style_size)
    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               size=args.style_size,
                                               keep_asp=True)
    content_image = content_image.unsqueeze(0)
    if args.cuda:
        style_model.cuda()
        content_image = content_image.cuda()

    content_image = Variable(utils.preprocess_batch(content_image))

    # for i, style_loader in enumerate(style_loaders):
    for i in range(style_loaders.size()):
        print(i)
        style_v = style_loaders.get(i)
        style_model.setTarget(style_v)
        output = style_model(content_image)
        filepath = "out/output" + str(i + 1) + '.jpg'
        print(filepath)
        utils.tensor_save_bgrimage(output.data[0], filepath, args.cuda)
def stylize(args):
    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               scale=args.content_scale)
    content_image = content_image.unsqueeze(0)
    content_image = Variable(utils.preprocess_batch(content_image))
    style_model = torch.load(args.model)
    output = style_model(content_image)
    utils.tensor_save_bgrimage(output.data[0], args.output_image)
Ejemplo n.º 15
0
def evaluate(raw_content_image, raw_content_size, style_image, style_size,
             cuda, output_name):
    content_image = utils.tensor_load_rgbimage(raw_content_image,
                                               size=raw_content_size,
                                               keep_asp=True)
    content_image = content_image.unsqueeze(0)
    style = utils.tensor_load_rgbimage(style_image, size=style_size)
    style = style.unsqueeze(0)
    style = utils.preprocess_batch(style)

    style_v = Variable(style)

    content_image = Variable(utils.preprocess_batch(content_image))
    style_model.setTarget(style_v)

    output = style_model(content_image)
    transfer_image = utils.tensor_save_bgrimage(output.data[0], output_name,
                                                cuda)
    return transfer_image
Ejemplo n.º 16
0
def evaluate(model_dir, c_img, s_img, img_size, out_img):
    content_image = utils.tensor_load_rgbimage(c_img, size=img_size, keep_asp=True)
    content_image = content_image.unsqueeze(0).to(device)
    style = utils.tensor_load_rgbimage(s_img, size=img_size)
    style = style.unsqueeze(0)    
    style = utils.preprocess_batch(style).to(device)

    style_model = Net(ngf=FILTER_CHANNEL, dv=device).to(device)
    style_model.load_state_dict(torch.load(model_dir), False)
    
    style_v = Variable(style)

    content_image = Variable(utils.preprocess_batch(content_image))
    style_model.setTarget(style_v)

    output = style_model(content_image)
    #output = utils.color_match(output, style_v)
    utils.tensor_save_bgrimage(output.data[0], out_img)
    print ('Done')
Ejemplo n.º 17
0
	def get(self, i):
		idx = i%len(self.files)
		filepath = os.path.join(self.folder, self.files[idx])
		style = utils.tensor_load_rgbimage(filepath, self.style_size)	
		style = style.unsqueeze(0)
		style = utils.preprocess_batch(style)
		if self.cuda:
			style = style.cuda()
		style_v = Variable(style, requires_grad=False)
		return style_v
Ejemplo n.º 18
0
def run_demo(eval_args):

    ## load parameters

    #content_image = 'images/content/xjtlu.jpg'
    #style_image = 'images/styles/starry_night.jpg'
    #eval_args = program_args(content_image,content_image,style_image,128,128,0)
    #eval_args = camero_args(style_image)

    if eval_args.cuda == 0:
        ctx = mx.cpu()
    else:
        ctx = mx.gpu()

    ## Change the content and style image using Style Loader
    #content_image = utils.tensor_load_rgbimage(eval_args.contentImage, ctx, size=eval_args.size, keep_asp=True)
    style_image = utils.tensor_load_rgbimage(eval_args.styleImage,
                                             ctx,
                                             size=eval_args.size)
    style_image = utils.preprocess_batch(style_image)

    style_model = net.Net(ngf=eval_args.ngf)
    style_model.load_parameters(eval_args.model, ctx=ctx)
    style_model.set_target(style_image)

    cam = cv2.VideoCapture(0)

    while True:
        ## read frame
        ret, frame = cam.read()
        # read content image (cimg)
        #cimg = img.copy()
        #img = np.array(img).transpose(2, 0, 1)
        content_img = load_image(frame, ctx, eval_args.size)

        output = style_model(content_img)
        tensor = output[0]
        #(b, g, r) = F.split(tensor, num_outputs=3, axis=0)
        #tensor = F.concat(r, g, b, dim=0)
        img = F.clip(tensor, 0, 255).asnumpy()
        img = img.transpose(1, 2, 0).astype('uint8')
        img = Image.fromarray(img)
        image = np.array(
            img.resize((frame.shape[1], frame.shape[0]), Image.ANTIALIAS))
        #print(frame.shape,image.shape)
        numpy_horizontal = np.hstack((frame, image))
        #cv2.imshow("Content Window",frame)
        #cv2.imshow("Style Window",grey)

        cv2.imshow("Test Window Shape", numpy_horizontal)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cam.release()
    cv2.destroyAllWindows()
Ejemplo n.º 19
0
def stylize(args):
    if args.model.endswith(".onnx"):
        return stylize_onnx_caffe2(args)

    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               scale=args.content_scale)
    content_image = content_image.unsqueeze(0)

    if args.cuda:
        content_image = content_image.cuda()
    content_image = Variable(utils.preprocess_batch(content_image),
                             requires_grad=False)
    style_model = TransformerNet()
    state_dict = torch.load(args.model)

    #    removed_modules = ['in2']
    in_names = [
        "in1.scale", "in1.shift", "in2.scale", "in2.shift", "in3.scale",
        "in3.shift", "res1.in1.scale", "res1.in1.shift", "res1.in2.scale",
        "res1.in2.shift", "res2.in1.scale", "res2.in1.shift", "res2.in2.scale",
        "res2.in2.shift", "res3.in1.scale", "res3.in1.shift", "res3.in2.scale",
        "res3.in2.shift", "res4.in1.scale", "res4.in1.shift", "res4.in2.scale",
        "res4.in2.shift", "res5.in1.scale", "res5.in1.shift", "res5.in2.scale",
        "res5.in2.shift", "in4.scale", "in4.shift", "in5.scale", "in5.shift"
    ]

    #   kl = list(state_dict.keys())
    #   for k in kl:

    for k in in_names:
        state_dict[k.replace("scale",
                             "weight").replace("shift",
                                               "bias")] = state_dict.pop(k)

    style_model.load_state_dict(state_dict)

    if args.cuda:
        style_model.cuda()

    if args.half:
        style_model.half()
        content_image = content_image.half()

    if args.export_onnx:
        assert args.export_onnx.endswith(
            ".onnx"), "Export model file should end with .onnx"
        output = torch.onnx._export(style_model, content_image,
                                    args.export_onnx)
    else:
        output = style_model(content_image)

    if args.half:
        output = output.float()

    utils.tensor_save_bgrimage(output.data[0], args.output_image, args.cuda)
Ejemplo n.º 20
0
  def _init_inputs(self):
    preproc_func = self.preproc_func
    input_shape = self.input_shape
    # Define input TF placeholder
    with tf.device('/gpu:0'):
      x_pre = tf.placeholder(tf.float32, shape=input_shape, name='x')
      x = preprocess_batch(x_pre, preproc_func)
      y = tf.placeholder(tf.float32, shape=(self.batch_size, 10),
                         name='y')

    self.g0_inputs = {'x_pre': x_pre, 'x': x, 'y': y}
Ejemplo n.º 21
0
    def _init_inputs(self):
        preproc_func = self.preproc_func
        input_shape = self.input_shape
        # Define input TF placeholder
        with tf.device('/gpu:0'):
            x_pre = tf.placeholder(tf.float32, shape=input_shape, name='x')
            x = preprocess_batch(x_pre, preproc_func)
            y = tf.placeholder(tf.float32, shape=(self.batch_size, 10),
                               name='y')

        self.g0_inputs = {'x_pre': x_pre, 'x': x, 'y': y}
Ejemplo n.º 22
0
def evaluate(args):
    # set output_image
    dirname = os.path.dirname(args.content_image)
    style_ = os.path.basename(args.style_image).split('.')[0]
    basename = style_ + '_' + os.path.basename(args.content_image)
    args.output_image = os.path.join(dirname, basename)

    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               size=args.content_size,
                                               keep_asp=True)
    content_image = content_image.unsqueeze(0)
    style = utils.tensor_load_rgbimage(args.style_image, size=args.style_size)
    style = style.unsqueeze(0)
    style = utils.preprocess_batch(style)

    model_dict = torch.load(args.model)
    model_dict_clone = model_dict.copy()  # We can't mutate while iterating

    for key, value in model_dict_clone.items():
        if key.endswith(('running_mean', 'running_var')):
            del model_dict[key]

    style_model = Net(ngf=args.ngf)
    style_model.load_state_dict(model_dict, False)

    #  style_model = Net(ngf=args.ngf)
    #  style_model.load_state_dict(torch.load(args.model), False)

    if args.cuda:
        style_model.cuda()
        content_image = content_image.cuda()
        style = style.cuda()

    style_v = Variable(style)

    content_image = Variable(utils.preprocess_batch(content_image))
    style_model.setTarget(style_v)

    output = style_model(content_image)
    #output = utils.color_match(output, style_v)
    utils.tensor_save_bgrimage(output.data[0], args.output_image, args.cuda)
Ejemplo n.º 23
0
    def _init_inputs(self):
        preproc_func = self.preproc_func
        input_shape = self.input_shape
        # Define input TF placeholder
        with tf.device("/gpu:0"):
            x_pre = tf.placeholder(tf.float32, shape=input_shape, name="x")
            x = preprocess_batch(x_pre, preproc_func)
            y = tf.placeholder(tf.float32,
                               shape=(self.batch_size, 10),
                               name="y")

        self.g0_inputs = {"x_pre": x_pre, "x": x, "y": y}
Ejemplo n.º 24
0
    def transfer(self):
        if self.is_first:
            content_img, style_img, content_source_size = load_images_norm(
                config.TARGET_SIZE1)
            # stylize
            self.transfer1.build_model(content_img, style_img)
            stylized_img = self.transfer1.run(config.NUM_STEPS)

            save_image_norm(stylized_img, content_source_size)
        else:
            content_img, style_img, content_source_size = load_images_rgb(
                self.style_num, config.TARGET_SIZE2)
            # stylize
            style_var = Variable(preprocess_batch(style_img))
            content_var = Variable(preprocess_batch(content_img))

            self.transfer2.setTarget(style_var)
            stylized_img = self.transfer2(content_var).detach()

            save_image_rgb(stylized_img, content_source_size)

        return True
Ejemplo n.º 25
0
def evaluate(args):
    content_image = utils.tensor_load_rgbimage(args.content_image, size=args.content_size, keep_asp=True)
    content_image = content_image.unsqueeze(0)
    style = utils.tensor_load_rgbimage(args.style_image, size=args.style_size)
    style = style.unsqueeze(0)
    style = utils.preprocess_batch(style)

    style_model = Net(ngf=args.ngf) # comment out for PyTorch 0.4
    style_model.load_state_dict(torch.load(args.model), False) # comment out for PyTorch 0.4

    # https://github.com/zhanghang1989/PyTorch-Multi-Style-Transfer/issues/21
    # Compatibility shim for PyTorch 0.4

    # model_dict = torch.load('models/icons2.model') # uncomment for PyTorch 0.4
    # model_dict_clone = model_dict.copy() # uncomment for PyTorch 0.4

    # for key, value in model_dict_clone.items(): # uncomment for PyTorch 0.4
    #     if key.endswith(('running_mean', 'running_var')): # uncomment for PyTorch 0.4
    #         del model_dict[key] # uncomment for PyTorch 0.4

    ### Next cell

    #style_model = Net(ngf=128)
    #style_model.load_state_dict(model_dict, False)

    if args.cuda:
        style_model.cuda()
        content_image = content_image.cuda()
        style = style.cuda()

    style_v = Variable(style)

    content_image = Variable(utils.preprocess_batch(content_image))
    style_model.setTarget(style_v)

    output = style_model(content_image)
    #output = utils.color_match(output, style_v)
    utils.tensor_save_bgrimage(output.data[0], args.output_image, args.cuda)
Ejemplo n.º 26
0
def evaluate(args):
    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               size=args.content_size,
                                               keep_asp=True)
    content_image = content_image.unsqueeze(0)
    style = utils.tensor_load_rgbimage(args.style_image, size=args.style_size)
    style = style.unsqueeze(0)
    style = utils.preprocess_batch(style)

    # style_model = Net(ngf=args.ngf)

    model_dict = torch.load(
        args.model
    )  # or args.resume, matching what's in the line with style_model.load_state_dict
    model_dict_clone = model_dict.copy()  # We can't mutate while iterating
    for key, value in model_dict_clone.items():
        if key.endswith(('running_mean', 'running_var')):
            del model_dict[key]

    style_model = Net(ngf=128)  # to run with torch-0.3.0.post4

    # style_model.load_state_dict(torch.load(args.model), False)
    style_model.load_state_dict(model_dict, False)

    if args.cuda:
        style_model.cuda()
        content_image = content_image.cuda()
        style = style.cuda()

    style_v = Variable(style)

    content_image = Variable(utils.preprocess_batch(content_image))
    style_model.setTarget(style_v)

    output = style_model(content_image)
    #output = utils.color_match(output, style_v)
    utils.tensor_save_bgrimage(output.data[0], args.output_image, args.cuda)
Ejemplo n.º 27
0
def evaluate(args):
    if args.cuda:
        ctx = mx.gpu(0)
    else:
        ctx = mx.cpu(0)
    # images
    content_image = utils.tensor_load_rgbimage(args.content_image,ctx, size=args.content_size, keep_asp=True)
    style_image = utils.tensor_load_rgbimage(args.style_image, ctx, size=args.style_size)
    style_image = utils.preprocess_batch(style_image)
    # model
    style_model = net.Net(ngf=args.ngf)
    style_model.collect_params().load(args.model, ctx=ctx)
    # forward
    style_model.setTarget(style_image)
    output = style_model(content_image)
    utils.tensor_save_bgrimage(output[0], args.output_image, args.cuda)
Ejemplo n.º 28
0
def stylize(args):
    content_image = utils.tensor_load_rgbimage(args.content_image,
                                               scale=args.content_scale)
    content_image = content_image.unsqueeze(0)

    if args.cuda:
        content_image = content_image.cuda()
    content_image = Variable(utils.preprocess_batch(content_image))
    style_model = TransformerNet()
    style_model.load_state_dict(torch.load(args.model))

    if args.cuda:
        style_model.cuda()

    output = style_model(content_image)
    utils.tensor_save_bgrimage(output.data[0], args.output_image, args.cuda)
Ejemplo n.º 29
0
def stylize(content_image, model, content_scale=None, cuda=0):
    content_image = utils.tensor_load_rgbimage(content_image,
                                               scale=content_scale)
    content_image = content_image.unsqueeze(0)

    if cuda:
        content_image = content_image.cuda()
    content_image = Variable(utils.preprocess_batch(content_image),
                             volatile=True)
    style_model = TransformerNet()
    style_model.load_state_dict(torch.load(model))

    if cuda:
        style_model.cuda()

    output = style_model(content_image)
    return utils.tensor_to_Image(output, cuda)
Ejemplo n.º 30
0
def fast_evaluate(args, basedir, contents, idx=0):
    # basedir to save the data
    # style_model = Net(ngf=args.ngf)

    model_dict = torch.load(
        args.model
    )  # or args.resume, matching what's in the line with style_model.load_state_dict
    model_dict_clone = model_dict.copy()  # We can't mutate while iterating
    for key, value in model_dict_clone.items():
        if key.endswith(('running_mean', 'running_var')):
            del model_dict[key]

    style_model = Net(ngf=128)  # to run with torch-0.3.0.post4

    # style_model.load_state_dict(torch.load(args.model), False)
    style_model.load_state_dict(model_dict, False)
    style_model.eval()
    if args.cuda:
        style_model.cuda()

    style_loader = StyleLoader(args.style_folder,
                               args.style_size,
                               cuda=args.cuda)

    for content_image in contents:
        idx += 1
        content_image = utils.tensor_load_rgbimage(content_image,
                                                   size=args.content_size,
                                                   keep_asp=True).unsqueeze(0)
        if args.cuda:
            content_image = content_image.cuda()
        content_image = Variable(utils.preprocess_batch(content_image))

        for isx in range(style_loader.size()):
            style_v = Variable(style_loader.get(isx).data)
            style_model.setTarget(style_v)
            output = style_model(content_image)
            filename = os.path.join(basedir, "{}_{}.png".format(idx, isx + 1))
            utils.tensor_save_bgrimage(output.data[0], filename, args.cuda)
            print(filename)
Ejemplo n.º 31
0
def evaluate_test():
    content_image = 'images/content/xjtlu.jpg'
    style_image = 'images/styles/starry_night.jpg'
    output_image = 'test_output.jpg'
    cuda = 0
    eval_args = program_args(content_image,content_image,style_image,128,128,0)

    if cuda == 0:
        ctx = mx.cpu(0)
    else:
        ctx = mx.gpu(0)

    content_image = utils.tensor_load_rgbimage(eval_args.contentImage, ctx, size=eval_args.size, keep_asp=True)
    style_image = utils.tensor_load_rgbimage(eval_args.styleImage, ctx, size=eval_args.size)
    style_image = utils.preprocess_batch(style_image)
    # model
    style_model = net.Net(ngf=eval_args.ngf)
    style_model.load_parameters(eval_args.model, ctx=ctx)
    # forward
    style_model.set_target(style_image)
    output = style_model(content_image)
    
    tensor = output[0]
    #(b, g, r) = F.split(tensor, num_outputs=3, axis=0)
    #tensor = F.concat(r, g, b, dim=0)
    img = F.clip(tensor, 0, 255).asnumpy()
    img = img.transpose(1, 2, 0).astype('uint8')
    img = Image.fromarray(img)
    #print(type(output[0]),output[0].asnumpy().shape)
    #np_array = output.asnumpy()
    #img_array = np.reshape(np_array,(np_array.shape[2],np_array.shape[3],np_array.shape[1]))
    #print(img_array.shape)
    #img = Image.fromarray(np.uint8(img_array))  

    original = Image.open(content_image_path)
    style = Image.open(style_image_path)
    style.show()
    original.show()
    img.show()
Ejemplo n.º 32
0
def train(args):
    np.random.seed(args.seed)
    if args.cuda:
        ctx = mx.gpu(0)
    else:
        ctx = mx.cpu(0)
    # dataloader
    transform = utils.Compose([utils.Scale(args.image_size),
                               utils.CenterCrop(args.image_size),
                               utils.ToTensor(ctx),
                               ])
    train_dataset = data.ImageFolder(args.dataset, transform)
    train_loader = gluon.data.DataLoader(train_dataset, batch_size=args.batch_size, last_batch='discard')
    style_loader = utils.StyleLoader(args.style_folder, args.style_size, ctx=ctx)
    print('len(style_loader):',style_loader.size())
    # models
    vgg = net.Vgg16()
    utils.init_vgg_params(vgg, 'models', ctx=ctx)
    style_model = net.Net(ngf=args.ngf)
    style_model.initialize(init=mx.initializer.MSRAPrelu(), ctx=ctx)
    if args.resume is not None:
        print('Resuming, initializing using weight from {}.'.format(args.resume))
        style_model.collect_params().load(args.resume, ctx=ctx)
    print('style_model:',style_model)
    # optimizer and loss
    trainer = gluon.Trainer(style_model.collect_params(), 'adam',
                            {'learning_rate': args.lr})
    mse_loss = gluon.loss.L2Loss()

    for e in range(args.epochs):
        agg_content_loss = 0.
        agg_style_loss = 0.
        count = 0
        for batch_id, (x, _) in enumerate(train_loader):
            n_batch = len(x)
            count += n_batch
            # prepare data
            style_image = style_loader.get(batch_id)
            style_v = utils.subtract_imagenet_mean_preprocess_batch(style_image.copy())
            style_image = utils.preprocess_batch(style_image)

            features_style = vgg(style_v)
            gram_style = [net.gram_matrix(y) for y in features_style]

            xc = utils.subtract_imagenet_mean_preprocess_batch(x.copy())
            f_xc_c = vgg(xc)[1]
            with autograd.record():
                style_model.setTarget(style_image)
                y = style_model(x)

                y = utils.subtract_imagenet_mean_batch(y)
                features_y = vgg(y)

                content_loss = 2 * args.content_weight * mse_loss(features_y[1], f_xc_c)

                style_loss = 0.
                for m in range(len(features_y)):
                    gram_y = net.gram_matrix(features_y[m])
                    _, C, _ = gram_style[m].shape
                    gram_s = F.expand_dims(gram_style[m], 0).broadcast_to((args.batch_size, 1, C, C))
                    style_loss = style_loss + 2 * args.style_weight * mse_loss(gram_y, gram_s[:n_batch, :, :])

                total_loss = content_loss + style_loss
                total_loss.backward()
                
            trainer.step(args.batch_size)
            mx.nd.waitall()

            agg_content_loss += content_loss[0]
            agg_style_loss += style_loss[0]

            if (batch_id + 1) % args.log_interval == 0:
                mesg = "{}\tEpoch {}:\t[{}/{}]\tcontent: {:.3f}\tstyle: {:.3f}\ttotal: {:.3f}".format(
                    time.ctime(), e + 1, count, len(train_dataset),
                                agg_content_loss.asnumpy()[0] / (batch_id + 1),
                                agg_style_loss.asnumpy()[0] / (batch_id + 1),
                                (agg_content_loss + agg_style_loss).asnumpy()[0] / (batch_id + 1)
                )
                print(mesg)

            
            if (batch_id + 1) % (4 * args.log_interval) == 0:
                # save model
                save_model_filename = "Epoch_" + str(e) + "iters_" + str(count) + "_" + str(time.ctime()).replace(' ', '_') + "_" + str(
                    args.content_weight) + "_" + str(args.style_weight) + ".params"
                save_model_path = os.path.join(args.save_model_dir, save_model_filename)
                style_model.collect_params().save(save_model_path)
                print("\nCheckpoint, trained model saved at", save_model_path)

    # save model
    save_model_filename = "Final_epoch_" + str(args.epochs) + "_" + str(time.ctime()).replace(' ', '_') + "_" + str(
        args.content_weight) + "_" + str(args.style_weight) + ".params"
    save_model_path = os.path.join(args.save_model_dir, save_model_filename)
    style_model.collect_params().save(save_model_path)
    print("\nDone, trained model saved at", save_model_path)