{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import torch\n", "from hydra import compose, initialize\n", "from PIL import Image \n", "\n", "# Ensure that the necessary repository is cloned and installed. You may need to run: \n", "# git clone git@github.com:WongKinYiu/YOLO.git\n", "# cd YOLO \n", "# pip install .\n", "from yolo import AugmentationComposer, bbox_nms, Config, create_model, custom_logger, draw_bboxes, Vec2Box" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "CONFIG_PATH = \"../yolo/config\"\n", "CONFIG_NAME = \"config\"\n", "MODEL = \"v9-c\"\n", "\n", "DEVICE = 'cuda:0'\n", "CLASS_NUM = 80\n", "IMAGE_PATH = '../demo/images/inference/image.png'\n", "\n", "custom_logger()\n", "device = torch.device(DEVICE)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with initialize(config_path=CONFIG_PATH, version_base=None, job_name=\"notebook_job\"):\n", " cfg: Config = compose(config_name=CONFIG_NAME, overrides=[\"task=inference\", f\"task.data.source={IMAGE_PATH}\", f\"model={MODEL}\"])\n", " model = create_model(cfg.model, class_num=CLASS_NUM).to(device)\n", " transform = AugmentationComposer([], cfg.image_size)\n", " vec2box = Vec2Box(model, cfg.image_size, device)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pil_image = Image.open(IMAGE_PATH)\n", "image, bbox, rev_tensor = transform(pil_image)\n", "image = image.to(device)[None]\n", "rev_tensor = rev_tensor.to(device)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with torch.no_grad():\n", " predict = model(image)\n", " pred_class, _, pred_bbox = vec2box(predict[\"Main\"])\n", "\n", "pred_bbox = (pred_bbox / rev_tensor[0] - rev_tensor[None, None, 1:]) \n", "pred_bbox = bbox_nms(pred_class, pred_bbox, cfg.task.nms)\n", "draw_bboxes(pil_image, pred_bbox, idx2label=cfg.class_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sample Output:\n", "\n", "![image](../demo/images/output/visualize.png)" ] } ], "metadata": { "kernelspec": { "display_name": "yolomit", "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.14" } }, "nbformat": 4, "nbformat_minor": 2 }