-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
51 lines (34 loc) · 1.38 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
import os
import warnings
from models.getter import model_getter
from options.train_options import TrainOptions
from data.data_loader import get_data_loader
from data.data_generator import get_data_generator
from utils.utils import save_history, save_model, get_latest_checkpoint
from utils.callbacks import get_callbacks
warnings.filterwarnings("ignore")
def train(opt):
# Get data generator
data_generator = get_data_generator(opt)
# Load data
data_generator = get_data_loader(opt, data_generator, opt.train_dataset_dir)
# Get model
model = model_getter(opt)
# Get callbacks
callbacks = get_callbacks(opt)
# Get checkpoint and initial epoch for resume training
checkpoint_path, initial_epoch = get_latest_checkpoint(opt)
# Load weights from checkpoint
if checkpoint_path:
model.load_weights(checkpoint_path)
# Fit model
history = model.fit(data_generator, batch_size=opt.batch_size, epochs=opt.epoch, callbacks=callbacks, initial_epoch=initial_epoch)
# Save history
history_path = os.path.join(opt.results_dir, 'model', 'history.json')
save_history(history_path, history)
# Save model
model_path = os.path.join(opt.results_dir, 'model', 'model.h5')
save_model(model_path, model)
if __name__ == '__main__':
opt = TrainOptions().parse()
train(opt)