Пример #1
0
def main(opset=13):
    print("[main]")
    url = "https://tfhub.dev/google/imagenet/mobilenet_v3_small_075_224/feature_vector/5?tf-hub-format=compressed"
    dest = "tf-mobilenet-v3-small-075-224"
    name = "mobilenet-v3-small-075-224"
    onnx_name = os.path.join(dest, "%s-%d.onnx" % (name, opset))

    imgs = generate_random_images(shape=(1, 224, 224, 3), scale=1.)

    print("[benchmark]")
    benchmark(url, dest, onnx_name, opset, imgs)
    print("[end]")
Пример #2
0
def main(opset=13):
    url = "https://tfhub.dev/google/yamnet/1?tf-hub-format=compressed"
    dest = "tf-yamnet-tf"
    name = "yamnet"
    onnx_name = os.path.join(dest, "%s-%d.onnx" % (name, opset))
    tfl = os.path.join(dest, 'model.tflite')

    imgs = generate_random_images(shape=(16000, ), dtype=numpy.float32, scale=0.)

    # benchmark(url, dest, onnx_name, opset, imgs, convert_tflite=tfl)

    onnx_name = os.path.join(dest, "%s-tfl-%d.onnx" % (name, opset))
    benchmark_tflite(tfl, dest, onnx_name, opset, imgs)
Пример #3
0
def main(opset=13):
    url = "https://tfhub.dev/google/humpback_whale/1?tf-hub-format=compressed"
    dest = "tf-humpback-whale"
    name = "humpback-whale"
    onnx_name = os.path.join(dest, "%s-%d.onnx" % (name, opset))
    print("[download data]")
    FILENAME = 'gs://bioacoustics-www1/sounds/Cross_02_060203_071428.d20_7.wav'
    pkl_name = os.path.join(dest, "data.pkl")
    if not os.path.exists(pkl_name):
        with open(pkl_name, "wb") as f:
            waveform, sample_rate = tf.audio.decode_wav(
                tf.io.read_file(FILENAME))
            waveform = tf.expand_dims(waveform, 0)  # makes a batch of size 1
            context_step_samples = tf.cast(sample_rate, tf.int64)
            data = dict(waveform=waveform,
                        context_step_samples=context_step_samples)
            pickle.dump(data, f)
    else:
        with open(pkl_name, "rb") as f:
            data = pickle.load(f)
        waveform = data["waveform"]
        context_step_samples = data["context_step_samples"]
    print("[data] done. context_step_samples=", context_step_samples.numpy())

    def benchmark_custom(local_name):
        model = hub.load(local_name)
        score_fn = model.signatures['score']
        scores = score_fn(waveform=waveform,
                          context_step_samples=context_step_samples)
        imgs_tf = [
            dict(waveform=waveform, context_step_samples=context_step_samples)
        ]
        results_tf, duration_tf = measure_time(
            lambda inputs: score_fn(**inputs), imgs_tf)
        return scores, results_tf, duration_tf

    imgs = generate_random_images(shape=(1, 750000, 1), scale=1., n=2)
    inputs = [
        dict(waveform=waveform.numpy(),
             context_step_samples=numpy.array(context_step_samples.numpy(),
                                              dtype=numpy.int64))
    ]
    benchmark(url,
              dest,
              onnx_name,
              opset,
              inputs,
              optimize=False,
              signature='score',
              custom_tf=benchmark_custom)
Пример #4
0
def main(opset=13):
    url = "https://tfhub.dev/google/humpback_whale/1?tf-hub-format=compressed"
    dest = "tf-humpback-whale"
    name = "humpback-whale"
    onnx_name = os.path.join(dest, "%s-%d.onnx" % (name, opset))

    imgs = generate_random_images(shape=(1, 1024, 1))
    inputs = [
        dict(waveform=img,
             context_step_samples=numpy.array(512, dtype=numpy.int64))
        for img in imgs
    ]

    benchmark(url, dest, onnx_name, opset, inputs, optimize=False)
