forked from davidtellez/contrastive-predictive-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_model.py
142 lines (122 loc) · 4.32 KB
/
benchmark_model.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
''' This module evaluates the performance of a trained CPC encoder '''
from data_utils import MnistGenerator
from os.path import join, basename, dirname, exists
import keras
import datetime
import argparse
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
def build_model(encoder_path, image_shape, learning_rate):
# Read the encoder
encoder = keras.models.load_model(encoder_path)
# Freeze weights
encoder.trainable = False
for layer in encoder.layers:
layer.trainable = False
# Define the classifier
x_input = keras.layers.Input(image_shape)
x = encoder(x_input)
x = keras.layers.Dense(units=128, activation='linear')(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.LeakyReLU()(x)
x = keras.layers.Dense(units=10, activation='softmax')(x)
# Model
model = keras.models.Model(inputs=x_input, outputs=x)
# Compile model
model.compile(
optimizer=keras.optimizers.Adam(lr=learning_rate),
loss='categorical_crossentropy',
metrics=['categorical_accuracy']
)
model.summary()
return model
def benchmark_model(args, encoder_path, epochs, batch_size, output_dir, lr=1e-4, image_size=28, color=False):
# Prepare data
train_data = MnistGenerator(batch_size, subset='train', image_size=image_size, color=color, rescale=True)
validation_data = MnistGenerator(batch_size, subset='valid', image_size=image_size, color=color, rescale=True)
# Prepares the model
model = build_model(encoder_path, image_shape=(image_size, image_size, 3), learning_rate=lr)
# Callbacks
callbacks = [
# keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=1/3, patience=2, min_lr=1e-4),
keras.callbacks.TensorBoard(log_dir='./logs/bm_' + args.name + '_' +datetime.datetime.now().strftime('%d_%H-%M-%S ') , histogram_freq=0, batch_size=32, write_graph=True, write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None, embeddings_data=None, update_freq='epoch')
]
# Trains the model
model.fit_generator(
generator=train_data,
steps_per_epoch=len(train_data),
validation_data=validation_data,
validation_steps=len(validation_data),
epochs=epochs,
verbose=1,
callbacks=callbacks
)
# Saves the model
model.save(join(output_dir, 'supervised.h5'))
if __name__ == "__main__":
argparser = argparse.ArgumentParser(
description='CPC')
# argparser.add_argument(
# '--host',
# metavar='H',
# default='localhost',
# help='IP of the host server (default: localhost)')
argparser.add_argument(
'--name',
default='cpc',
help='name')
# argparser.add_argument(
# '-p', '--port',
# metavar='P',
# default=2000,
# type=int,
# help='TCP port to listen to (default: 2000)')
# argparser.add_argument(
# '-s', '--step-limit',
# default=256,
# type=int,
# help='Step limit of each run. (default: 256)')
# argparser.add_argument(
# '-i', '--image-size',
# default=160,
# type=int,
# help='Size of images (default: 320).')
# argparser.add_argument(
# '-b', '--batch-size',
# default=32,
# type=int,
# help='Size of batches.')
# argparser.add_argument(
# '-t', '--train-epoch',
# default=100,
# type=int,
# help='Times of train.')
# argparser.add_argument(
# '--vaealpha',
# default=1,
# type=int,
# help='Times of train.')
# argparser.add_argument(
# '--mse-weight',
# default=0.01,
# type=float,
# help='Weight of MSE.')
# argparser.add_argument(
# '--name',
# default=0.01,
# type=float,
# help='Weight of MSE.')
# argparser.add_argument('--include-throttle', action='store_true', default=False, help='Include Throttle')
args = argparser.parse_args()
benchmark_model(
args,
encoder_path='models/64x64/encoder_' + args.name + '.h5',
epochs=15,
batch_size=64,
output_dir='models/64x64',
lr=1e-3,
image_size=64,
color=True
)