-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptioning_trainer.py
59 lines (46 loc) · 1.62 KB
/
captioning_trainer.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
""" Train and Test """
import os
from argparse import ArgumentParser
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
from captioning import ImageCaptioning
from utils import get_best_model
# import warnings
#
# warnings.filterwarnings("ignore")
def main(hparams):
""" Main Function """
model = ImageCaptioning(hparams)
checkpoint_callback = ModelCheckpoint(filepath="models/{epoch}-{val_loss:.2f}")
trainer = pl.Trainer(
checkpoint_callback=checkpoint_callback,
max_epochs=hparams.epochs,
gpus=hparams.gpus,
distributed_backend=hparams.distributed_backend,
fast_dev_run=True,
)
if not hparams.test:
trainer.fit(model)
# trainer.test(model)
else:
checkpoint_path = get_best_model("models/")
model = ImageCaptioning.load_from_checkpoint(checkpoint_path=checkpoint_path)
trainer.test(model)
if __name__ == "__main__":
root_dir = os.path.dirname(os.path.realpath(__file__))
parent_parser = ArgumentParser(add_help=False)
parent_parser.add_argument(
"--test", dest="test", action="store_true", help="run testing"
)
# gpu args
parent_parser.add_argument("--gpus", type=int, default=0, help="how many gpus")
parent_parser.add_argument(
"--distributed_backend",
type=str,
default="dp",
help="supports three options dp, ddp, ddp2",
)
# each LightningModule defines arguments relevant to it
parser = ImageCaptioning.add_model_specific_args(parent_parser, root_dir)
hyperparams = parser.parse_args()
main(hyperparams)