forked from arthurkosmala/EwaldMP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperiment_oc20.py
More file actions
168 lines (145 loc) · 6.27 KB
/
experiment_oc20.py
File metadata and controls
168 lines (145 loc) · 6.27 KB
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
import logging
import os
import seml
import torch
from sacred import Experiment
from ocpmodels import models
from ocpmodels.common import logger
from ocpmodels.common.registry import registry
from ocpmodels.common.utils import setup_logging
from ocpmodels.trainers import ForcesTrainer
ex = Experiment()
seml.setup_logger(ex)
@ex.post_run_hook
def collect_stats(_run):
seml.collect_exp_stats(_run)
@ex.config
def config():
overwrite = None
db_collection = None
if db_collection is not None:
ex.observers.append(
seml.create_mongodb_observer(db_collection, overwrite=overwrite)
)
@ex.automain
def run(
dataset_train,
dataset_id,
dataset_ood_ads,
dataset_ood_cat,
dataset_ood_both,
task,
model,
optimizer,
logger,
name,
):
setup_logging()
# ************************************************************************************************************************************************
# Comment out the part enclosed in stars if you only want to validate or test on OC20
# ************************************************************************************************************************************************
trainer = ForcesTrainer(
task=task,
model=model,
dataset=dataset_train,
optimizer=optimizer,
identifier=name,
run_dir="./",
# directory to save results if is_debug=False. Prediction files are saved here so be careful not to override!
is_debug=False, # if True, do not save checkpoint, logs, or results
print_every=5000,
seed=0, # random seed to use
logger=logger, # logger of choice (tensorboard and wandb supported)
local_rank=0,
amp=False, # use PyTorch Automatic Mixed Precision (faster training and less memory usage)
)
trainer.load_checkpoint(checkpoint_path=checkpoint_path)
trainer.train()
checkpoint_path = os.path.join(
trainer.config["cmd"]["checkpoint_dir"], "best_checkpoint.pt"
)
# ************************************************************************************************************************************************
# ************************************************************************************************************************************************
# ************************************************************************************************************************************************
#### Validation part ####
# checkpoint_path = [your checkpoint path if you only want to validate or test]
trainer = ForcesTrainer(
task=task,
model=model,
dataset=dataset_id,
optimizer=optimizer,
identifier="val_id",
run_dir="./",
# directory to save results if is_debug=False. Prediction files are saved here so be careful not to override!
is_debug=True, # if True, do not save checkpoint, logs, or results, set to False if you want to test not validate (results file needed)
print_every=5000,
seed=0, # random seed to use
logger=logger, # logger of choice (tensorboard and wsqueueandb supported)
local_rank=0,
amp=False, # use PyTorch Automatic Mixed Precision (faster training and less memory usage)
)
trainer.load_checkpoint(checkpoint_path=checkpoint_path)
metrics = trainer.validate()
results_id = {key: val["metric"] for key, val in metrics.items()}
trainer = ForcesTrainer(
task=task,
model=model,
dataset=dataset_ood_ads,
optimizer=optimizer,
identifier="val_ood_ads",
run_dir="./",
# directory to save results if is_debug=False. Prediction files are saved here so be careful not to override!
is_debug=True, # if True, do not save checkpoint, logs, or results, set to False if you want to test not validate (results file needed)
print_every=5000,
seed=0, # random seed to use
logger=logger, # logger of choice (tensorboard and wandb supported)
local_rank=0,
amp=False, # use PyTorch Automatic Mixed Precision (faster training and less memory usage)
)
trainer.load_checkpoint(checkpoint_path=checkpoint_path)
metrics = trainer.validate()
results_ood_ads = {key: val["metric"] for key, val in metrics.items()}
trainer = ForcesTrainer(
task=task,
model=model,
dataset=dataset_ood_cat,
optimizer=optimizer,
identifier="val_ood_cat",
run_dir="./",
# directory to save results if is_debug=False. Prediction files are saved here so be careful not to override!
is_debug=True, # if True, do not save checkpoint, logs, or results, set to False if you want to test not validate (results file needed)
print_every=5000,
seed=0, # random seed to use
logger=logger, # logger of choice (tensorboard and wandb supported)
local_rank=0,
amp=False, # use PyTorch Automatic Mixed Precision (faster training and less memory usage)
)
trainer.load_checkpoint(checkpoint_path=checkpoint_path)
metrics = trainer.validate()
results_ood_cat = {key: val["metric"] for key, val in metrics.items()}
trainer = ForcesTrainer(
task=task,
model=model,
dataset=dataset_ood_both,
optimizer=optimizer,
identifier="val_ood_both",
run_dir="./",
# directory to save results if is_debug=False. Prediction files are saved here so be careful not to override!
is_debug=True, # if True, do not save checkpoint, logs, or results, set to False if you want to test not validate (results file needed)
print_every=5000,
seed=0, # random seed to use
logger=logger, # logger of choice (tensorboard and wandb supported)
local_rank=0,
amp=False, # use PyTorch Automatic Mixed Precision (faster training and less memory usage)
)
trainer.load_checkpoint(checkpoint_path=checkpoint_path)
metrics = trainer.validate()
results_ood_both = {key: val["metric"] for key, val in metrics.items()}
results = {
"id": results_id,
"ood_ads": results_ood_ads,
"ood_cat": results_ood_cat,
"ood_both": results_ood_both,
}
# the returned result will be written into the database
return results