-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbenchmark_keras.py
More file actions
162 lines (131 loc) · 5.07 KB
/
Copy pathbenchmark_keras.py
File metadata and controls
162 lines (131 loc) · 5.07 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import numpy as np
from time import time
import keras
import keras.backend as K
from keras.models import Model
from keras.layers import Flatten, Dense, Input
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
from keras.optimizers import SGD
from keras.layers import Activation
from keras.layers.normalization import BatchNormalization
from keras.datasets import cifar10
import utils
from keras.utils import np_utils
def vgg_block(x, nb_filters, use_bn, nb_conv, bn_axis, data_format):
for i in range(nb_conv):
x = Conv2D(filters=nb_filters, kernel_size=(3, 3), padding='same', data_format=data_format)(x)
if use_bn:
x = BatchNormalization(scale=False, axis=bn_axis)(x)
x = Activation("relu")(x)
x = MaxPooling2D((2, 2), strides=(2, 2), padding='valid', data_format=data_format)(x)
return x
def run_VGG16(batch_size=16, n_trials=100, use_bn=False, data_format="NCHW"):
"""Run VGG16 experiment
Args:
batch_size: mini batch size (default: {16})
n_trials: number of forward + backward + weight update iterations (default: {100})
"""
# Determine proper input shape
if data_format == "NCHW":
assert K.image_data_format() == 'channels_first', "Change your keras.json file"
# Update NCHW to channels_first (keras conventions)
data_format = "channels_first"
input_shape = (3, 224, 224)
bn_axis = 1
else:
assert K.image_data_format() == 'channels_last', "Change your keras.json file"
data_format = "channels_last"
input_shape = (224, 224, 3)
bn_axis = -1
img_input = Input(shape=input_shape)
# Block 1
x = vgg_block(img_input, 64, use_bn, 2, bn_axis, data_format)
# Block 2
x = vgg_block(x, 128, use_bn, 2, bn_axis, data_format)
# Block 3
x = vgg_block(x, 256, use_bn, 3, bn_axis, data_format)
# Block 4
x = vgg_block(x, 512, use_bn, 3, bn_axis, data_format)
# Block 5
x = vgg_block(x, 512, use_bn, 3, bn_axis, data_format)
# Classification block
x = Flatten(name='flatten')(x)
x = Dense(units=4096, activation='relu', name='fc1')(x)
x = Dense(units=4096, activation='relu', name='fc2')(x)
x = Dense(units=1000, activation='softmax', name='predictions')(x)
# Create model
model = Model(inputs=img_input, outputs=x)
model.summary()
opt = SGD()
model.compile(loss='categorical_crossentropy', optimizer=opt)
# Input data
X = np.zeros((batch_size,) + input_shape, dtype=np.float32)
Y = np.zeros((batch_size, 1000), dtype=np.float32)
# warmup
model.train_on_batch(X, Y)
t0 = time()
for i in range(n_trials):
model.train_on_batch(X, Y)
t1 = time()
# Import backend to get version number
if K.backend() == "tensorflow":
import tensorflow as backend
elif K.backend() == "theano":
import theano as backend
# Print summary
utils.print_module("Keras version: %s" % keras.__version__)
utils.print_module("Keras backend: %s" % K.backend())
utils.print_module("Backend version: %s" % backend.__version__)
utils.print_result("%7.3f ms." % (1000. * (t1 - t0) / n_trials))
def run_SimpleCNN(batch_size):
# Determine proper input shape
if K.image_dim_ordering() == 'th':
input_shape = (3, 32, 32)
else:
input_shape = (32, 32, 3)
img_input = Input(shape=input_shape)
# Block 1
x = Conv2D(32, 3, 3, border_mode="same", activation="relu")(img_input)
x = Conv2D(32, 3, 3, border_mode="same", activation="relu")(x)
x = MaxPooling2D()(x)
# Block 2
x = Conv2D(64, 3, 3, border_mode="same", activation="relu")(x)
x = Conv2D(64, 3, 3, border_mode="same", activation="relu")(x)
x = MaxPooling2D()(x)
# Dense part
x = Flatten(name='flatten')(x)
x = Dense(512,activation="relu")(x)
x = Dense(10,activation="relu")(x)
# Create model
model = Model(img_input, x)
opt = SGD()
model.compile(loss='categorical_crossentropy', optimizer=opt)
# Input data
data = cifar10.load_data()
X, Y = data[0][0], data[0][1]
Y = np_utils.to_categorical(Y, nb_classes=10)
# warmup
model.train_on_batch(X[:32], Y[:32])
# Split in chunks of size batch size
num_elem = X.shape[0]
chunk_size = batch_size
num_chunks = num_elem / chunk_size
list_chunks = np.array_split(np.arange(num_elem), num_chunks)
for e in range(10):
t0 = time()
for index, chunk_idx in enumerate(list_chunks):
X_batch, Y_batch = X[chunk_idx], Y[chunk_idx]
model.train_on_batch(X_batch, Y_batch)
t1 = time()
print t1 - t0
# Import backend to get version number
if K.backend() == "tensorflow":
import tensorflow as backend
elif K.backend() == "theano":
import theano as backend
# Print summary
utils.print_module("Keras version: %s" % keras.__version__)
utils.print_module("Keras backend: %s" % K.backend())
utils.print_module("Backend version: %s" % backend.__version__)
utils.print_result("%7.3f ms." % (1000. * (t1 - t0) / n_trials))