-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
132 lines (116 loc) · 3.25 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
import argparse
import os
os.environ['PROJ_NETWORK'] = 'OFF'
import warnings
import lightning.pytorch as pl
from src.datamodules import ChesapeakeRSCDataModule
from src.modules import CustomSemanticSegmentationTask
warnings.filterwarnings("ignore", category=UserWarning, module="torch.nn.functional")
def setup_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Train a semantic segmentation model.")
parser.add_argument(
"--batch_size", type=int, default=64, help="Size of each mini-batch."
)
parser.add_argument(
"--model",
choices=["deeplabv3+", "fcn", "custom_fcn", "unet", "unet++"],
default="unet",
help="Model architecture to use.",
)
parser.add_argument(
"--num_epochs",
type=int,
default=150,
help="Number of epochs to train for.",
)
parser.add_argument(
"--num_filters",
type=int,
default=64,
help="Number of filters to use with FCN models.",
)
parser.add_argument(
"--backbone",
choices=[
"resnet18",
"resnet34",
"resnet50",
"resnet101",
"resnet152",
"resnext50_32x4d",
"resnext101_32x8d",
],
default="resnet50",
help="Backbone architecture to use.",
)
parser.add_argument(
"--lr",
type=float,
default=1e-3,
help="Learning rate to use for training.",
)
parser.add_argument(
"--tmax",
type=int,
default=50,
help="Cycle size for cosine lr scheudler.",
)
parser.add_argument(
"--experiment_name",
type=str,
required=False,
help="Name of the experiment to run.",
)
parser.add_argument(
"--gpu_id",
type=int,
required=False,
help="GPU ID to use (defaults to all GPUs if none).",
)
parser.add_argument(
"--root_dir",
type=str,
default="./data/ChesapeakeRSC/",
help="Root directory of the dataset.",
)
return parser
def main(args: argparse.Namespace) -> None:
"""Main training routine."""
# torch.set_float32_matmul_precision('medium')
dm = ChesapeakeRSCDataModule(
root=args.root_dir,
batch_size=args.batch_size,
num_workers=8,
differentiate_tree_canopy_over_roads=False,
)
task = CustomSemanticSegmentationTask(
model=args.model,
backbone=args.backbone,
weights=True,
in_channels=4,
num_classes=2,
num_filters=args.num_filters,
loss="ce",
lr=args.lr,
tmax=args.tmax,
class_weights=None,
)
experiment_name = None
if args.experiment_name is not None:
experiment_name = os.path.join("logs", args.experiment_name)
gpu_id = None
if args.gpu_id is not None:
gpu_id = [args.gpu_id]
trainer = pl.Trainer(
accelerator="gpu",
devices=gpu_id,
min_epochs=args.num_epochs,
max_epochs=args.num_epochs,
log_every_n_steps=15,
default_root_dir=experiment_name,
)
trainer.fit(task, dm)
if __name__ == "__main__":
parser = setup_argparse()
args = parser.parse_args()
main(args)