-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
148 lines (126 loc) · 5.28 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
from pathlib import Path
from omegaconf import OmegaConf
import hydra
import pytorch_lightning as pl
import torch
from pytorch_lightning.callbacks import LearningRateMonitor, ModelCheckpoint
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
from pytorch_lightning.loggers import WandbLogger
from classification.load_model_and_config import get_modules
@hydra.main(config_path="../configs/", config_name="config.yaml", version_base="1.2")
def train_model_main(config):
print(config)
pl.seed_everything(config.seed, workers=True)
wandb_logger = WandbLogger(save_dir="outputs", project=config.project_name)
output_dir = Path(f"outputs/run_{wandb_logger.experiment.id}") # type: ignore
print("Saving to" + str(output_dir.absolute()))
data_module, model_module = get_modules(config)
wandb_logger.watch(model_module, log="all", log_freq=100)
wandb_logger.log_hyperparams(
OmegaConf.to_container(config, resolve=True, throw_on_missing=True)
)
callbacks = [LearningRateMonitor()]
checkpoint_callback = ModelCheckpoint(dirpath=output_dir, filename="{epoch}")
callbacks.append(checkpoint_callback)
checkpoint_callback_best = ModelCheckpoint(
dirpath=output_dir,
monitor=config.trainer.metric_to_monitor,
mode=config.trainer.metric_to_monitor_mode,
filename="best",
)
callbacks.append(checkpoint_callback_best)
if config.trainer.loss == "ce":
early_stopping = EarlyStopping(
monitor=config.trainer.metric_to_monitor,
mode=config.trainer.metric_to_monitor_mode,
patience=round(3 * config.trainer.patience_for_scheduler),
)
callbacks.append(early_stopping)
precision = "32-true"
torch.set_float32_matmul_precision("medium")
if config.mixed_precision:
precision = "16-mixed"
n_gpus = (
config.trainer.device
if isinstance(config.trainer.device, int)
else len(config.trainer.device)
)
trainer = pl.Trainer(
deterministic="warn",
accelerator="auto",
devices=config.trainer.device,
strategy="ddp_find_unused_parameters_true" if n_gpus > 1 else "auto",
max_epochs=config.trainer.num_epochs
if config.trainer.max_steps == "None"
else -1,
max_steps=config.trainer.max_steps
if config.trainer.max_steps != "None"
else -1,
logger=wandb_logger,
callbacks=callbacks,
precision=precision,
fast_dev_run=config.is_unit_test_config,
val_check_interval=min(
config.trainer.val_check_interval, len(data_module.train_dataloader())
)
if config.trainer.val_check_interval != "None"
else None,
limit_val_batches=250,
)
if config.trainer.finetune_path != "None":
state_dict = torch.load(config.trainer.finetune_path, map_location="cuda:0")[
"state_dict"
]
if "model.net.fc.weight" in state_dict.keys():
state_dict.pop("model.net.fc.weight")
state_dict.pop("model.net.fc.bias")
if "model.student.cls_token" in state_dict.keys():
assert config.model.encoder_name.startswith("dino_vit")
new_state_dict = {}
for k, v in state_dict.items():
if k.startswith("model.student"):
new_state_dict[k.replace("model.student", "model")] = v
print(model_module.load_state_dict(new_state_dict, strict=False))
else:
print(model_module.load_state_dict(state_dict, strict=False))
print("Model loaded successfully")
if config.trainer.freeze_encoder:
model_module = model_module.eval()
for p in model_module.parameters():
p.requires_grad = False
else:
model_module = model_module.train()
model_module.model.reset_classifier(data_module.num_classes)
elif config.model.pretrained_encoder_path != "None":
state_dict = torch.load(
config.model.pretrained_encoder_path, map_location="cuda:0"
)
if config.trainer.loss == "dino":
state_dict_update = {}
for k, v in state_dict.items():
if k != "pos_embed" and not k.startswith("patch_embed"):
if "blocks" in k:
new_key = f"model.student.{k}"
new_key = new_key.replace("blocks.", "blocks.0.")
state_dict_update[new_key] = v
new_key = new_key.replace("student.", "teacher.")
state_dict_update[new_key] = v
else:
state_dict_update[f"model.student.{k}"] = v
state_dict_update[f"model.teacher.{k}"] = v
print(model_module.load_state_dict(state_dict_update, strict=False))
else:
print(model_module.load_state_dict(state_dict, strict=False))
print("Pretrained model loaded successfully")
trainer.fit(
model_module,
data_module,
)
if __name__ == "__main__":
"""
Script to run one particular configuration.
"""
import os
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512"
torch.multiprocessing.set_sharing_strategy("file_system")
train_model_main()