Skip to content

Instantly share code, notes, and snippets.

@akirasosa
Created November 8, 2017 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akirasosa/b31c3096d9cc1959cbbd8af40993c92d to your computer and use it in GitHub Desktop.
Save akirasosa/b31c3096d9cc1959cbbd8af40993c92d to your computer and use it in GitHub Desktop.
Benchmark conv block and depthwise conv block.
import time
import numpy as np
from keras import Input
from keras.applications.mobilenet import DepthwiseConv2D
from keras.engine import Model
from keras.layers import Conv2D
batch_num = 16
def conv_net(inputs):
x = Conv2D(128, (3, 3), padding='same', strides=(2, 2))(inputs)
return Model(inputs, x)
def depthwise_conv_net(inputs):
x = DepthwiseConv2D((3, 3), padding='same', strides=(2, 2))(inputs)
x = Conv2D(128, (1, 1))(x)
return Model(inputs, x)
def main():
input_tensor = Input(shape=(64, 64, 64))
experiments = [
{'name': 'conv', 'model': conv_net(input_tensor)},
{'name': 'depthwise_conv', 'model': depthwise_conv_net(input_tensor)},
]
inputs = np.random.randn(batch_num, 64, 64, 64)
for e in experiments:
time_per_batch = []
for i in range(10):
start = time.time()
e['model'].predict(inputs, batch_size=batch_num)
elapsed = time.time() - start
time_per_batch.append(elapsed)
print(elapsed, e['name'])
time_per_batch = np.array(time_per_batch)
print(e['name'])
print(time_per_batch[1:].mean())
print(time_per_batch[1:].std())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment