-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
172 lines (146 loc) · 5.47 KB
/
test.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# coding=utf-8
from argparse import ArgumentParser
import pickle
import numpy as np
import os.path as osp
from PIL import Image
from glob import glob
from tqdm import tqdm
from scipy import stats
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision.transforms import transforms
from dataset import monoSimDataset
from model.quality_model import MobileNetV2_Lite
class LoadConfig(object):
def __init__(self):
self.mode = 'test'
self.dataset_path = 'data/cx1'
self.model_path = "model/pretrained/1211_202056_MobileNetV2_Lite_cx2.pth"
self.cfg.result_path = ""
self.seed = 2248
self.batch_size = 24
self.device = "cuda:2"
self.num_workers = 2
self._change_cfg()
def _change_cfg(self):
parser = ArgumentParser()
for name, value in vars(self).items():
parser.add_argument('--' + name, type=type(value), default=value)
args = parser.parse_args()
for name, value in vars(args).items():
if self.__dict__[name] != value:
self.__dict__[name] = value
def test(cfg):
# cpu or gpu?
if torch.cuda.is_available() and cfg.device is not None:
device = torch.device(cfg.device)
else:
if not torch.cuda.is_available():
print("hey man, buy a GPU!")
device = torch.device("cpu")
# data
print('Loading Data')
test_data = monoSimDataset(path=cfg.dataset_path,
mode='test',
seed=cfg.seed,
debug_data=False)
test_data_loader = DataLoader(test_data,
cfg.batch_size,
shuffle=False,
drop_last=True,
num_workers=cfg.num_workers)
# configure model
print('Loading Model')
model = MobileNetV2_Lite()
model.to(device)
if cfg.model_path:
cp_data = torch.load(cfg.model_path, map_location=device)
try:
model.load_state_dict(cp_data['model'])
except Exception as e:
model.load_state_dict(cp_data['model'], strict=False)
print(e)
cp_data['cfg'] = '' if 'cfg' not in cp_data else cp_data['cfg']
print(cp_data['cfg'])
# Start!
model.eval()
with torch.no_grad():
test_pred_loss = 0
scores = np.zeros((1))
prediction = np.zeros((1))
for img, mask, target, _ in tqdm(
test_data_loader,
desc='Test',
bar_format='{desc}: {n_fmt}/{total_fmt} -{percentage:3.0f}%'):
img = img.to(device)
target = target.to(device)
pred, _ = model(img)
test_pred_loss += nn.functional.mse_loss(pred,
target,
reduction='sum')
scores = np.append(scores, target.cpu().numpy().reshape((-1)))
prediction = np.append(prediction,
pred.cpu().numpy().reshape((-1)))
test_pred_loss = test_pred_loss / len(test_data)
prediction = np.nan_to_num(prediction)
srocc = stats.spearmanr(prediction[1:], scores[1:])[0]
lcc = stats.pearsonr(prediction[1:], scores[1:])[0]
print("Test - MSE: {:.4e}".format(test_pred_loss))
print("Test - LCC: {:.4f}, SROCC: {:.4f}".format(lcc, srocc))
def predict(cfg):
# cpu or gpu?
if torch.cuda.is_available() and cfg.device is not None:
device = torch.device(cfg.device)
else:
if not torch.cuda.is_available():
print("hey man, buy a GPU!")
device = torch.device("cpu")
# data
img_list = glob(osp.join(cfg.dataset_path, '*.bmp')) + glob(
osp.join(cfg.dataset_path, '*.png')) + glob(
osp.join(cfg.dataset_path, '*.jpg'))
transform = transforms.Compose([
transforms.Resize(size),
transforms.ToTensor(),
transforms.Normalize(mean=[0.480], std=[0.200], inplace=False)
])
# configure model
print('Loading Model')
model = MobileNetV2_Lite()
model.to(device)
if cfg.model_path:
cp_data = torch.load(cfg.model_path, map_location=device)
try:
model.load_state_dict(cp_data['model'])
except Exception as e:
model.load_state_dict(cp_data['model'], strict=False)
print(e)
cp_data['cfg'] = '' if 'cfg' not in cp_data else cp_data['cfg']
print(cp_data['cfg'])
# Start!
model.eval()
prediction = {}
with torch.no_grad():
for path in tqdm(img_list):
img_name = osp.basename(path).split('.')[0]
img = transform(Image.open(path))
img = img.to(device)
target = target.to(device)
pred, heatmap = model(img)
prediction[img_name] = pred.cpu().numpy().reshape((-1))
if cfg.result_path:
heatmap = torch.softmax(heatmap, 0)[1, :, :].cpu().numpy()
heatmap = Image.fromarray(heatmap)
heatmap.save(
osp.join(cfg.result_path, img_name + '_heatmap.png'))
if cfg.result_path:
pickle.dump(prediction, osp.join(cfg.result_path, 'prediction.pkl'))
return prediction
if __name__ == '__main__':
cfg = LoadConfig()
if cfg.mode == 'test':
test(cfg)
elif cfg.mode == 'predict':
prediction = predict(cfg)