<jupyter_start><jupyter_text>INSTALL OpenVINO™ integration with TensorFlow<jupyter_code># Upload the required wheel files, models and images in a google drive folder
# Uncomment and run the below command to copy them in your current workspace
#!cp /content/drive/MyDrive/TF-OV/working_dir_files/* . 

!pip install --upgrade pip
!pip install pillow

# Install stock TensorFlow
!pip install tensorflow==2.5.0 

# Install OpenVINO™ integration with TensorFlow
!pip install openvino-tensorflow
!ldd --version
!python3 --version
!git clone https://github.com/openvinotoolkit/openvino_tensorflow.git
%cd openvino_tensorflow
!git submodule init
!git submodule update --recursive
%cd ..<jupyter_output>fatal: destination path 'openvino_tensorflow' already exists and is not an empty directory.
/content/openvino_tensorflow
/content<jupyter_text>Lets get the model<jupyter_code>!curl -L "https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz" | tar -C openvino_tensorflow/examples/data -xz<jupyter_output>% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 84.5M  100 84.5M    0     0  49.8M      0  0:00:01  0:00:01 --:--:-- 49.8M<jupyter_text>Now lets infer<jupyter_code>from __future__ import absolute_import, division, print_function, unicode_literals
from tensorflow.keras import backend as K
from IPython.display import HTML

import tensorflow as tf
import numpy as np
import time
import argparse
import matplotlib.pyplot as plt
import os
import openvino_tensorflow as ovtf
from subprocess import check_output, call

def load_graph(model_file):
    graph = tf.Graph()
    graph_def = tf.compat.v1.GraphDef()

    assert os.path.exists(model_file), "Could not find directory"
    with open(model_file, "rb") as f:
        graph_def.ParseFromString(f.read())
    with graph.as_default():
        tf.import_graph_def(graph_def)

    return graph

def read_tensor_from_image_file(file_name,
                                input_height=299,
                                input_width=299,
                                input_mean=0,
                                input_std=255):
    input_name = "file_reader"
    output_name = "normalized"
    file_reader = tf.io.read_file(file_name, input_name)
    if file_name.endswith(".png"):
        image_reader = tf.image.decode_png(
            file_reader, channels=3, name="png_reader")
    elif file_name.endswith(".gif"):
        image_reader = tf.squeeze(
            tf.image.decode_gif(file_reader, name="gif_reader"))
    elif file_name.endswith(".bmp"):
        image_reader = tf.image.decode_bmp(file_reader, name="bmp_reader")
    else:
        image_reader = tf.image.decode_jpeg(
            file_reader, channels=3, name="jpeg_reader")
    float_caster = tf.cast(image_reader, tf.float32)
    dims_expander = tf.expand_dims(float_caster, 0)
    resized = tf.compat.v1.image.resize_bilinear(dims_expander,
                                                 [input_height, input_width])
    normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
    sess = tf.compat.v1.Session()
    result = sess.run(normalized)

    return result
def load_labels(label_file):
    label = []
    proto_as_ascii_lines = tf.io.gfile.GFile(label_file).readlines()
    for l in proto_as_ascii_lines:
        label.append(l.rstrip())
    return label
def infer_openvino_tensorflow(model_file, input_layer, output_layer, file_name , input_height, input_width, input_mean, input_std, label_file):
    print("CREATE MODEL - BEGIN")
    graph = load_graph(model_file)
    print("CREATE MODEL - END")

    input_name = "import/" + input_layer
    output_name = "import/" + output_layer
    input_operation = graph.get_operation_by_name(input_name)
    output_operation = graph.get_operation_by_name(output_name)


    # update config params for openvino tensorflow
    config = tf.compat.v1.ConfigProto()
    config_ngraph_enabled = ovtf.update_config(config)

    print("PREDICTION - BEGIN") 

    with tf.compat.v1.Session(
            graph=graph, config=config_ngraph_enabled) as sess:
        t = read_tensor_from_image_file(
            file_name,
            input_height=input_height,
            input_width=input_width,
            input_mean=input_mean,
            input_std=input_std)
        # Warmup
        results = sess.run(output_operation.outputs[0],
                           {input_operation.outputs[0]: t})
        # Run
        
        for num_times in range(10):
            start = time.time()
            results = sess.run(output_operation.outputs[0],
                               {input_operation.outputs[0]: t})
            elapsed = time.time() - start
            print('Inference time in ms: %f' % (elapsed * 1000))
            
    print("PREDICTION - END")
    results = np.squeeze(results)

    if label_file:
        top_k = results.argsort()[-5:][::-1]
        labels = load_labels(label_file)
        for i in top_k:
            print(labels[i], results[i])
    else:
        print("No label file provided. Cannot print classification results")
%cd openvino_tensorflow/examples/data/
file_name = "grace_hopper.jpg"
model_file = "inception_v3_2016_08_28_frozen.pb"
label_file = "imagenet_slim_labels.txt"
input_height = 299
input_width = 299
input_mean = 0
input_std = 255
input_layer = "input"
output_layer = "InceptionV3/Predictions/Reshape_1"
backend_name = "CPU"
    

#Print list of available backends
print('Available Backends:')
backends_list = ovtf.list_backends()
for backend in backends_list:
    print(backend)
ovtf.set_backend(backend_name)

print("OpenVINO TensorFlow is enabled")
infer_openvino_tensorflow(model_file, input_layer, output_layer, file_name, input_height, input_width, input_mean, input_std, label_file )


#Disable
ovtf.disable()
print("")
print("")
print("OpenVINO TensorFlow is disabled")
infer_openvino_tensorflow(model_file, input_layer, output_layer, file_name, input_height, input_width, input_mean, input_std, label_file )
ovtf.enable()<jupyter_output>OpenVINO TensorFlow is disabled
CREATE MODEL - BEGIN
CREATE MODEL - END
PREDICTION - BEGIN
Inference time in ms: 210.209131
Inference time in ms: 199.272156
Inference time in ms: 206.316471
Inference time in ms: 204.569101
Inference time in ms: 208.819628
Inference time in ms: 213.358879
Inference time in ms: 203.723669
Inference time in ms: 200.221777
Inference time in ms: 209.861755
Inference time in ms: 196.017742
PREDICTION - END
military uniform 0.8343066
mortarboard 0.021869425
academic gown 0.010358032
pickelhaube 0.008008148
bulletproof vest 0.0053508785
{'fraction_non_alphanumeric': 0.09090909090909091, 'fraction_numerical': 0.03522913867741454, 'mean_word_length': 3.2976266837716484, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '130974', 'license': 'Apache-2.0', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2148, 'n_words': 506, 'path': 'docs/cloud_instructions/Colab_OpenVINO_TensorFlow_classification_example.ipynb', 'repo_name': 'iazaidi7/openvino_tensorflow', 'size': '6699'}