-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
163 lines (139 loc) · 3.9 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
163
# -*- coding: utf-8 -*-
#!/usr/bin/env python -W ignore::DeprecationWarning
"""
Runs a model on a single node across N-gpus.
"""
import os
from argparse import ArgumentParser
import numpy as np
import torch
from lightning_module import LightningTemplateModel
from pytorch_lightning import Trainer
from pytorch_lightning.logging import TensorBoardLogger
from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
import re
import pathlib
import collections
def getLastCP():
py = pathlib.Path().glob("checkpoints/*.ckpt")
cpts={}
try:
for file in py:
ind=re.match('.*?_([0-9]+).*$', str(file) ).group(1)
cpts[int(ind)]= str(file)
print(file)
print(ind)
cpts = collections.OrderedDict(sorted(cpts.items()))
k=list(cpts.keys() )
klast=k[len(k)-1]
print(cpts[klast])
return cpts[klast]
except:
return None
#SEED = 2334
#torch.manual_seed(SEED)
#np.random.seed(SEED)
checkpoint_callback = ModelCheckpoint(
save_top_k=5,
period=1,
filepath='./checkpoints/',
prefix='',
verbose=True
)
earlystopp_callback=EarlyStopping(
monitor='val_loss',
min_delta=0.0,
patience=100,
verbose=0,
mode='auto')
logger = TensorBoardLogger(
save_dir=os.getcwd(),
name='lightning_logs'
)
def main(hparams):
"""
Main training routine specific for this project
:param hparams:
"""
# ------------------------
# 1 INIT LIGHTNING MODEL
# ------------------------
model = LightningTemplateModel(hparams)
last_cp= getLastCP()
#if (last_cp!=None):
# model.load_from_checkpoint(checkpoint_path=last_cp)
# ------------------------
# 2 INIT TRAINER
# ------------------------
trainer = Trainer(
default_save_path='./checkpoints/',
logger=logger,
amp_level='O2',
resume_from_checkpoint=last_cp,
use_amp=False,
gradient_clip_val=1,
gpus=hparams.gpus,
checkpoint_callback=checkpoint_callback,
early_stop_callback=earlystopp_callback,
distributed_backend=hparams.distributed_backend,
)
# ------------------------
# 3 START TRAINING
# ------------------------
trainer.fit(model)
if __name__ == '__main__':
# ------------------------
# TRAINING ARGUMENTS
# ------------------------
# these are project-wide arguments
root_dir = os.path.dirname(os.path.realpath(__file__))
parent_parser = ArgumentParser(add_help=False)
# gpu args
parent_parser.add_argument(
'--gpus',
type=int,
default=1,
help='how many gpus'
)
parent_parser.add_argument(
'--distributed_backend',
type=str,
default=None,
help='supports three options dp, ddp, ddp2'
)
parent_parser.add_argument(
'--use_16bit',
dest='use_16bit',
action='store_true',
help='if true uses 16 bit precision'
)
# input image size
parent_parser.add_argument(
'--input_size',
type=int,
default=256,
help='input image size'
)
# is input image color ?'
parent_parser.add_argument(
'--is_color',
type=bool,
default=True,
help='is input image color ?'
)
# dataset path
parent_parser.add_argument(
'--data_dir',
type=str,
default='Y:/PRNet_PyTorch/utils/300WLP_IBUG',
help='dataset path'
)
# each LightningModule defines arguments relevant to it
parser = LightningTemplateModel.add_model_specific_args(parent_parser, root_dir)
hyperparams = parser.parse_args()
# ---------------------
# RUN TRAINING
# ---------------------
main(hyperparams)