-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
161 lines (133 loc) · 4.9 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
import os
import sys
import time
import numpy as np
from tqdm import tqdm
import torch
from torch.utils.data.dataloader import DataLoader
from lsm.dataio import get_data
from lsm.models import get_model
from lsm.utils.logger import Logger
from lsm.utils.console_log import log
from lsm.utils.train_utils import count_trainable_parameters
from lsm.utils.load_config import create_args_parser, load_config, backup
from lsm.utils.distributed_util import (
init_env,
get_rank,
is_master,
get_local_rank,
get_world_size,
)
color_map = np.random.randint(0, 256, (500, 3), dtype=np.uint8)
color_map[0] = [0, 0, 0]
def main_function(args):
init_env(args)
rank = get_rank()
local_rank = get_local_rank()
world_size = get_world_size()
i_backup = (
int(args.training.i_backup // world_size) if args.training.i_backup > 0 else -1
)
i_val = int(args.training.i_val // world_size) if args.training.i_val > 0 else -1
exp_dir = args.training.exp_dir
device = torch.device("cuda", local_rank)
logger = Logger(
log_dir=exp_dir,
save_dir=os.path.join(exp_dir, "predictions"),
monitoring=args.training.get("monitoring", "tensorboard"),
monitoring_dir=os.path.join(exp_dir, "events"),
rank=rank,
is_master=is_master(),
multi_process_logging=(world_size > 1),
)
log.info(f"Experiments directory: {exp_dir}")
if is_master():
pass
# get data from dataloader
dataset, val_dataset = get_data(args=args, return_val=True)
batch_size = args.data.get("batch_size", None)
if args.ddp:
train_sampler = DistributedSampler(dataset)
dataloader = torch.utils.data.DataLoader(
dataset, sampler=train_sampler, batch_size=batch_size
)
val_sampler = DistributedSampler(val_dataset)
valloader = torch.utils.data.DataLoader(
val_dataset, sampler=val_sampler, batch_size=batch_size
)
else:
dataloader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=True,
pin_memory=args.data.get("pin_memory", False),
)
valloader = DataLoader(val_dataset, batch_size=1, shuffle=True)
# create model
model = get_model(args)
model.to(device)
log.info(model)
model_name = args.model.framework
if world_size > 1:
dist.barrier()
tick = time.time()
log.info(f"Start evaluating in {exp_dir}")
it, epoch_idx = 0, 0
end = it >= len(dataset)
with tqdm(range(len(dataset)), disable=not is_master()) as pbar:
if is_master():
pbar.update()
while it <= len(dataset) and not end:
try:
for (indices, model_input, ground_truth) in dataloader:
int_it = int(it // world_size)
norm_rgb = model_input["norm_rgb"]
orig_rgb = ground_truth["orig_rgb"]
pred_mask = model(norm_rgb)
# check if we have worked with 3d data
# save a single slice in that case
if args.data_dim == "3D":
logger.add_imgs_3d(
imgs=orig_rgb,
class_name="gt_rgb",
it=it,
save_seg=False,
)
logger.add_imgs_3d(
imgs=pred_mask, class_name="pred_mask", it=it, save_seg=True
)
elif args.data_dim == "2D":
# save images
logger.add_imgs_eval(
imgs=orig_rgb,
class_name="gt_rgb",
it=it,
save_seg=False,
)
logger.add_imgs_eval(
imgs=pred_mask,
class_name="pred_mask",
it=it,
save_seg=True,
)
else:
raise NotImplementedError(
f"Data dimension: {args.data_dim} not supported. Try one of 2D/3D"
)
if it >= len(dataset):
end = True
break
it += world_size
if is_master():
pbar.update(world_size)
epoch_idx += 1
except KeyboardInterrupt:
if is_master():
print(f"TODO: idk")
sys.exit()
if __name__ == "__main__":
parser = create_args_parser()
parser.add_argument("--ddp", action="store_true", help="Distributed processing")
args, unknown = parser.parse_known_args()
config = load_config(args, unknown)
main_function(config)