File: tf_separable_conv.py

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (26 lines) | stat: -rw-r--r-- 746 bytes parent folder | download | duplicates (2)
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
import tensorflow as tf
import time

with tf.device("/GPU:0"):
    img = tf.random.uniform([4, 112, 112, 32])
    depthwise_filter = tf.random.uniform([3, 3, 32, 1])
    pointwise_filter = tf.random.uniform([1, 1, 32 * 1, 16])

    best = None
    num_trials = 10
    num_iter = 10
    for j in range(num_trials):
        start = time.time()
        for i in range(num_iter):
            out = tf.nn.separable_conv2d(
                img,
                depthwise_filter,
                pointwise_filter,
                strides=(1, 1, 1, 1),
                padding="VALID",
            )
        end = time.time()
        t = (end - start) / num_iter
        if not best or t < best:
            best = t
    print(f"time: {1000 * best} ms")