-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
58 lines (49 loc) · 1.83 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
# set matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
# import packages
from config import sr_config as config
from pipeline.io import HDF5DatasetGenerator
from pipeline.nn.conv import SRCNN
from keras.optimizers import Adam
import matplotlib.pyplot as plt
import numpy as np
def super_res_generator(inputDataGen, targetDataGen):
# start an infinite loop for the training data
while True:
# grab the next input images and target outputs,
# discarding the class labels
inputData = next(inputDataGen)[0]
targetData = next(targetDataGen)[0]
# yield a tuple of the input data and target data
yield (inputData, targetData)
# initialize the input images and target output images generator
inputs = HDF5DatasetGenerator(config.INPUTS_DB, config.BATCH_SIZE)
targets = HDF5DatasetGenerator(config.OUTPUTS_DB, config.BATCH_SIZE)
# initialize the model and optimizer
print("[INFO] compiling model...")
opt = Adam(lr = 0.001, decay = 0.001 / config.NUM_EPOCHS)
model = SRCNN.build(width = config.INPUT_DIM, height = config.INPUT_DIM, depth = 3)
model.compile(loss = "mse", optimizer = opt)
# train the model using our generators
H = model.fit_generator(
super_res_generator(inputs.generator(), targets.generator()),
steps_per_epoch = inputs.numImages // config.BATCH_SIZE,
epochs = config.NUM_EPOCHS,
verbose = 1
)
# save the model to file
print("[INFO] serializing model...")
model.save(config.MODEL_PATH, overwrite = True)
# plot the training loss
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, config.NUM_EPOCHS), H.history["loss"], label = "loss")
plt.title("Loss on super resolution training")
plt.xlabel("Epoch #")
plt.ylabel("Loss")
plt.legend()
plt.savefig(config.PLOT_PATH)
# close the HDF5 datasets
inputs.close()
targets.close()