Snippets Collections
# 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/* . 

import os

# Enable these variables for runtime inference optimizations
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "1"

!python3 -m pip install --upgrade pip
!python3 -m pip -q install pillow
!python3 -m pip -q install -U tensorflow_hub

# Install TensorFlow (v2.8.0) and OpenVINO-TensorFlow (v2.0.0) only if they aren't found
!if python3 -c "import tensorflow"; then echo "Found TensorFlow. Skipping."; else echo "TensorFlow Not Found. Installing."; python3 -m pip -q install tensorflow==2.8.0; fi
!if python3 -c "import openvino_tensorflow"; then echo "Found OpenVINO-TensorFlow. Skipping."; else echo "OpenVINO-TensorFlow Not Found. Installing.";

# Now let's infer
from __future__ import absolute_import, division, print_function, unicode_literals
from IPython.display import HTML

import numpy as np
import tensorflow as tf
import openvino_tensorflow as ovtf
import tensorflow_hub as hub

from PIL import Image
import time

def preprocess_image(file_name,
    """Reads input image file, resizes it to given input height and width
       and returns the pre-processed image
    image =
    resized_image = image.resize((input_height,input_width))
    resized_image = np.asarray(resized_image, np.float32)
    normalized_image = (resized_image - input_mean) / input_std
    result = np.expand_dims(normalized_image, 0)
    return result

def load_labels(label_file):
    """Parses the label file, assuming that labels are separated with a newline
       in the file and returns the list of labels.
    label = []
    proto_as_ascii_lines =
    for l in proto_as_ascii_lines:
    return label

def infer_openvino_tensorflow(model_file, file_name , input_height, input_width, input_mean, input_std, label_file):
    """Takes the tensorflow model and all other input parameters as arguments. 
       Runs inference with the classification model and prints the predictions.
    print("CREATE MODEL - BEGIN")
    if model_file=="":
        model = hub.load("")
        model = tf.saved_model.load(model_file)
    print("CREATE MODEL - END")

    print("PREDICTION - BEGIN") 

    img =  tf.convert_to_tensor(preprocess_image(
            file_name, input_height=input_height, input_width=input_width, input_mean=input_mean, input_std=input_std))

    # Warmup
    results = model(img)
    # Run
    for num_times in range(10):
        start = time.time()
        results = model(img)
        elapsed = time.time() - start
        print('Inference time in ms: %f' % (elapsed * 1000))
    print("PREDICTION - END")
    results = tf.nn.softmax(results).numpy()
    if label_file:
        top_5 = tf.argsort(results, axis=-1, direction="DESCENDING")[0][:5].numpy()
        labels = load_labels(label_file)
        for i,item in enumerate(top_5):
            print(labels[item], results[0][top_5][i])
        print("No label file provided. Cannot print classification results")

# Set all the parameters needed for inference
# Enable OpenVINO™ integration with TensorFlow, and set Backend in just a few simple lines of code to boost performace
#Infer the input image
# Output the top 5 predicted classes, and the inference time with OpenVINO™ integration with TensorFlow enabled

file_name = tf.keras.utils.get_file(
model_file = ""
label_file = tf.keras.utils.get_file(
input_height = 299
input_width = 299
input_mean = 0
input_std = 255
backend_name = "CPU"

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

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

# Disable OpenVINO™ integration with TensorFlow to gauge the achieved performance boost
# Infer the input image again
# Output the top 5 predicted classes, and the inference time with OpenVINO™ integration with TensorFlow disabled

ovtf.disable() ## Disabling OVTF
print("OpenVINO TensorFlow is disabled")
infer_openvino_tensorflow(model_file, file_name, input_height, input_width, input_mean, input_std, label_file )
from sklearn.model_selection import train_test_split,cross_val_predict,StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report,roc_auc_score,roc_curve
from imblearn.pipeline import Pipeline as imbPipe
from imblearn.over_sampling import SMOTE
from sklearn.ensemble import VotingClassifier
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import LinearSVC, SVC
from sklearn.neighbors import KNeighborsClassifier

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42,
                                                    shuffle=True, stratify  = y)

dct = DecisionTreeClassifier(random_state=42)
sgd = SGDClassifier(random_state=42)
log = LogisticRegression(random_state=42)
svm_rbf = SVC(kernel="rbf", random_state=42)
svm_lin = LinearSVC(loss="hinge")

kfold = StratifiedKFold(n_splits=4, shuffle=True, random_state=42)

Voting_pipeline = imbPipe([
    ("scaler", StandardScaler()),
    ("smote", SMOTE(random_state=42,n_jobs=-1)),
    ("voting", VotingClassifier(estimators=[("dct", dct),
                                            ("sgd", sgd),
                                            ("svm_rbf", svm_rbf),
                                            ("smv_lin", svm_lin),
                                            ("log", log)],voting="hard",n_jobs=-1))

y_pred = cross_val_predict(Voting_pipeline, X_train, y_train, cv = kfold)
print(classification_report(y_train, y_pred))

Thu Jun 16 2022 09:20:59 GMT+0000 (UTC)

#python #openvino #object-classification #classification #tensorflow

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension