{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "pip install numpy pandas matplotlib opencv-contrib-python mlflow==2.14.1 setuptools supervision ultralytics transformers timm\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n" ] } ], "source": [ "import cv2\n", "import matplotlib.pyplot as plt\n", "import supervision as sv\n", "import torch\n", "from PIL import Image\n", "from transformers import DetrImageProcessor, DetrForObjectDetection\n", "import timm" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Some weights of the model checkpoint at facebook/detr-resnet-50 were not used when initializing DetrForObjectDetection: ['model.backbone.conv_encoder.model.layer1.0.downsample.1.num_batches_tracked', 'model.backbone.conv_encoder.model.layer2.0.downsample.1.num_batches_tracked', 'model.backbone.conv_encoder.model.layer3.0.downsample.1.num_batches_tracked', 'model.backbone.conv_encoder.model.layer4.0.downsample.1.num_batches_tracked']\n", "- This IS expected if you are initializing DetrForObjectDetection from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n", "- This IS NOT expected if you are initializing DetrForObjectDetection from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n" ] } ], "source": [ "processor = DetrImageProcessor.from_pretrained(\"facebook/detr-resnet-50\")\n", "model = DetrForObjectDetection.from_pretrained(\"facebook/detr-resnet-50\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "image = Image.open(\"champi.png\")\n", "inputs = processor(images=image, return_tensors=\"pt\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "with torch.no_grad():\n", " outputs = model(**inputs)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "640" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "480" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "width, height = image.size\n", "display(width)\n", "display(height)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[480, 640]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "target_size = torch.tensor([[height, width]])\n", "target_size" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'scores': tensor([0.7854]),\n", " 'labels': tensor([53]),\n", " 'boxes': tensor([[ 63.1457, 74.5972, 281.8959, 392.3910]])}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results = processor.post_process_object_detection(\n", " outputs=outputs, target_sizes=target_size)[0]\n", "\n", "results" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Detections(xyxy=array([[ 63.145706, 74.59721 , 281.89594 , 392.39096 ]], dtype=float32), mask=None, confidence=array([0.7854072], dtype=float32), class_id=array([53]), tracker_id=None, data={'class_name': array(['apple'], dtype='" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bounding_box_annotator = sv.BoundingBoxAnnotator()\n", "label_annotator = sv.LabelAnnotator()\n", "\n", "annotated_image = bounding_box_annotator.annotate(\n", " scene=image, detections=detections)\n", "annotated_image = label_annotator.annotate(\n", " scene=annotated_image, detections=detections)\n", "\n", "plt.figure(figsize=(15, 10))\n", "plt.imshow(annotated_image)\n", "plt.show();" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.12.1" } }, "nbformat": 4, "nbformat_minor": 2 }