-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_pet_recon.py
134 lines (116 loc) · 4.03 KB
/
train_pet_recon.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
import sys
import os
import re
from pathlib import Path
import tensorflow as tf
import numpy as np
sys.path.append('./models')
# Get parameters from command line
if(len(sys.argv) != 3):
print('Usage: python train_pet_recon.py model (pre_trained | random_init)')
sys.exit()
else:
model_name = sys.argv[1]
mode = sys.argv[2]
N_CLASSES = 3
# Load the corresponding model
if model_name == 'xception':
from pet_recon_xception import ModelTools as model_tools
if mode == 'pre_trained':
model = model_tools.create_model(N_CLASSES, 'imagenet')
elif mode == 'random_init':
model = model_tools.create_model(N_CLASSES, None)
else:
print('Model ' + model_name + ' could not be found.')
sys.exit()
TOTAL_EPOCHS = 30
BATCH_SIZE = 16
TRAIN_DATASET_PATH = './pet_dataset/train'
VALIDATION_DATASET_PATH = './pet_dataset/validation'
CHECKPOINT_DIRECTORY = './checkpoints/pet_classifier/{0}_{1}'.format(model_name, mode)
SAVE_CHECKPOINT_PATH = CHECKPOINT_DIRECTORY + '/{epoch:02d}_{val_acc:.4f}.h5'
if not os.path.exists(CHECKPOINT_DIRECTORY):
os.makedirs(CHECKPOINT_DIRECTORY)
# Declare generators that read from folders
train_generator = tf.keras.preprocessing.image.ImageDataGenerator(
horizontal_flip=True,
data_format='channels_last',
rescale=1. / 255
)
train_batches = train_generator.flow_from_directory(
batch_size=BATCH_SIZE,
directory=TRAIN_DATASET_PATH,
target_size=[100, 100],
class_mode='categorical'
)
val_generator = tf.keras.preprocessing.image.ImageDataGenerator(
data_format='channels_last',
rescale=1. / 255
)
val_batches = train_generator.flow_from_directory(
batch_size=BATCH_SIZE,
directory=VALIDATION_DATASET_PATH,
target_size=[100, 100],
class_mode='categorical'
)
TRAIN_DATASET_SIZE = len(train_batches)
VAL_DATASET_SIZE = len(val_batches)
# Weighted losses for class equilibrium
unique, counts = np.unique(train_batches.classes, return_counts=True)
class_weigths = dict(zip(unique, np.true_divide(counts.sum(), N_CLASSES*counts)))
# Creates some callbacks to be called each epoch
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
SAVE_CHECKPOINT_PATH,
save_weights_only=True,
verbose=1,
monitor='val_acc',
save_best_only=True,
mode='max'
)
tensorboard_callback = tf.keras.callbacks.TensorBoard(
log_dir='./logs/pet_classifier/{0}_{1}'.format(model_name, mode),
histogram_freq=0,
batch_size=BATCH_SIZE
)
reduce_lr_callback = tf.keras.callbacks.ReduceLROnPlateau(
monitor='val_loss',
factor=0.5,
patience=3,
min_lr=1e-6
)
# Loads best weights if avaiable
if Path(CHECKPOINT_DIRECTORY).exists():
epoch_number_array = []
val_accuracy_array = []
file_name_array = []
for file in os.listdir(CHECKPOINT_DIRECTORY):
epoch, val_acc = re.search(r'(\d\d)_(\d\.\d{4})\.h5', file).group(1,2)
epoch_number_array.append(int(epoch))
val_accuracy_array.append(float(val_acc))
file_name_array.append(file)
if len(val_accuracy_array) == 0:
INITIAL_EPOCH = 0
else:
highest_acc = val_accuracy_array.index(max(val_accuracy_array))
INITIAL_EPOCH = epoch_number_array[highest_acc]
model_checkpoint_callback.best = val_accuracy_array[highest_acc]
model.load_weights('./checkpoints/pet_classifier/' + '{0}_{1}/'.format(model_name, mode) + file_name_array[highest_acc])
else:
os.makedirs(CHECKPOINT_DIRECTORY)
INITIAL_EPOCH = 0
# Prepares the model to run
model.compile(optimizer = tf.keras.optimizers.Adam(),
loss = 'categorical_crossentropy',
metrics = ['accuracy']
)
# Starts training the model
model.fit_generator(train_batches,
epochs=TOTAL_EPOCHS,
verbose=1,
steps_per_epoch=TRAIN_DATASET_SIZE,
validation_data=val_batches,
initial_epoch=INITIAL_EPOCH,
validation_steps=VAL_DATASET_SIZE,
class_weight=class_weigths,
callbacks=[model_checkpoint_callback, tensorboard_callback, reduce_lr_callback]
)