<jupyter_start><jupyter_text>Testing TF Models in Tx-2  Model benchmarker mobilenet_v1_1.0_224<jupyter_code># Run setup or download and extract the below models

# wget -nc http://download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_1.0_224.tgz
# tar -xvf mobilenet_v1_1.0_224.tgz

import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile

from distutils.version import StrictVersion
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
import json
import time
import cv2
## Not used code
## Path to frozen graph is added directly

# MODEL_NAME = 'mobilenet_v1_1.0_224'

# MODEL_FILE = MODEL_NAME + '.tgz'
# DOWNLOAD_BASE = 'http://download.tensorflow.org/models/mobilenet_v1_2018_08_02/'

# info='Time taken to download Model:'

# start_time=time.time()

# opener = urllib.request.URLopener()
# opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)

# end_time=time.time()
# time_taken=end_time-start_time
# print(info,time_taken)

# Path to frozen detection graph. This is the actual model that is used for the object detection.
# Graph = Tests/data/mobilenet_v1_1.0_224
# Extracted graph path

PATH_TO_FROZEN_GRAPH = '../data/mobilenet_v2_1.4_224/mobilenet_v2_1.4_224_frozen.pb'

info='Time taken to load Model into memory:'
start_time=time.time()

detection_graph = tf.Graph()
with detection_graph.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')
    
end_time=time.time()
time_taken=end_time-start_time
print(info,time_taken)
# Load the labels

#Load preprocessing data
mean = 128 
std = 1.0/128.0 

#Load categories
categories = []
with open('../data/' + 'categories.txt', 'r') as f:
    for line in f:
        cat = line.split('\n')[0]
        if cat != 'classes':
            categories.append(cat)
    f.close()
    print('Number of categories:', len(categories))
#categories
# Load image size
with open('../data/' + 'inputsize.txt', 'r') as f:
    reqsize = int(f.readline().split('\n')[0])
print(reqsize)<jupyter_output>224<jupyter_text>Preparing Input<jupyter_code>#image_filename = '../data/' + 'Tiger.jpg'

image_filename = '../data/' + 'image1.jpg'

img = cv2.imread(image_filename)#.astype(numpy.float32)

print(img.shape)
# Resizing
# dx,dy,dz= img.shape
# delta=float(abs(dy-dx))
# if dx > dy: #crop the x dimension
#     img=img[int(0.5*delta):dx-int(0.5*delta),0:dy]
# else:
#     img=img[0:dx,int(0.5*delta):dy-int(0.5*delta)]


img = cv2.resize(img, (reqsize, reqsize))

img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)

#for i in range(3):
#    img[:,:,i] = (img[:,:,i] - mean) * std

#img = cv2.resize(img, (reqsize, reqsize))
img = img.astype(float)
print(img.shape)
def preprocess_input(x):
    x /= 255.0
    x -= 0.5
    x *= 2.0
    return x
img = preprocess_input(img)
from matplotlib import pyplot as plt

plt.rcParams.update({'font.size': 18})

plt.figure(figsize=(10,10))

plt.imshow(img)
plt.show()<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).<jupyter_text>Running inference using TF<jupyter_code>sess=tf.Session(graph=detection_graph)
def run_inference(key_name,image, graph):
    
    #model output layer name
    ops = detection_graph.get_operations()
    all_tensor_names = {output.name for op in ops for output in op.outputs}
    #print(all_tensor_names)
    tensor_dict = {}
    for key in [key_name]:
        tensor_name = key + ':0'
        if tensor_name in all_tensor_names:
            tensor_dict[key] = detection_graph.get_tensor_by_name(tensor_name)
            
    image=image.reshape(1,image.shape[0],image.shape[1],image.shape[2])
    image_tensor = graph.get_tensor_by_name('input:0')
    # Run inference
    info='Time taken to run inference:'
    start_time=time.time()

    output_dict = sess.run(tensor_dict,
                          feed_dict={image_tensor: image})
    
    end_time=time.time()
    time_taken=end_time-start_time
    print(info,time_taken)

    #print(output_dict)
    top_inds = output_dict[key_name][0].argsort()[::-1][:10]
    print(output_dict[key_name][0].shape)
    result=[]
    for i in range(10):
        result.append([top_inds[i], categories[top_inds[i]], output_dict[key_name][0][top_inds[i]]])
    return result
# key_name='MobilenetV1/Predictions/Softmax'
# run_inference(key_name,img,detection_graph)

#Gives the same results as Reshape_1 output layer
key_name='MobilenetV2/Predictions/Reshape_1'
result=run_inference(key_name,img,detection_graph)
result<jupyter_output><empty_output><jupyter_text>benchmarking the TF model by running inference 100 times<jupyter_code># Settings: run_inference_b1
# 1. Model is in memory.
# 2. Input is well prepared and in memory
# 3. Calculating time just to run the inference.
def run_inference_b1(key_name,image, graph,no_of_run):
    
    #model output layer name
    ops = detection_graph.get_operations()
    all_tensor_names = {output.name for op in ops for output in op.outputs}
    #print(all_tensor_names)
    tensor_dict = {}
    for key in [key_name]:
        tensor_name = key + ':0'
        if tensor_name in all_tensor_names:
            tensor_dict[key] = detection_graph.get_tensor_by_name(tensor_name)
            
    image=image.reshape(1,image.shape[0],image.shape[1],image.shape[2])
    image_tensor = graph.get_tensor_by_name('input:0')
    # Run inference
    info='Time taken to run inference: run_inference_b1:'+str(no_of_run)+' Times: '
    start_time=time.time()
    
    for i in range(no_of_run):
        output_dict = sess.run(tensor_dict,
                          feed_dict={image_tensor: image})
    
    end_time=time.time()
    time_taken=end_time-start_time
    print(info,time_taken)

    #print(output_dict)
    top_inds = output_dict[key_name][0].argsort()[::-1][:5]
    
    result=[]
    for i in range(5):
        result.append([top_inds[i], categories[top_inds[i]], output_dict[key_name][0][top_inds[i]]])
    return result, time_taken
 
key_name='MobilenetV1/Predictions/Reshape_1'
result,time_taken=run_inference_b1(key_name,img,detection_graph,10000)
print('Time Taken is:',time_taken)
print(result)
# With img all pointint to '0'

img2 = np.zeros((224, 224,3))

key_name='MobilenetV1/Predictions/Reshape_1'
result2,time_taken2=run_inference_b1(key_name,img2,detection_graph,10000)
print('Time Taken is:',time_taken2)
print(result2)
from matplotlib import pyplot as plt

plt.rcParams.update({'font.size': 18})

plt.figure(figsize=(10,10))

plt.imshow(img2)
plt.show()
# With img all pointint to '1'

img3 = np.ones((224, 224,3))

key_name='MobilenetV1/Predictions/Reshape_1'
result3,time_taken3=run_inference_b1(key_name,img3,detection_graph,10000)
print('Time Taken is:',time_taken3)
print(result3)
from matplotlib import pyplot as plt

plt.rcParams.update({'font.size': 18})

plt.figure(figsize=(10,10))

plt.imshow(img3)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12730833799664243, 'fraction_numerical': 0.03077783995523223, 'mean_word_length': 4.516203703703703, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '614778', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2804, 'n_tokens_neox': 2646, 'n_words': 527, 'path': 'computation/Tests/Jetson/TF_models.ipynb', 'repo_name': 'y-x-c/Heliot', 'size': '7148'}