-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrain.py
235 lines (194 loc) · 8.21 KB
/
train.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import os
import yaml
import time
import shutil
import torch
import random
import argparse
import numpy as np
import copy
import timeit
import statistics
import datetime
from torch.utils import data
from tqdm import tqdm
import cv2
from ptsemseg.process_img import generate_noise
from ptsemseg.models import get_model
from ptsemseg.loss import get_loss_function
from ptsemseg.loader import get_loader
from ptsemseg.utils import get_logger, init_weights
from ptsemseg.metrics import runningScore
from ptsemseg.augmentations import get_composed_augmentations
from ptsemseg.schedulers import get_scheduler
from ptsemseg.optimizers import get_optimizer
from ptsemseg.utils import convert_state_dict
from ptsemseg.trainer import *
from tensorboardX import SummaryWriter
# main function
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="config")
parser.add_argument(
"--config",
nargs="?",
type=str,
default="configs/your_configs.yml",
help="Configuration file to use",
)
parser.add_argument(
"--gpu",
nargs="?",
type=str,
default="0",
help="Used GPUs",
)
parser.add_argument(
"--run_time",
nargs="?",
type=int,
default=1,
help="run_time",
)
args = parser.parse_args()
# Set the gpu
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
run_times = args.run_time
with open(args.config) as fp:
cfg = yaml.load(fp)
data_splits = ['val_split', 'test_split']
# initialize for results stats
score_list = {}
class_iou_list = {}
acc_list = {}
if cfg['model']['arch'] == 'LearnWho2Com':
for infer in ['softmax', 'argmax_test']:
score_list[infer] = {}
class_iou_list[infer] = {}
acc_list[infer] = {}
for data_sp in data_splits:
score_list[infer][data_sp] = []
class_iou_list[infer][data_sp] = []
acc_list[infer][data_sp] = []
elif cfg['model']['arch'] == 'LearnWhen2Com' or \
cfg['model']['arch'] == 'MIMOcom' or \
cfg['model']['arch'] == 'MIMOcomMultiWarp' or \
cfg['model']['arch'] == 'MIMOcomWho' :
for infer in ['softmax', 'argmax_test', 'activated']:
score_list[infer] = {}
class_iou_list[infer] = {}
acc_list[infer] = {}
for data_sp in data_splits:
score_list[infer][data_sp] = []
class_iou_list[infer][data_sp] = []
acc_list[infer][data_sp] = []
elif cfg['model']['arch'] == 'Single_agent' or cfg['model']['arch'] == 'All_agents' or cfg['model']['arch'] == 'MIMO_All_agents':
for infer in ['default']:
score_list[infer] = {}
class_iou_list[infer] = {}
acc_list[infer] = {}
for data_sp in data_splits:
score_list[infer][data_sp] = []
class_iou_list[infer][data_sp] = []
acc_list[infer][data_sp] = []
for _ in range(run_times):
run_id = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
logdir = os.path.join("runs", os.path.basename(args.config)[:-4], str(run_id))
writer = SummaryWriter(logdir=logdir)
print("RUNDIR: {}".format(logdir))
shutil.copy(args.config, logdir)
# ============= Training =============
# logger
logger = get_logger(logdir)
logger.info("Begin")
# Setup seeds
torch.manual_seed(cfg.get("seed", 1337))
torch.cuda.manual_seed(cfg.get("seed", 1337))
np.random.seed(cfg.get("seed", 1337))
random.seed(cfg.get("seed", 1337))
# Setup device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Setup Dataloader
data_loader = get_loader(cfg["data"]["dataset"])
data_path = cfg["data"]["path"]
# Load communication label (note that some datasets do not provide this)
if 'commun_label' in cfg["data"]:
if_commun_label = cfg["data"]['commun_label']
else:
if_commun_label = 'None'
# dataloaders
t_loader = data_loader(
data_path,
is_transform=True,
split=cfg["data"]["train_split"],
img_size=(cfg["data"]["img_rows"], cfg["data"]["img_cols"]),
augmentations=get_composed_augmentations(cfg["training"].get("augmentations", None)),
target_view=cfg["data"]["target_view"],
commun_label=if_commun_label
)
v_loader = data_loader(
data_path,
is_transform=True,
split=cfg["data"]["val_split"],
img_size=(cfg["data"]["img_rows"], cfg["data"]["img_cols"]),
target_view=cfg["data"]["target_view"],
commun_label=if_commun_label
)
trainloader = data.DataLoader(
t_loader,
batch_size=cfg["training"]["batch_size"],
num_workers=cfg["training"]["n_workers"],
shuffle=True,
drop_last=True
)
valloader = data.DataLoader(
v_loader,
batch_size=cfg["training"]["batch_size"],
num_workers=cfg["training"]["n_workers"]
)
# Setup Model
model = get_model(cfg, t_loader.n_classes).to(device)
model = torch.nn.DataParallel(model, device_ids=range(torch.cuda.device_count()))
# import pdb; pdb.set_trace()
# Setup optimizer
optimizer_cls = get_optimizer(cfg)
optimizer_params = {k: v for k, v in cfg["training"]["optimizer"].items() if k != "name"}
optimizer = optimizer_cls(model.parameters(), **optimizer_params)
logger.info("Using optimizer {}".format(optimizer))
# Setup scheduler
scheduler = get_scheduler(optimizer, cfg["training"]["lr_schedule"])
# Setup loss
loss_fn = get_loss_function(cfg)
logger.info("Using loss {}".format(loss_fn))
# ================== TRAINING ==================
if cfg['model']['arch'] == 'LearnWhen2Com': # Our when2com
trainer = Trainer_LearnWhen2Com(cfg, writer, logger, model, loss_fn, trainloader, valloader, optimizer, scheduler, device)
elif cfg['model']['arch'] == 'LearnWho2Com': # Our who2com
trainer = Trainer_LearnWho2Com(cfg, writer, logger, model, loss_fn, trainloader, valloader, optimizer, scheduler, device)
elif cfg['model']['arch'] == 'MIMOcom': #
trainer = Trainer_MIMOcom(cfg, writer, logger, model, loss_fn, trainloader, valloader, optimizer, scheduler, device)
elif cfg['model']['arch'] == 'MIMOcomMultiWarp':
trainer = Trainer_MIMOcomMultiWarp(cfg, writer, logger, model, loss_fn, trainloader, valloader, optimizer, scheduler, device)
elif cfg['model']['arch'] == 'MIMOcomWho':
trainer = Trainer_MIMOcomWho(cfg, writer, logger, model, loss_fn, trainloader, valloader, optimizer, scheduler, device)
elif cfg['model']['arch'] == 'Single_agent':
trainer = Trainer_Single_agent(cfg, writer, logger, model, loss_fn, trainloader, valloader, optimizer, scheduler, device)
elif cfg['model']['arch'] == 'All_agents':
trainer = Trainer_All_agents(cfg, writer, logger, model, loss_fn, trainloader, valloader, optimizer, scheduler, device)
elif cfg['model']['arch'] == 'MIMO_All_agents':
trainer = Trainer_MIMO_All_agents(cfg, writer, logger, model, loss_fn, trainloader, valloader, optimizer, scheduler, device)
else:
raise ValueError('Unknown arch name for training')
model_path = trainer.train()
# ================ Val + Test ================
te_loader = data_loader(
data_path,
split=cfg["data"]['test_split'],
is_transform=True,
img_size=(cfg["data"]["img_rows"], cfg["data"]["img_cols"]),
target_view=cfg["data"]["target_view"],
commun_label=if_commun_label)
n_classes = te_loader.n_classes
testloader = data.DataLoader(te_loader, batch_size=cfg["training"]["batch_size"], num_workers=8)
# load best weight
trainer.load_weight(model_path)
_ = trainer.evaluate(testloader)