-
Notifications
You must be signed in to change notification settings - Fork 36
/
test_image.py
128 lines (107 loc) · 4.18 KB
/
test_image.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
"""
Example Test:
python test_image.py \
--images-dir "PATH_TO_IMAGES_DIR" \
--result-dir "PATH_TO_RESULT_DIR" \
--pretrained-weight ./pretrained/SGHM-ResNet50.pth
Example Evaluation:
python test_image.py \
--images-dir "PATH_TO_IMAGES_DIR" \
--gt-dir "PATH_TO_GT_ALPHA_DIR" \
--result-dir "PATH_TO_RESULT_DIR" \
--pretrained-weight ./pretrained/SGHM-ResNet50.pth
"""
import argparse
import os
import glob
import cv2
import numpy as np
from PIL import Image
import torch
import torch.nn as nn
from torchvision.utils import save_image
from model.model import HumanSegment, HumanMatting
import utils
import inference
# --------------- Arguments ---------------
parser = argparse.ArgumentParser(description='Test Images')
parser.add_argument('--images-dir', type=str, required=True)
parser.add_argument('--result-dir', type=str, required=True)
parser.add_argument('--gt-dir', type=str, default=None)
parser.add_argument('--pretrained-weight', type=str, required=True)
args = parser.parse_args()
if not os.path.exists(args.pretrained_weight):
print('Cannot find the pretrained model: {0}'.format(args.pretrained_weight))
exit()
# --------------- Main ---------------
# Load Model
model = HumanMatting(backbone='resnet50')
device = 'cuda' if torch.cuda.is_available() else 'cpu'
if device == 'cuda':
model = nn.DataParallel(model).cuda().eval()
model.load_state_dict(torch.load(args.pretrained_weight))
else:
state_dict = torch.load(args.pretrained_weight, map_location="cpu")
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:]
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
model.eval()
print("Load checkpoint successfully ...")
# Load Images
image_list = sorted([*glob.glob(os.path.join(args.images_dir, '**', '*.jpg'), recursive=True),
*glob.glob(os.path.join(args.images_dir, '**', '*.png'), recursive=True)])
if args.gt_dir is not None:
gt_list = sorted([*glob.glob(os.path.join(args.gt_dir, '**', '*.jpg'), recursive=True),
*glob.glob(os.path.join(args.gt_dir, '**', '*.png'), recursive=True)])
num_image = len(image_list)
print("Find ", num_image, " images")
metric_mad = utils.MetricMAD()
metric_mse = utils.MetricMSE()
metric_grad = utils.MetricGRAD()
metric_conn = utils.MetricCONN()
metric_iou = utils.MetricIOU()
mean_mad = 0.0
mean_mse = 0.0
mean_grad = 0.0
mean_conn = 0.0
mean_iou = 0.0
# Process
for i in range(num_image):
image_path = image_list[i]
image_name = image_path[image_path.rfind('/')+1:image_path.rfind('.')]
print(i, '/', num_image, image_name)
with Image.open(image_path) as img:
img = img.convert("RGB")
if args.gt_dir is not None:
gt_path = gt_list[i]
gt_name = gt_path[gt_path.rfind('/')+1:gt_path.rfind('.')]
assert image_name == gt_name
with Image.open(gt_path) as gt_alpha:
gt_alpha = gt_alpha.convert("L")
gt_alpha = np.array(gt_alpha) / 255.0
# inference
pred_alpha, pred_mask = inference.single_inference(model, img, device=device)
# evaluation
if args.gt_dir is not None:
batch_mad = metric_mad(pred_alpha, gt_alpha)
batch_mse = metric_mse(pred_alpha, gt_alpha)
batch_grad = metric_grad(pred_alpha, gt_alpha)
batch_conn = metric_conn(pred_alpha, gt_alpha)
batch_iou = metric_iou(pred_alpha, gt_alpha)
print(" mad ", batch_mad, " mse ", batch_mse, " grad ", batch_grad, " conn ", batch_conn, " iou ", batch_iou)
mean_mad += batch_mad
mean_mse += batch_mse
mean_grad += batch_grad
mean_conn += batch_conn
mean_iou += batch_iou
# save results
output_dir = args.result_dir + image_path[len(args.images_dir):image_path.rfind('/')]
if not os.path.exists(output_dir):
os.makedirs(output_dir)
save_path = output_dir + '/' + image_name + '.png'
Image.fromarray(((pred_alpha * 255).astype('uint8')), mode='L').save(save_path)
print("Total mean mad ", mean_mad/num_image, " mean mse ", mean_mse/num_image, " mean grad ", \
mean_grad/num_image, " mean conn ", mean_conn/num_image, " mean iou ", mean_iou/num_image)