-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
142 lines (106 loc) · 4.7 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
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
135
136
137
138
139
140
141
142
from tensorflow.keras.callbacks import ModelCheckpoint
import h5py
import numpy as np
from tensorflow import keras
(train_images, train_labels), (test_images, test_labels) = keras.datasets.mnist.load_data()
train_labels = train_labels[:1000]
test_labels = test_labels[:1000]
train_images = train_images[:1000].reshape(-1, 28 * 28) / 255.0
test_images = test_images[:1000].reshape(-1, 28 * 28) / 255.0
# Returns a short sequential model
def create_model():
model = keras.models.Sequential([
keras.layers.Dense(512, activation="relu", input_shape=(784,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation="softmax")
])
model.compile(optimizer=keras.optimizers.Adam(),
loss=keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
return model
# Create a basic model instance
model = create_model()
model.summary()
model = create_model()
import os
if not os.path.exists('./results/'):
os.mkdir('./results/')
class MetaCheckpoint(ModelCheckpoint):
def __init__(self, filepath, monitor='val_loss', verbose=0,
save_best_only=False, save_weights_only=False,
mode='auto', period=1, training_args=None, meta=None):
super(MetaCheckpoint, self).__init__(filepath,
monitor=monitor,
verbose=verbose,
save_best_only=save_best_only,
save_weights_only=save_weights_only,
mode=mode,
period=period)
self.filepath = filepath
self.new_file_override = True
self.meta = meta or {'epochs': [], self.monitor: []}
if training_args:
self.meta['training_args'] = training_args
def on_train_begin(self, logs={}):
if self.save_best_only:
if 'acc' in self.monitor or self.monitor.startswith('fmeasure'):
self.best = max(self.meta[self.monitor], default=-np.Inf)
else:
self.best = min(self.meta[self.monitor], default=np.Inf)
super(MetaCheckpoint, self).on_train_begin(logs)
def on_epoch_end(self, epoch, logs={}):
# 只有在‘只保存’最优版本且生成新的.h5文件的情况下
if self.save_best_only:
current = logs.get(self.monitor)
if self.monitor_op(current, self.best):
self.new_file_override = True
else:
self.new_file_override = False
super(MetaCheckpoint, self).on_epoch_end(epoch, logs)
# Get statistics
self.meta['epochs'].append(epoch)
for k, v in logs.items():
# Get default gets the value or sets (and gets) the default value
self.meta.setdefault(k, []).append(v)
# Save to file
filepath = self.filepath.format(epoch=epoch, **logs)
if self.new_file_override and self.epochs_since_last_save == 0:
# 只有在‘只保存’最优版本且生成新的.h5文件的情况下 才会继续添加meta
with h5py.File(filepath, 'r+') as f:
meta_group = f.create_group('meta')
meta_group.attrs['training_args'] = yaml.dump(
self.meta.get('training_args', '{}'))
meta_group.create_dataset('epochs', data=np.array(self.meta['epochs']))
for k in logs:
meta_group.create_dataset(k, data=np.array(self.meta[k]))
import yaml
def load_meta(model_fname):
"""
Load meta configuration
:param model_fname: model file name
:return: meta info
"""
meta = {}
with h5py.File(model_fname, 'r') as f:
meta_group = f['meta']
meta['training_args'] = yaml.load(
meta_group.attrs['training_args'])
for k in meta_group.keys():
meta[k] = list(meta_group[k])
return meta
def get_last_status(model):
last_epoch = -1
last_meta = {}
if os.path.exists("./results/pointnet.h5"):
model.load_weights("./results/pointnet.h5")
last_meta = load_meta("./results/pointnet.h5")
last_epoch = last_meta.get('epochs')[-1]
return last_epoch, last_meta
last_epoch, last_meta = get_last_status(model)
checkpoint = MetaCheckpoint('./results/pointnet.h5', monitor='val_acc',
save_weights_only=True, save_best_only=True,
verbose=1, meta=last_meta)
model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels),
callbacks=[checkpoint],
initial_epoch=last_epoch + 1)