-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
133 lines (119 loc) · 4.9 KB
/
demo.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#encoding=utf8
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import copy
import argparse
import os.path as osp
from re import I
import numpy as np
from PIL import Image
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
from skimage.color import rgb2lab, lab2rgb, rgb2gray
import paddle
import paddle.vision.transforms as transforms
from models.layers import decode
from models.model import Color_model
from models.torch_model import ONNXModel, ECCVGenerator
from utils.utils import preprocess_img, postprocess_tens, load_img
def load_image(image_path, transform=None):
image = Image.open(image_path)
if transform is not None:
image = transform(image)
image = np.asarray(image)
img_original = copy.deepcopy(image)
image_gray = rgb2lab(image)[:,:,0]
image = image_gray-50.
image = paddle.to_tensor(image).unsqueeze(0)
return img_original, image_gray, image
def main(args):
## Data
data_dir = osp.join(args.data_path, args.split)
if args.split == 'val':
with open('dataloader/cval.txt', 'r') as f:
files = f.readlines()
elif args.split == 'test':
with open('dataloader/ctest.txt', 'r') as f:
files = f.readlines()
file_list = []
for file in files:
file = file.strip().split(' ')[0]
file = file.split('/')[-1]
file_list.append(file)
## Model
if 'torch' in args.model_path:
if 'original' in args.model_path:
color_model = ECCVGenerator() # ECCV原始模型
else:
color_model = ONNXModel() # ECCV原始模型去除最后几个分支,输出为313通道概率
color_model.load_dict(paddle.load(args.model_path))
else:
color_model = Color_model()
color_model.load_dict(paddle.load(args.model_path)['state_dict'])
color_model.eval()
print("load pdparams successfully")
## Inference
for file in file_list:
if 'original' in args.model_path:
img_original = load_img(osp.join(data_dir, file))
(tens_l_orig, tens_l_rs) = preprocess_img(img_original, HW=(256,256))
# resize and concatenate to original L channel
img_gray = postprocess_tens(tens_l_orig, paddle.concat((0*tens_l_orig, 0*tens_l_orig), axis=1))
color_img = postprocess_tens(tens_l_orig, color_model(tens_l_rs).cpu())
else:
## 读取数据
img_original, img_gray, image = load_image(osp.join(data_dir, file))
img_gray = img_gray/100*255
img_gray = img_gray.astype(np.uint8)
image = paddle.cast(image, dtype='float32').unsqueeze(0)
## 获得ab量化空间预测
img_ab_313 = color_model(image)
## 将量化空间解码为原空间
color_img = decode(image, img_ab_313)
color_img = color_img*255.
color_img = color_img.astype(np.uint8)
## 保存结果
if not osp.exists(osp.join(args.save_path, args.split)):
os.mkdir(osp.join(args.save_path, args.split))
save_name = osp.join(args.save_path, args.split, file[:-4]+'png')
plt.figure(figsize=(10,3))
plt.subplot(1,3,1)
plt.imshow(img_original)
plt.axis('off')
plt.xticks([])
plt.yticks([])
plt.subplot(1,3,2)
plt.imshow(img_gray, cmap ='gray')
plt.axis('off')
plt.xticks([])
plt.yticks([])
plt.subplot(1,3,3)
plt.imshow(color_img)
plt.axis('off')
plt.xticks([])
plt.yticks([])
plt.tight_layout()
plt.savefig(save_name, dpi=600)
break
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Colorization!')
parser.add_argument('--data_path', type=str, default='/home/ubuntu/lsz/dataset/imagenet/ILSVRC/Resize',
help='path to dataset splits data folder')
parser.add_argument('--split', type=str, default='val', help='dataset split')
parser.add_argument('--model_path', default='saved_models/colornet_66000_checkpoint.pdparams', type=str, metavar='PATH',
help='path to latest checkpoint')
parser.add_argument('--save_path', type=str, default='results',
help='path to save results')
args = parser.parse_args()
with paddle.no_grad():
main(args)