-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
195 lines (166 loc) · 6.23 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import argparse
import pyjson5 as json
import pprint
import random
from pathlib import Path
import numpy as np
import torch
import wandb
from models.model_utilities import *
from torchmetrics import Accuracy, F1Score, Precision, Recall
import training.train_mae
from training.change_detection_trainer import (
eval_change_detection,
train_change_detection,
)
from training.segmentation_trainer import (
eval_semantic_segmentation,
train_semantic_segmentation,
)
from training.recurrent_trainer import (
eval_recurrent_segmentation,
train_recurrent_segmentation
)
from utilities.utilities import *
parser = argparse.ArgumentParser()
parser.add_argument("--method", default=None)
parser.add_argument("--backbone", default=None)
parser.add_argument("--dem", type=int, default=None)
parser.add_argument("--slope", type=int, default=None)
parser.add_argument("--batch_size", default=None)
parser.add_argument("--inputs", nargs="+", default=None)
parser.add_argument("--seed", type=int, default=999)
args = parser.parse_args()
# Seed stuff
np.random.seed(args.seed)
random.seed(args.seed)
torch.manual_seed(args.seed)
if __name__ == "__main__":
configs = json.load(open("configs/config.json", "r"))
if args.method is not None:
configs["method"] = args.method
if configs["method"] == "convlstm":
model_configs = json.load(open("configs/method/temporal/convlstm.json", "r"))
elif configs["method"] == "vivit":
model_configs = json.load(open("configs/method/temporal/vivit.json", "r"))
else:
model_configs = json.load(
open(
f'configs/method/{configs["method"].lower()}/{configs["method"].lower().replace("-", "_")}.json'
)
)
if args.backbone is not None:
model_configs["backbone"] = args.backbone
configs.update(model_configs)
if args.inputs is None and args.dem is None:
configs = update_config(configs, None)
else:
configs = update_config(configs, args)
checkpoint_path = create_checkpoint_directory(configs, model_configs)
if args.batch_size is not None:
configs["batch_size"] = int(args.batch_size)
configs["checkpoint_path"] = checkpoint_path
pprint.pprint(configs)
# Create Loaders
train_loader, val_loader, test_loader = prepare_loaders(configs)
# Begin Training
if configs["task"] == "segmentation":
if configs['method'] == 'convlstm':
if not configs['test']:
model = initialize_recurrent_model(configs, model_configs)
train_recurrent_segmentation(
model,
train_loader,
val_loader,
test_loader,
configs=configs,
model_configs=model_configs,
)
# Evaluate on Test Set
model = initialize_recurrent_model(configs, model_configs)
ckpt_path = Path(configs["checkpoint_path"]) / f'{rep_i}' / "best_segmentation.pt"
print(f"Loading model from: {ckpt_path}")
checkpoint = torch.load(ckpt_path, map_location=configs['device'])
model.load_state_dict(checkpoint["model_state_dict"])
test_acc, test_score, miou = eval_recurrent_segmentation(
model,
test_loader,
ckpt_path.parent,
settype="Test",
configs=configs,
model_configs=model_configs,
)
# Print final results
print("Test Mean IOU: ", miou)
else:
# Create model
model = initialize_segmentation_model(configs, model_configs)
if not configs["test"]:
train_semantic_segmentation(
model,
train_loader,
val_loader,
test_loader,
configs=configs,
model_configs=model_configs,
)
else:
if configs["wandb_activate"]:
# Store wandb id to continue run
id = wandb.util.generate_id()
json.dump(
{"run_id": id}, open(configs["checkpoint_path"] + "/id.json", "w")
)
wandb.init(
project=configs["wandb_project"],
entity=configs["wandb_entity"],
config=configs,
id=id,
resume="allow",
)
wandb.watch(model, log_freq=20)
# Evaluate on Test Set
print(
"Loading model from: ",
configs["checkpoint_path"] + "/" + "best_segmentation.pt",
)
model = torch.load(configs["checkpoint_path"] + "/" + "best_segmentation.pt")
test_acc, test_score, miou = eval_semantic_segmentation(
model,
test_loader,
settype="Test",
configs=configs,
model_configs=model_configs,
)
print("Test Mean IOU: ", miou)
elif configs["task"] == "mae":
print("Initializing Self-Supervised learning training with configs:")
pprint.pprint(configs)
training.train_mae.train(configs)
elif configs["task"] == "cd":
model = initialize_cd_model(configs, model_configs, "train")
train_change_detection(
model,
train_loader,
val_loader,
test_loader,
configs=configs,
model_configs=model_configs,
)
# Evaluate on Test Set
print(
"Loading model from: ",
configs["checkpoint_path"] + "/" + "best_segmentation.pt",
)
checkpoint = torch.load(
configs["checkpoint_path"] + "/" + "best_segmentation.pt", map_location=configs['device']
)
model.load_state_dict(checkpoint["model_state_dict"])
test_acc, test_score, miou = eval_change_detection(
model,
test_loader,
settype="Test",
configs=configs,
model_configs=model_configs,
)
print("Test Mean IOU: ", miou.item())