1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Copyright (c) Microsoft. All rights reserved.

# Licensed under the MIT license. See LICENSE file in the project root for full license information.

if __name__ == '__main__':
    import os
    import argparse
    from PIL import Image
    import numpy as np
    import cv2

    import torch
    from torch.autograd import Variable
    from torch.utils.data import DataLoader

    from models.ExampleColorNet import ExampleColorNet

    from utils.util import mkdir_if_not, lab2rgb_transpose_mc

    from lib.TestDataset import TestDataset
    import lib.TestTransform as transforms

    parser = argparse.ArgumentParser()
    parser.add_argument('--data_root', default='', type=str)
    parser.add_argument('--test_model', type=str, default='models/example_net.pth')
    parser.add_argument('--gpu_id',type=int, default=0)
    parser.add_argument('--out_dir', type=str, default='output')
    parser.add_argument('--short_size', type=int, default=256)

    opt = parser.parse_args()
    mkdir_if_not(opt.out_dir)
    if opt.gpu_id >= 0:
        torch.cuda.set_device(opt.gpu_id)
    if opt.short_size > 0:
        test_dataset = TestDataset(opt.data_root,
                                   transform=transforms.Compose([transforms.Resize(opt.short_size),
                                                                 transforms.RGB2Lab(),
                                                                 transforms.ToTensor(),
                                                                 transforms.Normalize(),
                                                                 transforms.Concatenate()]))
    else:
        test_dataset = TestDataset(opt.data_root,
                                   transform=transforms.Compose([transforms.RGB2Lab(),
                                                                 transforms.ToTensor(),
                                                                 transforms.Normalize(),
                                                                 transforms.Concatenate()]))

    data_loader = DataLoader(test_dataset, batch_size=1, shuffle=False,
                             num_workers=1, pin_memory=True)

    color_net = ExampleColorNet(ic=13)
    assert os.path.exists(opt.test_model), 'cannot find the test model: %s ' % opt.test_model
    color_net.load_state_dict(torch.load(opt.test_model, map_location=lambda storage, loc: storage))
    color_net.eval()

    if opt.gpu_id >= 0:
        color_net.cuda()

    size_unit = 8
    for iter, data in enumerate(data_loader):
        orig_im_l, orig_err_ba, orig_warped_ba, orig_warped_aba, orig_im_ab, orig_err_ab = data
        out_name = test_dataset.get_out_name(iter)
        basename, ext = os.path.splitext(os.path.basename(out_name))
        print('testing for [%d/%d] %s ' % (iter, len(test_dataset), out_name))

        orig_h, orig_w = orig_im_l.size(2), orig_im_l.size(3)
        unit_h = int(orig_h/size_unit) * size_unit
        unit_w = int(orig_w/size_unit) * size_unit
        if unit_h != orig_h or unit_w != orig_w:
            im_l = torch.from_numpy(cv2.resize(orig_im_l[0].numpy().transpose((1,2,0)), (unit_w, unit_h))[np.newaxis,np.newaxis, ...])
            err_ba = torch.from_numpy(cv2.resize(orig_err_ba[0].numpy().transpose((1, 2, 0)), (unit_w, unit_h)).transpose((2, 0, 1))[np.newaxis, ...])
            warped_ba = torch.from_numpy(cv2.resize(orig_warped_ba[0].numpy().transpose((1, 2, 0)), (unit_w, unit_h)).transpose((2, 0, 1))[np.newaxis, ...])
            warped_aba = torch.from_numpy(cv2.resize(orig_warped_aba[0].numpy().transpose((1, 2, 0)), (unit_w, unit_h)).transpose((2, 0, 1))[np.newaxis, ...])
            im_ab = torch.from_numpy(cv2.resize(orig_im_ab[0].numpy().transpose((1, 2, 0)), (unit_w, unit_h)).transpose((2, 0, 1))[np.newaxis, ...])
            err_ab = torch.from_numpy(cv2.resize(orig_err_ab[0].numpy().transpose((1, 2, 0)), (unit_w, unit_h)).transpose((2, 0, 1))[np.newaxis, ...])
        else:
            im_l, err_ba, warped_ba, warped_aba, im_ab, err_ab = orig_im_l, orig_err_ba, orig_warped_ba, orig_warped_aba, orig_im_ab, orig_err_ab

        colornet_input = torch.cat((im_l, warped_ba[:, 1:, ...], err_ba, err_ab), dim=1)

        if opt.gpu_id >= 0:
            colornet_input_v = Variable(colornet_input.cuda())
        else:
            colornet_input_v = Variable(colornet_input)

        pred_ab_v = color_net(colornet_input_v)
        if unit_h != orig_h or unit_w != orig_w:
            pred_orig_ab = torch.from_numpy(cv2.resize(pred_ab_v.data[0].cpu().numpy().transpose((1, 2, 0)), (orig_w, orig_h), interpolation=cv2.INTER_CUBIC).transpose((2, 0, 1))[np.newaxis, ...])
        else:
            pred_orig_ab = pred_ab_v

        warpba_color_im = lab2rgb_transpose_mc(orig_im_l[0], pred_orig_ab[0])
        Image.fromarray(warpba_color_im).save(os.path.join(opt.out_dir, out_name))