-
Notifications
You must be signed in to change notification settings - Fork 12
/
eval.py
75 lines (64 loc) · 2.53 KB
/
eval.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
from data.nj import NJLoader
import logging
logger = logging.getLogger(__name__)
from utils.tools import *
from utils.my_tools import *
from ever.util.param_util import count_model_parameters
from module.viz import VisualizeSegmm
def evaluate_nj(model, cfg, is_training=False, ckpt_path=None, logger=None):
# torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.benchmark = False
# torch.backends.cudnn.enabled = False
if cfg.SNAPSHOT_DIR is not None:
vis_dir = os.path.join(cfg.SNAPSHOT_DIR, 'vis-{}'.format(os.path.basename(ckpt_path)))
palette = np.asarray(list(COLOR_MAP.values())).reshape((-1,)).tolist()
viz_op = VisualizeSegmm(vis_dir, palette)
if not is_training:
model_state_dict = torch.load(ckpt_path)
model.load_state_dict(model_state_dict, strict=True)
logger.info('[Load params] from {}'.format(ckpt_path))
count_model_parameters(model, logger)
model.eval()
eval_dataloader = NJLoader(cfg.EVAL_DATA_CONFIG)
metric_op = er.metric.PixelMetric(len(COLOR_MAP.keys()), logdir=cfg.SNAPSHOT_DIR, logger=logger)
with torch.no_grad():
for ret, ret_gt in tqdm(eval_dataloader):
ret = ret.to(torch.device('cuda'))
# cls = model(ret)
# slide predict
cls = pre_slide(model, ret, tta=False)
# cls = tta_predict(model, ret)
cls = cls.argmax(dim=1).cpu().numpy()
cls_gt = ret_gt['cls'].cpu().numpy().astype(np.int32)
mask = cls_gt >= 0
y_true = cls_gt[mask].ravel()
y_pred = cls[mask].ravel()
metric_op.forward(y_true, y_pred)
if cfg.SNAPSHOT_DIR is not None:
for fname, pred in zip(ret_gt['fname'], cls):
viz_op(pred, fname.replace('tif', 'png'))
metric_op.summary_all()
torch.cuda.empty_cache()
if __name__ == '__main__':
seed_torch(2333)
ckpt_path = './log/DCA/2rural/RURAL4000.pth'
from module.Encoder import Deeplabv2
model = Deeplabv2(dict(
backbone=dict(
resnet_type='resnet50',
output_stride=16,
pretrained=True,
),
multi_layer=True,
cascade=False,
use_ppm=True,
ppm=dict(
num_classes=7,
use_aux=False,
),
inchannels=2048,
num_classes=7
)).cuda()
cfg = import_config('st.my.2urban')
logger = get_console_file_logger(name='Baseline', logdir=cfg.SNAPSHOT_DIR)
evaluate_nj(model, cfg, False, ckpt_path, logger)