-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transferred all the merged files from old repo
- Loading branch information
1 parent
a8e1bb6
commit 47e47fc
Showing
123 changed files
with
2,800 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import tensorflow as tf | ||
|
||
from tensorflow import keras | ||
# Define a simple sequential model | ||
model = tf.keras.models.Sequential([ | ||
tf.keras.layers.Flatten(input_shape=(28, 28)), | ||
tf.keras.layers.Dense(128, activation='relu'), | ||
tf.keras.layers.Dropout(0.2), | ||
tf.keras.layers.Dense(10) | ||
]) | ||
|
||
model.compile(optimizer='adam', | ||
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), | ||
metrics=['accuracy']) | ||
|
||
mnist = tf.keras.datasets.mnist | ||
|
||
(x_train, y_train), (x_test, y_test) = mnist.load_data() | ||
x_train, x_test = x_train / 255.0, x_test / 255.0 | ||
x_train = tf.cast(x_train, dtype=tf.float32) | ||
y_train = tf.cast(y_train, dtype=tf.float32) | ||
x_test = tf.cast(x_test, dtype=tf.float32) | ||
y_test = tf.cast(y_test, dtype=tf.float32) | ||
|
||
|
||
# Train the model | ||
#model.fit(x_train, y_train, epochs=1) | ||
|
||
# Evaluate your model accuracy | ||
#model.evaluate(x_test, y_test, verbose=2) | ||
|
||
# Save model in the saved_model format | ||
SAVED_MODEL_DIR="./models/native_saved_model" | ||
model.save(SAVED_MODEL_DIR) | ||
|
||
from tensorflow.python.compiler.tensorrt import trt_convert as trt | ||
|
||
# Instantiate the TF-TRT converter | ||
converter = trt.TrtGraphConverterV2( | ||
input_saved_model_dir=SAVED_MODEL_DIR, | ||
precision_mode=trt.TrtPrecisionMode.INT8, | ||
use_calibration=True | ||
) | ||
|
||
# Use data from the test/validation set to perform INT8 calibration | ||
BATCH_SIZE=32 | ||
NUM_CALIB_BATCHES=10 | ||
def calibration_input_fn(): | ||
for i in range(NUM_CALIB_BATCHES): | ||
start_idx = i * BATCH_SIZE | ||
end_idx = (i + 1) * BATCH_SIZE | ||
x = x_test[start_idx:end_idx, :] | ||
yield [x] | ||
|
||
# Convert the model and build the engine with valid calibration data | ||
func = converter.convert(calibration_input_fn=calibration_input_fn) | ||
converter.build(calibration_input_fn) | ||
|
||
OUTPUT_SAVED_MODEL_DIR="./models/tftrt_saved_model" | ||
converter.save(output_saved_model_dir=OUTPUT_SAVED_MODEL_DIR) | ||
|
||
converter.summary() | ||
|
||
# Run some inferences! | ||
for step in range(10): | ||
start_idx = step * BATCH_SIZE | ||
end_idx = (step + 1) * BATCH_SIZE | ||
|
||
print(f"Step: {step}") | ||
x = x_test[start_idx:end_idx, :] | ||
func(x) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from jtop import jtop | ||
import csv | ||
import argparse | ||
import os | ||
import time | ||
|
||
# arguments: --duration 1 --file jtop.csv | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description='Simple jtop logger') | ||
parser.add_argument('--duration', action="store", dest="duration", type=int, default=1) # 1 minute | ||
parser.add_argument('--file', action="store", dest="file", default="jtop.csv") | ||
parser.add_argument('--verbose', action="store_true", dest="verbose", default=True) | ||
|
||
args = parser.parse_args() | ||
|
||
with jtop() as jetson: | ||
with open(args.file, 'w', newline='') as csvfile: | ||
count = 0 | ||
fieldnames = ['cpu_usage', 'memory_usage', 'gpu_usage', 'cpu_temp', 'gpu_temp', 'gpu_mem', 'system_voltage', 'system_current', 'system_power'] | ||
# Initialize csv writer | ||
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | ||
# Write header | ||
writer.writeheader() | ||
# Start loop | ||
os.system("clear") # start ros2 launch | ||
while jetson.ok() and count < args.duration * 60: | ||
cpu_usage = jetson.cpu['total']['system'] + jetson.cpu['total']['user'] | ||
memory_usage = (jetson.memory['RAM']['used'] / jetson.memory['RAM']['tot']) * 100 | ||
gpu_usage = jetson.gpu['ga10b']['status']['load'] | ||
cpu_temp = jetson.temperature['CPU']['temp'] | ||
gpu_temp = jetson.temperature['GPU']['temp'] | ||
gpu_mem = int(str(jetson.memory['RAM']['shared'])[:3]) | ||
system_voltage = jetson.power['tot']['volt'] | ||
system_current = jetson.power['tot']['curr'] | ||
system_power = jetson.power['tot']['power'] | ||
|
||
if args.verbose: | ||
print(f"CPU Usage: {cpu_usage:.1f}%") | ||
print(f"Memory Usage: {memory_usage:.1f}%") | ||
print(f"GPU Usage: {gpu_usage:.1f}%") | ||
print(f"GPU Memory: {gpu_mem} MB") | ||
print(f"CPU Temperature: {cpu_temp:.1f}°C") | ||
print(f"GPU Temperature: {gpu_temp:.1f}°C") | ||
print(f"System Voltage: {system_voltage} V") | ||
print(f"System Current: {system_current} mA") | ||
print(f"System Power: {system_power} W") | ||
|
||
line = { | ||
'cpu_usage': cpu_usage, | ||
'memory_usage': memory_usage, | ||
'gpu_usage': gpu_usage, | ||
'cpu_temp': cpu_temp, | ||
'gpu_temp': gpu_temp, | ||
'gpu_mem': gpu_mem, | ||
'system_voltage': system_voltage, | ||
'system_current': system_current, | ||
'system_power': system_power | ||
} | ||
writer.writerow(line) | ||
|
||
count += 1 | ||
time.sleep(1) | ||
|
||
print("Logging finished") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# https://github.com/tensorflow/tensorrt/blob/master/tftrt/examples-py/PTQ_example.ipynb | ||
model = tf.keras.models.load_model('resnet50_saved_model') | ||
|
||
batch_size = 8 | ||
batched_input = np.zeros((batch_size, 224, 224, 3), dtype=np.float32) | ||
|
||
for i in range(batch_size): | ||
img_path = './data/img%d.JPG' % (i % 4) | ||
img = image.load_img(img_path, target_size=(224, 224)) | ||
x = image.img_to_array(img) | ||
x = np.expand_dims(x, axis=0) | ||
x = preprocess_input(x) | ||
batched_input[i, :] = x | ||
batched_input = tf.constant(batched_input) | ||
print('batched_input shape: ', batched_input.shape) | ||
|
||
# Benchmarking throughput | ||
N_warmup_run = 50 | ||
N_run = 1000 | ||
elapsed_time = [] | ||
|
||
for i in range(N_warmup_run): | ||
preds = model.predict(batched_input) | ||
|
||
for i in range(N_run): | ||
start_time = time.time() | ||
preds = model.predict(batched_input) | ||
end_time = time.time() | ||
elapsed_time = np.append(elapsed_time, end_time - start_time) | ||
if i % 50 == 0: | ||
print('Step {}: {:4.1f}ms'.format(i, (elapsed_time[-50:].mean()) * 1000)) | ||
|
||
print('Throughput: {:.0f} images/s'.format(N_run * batch_size / elapsed_time.sum())) | ||
|
||
def predict_tftrt(input_saved_model): | ||
"""Runs prediction on a single image and shows the result. | ||
input_saved_model (string): Name of the input model stored in the current dir | ||
""" | ||
img_path = './data/img0.JPG' # Siberian_husky | ||
img = image.load_img(img_path, target_size=(224, 224)) | ||
x = image.img_to_array(img) | ||
x = np.expand_dims(x, axis=0) | ||
x = preprocess_input(x) | ||
x = tf.constant(x) | ||
|
||
saved_model_loaded = tf.saved_model.load(input_saved_model, tags=[tag_constants.SERVING]) | ||
signature_keys = list(saved_model_loaded.signatures.keys()) | ||
print(signature_keys) | ||
|
||
infer = saved_model_loaded.signatures['serving_default'] | ||
print(infer.structured_outputs) | ||
|
||
labeling = infer(x) | ||
preds = labeling['predictions'].numpy() | ||
print('{} - Predicted: {}'.format(img_path, decode_predictions(preds, top=3)[0])) | ||
plt.subplot(2,2,1) | ||
plt.imshow(img); | ||
plt.axis('off'); | ||
plt.title(decode_predictions(preds, top=3)[0][0][1]) | ||
|
||
def benchmark_tftrt(input_saved_model): | ||
saved_model_loaded = tf.saved_model.load(input_saved_model, tags=[tag_constants.SERVING]) | ||
infer = saved_model_loaded.signatures['serving_default'] | ||
|
||
N_warmup_run = 50 | ||
N_run = 1000 | ||
elapsed_time = [] | ||
|
||
for i in range(N_warmup_run): | ||
labeling = infer(batched_input) | ||
|
||
for i in range(N_run): | ||
start_time = time.time() | ||
labeling = infer(batched_input) | ||
end_time = time.time() | ||
elapsed_time = np.append(elapsed_time, end_time - start_time) | ||
if i % 50 == 0: | ||
print('Step {}: {:4.1f}ms'.format(i, (elapsed_time[-50:].mean()) * 1000)) | ||
|
||
print('Throughput: {:.0f} images/s'.format(N_run * batch_size / elapsed_time.sum())) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import time | ||
import sys | ||
import cv2 | ||
import os | ||
from pathlib import Path | ||
import pyzed.sl as sl | ||
|
||
import rclpy | ||
from rclpy.node import Node | ||
from sensor_msgs.msg import Image | ||
from std_msgs.msg import Header, String | ||
from cv_bridge import CvBridge, CvBridgeError | ||
from example_interfaces.srv import Trigger | ||
|
||
class CameraNode(Node): | ||
def __init__(self): | ||
super().__init__('image_publisher') #type: ignore | ||
self.bridge = CvBridge() | ||
self.download_path = str(Path.home() / 'Downloads') | ||
# replace self.camera with parameter | ||
self.index, self.camera, self.frames, self.type, self.total_data, self.done = 0, True, [], "8UC4", 0, False | ||
# replace with action server-client architecture later after verifying frame consistency | ||
self.off_publisher = self.create_publisher(String, 'off', 10) | ||
self.model_publisher = self.create_publisher(Image, 'image_data', 10) | ||
self.srv = self.create_service(Trigger, 'image_data_service', self.image_service) | ||
|
||
if self.camera == True: | ||
self.camera_publisher() | ||
else: | ||
self.type = "JPG" | ||
self.picture_publisher() | ||
|
||
def picture_publisher(self): | ||
for filename in os.listdir(self.download_path): | ||
img = cv2.imread(os.path.join(self.download_path, filename)) | ||
if img is not None: | ||
self.index += 1 | ||
self.frames.append(img) | ||
self.publish_image(img) | ||
|
||
def camera_publisher(self): | ||
init = sl.InitParameters() | ||
init.camera_resolution = sl.RESOLUTION.AUTO | ||
init.camera_fps = 30 | ||
cam = sl.Camera() | ||
runtime = sl.RuntimeParameters() | ||
mat = sl.Mat() | ||
|
||
if not cam.is_opened(): | ||
print("Opening ZED Camera ") | ||
status = cam.open(init) | ||
if status != sl.ERROR_CODE.SUCCESS: | ||
print(repr(status)) | ||
exit() | ||
|
||
key = '' | ||
while key != 113: # for 'q' key | ||
err = cam.grab(runtime) | ||
if err == sl.ERROR_CODE.SUCCESS: | ||
self.tic = time.perf_counter() | ||
cam.retrieve_image(mat, sl.VIEW.LEFT_UNRECTIFIED) | ||
self.index += 1 | ||
self.frames.append(mat.get_data()) | ||
cv2.imshow(f"ZED Camera Frame {self.index}", mat.get_data()) | ||
self.publish_image(mat.get_data()) | ||
key = cv2.waitKey(5) | ||
else: | ||
key = cv2.waitKey(5) | ||
self.done = True | ||
cv2.destroyAllWindows() | ||
cam.close() | ||
print("ZED Camera closed ") | ||
|
||
def image_service(self, request, response): | ||
req_frame = request.index | ||
image = self.frames[req_frame-1] | ||
try: | ||
image_msg = self.bridge.cv2_to_imgmsg(image, encoding=self.type) | ||
except CvBridgeError as e: | ||
self.get_logger().info(e) | ||
print(e) | ||
return | ||
|
||
response.success = True | ||
response.message = image_msg | ||
return response | ||
|
||
def publish_image(self, image): | ||
if self.done != True: | ||
header = Header() | ||
header.stamp = self.get_clock().now().to_msg() | ||
header.frame_id = str(self.index) | ||
|
||
try: | ||
image_msg = self.bridge.cv2_to_imgmsg(image, encoding=self.type) | ||
except CvBridgeError as e: | ||
self.get_logger().info(e) | ||
print(e) | ||
|
||
image_msg.header = header | ||
image_msg.is_bigendian = 0 | ||
image_msg.step = image_msg.width * 3 | ||
|
||
self.model_publisher.publish(image_msg) | ||
size = sys.getsizeof(image_msg) | ||
self.get_logger().info(f'Published image frame: {self.index} with message size {size} bytes') | ||
self.total_data += size | ||
else: | ||
self.off_publisher.publish("Done") | ||
self.display_metrics() | ||
|
||
def display_metrics(self): | ||
toc = time.perf_counter() | ||
bandwidth = self.total_data / (toc - self.tic) | ||
self.get_logger().info(f'Published {len(self.frames)} images in {toc - self.tic:0.4f} seconds with average network bandwidth of {round(bandwidth)} bytes per second') | ||
self.get_logger().info('Shutting down display node...') | ||
raise SystemExit | ||
|
||
def main(args=None): | ||
rclpy.init(args=args) | ||
image_publisher = CameraNode() | ||
try: | ||
rclpy.spin(image_publisher) | ||
except SystemExit: | ||
rclpy.logging.get_logger("Quitting").info('Done') | ||
image_publisher.destroy_node() | ||
rclpy.shutdown() | ||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.