diff --git a/conversion_tools/model_exporter.py b/conversion_tools/model_exporter.py new file mode 100644 index 0000000..5409a1f --- /dev/null +++ b/conversion_tools/model_exporter.py @@ -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 + diff --git a/conversion_tools/model_inference.py b/conversion_tools/model_inference.py new file mode 100644 index 0000000..584d132 --- /dev/null +++ b/conversion_tools/model_inference.py @@ -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() \ No newline at end of file diff --git a/conversion_tools/ultralytics_test.py b/conversion_tools/model_post.py similarity index 100% rename from conversion_tools/ultralytics_test.py rename to conversion_tools/model_post.py diff --git a/conversion_tools/ultralytics_test_2.py b/conversion_tools/ultralytics_test_2.py deleted file mode 100644 index 364ecd9..0000000 --- a/conversion_tools/ultralytics_test_2.py +++ /dev/null @@ -1,22 +0,0 @@ -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") - -# 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", - ) - print(f"Elapsed time: {(time.perf_counter_ns() - tic) / 1e6:.2f} ms") - annotated_frame = result[0].plot() - cv2.imshow("YOLOv8 Inference", annotated_frame) - if cv2.waitKey(1) & 0xFF == ord("q"): - break - -cv2.destroyAllWindows() \ No newline at end of file