{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "1117ce33", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/vap/.local/lib/python3.10/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and will be removed in 0.15. The current behavior is equivalent to passing `weights=MobileNet_V2_QuantizedWeights.IMAGENET1K_QNNPACK_V1`. You can also use `weights=MobileNet_V2_QuantizedWeights.DEFAULT` to get the most up-to-date weights.\n", " warnings.warn(msg)\n", "/home/vap/.local/lib/python3.10/site-packages/torch/ao/quantization/utils.py:280: UserWarning: must run observer before calling calculate_qparams. Returning default values.\n", " warnings.warn(\n", "[W TensorImpl.h:1408] Warning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (function operator())\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "[ INFO:0@0.006] global /builddir/build/BUILD/opencv-4.5.5/modules/videoio/src/videoio_registry.cpp (223) VideoBackendRegistry VIDEOIO: Enabled backends(8, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); V4L2(970); CV_IMAGES(960); CV_MJPEG(950); FIREWIRE(940); UEYE(930)\n", "0.720033647202218 fps\n", "0.2923693030020889 fps\n", "9.910788105742382 fps\n", "10.228304282744872 fps\n", "9.590406812765337 fps\n", "10.909935825359092 fps\n", "11.182936903461064 fps\n", "9.787883879398862 fps\n", "6.704056690133046 fps\n", "7.7539939926616 fps\n", "9.982124391483204 fps\n", "9.598538308527846 fps\n", "6.456814086349396 fps\n" ] } ], "source": [ "\n", "# https://pytorch.org/tutorials/intermediate/realtime_rpi.html\n", "\n", "import time\n", "\n", "import torch\n", "import numpy as np\n", "from torchvision import models, transforms\n", "\n", "import cv2\n", "from PIL import Image\n", "\n", "# Raspberry pi4: The aarch64 version of pytorch requires using the qnnpack engine.\n", "torch.backends.quantized.engine = 'qnnpack'\n", "\n", "cap = cv2.VideoCapture(0, cv2.CAP_V4L2)\n", "cap.set(cv2.CAP_PROP_FRAME_WIDTH, 224)\n", "cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 224)\n", "cap.set(cv2.CAP_PROP_FPS, 36)\n", "\n", "preprocess = transforms.Compose([\n", " transforms.ToTensor(),\n", " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n", "])\n", "\n", "# Vesa:\n", "#net = models.quantization.mobilenet_v2(pretrained=True, quantize=True)\n", "net = models.quantization.mobilenet_v2(weights=True, quantize=True)\n", "\n", "# jit model to take it from ~20fps to ~30fps\n", "net = torch.jit.script(net)\n", "\n", "started = time.time()\n", "last_logged = time.time()\n", "frame_count = 0\n", "\n", "with torch.no_grad():\n", " while True:\n", " # read frame\n", " ret, image = cap.read()\n", " if not ret:\n", " raise RuntimeError(\"failed to read frame\")\n", "\n", " # convert opencv output from BGR to RGB\n", " image = image[:, :, [2, 1, 0]]\n", " permuted = image\n", "\n", " # preprocess\n", " input_tensor = preprocess(image)\n", "\n", " # create a mini-batch as expected by the model\n", " input_batch = input_tensor.unsqueeze(0)\n", "\n", " # run model\n", " output = net(input_batch)\n", " # do something with output ...\n", "\n", " # log model performance\n", " frame_count += 1\n", " now = time.time()\n", " if now - last_logged > 1:\n", " print(f\"{frame_count / (now-last_logged)} fps\")\n", " last_logged = now\n", " frame_count = 0\n", "torch.backends.quantized.engine = 'qnnpack'\n", "\n", "cap = cv2.VideoCapture(0, cv2.CAP_V4L2)\n", "cap.set(cv2.CAP_PROP_FRAME_WIDTH, 224)\n", "cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 224)\n", "cap.set(cv2.CAP_PROP_FPS, 36)\n", "\n", "preprocess = transforms.Compose([\n", " transforms.ToTensor(),\n", " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n", "])\n", "\n", "# Vesa:\n", "#net = models.quantization.mobilenet_v2(pretrained=True, quantize=True)\n", "net = models.quantization.mobilenet_v2(weights=True, quantize=True)\n", "\n", "# jit model to take it from ~20fps to ~30fps\n", "net = torch.jit.script(net)\n", "\n", "started = time.time()\n", "last_logged = time.time()\n", "frame_count = 0\n", "\n", "with torch.no_grad():\n", " while True:\n", " # read frame\n", " ret, image = cap.read()\n", " if not ret:\n", " raise RuntimeError(\"failed to read frame\")\n", "\n", " # convert opencv output from BGR to RGB\n", " image = image[:, :, [2, 1, 0]]\n", " permuted = image\n", "\n", " # preprocess\n", " input_tensor = preprocess(image)\n", "\n", " # create a mini-batch as expected by the model\n", " input_batch = input_tensor.unsqueeze(0)\n", "\n", " # run model\n", " output = net(input_batch)\n", " # do something with output ...\n", "\n", " # log model performance\n", " frame_count += 1\n", " now = time.time()\n", " if now - last_logged > 1:\n", " print(f\"{frame_count / (now-last_logged)} fps\")\n", " last_logged = now\n", " frame_count = 0" ] }, { "cell_type": "code", "execution_count": null, "id": "b270fe44", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "91b2b5dd", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.7" } }, "nbformat": 4, "nbformat_minor": 5 }