-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
163 lines (123 loc) · 4.5 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
"""
Copyright (c) 2022, salesforce.com, inc.
All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
"""
import argparse
import os
import random
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import lavis.tasks as tasks
from lavis.common.config import Config
from lavis.common.dist_utils import get_rank, init_distributed_mode
from lavis.common.logger import setup_logger
from lavis.common.optims import (
LinearWarmupCosineLRScheduler,
LinearWarmupStepLRScheduler,
)
from lavis.common.registry import registry
from lavis.common.utils import now
from lavis.datasets.builders import *
from lavis.models import *
from lavis.processors import *
from lavis.runners import *
from lavis.tasks import *
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:32'
os.environ['NCCL_IB_DISABLE'] = '1'
os.environ['NCCL_P2P_DISABLE'] = '1'
os.environ["TOKENIZERS_PARALLELISM"] = "false"
import h5py
def check_dataset(datasets):
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
if 'iu_xray' not in datasets:
print("iu_xray not found in datasets")
return
iu_xray_datasets = datasets['iu_xray']
splits = ['train', 'eval']
for split in splits:
if split not in iu_xray_datasets:
print(f"Split {split} not found in datasets")
continue
print(f"\n=== Check {split} dataset ===")
dataset = iu_xray_datasets[split]
loader = DataLoader(dataset, batch_size=2, shuffle=False, num_workers=0)
# get one batch
batch = next(iter(loader))
# show image
plt.figure(figsize=(15, 10))
for i in range(min(2, len(batch['image_0']))):
# show the first image
plt.subplot(2, 2, i*2 + 1)
img_0 = batch['image_0'][i].permute(1, 2, 0).cpu().numpy()
img_0 = (img_0 * 0.5 + 0.5).clip(0, 1)
plt.imshow(img_0, cmap='gray')
plt.title(f"Sample {i+1} - Frontal")
# show the second image
plt.subplot(2, 2, i*2 + 2)
img_1 = batch['image_1'][i].permute(1, 2, 0).cpu().numpy()
img_1 = (img_1 * 0.5 + 0.5).clip(0, 1)
plt.imshow(img_1, cmap='gray')
plt.title(f"Sample {i+1} - Lateral")
# print report
print(f"\nReport {i+1}: {batch['text_input'][i]}")
print(f"Study ID: {batch['study_id'][i]}")
plt.tight_layout()
plt.show()
print(f"\nDataset size: {len(dataset)}")
print(f"\nBatch strcuture:")
for k, v in batch.items():
if isinstance(v, (list, tuple)):
print(f"{k}: {len(v)} items")
else:
print(f"{k}: {v.shape if hasattr(v, 'shape') else type(v)}")
def parse_args():
parser = argparse.ArgumentParser(description="Training")
parser.add_argument("--cfg-path", required=True, help="path to configuration file.")
parser.add_argument(
"--options",
nargs="+",
help="override some settings in the used config, the key-value pair "
"in xxx=yyy format will be merged into config file (deprecate), "
"change to --cfg-options instead.",
)
args = parser.parse_args()
return args
def setup_seeds(config):
seed = config.run_cfg.seed + get_rank()
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
cudnn.benchmark = False
cudnn.deterministic = True
def get_runner_class(cfg):
"""
Get runner class from config. Default to epoch-based runner.
"""
runner_cls = registry.get_runner_class(cfg.run_cfg.get("runner", "runner_base"))
return runner_cls
def main():
# set before init_distributed_mode() to ensure the same job_id shared across all ranks.
job_id = now()
cfg = Config(parse_args())
import os
from pathlib import Path
init_distributed_mode(cfg.run_cfg)
setup_seeds(cfg)
# set after init_distributed_mode() to only log on master.
setup_logger()
cfg.pretty_print()
print("Config contents:", vars(cfg))
task = tasks.setup_task(cfg)
datasets = task.build_datasets(cfg)
model = task.build_model(cfg)
# print(model)
runner = get_runner_class(cfg)(
cfg=cfg, job_id=job_id, task=task, model=model, datasets=datasets
)
runner.train()
if __name__ == "__main__":
main()