erik @jakaskerl
Alright, I tried to build up some code, using GAI.
import depthai as dai
import time
# 1. Measure inference time since the image enters the NN unit
def measure_inference_time(pipeline, nn_node_name):
with dai.Device(pipeline) as device:
nn_queue = device.getOutputQueue(name=nn_node_name, maxSize=4, blocking=False)
start_time = time.time()
count=0
results = []
while True and len(results) < 60:
nn_data = nn_queue.tryGet()
if nn_data:
end_time = time.time()
inference_time = end_time - start_time
print(f"Inference time: {inference_time:.7f} seconds")
results.append(inference_time)
def print_available_ram():
with dai.Device() as device:
ram_size = device.getDdrMemoryUsage() # Approximation (actual RAM values are internal)
print(f"Available RAM: {ram_size} (estimated, based on device MX ID)")
# 3. RAM allocation for the model in RAM (estimated stats from pipeline)
def print_model_ram_allocations(pipeline):
with dai.Device(pipeline) as device:
pipeline_info = device.getDeviceInfo()
print(f"RAM allocations for the pipeline:\n{pipeline_info}")
# 4. Configure NN to run inference every 60 frames
def configure_nn_fps(pipeline, nn_node_name, fps_skip=60):
nn_node = pipeline.create(dai.node.NeuralNetwork)
nn_node.setFps(fps_skip) # Skips every 60 frames
nn_node.setName(nn_node_name)
# 5. Print metadata: shaves, threads, resolution mismatch, model info
def print_metadata(pipeline, nn_node_name):
with dai.Device(pipeline) as device:
nn_config = device.getPipelineConfig()
print(f"Shaves: {nn_config.shaves}")
print(f"Threads: {nn_config.threads}")
print(f"Resolution Mismatch: {nn_config.resolutionMismatch}")
print(f"Model Info: Channels={nn_config.channels}, Classes={nn_config.classes}")
# Main Function: Builds a minimal pipeline and runs tasks
def main():
# Minimal DepthAI pipeline setup
pipeline = dai.Pipeline()
# Add a Color Camera node
cam_rgb = pipeline.create(dai.node.ColorCamera)
cam_rgb.setPreviewSize(480, 480)
cam_rgb.setInterleaved(False)
cam_rgb.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
# Add Neural Network node
nn = pipeline.create(dai.node.NeuralNetwork)
nn.setBlobPath(rf"path/to/the/blob/file") # Replace with your NN model path
# nn.setNumThreads(2)
# nn.setNumShaves(4)
# Link Camera -> Neural Network
cam_rgb.preview.link(nn.input)
# Output NN Results
nn_out = pipeline.create(dai.node.XLinkOut)
nn_out.setStreamName("nn")
nn.out.link(nn_out.input)
# Call debugging helpers
measure_inference_time(pipeline, nn_node_name="nn")
print_available_ram()
print_model_ram_allocations(pipeline)
configure_nn_fps(pipeline, nn_node_name="nn", fps_skip=60)
print_metadata(pipeline, nn_node_name="nn")
if __name__ == "__main__":
main()
Some of these things don't run properly (don't exist or so), but the point is that I need these attributes or properties better to monitor the performance of the AI model and pipeline.