forked from kev98/Medical-Image-Segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
163 lines (148 loc) · 4.44 KB
/
main.py
File metadata and controls
163 lines (148 loc) · 4.44 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
"""
Main training script, which accepts configuration file and other training parameters from command line.
"""
import argparse
import sys
import random
import numpy as np
import torch
from pathlib import Path
from config import Config
import trainer
def parse_args():
parser = argparse.ArgumentParser(
description='Train a segmentation model for medical images',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'--config',
type=str,
required=True,
help='Path to configuration JSON file (e.g., config/config_atlas.json)'
)
parser.add_argument(
'--epochs',
type=int,
required=True,
help='Number of training epochs'
)
parser.add_argument(
'--save_path',
type=str,
required=True,
help='Directory path to save model checkpoints and metrics'
)
parser.add_argument(
'--trainer',
type=str,
required=True,
help='Trainer class name'
)
parser.add_argument(
'--validation',
action='store_true',
help='Enable validation during training'
)
parser.add_argument(
'--val_every',
type=int,
default=1,
help='Run validation every N epochs (default: 1)'
)
parser.add_argument(
'--resume',
action='store_true',
help='Resume training from last checkpoint in save_path'
)
parser.add_argument(
'--debug',
action='store_true',
help='Enable debug mode'
)
parser.add_argument(
'--eval_metric_type',
type=str,
default='mean',
choices=['mean', 'aggregated_mean'],
help='Metric type to use for model selection: "mean" for per-class mean of the first metric, "aggregated_mean" for aggregated regions mean of the first metric'
)
parser.add_argument(
'--wandb',
action='store_true',
help='Enable Weights & Biases logging'
)
parser.add_argument(
'--save_visualizations',
action='store_true',
help='Enable saving of visualizations during validation'
)
parser.add_argument(
'--mixed_precision',
type=str,
default=None,
choices=['fp16', 'bf16'],
help='Enable mixed precision: fp16 or bf16 (default: disabled)'
)
parser.add_argument(
'--pretrained_path',
type=str,
default=None,
help='Path to pretrained model weights for finetuning (default: None)'
)
parser.add_argument(
'--seed',
type=int,
default=42,
help='Global random seed for reproducibility'
)
return parser.parse_args()
def main():
args = parse_args()
config_path = Path(args.config)
if not config_path.exists():
print(f"Error: Configuration file not found: {args.config}")
sys.exit(1)
print(f"Loading configuration from: {args.config}")
config = Config(args.config)
# Set random seeds for reproducibility
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(args.seed)
# Create save directory if it doesn't exist
save_path = Path(args.save_path)
save_path.mkdir(parents=True, exist_ok=True)
print(f"Model checkpoints will be saved to: {args.save_path}")
TrainerClass = getattr(trainer, args.trainer)
print(f"\nInitializing {TrainerClass.__name__}...")
trainer_instance = TrainerClass(
config=config,
epochs=args.epochs,
validation=args.validation,
save_path=args.save_path,
resume=args.resume,
debug=args.debug,
eval_metric_type=args.eval_metric_type,
save_visualizations = args.save_visualizations,
use_wandb=args.wandb,
val_every=args.val_every,
mixed_precision=args.mixed_precision,
pretrained_path=args.pretrained_path
)
try:
trainer_instance.train()
print("\n" + "="*50)
print("Training completed successfully!")
print("="*50)
except KeyboardInterrupt:
print("\n\nTraining interrupted by user.")
print("Checkpoint saved at last epoch.")
sys.exit(0)
except Exception as e:
print(f"\n\nError during training: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == '__main__':
main()