-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevaluate.py
158 lines (124 loc) · 5.97 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
155
156
157
158
# -*- coding: utf-8 -*-
# This repo is licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2021 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 json
import logging
import argparse
import megengine.distributed as dist
import megengine.functional as F
import model.net as net
import dataset.data_loader as data_loader
from easydict import EasyDict
from common import utils
from common.manager import Manager
from loss.losses import compute_losses, compute_metrics
parser = argparse.ArgumentParser()
parser.add_argument('--model_dir', default='experiments/base_model', 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["img1"].shape[0]
# move to GPU if available
data_batch = utils.tensor_mge(data_batch)
data_batch["imgs"] = F.concat([data_batch["img1"] / 255.0, data_batch["img2"] / 255.0], 1)
# compute model output
output_batch = model(data_batch)
# compute all loss on this batch
# loss = compute_losses(data_batch, output_batch, manager.params)
metrics = {}
metrics["EPE"] = compute_metrics(data_batch, output_batch)
if world_size > 1:
# loss['total'] = F.distributed.all_reduce_sum(loss['total']) / world_size
metrics['EPE'] = F.distributed.all_reduce_sum(metrics['EPE']) / world_size
# manager.update_loss_status(loss, "val", bs)
# compute all metrics on this batch
manager.update_metric_status(metrics, "val", bs)
# manager.print_metrics("val", title="Val", color="green")
# 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()
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["img1"].shape[0]
# move to GPU if available
data_batch = utils.tensor_mge(data_batch)
data_batch["imgs"] = F.concat([data_batch["img1"], data_batch["img2"]], 1)
# compute model output
output_batch = model(data_batch)
# compute all metrics on this batch
metrics = {}
# identity_batch = {"flow_fw": [F.zeros_like(data_batch["gyro_field"])]}
# metrics["I33"] = compute_metrics(data_batch, identity_batch)
# gyro_batch = {"flow_fw": [data_batch["gyro_field"]]}
# metrics["GyroField"] = compute_metrics(data_batch, gyro_batch)
metrics["EPE"] = compute_metrics(data_batch, output_batch)
if data_batch["label"][0] == "RE":
metrics["RE"] = compute_metrics(data_batch, output_batch)
elif data_batch["label"][0] == "Rain":
metrics["Rain"] = compute_metrics(data_batch, output_batch)
elif data_batch["label"][0] == "Dark":
metrics["Dark"] = compute_metrics(data_batch, output_batch)
elif data_batch["label"][0] == "Fog":
metrics["Fog"] = compute_metrics(data_batch, output_batch)
manager.update_metric_status(metrics, "test", bs)
manager.print_metrics("test", title="Test", color="red")
# For each epoch, print the metric
print("The average results are: ")
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)
with open(json_path) as f:
params = EasyDict(json.load(f))
# 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
params.eval_type = 'test'
dataloaders = data_loader.fetch_dataloader(params)
# Define the model and optimizer
model = net.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)