Пример #5
0
def main(opset=13):
    print('[begin]')
    url = "https://tfhub.dev/deepmind/enformer/1?tf-hub-format=compressed"
    dest = "tf-enformer"
    name = "enformer"
    onnx_name = os.path.join(dest, "%s-%d.onnx" % (name, opset))

    model = None
    if not os.path.exists(onnx_name):
        if model is None:
            model = hub.load("https://tfhub.dev/deepmind/enformer/1").model

        tf2onnx.convert.from_function(
            model.predict_on_batch,
            [tf.TensorSpec([None, 393216, 4], tf.float32)],
            opset=13,
            output_path=onnx_name)

    # benchmark(url, dest, onnx_name, opset, imgs)
    print("[generate dummy images]")
    imgs = generate_random_images(shape=(1, 393216, 4), scale=0.)

    ort = InferenceSession(onnx_name)
    fct_ort = lambda img: ort.run(None, {'args_0': img})[0]

    if model is None:
        model = hub.load("https://tfhub.dev/deepmind/enformer/1").model

    fct_tf = lambda img: model.predict_on_batch(img)

    print('[benchmark tf]')
    imgs_tf = [convert_to_tensor(img) for img in imgs]
    results_tf, duration_tf = measure_time(fct_tf, imgs)
    print("TF", len(imgs), duration_tf)

    print('[benchmark ort]')
    results_ort, duration_ort = measure_time(fct_ort, imgs)
    print("ORT", len(imgs), duration_ort)

    mean_ort = sum(duration_ort) / len(duration_ort)
    mean_tf = sum(duration_tf) / len(duration_tf)
    print("ratio ORT=%r / TF=%r = %r" %
          (mean_ort, mean_tf, mean_ort / mean_tf))

    # discrepencies
    assert_almost_equal(results_tf[0]['human'], results_ort[0], decimal=4)
    print('[end]')
def main(opset=13):
    url = "https://tfhub.dev/google/lite-model/yamnet/classification/tflite/1?lite-format=tflite"
    dest = "tf-yamnet-tflite"
    name = "yamnet"
    onnx_name = os.path.join(dest, "%s-%d.onnx" % (name, opset))

    imgs = generate_random_images(shape=(15600, ),
                                  dtype=numpy.float32,
                                  scale=0.)

    benchmark_tflite(url,
                     dest,
                     onnx_name,
                     opset,
                     imgs,
                     names=[
                         ('stft/rfft3', 'FFT_stft/rfft4_reshape__190:0'),
                         ('magnitude_spectrogram',
                          'ComplexAbsmagnitude_spectrogram__206:0'),
                         ('log_mel_spectrogram', 'log_mel_spectrogram'),
                     ])
Пример #7
0
# SPDX-License-Identifier: Apache-2.0
import os
import numpy
import onnxruntime as ort
import tensorflow as tf
import tensorflow_hub as hub
import tf2onnx
from _tools import generate_random_images, check_discrepencies

imgs = generate_random_images(shape=(1, 224, 224, 3), scale=1.)

model = tf.keras.Sequential([
    hub.KerasLayer(
        "https://tfhub.dev/google/imagenet/resnet_v1_101/feature_vector/5",
        trainable=False)
])
model.build([None, 224, 224, 3])

expected_output = model(imgs[0])

dest = "tf-resnet_v1_101"
if not os.path.exists(dest):
    os.makedirs(dest)
dest_name = os.path.join(dest, "resnet_v1_101-13-keras.onnx")
if not os.path.exists(dest_name):
    tf2onnx.convert.from_keras(model, opset=13, output_path=dest_name)

sess = ort.InferenceSession(dest_name)
print('inputs', [_.name for _ in sess.get_inputs()])
ort_output = sess.run(None, {"keras_layer_input": imgs[0]})