-
Notifications
You must be signed in to change notification settings - Fork 10
/
evaluate.py
154 lines (127 loc) · 5.98 KB
/
evaluate.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
# -*- coding: utf-8 -*-
# This repo is licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2022 Megvii Inc. All rights reserved.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import os
import argparse
import logging
# import megbrain to avoid dead lock bug
import megbrain
import megengine.distributed as dist
import megengine.functional as F
import dataset.data_loader as data_loader
from common import utils
from loss.losses import compute_losses, compute_metrics
from common.manager import Manager
from model import fetch_net
parser = argparse.ArgumentParser()
parser.add_argument('--model_dir', default='experiments', help="Directory containing params.json")
parser.add_argument('--restore_file', default='best', help="name of the file in --model_dir containing weights to load")
def evaluate(model, manager):
rank = dist.get_rank()
world_size = dist.get_world_size()
# set model to evaluation mode
model.eval()
# compute metrics over the dataset
if manager.dataloaders["val"] is not None:
# loss status and val status initial
manager.reset_loss_status()
manager.reset_metric_status("val")
for data_batch in manager.dataloaders["val"]:
# compute the real batch size
bs = data_batch["label"].shape[0]
# move to GPU if available
data_batch = utils.tensor_mge(data_batch)
# compute model output
output_batch = model(data_batch)
# compute all loss on this batch
loss = compute_losses(data_batch, output_batch, manager.params)
metrics = compute_metrics(data_batch, output_batch, manager.params)
if world_size > 1:
loss['total'] = F.distributed.all_reduce_sum(loss['total']) / world_size
metrics['psnr'] = F.distributed.all_reduce_sum(metrics['psnr']) / world_size
metrics['psnr_mu'] = F.distributed.all_reduce_sum(metrics['psnr_mu']) / world_size
manager.update_loss_status(loss, "val", bs)
# compute all metrics on this batch
manager.update_metric_status(metrics, "val", bs)
# update data to tensorboard
if rank == 0:
manager.writer.add_scalar("Loss/val", manager.loss_status["total"].avg, manager.epoch)
manager.logger.info("Loss/valid epoch {}: {}".format(manager.epoch, manager.loss_status['total'].avg))
for k, v in manager.val_status.items():
manager.writer.add_scalar("Metric/val/{}".format(k), v.avg, manager.epoch)
manager.logger.info("Metric/valid epoch {}: {}".format(manager.epoch, v.avg))
# For each epoch, print the metric
manager.print_metrics("val", title="Val", color="green")
def test(model, manager):
# set model to evaluation mode
model.eval()
# compute metrics over the dataset
if manager.dataloaders["val"] is not None:
# loss status and val status initial
manager.reset_loss_status()
manager.reset_metric_status("val")
for data_batch in manager.dataloaders["val"]:
# compute the real batch size
bs = data_batch["label"].shape[0]
# move to GPU if available
data_batch = utils.tensor_mge(data_batch)
# compute model output
output_batch = model(data_batch)
# compute all loss on this batch
loss = compute_losses(data_batch, output_batch, manager.params)
manager.update_loss_status(loss, "val", bs)
# compute all metrics on this batch
metrics = compute_metrics(data_batch, output_batch, manager.params)
manager.update_metric_status(metrics, "val", bs)
# For each epoch, update and print the metric
manager.print_metrics("val", title="Val", color="green")
if manager.dataloaders["test"] is not None:
# loss status and test status initial
manager.reset_loss_status()
manager.reset_metric_status("test")
for data_batch in manager.dataloaders["test"]:
# compute the real batch size
bs = data_batch["label"].shape[0]
# move to GPU if available
data_batch = utils.tensor_mge(data_batch)
# compute model output
output_batch = model(data_batch)
# compute all loss on this batch
loss = compute_losses(data_batch, output_batch, manager.params)
manager.update_loss_status(loss, "test", bs)
# compute all metrics on this batch
metrics = compute_metrics(data_batch, output_batch, manager.params)
manager.update_metric_status(metrics, "test", bs)
# For each epoch, print the metric
manager.print_metrics("test", title="Test", color="red")
if __name__ == '__main__':
# Load the parameters
args = parser.parse_args()
json_path = os.path.join(args.model_dir, 'params.json')
assert os.path.isfile(json_path), "No json configuration file found at {}".format(json_path)
params = utils.Params(json_path)
# Only load model weights
params.only_weights = True
# Update args into params
params.update(vars(args))
# Get the logger
logger = utils.set_logger(os.path.join(args.model_dir, 'evaluate.log'))
# Create the input data pipeline
logging.info("Creating the dataset...")
# Fetch dataloaders
dataloaders = data_loader.fetch_dataloader(params)
# Define the model and optimizer
model = fetch_net(params)
# Initial status for checkpoint manager
manager = Manager(model=model, optimizer=None, scheduler=None, params=params, dataloaders=dataloaders, writer=None, logger=logger)
# Reload weights from the saved file
manager.load_checkpoints()
# Test the model
logger.info("Starting test")
# Evaluate
test(model, manager)