Skip to content

Commit

Permalink
model exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishaan-Datta committed Sep 28, 2024
1 parent 7530586 commit 41507fa
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 22 deletions.
39 changes: 39 additions & 0 deletions conversion_tools/model_exporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
# cv2.rectangle(img, c1, c2, color, thickness=2, lineType=cv2.LINE_AA)
# [batch size, channel number, height, width]
# cv2.dnn.NMSBoxes
# input shape (1,3,640,640) BCHW
# output shape (1,5,8400)

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.pt") # load an official model
model = YOLO("path/to/best.pt") # load a custom trained model

# Export the model
model.export(format="onnx")

from ultralytics import YOLO

# Load the YOLOv8 model
model = YOLO("yolov8n.pt")

# Export the model to TensorRT format
model.export(format="engine") # creates 'yolov8n.engine'

# Load the exported TensorRT model
tensorrt_model = YOLO("yolov8n.engine")

# Run inference
results = tensorrt_model("https://ultralytics.com/images/bus.jpg")

# logging style:
# PyTorch: starting from '/home/user/ROS/models/maize/Maize.pt' with input shape (1, 3, 640, 640) BCHW and output shape(s) (1, 5, 8400) (5.9 MB)
# ONNX: starting export with onnx 1.16.2 opset 12...
# ONNX: export success ✅ 1.7s, saved as '/home/user/ROS/models/maize/Maize.onnx' (11.7 MB)
# Export complete (3.8s)
# Results saved to /home/user/ROS/models/maize
# Predict: yolo predict task=detect model=/home/user/ROS/models/maize/Maize.onnx imgsz=640
# Validate: yolo val task=detect model=/home/user/ROS/models/maize/Maize.onnx imgsz=640 data=config.yaml

108 changes: 108 additions & 0 deletions conversion_tools/model_inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# stream off:
from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.pt") # pretrained YOLOv8n model

# Run batched inference on a list of images
results = model(["image1.jpg", "image2.jpg"]) # return a list of Results objects

# Process results list
for result in results:
boxes = result.boxes # Boxes object for bounding box outputs
masks = result.masks # Masks object for segmentation masks outputs
keypoints = result.keypoints # Keypoints object for pose outputs
probs = result.probs # Probs object for classification outputs
obb = result.obb # Oriented boxes object for OBB outputs
result.show() # display to screen
result.save(filename="result.jpg") # save to disk

# stream on:
from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.pt") # pretrained YOLOv8n model

# Run batched inference on a list of images
results = model(["image1.jpg", "image2.jpg"], stream=True) # return a generator of Results objects

# Process results generator
for result in results:
boxes = result.boxes # Boxes object for bounding box outputs
masks = result.masks # Masks object for segmentation masks outputs
keypoints = result.keypoints # Keypoints object for pose outputs
probs = result.probs # Probs object for classification outputs
obb = result.obb # Oriented boxes object for OBB outputs
result.show() # display to screen
result.save(filename="result.jpg") # save to disk


from ultralytics import YOLO

# Load a pretrained YOLOv8n model
model = YOLO("yolov8n.pt")

# Run inference on 'bus.jpg' with arguments
model.predict("bus.jpg", save=True, imgsz=320, conf=0.5)

from ultralytics import YOLO

# Load a pretrained YOLOv8n model
model = YOLO("yolov8n.pt")

# Run inference on an image
results = model("bus.jpg") # results list

# View results
for r in results:
print(r.boxes) # print the Boxes object containing the detection bounding boxes

from PIL import Image

from ultralytics import YOLO

# Load a pretrained YOLOv8n model
model = YOLO("yolov8n.pt")

# Run inference on 'bus.jpg'
results = model(["bus.jpg", "zidane.jpg"]) # results list

# Visualize the results
for i, r in enumerate(results):
# Plot results image
im_bgr = r.plot() # BGR-order numpy array
im_rgb = Image.fromarray(im_bgr[..., ::-1]) # RGB-order PIL image

# Show results to screen (in supported environments)
r.show()

# Save results to disk
r.save(filename=f"results{i}.jpg")

import cv2
from ultralytics import YOLO
import time

model = YOLO("/home/user/ROS/models/maize/Maize.engine")
image = cv2.imread("/home/user/ROS/assets/maize/IMG_1822_14.JPG")

sum = 0
# stream = True?
for _ in range(100):
tic = time.perf_counter_ns()
result = model.predict(
image, # batch=8 of the same image
verbose=False,
device="cuda",
)
elapsed_time = (time.perf_counter_ns() - tic) / 1e6
print(f"Elapsed time: {(elapsed_time):.2f} ms")
sum += elapsed_time
annotated_frame = result[0].plot()
cv2.imshow("YOLOv8 Inference", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break

avearage_time = (sum - 2660) / 100
print(f"Average time: {avearage_time:.2f} ms")
cv2.destroyAllWindows()
File renamed without changes.
22 changes: 0 additions & 22 deletions conversion_tools/ultralytics_test_2.py

This file was deleted.

0 comments on commit 41507fa

Please sign in to comment.