forked from NeerajG03/Modified-Project-Tomato
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
55 lines (43 loc) · 1.75 KB
/
train.py
File metadata and controls
55 lines (43 loc) · 1.75 KB
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
import os
import json
import time
import numpy as np
from utils import print_config
from utils import load_callbacks
from utils import save_training_history
from utils import plot_training_summary
from dataset import load_dataset
from model import build_model
def run():
# Loading the running configuration
config = json.load(open("config.json", "r"))
print_config(config)
# Loading the dataloaders
train_generator, valid_generator, test_generator = load_dataset()
# Loading the model
model = build_model()
# Training the model
start = time.time()
train_history = model.fit(train_generator,
epochs=config["epochs"],
steps_per_epoch=len(train_generator),
validation_data=valid_generator,
validation_steps=len(valid_generator),
callbacks=load_callbacks(config))
end = time.time()
# Saving the model
if not os.path.exists(config["checkpoint_filepath"]):
print(f"[INFO] Creating directory {config['checkpoint_filepath']} to save the trained model")
os.mkdir(config["checkpoint_filepath"])
print(f"[INFO] Saving the model and log in \"{config['checkpoint_filepath']}\" directory")
model.save(os.path.join(config["checkpoint_filepath"], 'saved_model'))
# Saving the Training History
save_training_history(train_history, config)
# Plotting the Training History
plot_training_summary(config)
# Training Summary
training_time_elapsed = end - start
print(f"[INFO] Total Time elapsed: {training_time_elapsed} seconds")
print(f"[INFO] Time per epoch: {training_time_elapsed//config['epochs']} seconds")
if __name__ == "__main__":
run()