Skip to content

Instantly share code, notes, and snippets.

View Tony607's full-sized avatar

Chengwei Zhang Tony607

View GitHub Profile
@Tony607
Tony607 / benchmark.py
Created May 4, 2019 07:45
How to run Keras model on RK3399Pro | DLology
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
@Tony607
Tony607 / inference.py
Created May 4, 2019 07:44
How to run Keras model on RK3399Pro | DLology
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")
@Tony607
Tony607 / convert_rknn_snippets.py
Created May 4, 2019 07:42
How to run Keras model on RK3399Pro | DLology
from rknn.api import RKNN
INPUT_NODE = ["input_1"]
OUTPUT_NODE = ["predictions/Softmax"]
img_height = 299
# Create RKNN object
rknn = RKNN()
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
@Tony607
Tony607 / install.sh
Created May 4, 2019 07:38
How to run Keras model on RK3399Pro | DLology
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
@Tony607
Tony607 / visualize.py
Created April 22, 2019 01:10
How to run TensorFlow Object Detection model on Jetson Nano | DLology
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)
@Tony607
Tony607 / sess_run.py
Created April 22, 2019 01:10
How to run TensorFlow Object Detection model on Jetson Nano | DLology
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]
@Tony607
Tony607 / get_frozen_graph.py
Created April 22, 2019 01:10
How to run TensorFlow Object Detection model on Jetson Nano | DLology
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.
@Tony607
Tony607 / download.py
Created April 22, 2019 01:09
How to run TensorFlow Object Detection model on Jetson Nano | DLology
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')
@Tony607
Tony607 / create_inference_graph.py
Created April 22, 2019 01:09
How to run TensorFlow Object Detection model on Jetson Nano | DLology
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
)