-
Notifications
You must be signed in to change notification settings - Fork 56
/
test_semantic.py
159 lines (139 loc) · 4.21 KB
/
test_semantic.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
"""
Script for semantic inference with pre-trained models
Author: Vivien Sainte Fare Garnot (github/VSainteuf)
License: MIT
"""
import argparse
import json
import os
import pprint
import numpy as np
import torch
import torch.nn as nn
import torch.utils.data as data
from src import utils, model_utils
from src.dataset import PASTIS_Dataset
from train_semantic import iterate, overall_performance, save_results, prepare_output
parser = argparse.ArgumentParser()
# Model parameters
parser.add_argument(
"--weight_folder",
type=str,
default="",
help="Path to the main folder containing the pre-trained weights",
)
parser.add_argument(
"--dataset_folder",
default="",
type=str,
help="Path to the folder where the results are saved.",
)
parser.add_argument(
"--res_dir",
default="./inference_utae",
type=str,
help="Path to directory where results are written."
)
parser.add_argument(
"--num_workers", default=8, type=int, help="Number of data loading workers"
)
parser.add_argument(
"--fold",
default=None,
type=int,
help="Do only one of the five fold (between 1 and 5)",
)
parser.add_argument(
"--device",
default="cuda",
type=str,
help="Name of device to use for tensor computations (cuda/cpu)",
)
parser.add_argument(
"--display_step",
default=50,
type=int,
help="Interval in batches between display of training metrics",
)
def main(config):
fold_sequence = [
[[1, 2, 3], [4], [5]],
[[2, 3, 4], [5], [1]],
[[3, 4, 5], [1], [2]],
[[4, 5, 1], [2], [3]],
[[5, 1, 2], [3], [4]],
]
np.random.seed(config.rdm_seed)
torch.manual_seed(config.rdm_seed)
device = torch.device(config.device)
prepare_output(config)
model = model_utils.get_model(config, mode="semantic")
model = model.to(device)
config.N_params = utils.get_ntrainparams(model)
print(model)
print("TOTAL TRAINABLE PARAMETERS :", config.N_params)
fold_sequence = (
fold_sequence if config.fold is None else [fold_sequence[config.fold - 1]]
)
for fold, (train_folds, val_fold, test_fold) in enumerate(fold_sequence):
if config.fold is not None:
fold = config.fold - 1
# Dataset definition
dt_test = PASTIS_Dataset(
folder=config.dataset_folder,
norm=True,
reference_date=config.ref_date,
mono_date=config.mono_date,
target="semantic",
sats=["S2"],
folds=test_fold,
)
collate_fn = lambda x: utils.pad_collate(x, pad_value=config.pad_value)
test_loader = data.DataLoader(
dt_test,
batch_size=config.batch_size,
shuffle=True,
drop_last=True,
collate_fn=collate_fn,
)
# Load weights
sd = torch.load(
os.path.join(config.weight_folder, "Fold_{}".format(fold+1), "model.pth.tar"),
map_location=device,
)
model.load_state_dict(sd["state_dict"])
# Loss
weights = torch.ones(config.num_classes, device=device).float()
weights[config.ignore_index] = 0
criterion = nn.CrossEntropyLoss(weight=weights)
# Inference
print("Testing . . .")
model.eval()
test_metrics, conf_mat = iterate(
model,
data_loader=test_loader,
criterion=criterion,
config=config,
optimizer=None,
mode="test",
device=device,
)
print(
"Loss {:.4f}, Acc {:.2f}, IoU {:.4f}".format(
test_metrics["test_loss"],
test_metrics["test_accuracy"],
test_metrics["test_IoU"],
)
)
save_results(fold + 1, test_metrics, conf_mat.cpu().numpy(), config)
if config.fold is None:
overall_performance(config)
if __name__ == "__main__":
test_config = parser.parse_args()
with open(os.path.join(test_config.weight_folder, "conf.json")) as file:
model_config = json.loads(file.read())
config = {**model_config, **vars(test_config)}
config = argparse.Namespace(**config)
config.fold = test_config.fold
pprint.pprint(config)
main(config)