This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
times = [] | |
# Run inference 20 times and do the average. | |
for i in range(20): | |
start_time = time.time() | |
# Use the API internal call directly. | |
results = rknn.rknn_base.inference( | |
inputs=[img], data_type="uint8", data_format="nhwc", outputs=None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
from rknn.api import RKNN | |
# Create RKNN object | |
rknn = RKNN() | |
img_height = 299 | |
# Direct Load RKNN Model | |
ret = rknn.load_rknn("./inception_v3.rknn") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rknn.api import RKNN | |
INPUT_NODE = ["input_1"] | |
OUTPUT_NODE = ["predictions/Softmax"] | |
img_height = 299 | |
# Create RKNN object | |
rknn = RKNN() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip3 install -U tensorflow scipy onnx | |
pip3 install rknn_toolkit-0.9.9-cp36-cp36m-linux_x86_64.whl | |
# Or if you have Python 3.5 | |
# pip3 install rknn_toolkit-0.9.9-cp35-cp35m-linux_x86_64.whl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo dnf update -y | |
sudo dnf install -y cmake gcc gcc-c++ protobuf-devel protobuf-compiler lapack-devel | |
sudo dnf install -y python3-devel python3-opencv python3-numpy-f2py python3-h5py python3-lmdb | |
sudo dnf install -y python3-grpcio | |
sudo pip3 install scipy-1.2.0-cp36-cp36m-linux_aarch64.whl | |
sudo pip3 install onnx-1.4.1-cp36-cp36m-linux_aarch64.whl | |
sudo pip3 install tensorflow-1.10.1-cp36-cp36m-linux_aarch64.whl | |
sudo pip3 install rknn_toolkit-0.9.9-cp36-cp36m-linux_aarch64.whl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.display import Image as DisplayImage | |
# Boxes unit in pixels (image coordinates). | |
boxes_pixels = [] | |
for i in range(num_detections): | |
# scale box to image coordinates | |
box = boxes[i] * np.array([image.shape[0], | |
image.shape[1], image.shape[0], image.shape[1]]) | |
box = np.round(box).astype(int) | |
boxes_pixels.append(box) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
IMAGE_PATH = "./data/dogs.jpg" | |
image = cv2.imread(IMAGE_PATH) | |
image = cv2.resize(image, (300, 300)) | |
scores, boxes, classes, num_detections = tf_sess.run([tf_scores, tf_boxes, tf_classes, tf_num_detections], feed_dict={ | |
tf_input: image[None, ...] | |
}) | |
boxes = boxes[0] # index by 0 to remove batch dimension | |
scores = scores[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
def get_frozen_graph(graph_file): | |
"""Read Frozen Graph file from disk.""" | |
with tf.gfile.FastGFile(graph_file, "rb") as f: | |
graph_def = tf.GraphDef() | |
graph_def.ParseFromString(f.read()) | |
return graph_def | |
# The TensorRT inference graph file downloaded from Colab or your local machine. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('./data/trt_graph.pb', 'wb') as f: | |
f.write(trt_graph.SerializeToString()) | |
# Download the tensorRT graph .pb file from colab to your local machine. | |
from google.colab import files | |
files.download('./data/trt_graph.pb') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow.contrib.tensorrt as trt | |
trt_graph = trt.create_inference_graph( | |
input_graph_def=frozen_graph, | |
outputs=output_names, | |
max_batch_size=1, | |
max_workspace_size_bytes=1 << 25, | |
precision_mode='FP16', | |
minimum_segment_size=50 | |
) |