-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_all.py
200 lines (172 loc) · 7.04 KB
/
train_all.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
import argparse
import collections
import random
import sys
from pathlib import Path
import numpy as np
import PIL
import torch
import torchvision
from sconf import Config
from prettytable import PrettyTable
from domainbed.datasets import get_dataset
from domainbed import hparams_registry
from domainbed.lib import misc
from domainbed.lib.writers import get_writer
from domainbed.lib.logger import Logger
from domainbed.trainer import train
from domainbed.trainer_DN import train as train_dn
def main():
parser = argparse.ArgumentParser(description="Domain generalization", allow_abbrev=False)
parser.add_argument("name", type=str)
parser.add_argument("configs", nargs="*")
parser.add_argument("--data_dir", type=str, default="datadir/")
parser.add_argument("--dataset", type=str, default="PACS")
parser.add_argument("--algorithm", type=str, default="ERM")
parser.add_argument(
"--trial_seed",
type=int,
default=0,
help="Trial number (used for seeding split_dataset and random_hparams).",
)
parser.add_argument("--seed", type=int, default=0, help="Seed for everything else")
parser.add_argument(
"--steps", type=int, default=None, help="Number of steps. Default is dataset-dependent."
)
parser.add_argument(
"--checkpoint_freq",
type=int,
default=None,
help="Checkpoint every N steps. Default is dataset-dependent.",
)
parser.add_argument("--test_envs", type=int, nargs="+", default=None)
parser.add_argument("--holdout_fraction", type=float, default=0.2)
parser.add_argument("--model_save", default=None, type=int, help="Model save start step")
# parser.add_argument("--deterministic", action="store_true")
parser.add_argument("--tb_freq", default=10)
parser.add_argument("--debug", action="store_true", help="Run w/ debug mode")
parser.add_argument("--show", action="store_true", help="Show args and hparams w/o run")
parser.add_argument(
"--evalmode",
default="fast",
help="[fast, all]. if fast, ignore train_in datasets in evaluation time.",
)
parser.add_argument("--prebuild_loader", action="store_true", help="Pre-build eval loaders")
parser.add_argument("--inter_freq", type=int, default=600, help="interpolate after inter_freq steps")
args, left_argv = parser.parse_known_args()
args.deterministic = True
# setup hparams
hparams = hparams_registry.default_hparams(args.algorithm, args.dataset)
keys = ["config.yaml"] + args.configs
keys = [open(key, encoding="utf8") for key in keys]
hparams = Config(*keys, default=hparams)
hparams.argv_update(left_argv)
# setup debug
if args.debug:
args.checkpoint_freq = 5
args.steps = 10
args.name += "_debug"
timestamp = misc.timestamp()
args.unique_name = f"{timestamp}_{args.name}"
# path setup
args.work_dir = Path(".")
args.data_dir = Path(args.data_dir)
args.out_root = args.work_dir / Path("train_output") / args.dataset
args.out_dir = args.out_root / args.unique_name
args.out_dir.mkdir(exist_ok=True, parents=True)
writer = get_writer(args.out_root / "runs" / args.unique_name)
logger = Logger.get(args.out_dir / "log.txt")
if args.debug:
logger.setLevel("DEBUG")
cmd = " ".join(sys.argv)
logger.info(f"Command :: {cmd}")
logger.nofmt("Environment:")
logger.nofmt("\tPython: {}".format(sys.version.split(" ")[0]))
logger.nofmt("\tPyTorch: {}".format(torch.__version__))
logger.nofmt("\tTorchvision: {}".format(torchvision.__version__))
logger.nofmt("\tCUDA: {}".format(torch.version.cuda))
logger.nofmt("\tCUDNN: {}".format(torch.backends.cudnn.version()))
logger.nofmt("\tNumPy: {}".format(np.__version__))
logger.nofmt("\tPIL: {}".format(PIL.__version__))
# Different to DomainBed, we support CUDA only.
assert torch.cuda.is_available(), "CUDA is not available"
logger.nofmt("Args:")
for k, v in sorted(vars(args).items()):
logger.nofmt("\t{}: {}".format(k, v))
logger.nofmt("HParams:")
for line in hparams.dumps().split("\n"):
logger.nofmt("\t" + line)
if args.show:
exit()
# seed
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.backends.cudnn.deterministic = args.deterministic
torch.backends.cudnn.benchmark = not args.deterministic
# Dummy datasets for logging information.
# Real dataset will be re-assigned in train function.
# test_envs only decide transforms; simply set to zero.
dataset, _in_splits, _out_splits = get_dataset([0], args, hparams)
# print dataset information
logger.nofmt("Dataset:")
logger.nofmt(f"\t[{args.dataset}] #envs={len(dataset)}, #classes={dataset.num_classes}")
for i, env_property in enumerate(dataset.environments):
logger.nofmt(f"\tenv{i}: {env_property} (#{len(dataset[i])})")
logger.nofmt("")
n_steps = args.steps or dataset.N_STEPS
checkpoint_freq = args.checkpoint_freq or dataset.CHECKPOINT_FREQ
logger.info(f"n_steps = {n_steps}")
logger.info(f"checkpoint_freq = {checkpoint_freq}")
org_n_steps = n_steps
n_steps = (n_steps // checkpoint_freq) * checkpoint_freq + 1
logger.info(f"n_steps is updated to {org_n_steps} => {n_steps} for checkpointing")
if not args.test_envs:
args.test_envs = [[te] for te in range(len(dataset))]
logger.info(f"Target test envs = {args.test_envs}")
###########################################################################
# Run
###########################################################################
all_records = []
results = collections.defaultdict(list)
for test_env in args.test_envs:
if args.dataset=="DomainNet":
print("===== DN ======")
res, records = train_dn(
test_env,
args=args,
hparams=hparams,
n_steps=n_steps,
checkpoint_freq=checkpoint_freq,
logger=logger,
writer=writer,
)
else:
print("===== others ======")
res, records = train(
test_env,
args=args,
hparams=hparams,
n_steps=n_steps,
checkpoint_freq=checkpoint_freq,
logger=logger,
writer=writer,
)
all_records.append(records)
for k, v in res.items():
results[k].append(v)
# log summary table
logger.info("=== Summary ===")
logger.info(f"Command: {' '.join(sys.argv)}")
logger.info("Unique name: %s" % args.unique_name)
logger.info("Out path: %s" % args.out_dir)
logger.info("Algorithm: %s" % args.algorithm)
logger.info("Dataset: %s" % args.dataset)
table = PrettyTable(["Selection"] + dataset.environments + ["Avg."])
for key, row in results.items():
row.append(np.mean(row))
row = [f"{acc:.3%}" for acc in row]
table.add_row([key] + row)
logger.nofmt(table)
if __name__ == "__main__":
main()