-
Notifications
You must be signed in to change notification settings - Fork 12
/
eval_vigor_cross.py
153 lines (111 loc) · 5.85 KB
/
eval_vigor_cross.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
import os
import torch
from dataclasses import dataclass
from torch.utils.data import DataLoader
from sample4geo.dataset.vigor import VigorDatasetEval
from sample4geo.transforms import get_transforms_val
from sample4geo.evaluate.vigor import evaluate
from sample4geo.model import TimmModel
@dataclass
class Configuration:
# Model
model: str = 'convnext_base.fb_in22k_ft_in1k_384'
# Override model image size
img_size: int = 384
# Evaluation
batch_size: int = 128
verbose: bool = True
gpu_ids: tuple = (0,)
normalize_features: bool = True
# Dataset
data_folder = "./data/VIGOR"
same_area: bool = False # True: same | False: cross
ground_cutting = 0 # cut ground upper and lower
# Checkpoint to start from
checkpoint_start = 'pretrained/vigor_cross/convnext_base.fb_in22k_ft_in1k_384/weights_e40_0.6109.pth'
# set num_workers to 0 if on Windows
num_workers: int = 0 if os.name == 'nt' else 4
# train on GPU if available
device: str = 'cuda' if torch.cuda.is_available() else 'cpu'
#-----------------------------------------------------------------------------#
# Config #
#-----------------------------------------------------------------------------#
config = Configuration()
if __name__ == '__main__':
#-----------------------------------------------------------------------------#
# Model #
#-----------------------------------------------------------------------------#
print("\nModel: {}".format(config.model))
model = TimmModel(config.model,
pretrained=True,
img_size=config.img_size)
data_config = model.get_config()
print(data_config)
mean = data_config["mean"]
std = data_config["std"]
img_size = config.img_size
image_size_sat = (img_size, img_size)
new_width = img_size*2
new_hight = int(((1024 - 2 * config.ground_cutting) / 2048) * new_width)
img_size_ground = (new_hight, new_width)
# load pretrained Checkpoint
if config.checkpoint_start is not None:
print("Start from:", config.checkpoint_start)
model_state_dict = torch.load(config.checkpoint_start)
model.load_state_dict(model_state_dict, strict=False)
# Data parallel
print("GPUs available:", torch.cuda.device_count())
if torch.cuda.device_count() > 1 and len(config.gpu_ids) > 1:
model = torch.nn.DataParallel(model, device_ids=config.gpu_ids)
# Model to device
model = model.to(config.device)
print("\nImage Size Sat:", image_size_sat)
print("Image Size Ground:", img_size_ground)
print("Mean: {}".format(mean))
print("Std: {}\n".format(std))
#-----------------------------------------------------------------------------#
# DataLoader #
#-----------------------------------------------------------------------------#
# Eval
sat_transforms_val, ground_transforms_val = get_transforms_val(image_size_sat,
img_size_ground,
mean=mean,
std=std,
ground_cutting=config.ground_cutting)
# Reference Satellite Images Test
reference_dataset_test = VigorDatasetEval(data_folder=config.data_folder ,
split="test",
img_type="reference",
same_area=config.same_area,
transforms=sat_transforms_val,
)
reference_dataloader_test = DataLoader(reference_dataset_test,
batch_size=config.batch_size,
num_workers=config.num_workers,
shuffle=False,
pin_memory=True)
# Query Ground Images Test
query_dataset_test = VigorDatasetEval(data_folder=config.data_folder ,
split="test",
img_type="query",
same_area=config.same_area,
transforms=ground_transforms_val,
)
query_dataloader_test = DataLoader(query_dataset_test,
batch_size=config.batch_size,
num_workers=config.num_workers,
shuffle=False,
pin_memory=True)
print("Query Images Test:", len(query_dataset_test))
print("Reference Images Test:", len(reference_dataset_test))
#-----------------------------------------------------------------------------#
# Evaluate #
#-----------------------------------------------------------------------------#
print("\n{}[{}]{}".format(30*"-", "VIGOR Cross", 30*"-"))
r1_test = evaluate(config=config,
model=model,
reference_dataloader=reference_dataloader_test,
query_dataloader=query_dataloader_test,
ranks=[1, 5, 10],
step_size=1000,
cleanup=True)