This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
137 lines (117 loc) · 3.62 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
import lightning as L
import os
import glob
import gc
from dataset import *
from model import SARModel
from torch.utils.data import DataLoader
from lightning import Trainer, seed_everything
from lightning.pytorch.loggers import WandbLogger
from lightning.pytorch.callbacks import (
LearningRateMonitor,
ModelCheckpoint,
RichModelSummary,
RichProgressBar,
)
import wandb
# -------- 设置 ---------
project_p = "/home/ao/Desktop/ieee/"
train_data_p = project_p + "data/Track1/train/images/"
train_label_p = project_p + "data/Track1/train/labels/"
test_data_p = project_p + "data/Track1/val/images/"
# --------- 参数 ---------
max_ep = 120
val_rate = 0.1
batch_size = 4
# img_num = 1631
## callback pylighting model setting
seed_everything(42, workers=True)
lr_monitor = LearningRateMonitor("epoch")
progress_bar = RichProgressBar()
model_summary = RichModelSummary(max_depth=3)
# ---------------------------
if __name__ == "__main__":
img_l = glob.glob(os.path.join(train_data_p, "*.tif"))
label_l = glob.glob(os.path.join(train_label_p, "*.png"))
img_l.sort()
label_l.sort()
# img_l = [train_data_p + f"{i}.tif" for i in range(img_num)]
# label_l = [project_p + f"data/Track1/train/labels/{i}.png" for i in range(img_num)]
# ----- build train, val set
# dataset -> copy -> to_train
# -> to_val
# del dataset
dataset = SARdataset(img_l, label_l, mode="train", normal=True)
train_set, val_set = build_dataset(dataset, val_rate)
del dataset
gc.collect()
train_loader = DataLoader(
train_set,
batch_size=batch_size,
shuffle=True,
pin_memory=True,
num_workers=os.cpu_count() - 1,
drop_last=True,
)
val_loader = DataLoader(
val_set,
batch_size=batch_size * 2,
shuffle=False,
pin_memory=True,
num_workers=os.cpu_count() - 1,
drop_last=True,
)
model = SARModel(
"UnetPlusPlus",
encoder_name="timm-resnest269e",
in_channels=6,
encoder_weights="imagenet",
)
# model=SARModel("UnetPlusPlus", encoder_name="resnet34", in_channels=6, encoder_weights = 'imagenet')
# ------ wandb --------
wandb_logger = WandbLogger(
project="sar",
# log_model="all",
name="f1_fix_ep=120_lr=8e-5_wd=4e-3",
)
loss_checkpoint_callback = ModelCheckpoint(
verbose=True,
filename=f"val_loss_" + "{epoch}-{val_loss:.4f}-{score_f1_val:.4f}",
monitor="val_loss",
mode="min",
save_top_k=20,
save_last=True,
save_weights_only=True,
auto_insert_metric_name=True,
# every_n_epochs = max_ep//20,
)
score_checkpoint_callback = ModelCheckpoint(
verbose=True,
filename=f"val_score_" + "{epoch}-{val_loss:.4f}-{score_f1_val:.4f}",
monitor="score_f1_val",
save_top_k=5,
save_weights_only=True,
mode="max",
auto_insert_metric_name=True,
)
lr_monitor = LearningRateMonitor(logging_interval="epoch")
# ---------------
trainer = L.Trainer(
accelerator="auto",
logger=wandb_logger,
max_steps=max_ep * len(train_loader),
max_epochs=max_ep,
gradient_clip_val=1.0,
# accumulate_grad_batches = 1,
# sync_batchnorm=True,
callbacks=[
loss_checkpoint_callback,
score_checkpoint_callback,
lr_monitor,
progress_bar,
model_summary,
],
)
trainer.fit(model=model, train_dataloaders=train_loader, val_dataloaders=val_loader)
wandb.finish()
# print(trainer.max_steps)