{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Gradio.ipynb","provenance":[],"collapsed_sections":[],"authorship_tag":"ABX9TyMP5m/oK5+rqNLpzprjWgfY"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"68CgGISA0OQu","executionInfo":{"status":"ok","timestamp":1657789122929,"user_tz":-180,"elapsed":20255,"user":{"displayName":"orhan ekinci","userId":"08596083551140132378"}},"outputId":"de975cdf-9dd8-4454-f587-bf831c035834"},"outputs":[{"output_type":"stream","name":"stdout","text":["Mounted at /content/drive\n"]}],"source":["from google.colab import drive\n","drive.mount('/content/drive')"]},{"cell_type":"code","source":["!ls \"/content/drive/My Drive/Colab Notebooks/Model Representation/\"\n","%cd drive/My Drive/Colab Notebooks/Model Representation/"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"NN8c3dXt0hfN","executionInfo":{"status":"ok","timestamp":1657789124086,"user_tz":-180,"elapsed":1161,"user":{"displayName":"orhan ekinci","userId":"08596083551140132378"}},"outputId":"0626aab3-f0ea-439e-f487-2c704b22a575"},"execution_count":2,"outputs":[{"output_type":"stream","name":"stdout","text":["datasets flagged Gradio.ipynb saved_model yolov5\n","/content/drive/My Drive/Colab Notebooks/Model Representation\n"]}]},{"cell_type":"code","source":["import os\n","cwd = os.getcwd()\n","cwd"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":35},"id":"vN36F7c50mF4","executionInfo":{"status":"ok","timestamp":1657789124087,"user_tz":-180,"elapsed":11,"user":{"displayName":"orhan ekinci","userId":"08596083551140132378"}},"outputId":"85d491ab-1f37-40bd-f344-0ca02e38065c"},"execution_count":3,"outputs":[{"output_type":"execute_result","data":{"text/plain":["'/content/drive/My Drive/Colab Notebooks/Model Representation'"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"}},"metadata":{},"execution_count":3}]},{"cell_type":"code","source":["!pip install -qr https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt gradio # install dependencies"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"8xT1CQBM1Afx","executionInfo":{"status":"ok","timestamp":1657789272147,"user_tz":-180,"elapsed":148069,"user":{"displayName":"orhan ekinci","userId":"08596083551140132378"}},"outputId":"f45c871a-7627-45c3-c529-aca081b2253d"},"execution_count":4,"outputs":[{"output_type":"stream","name":"stdout","text":["\u001b[K |████████████████████████████████| 5.1 MB 4.3 MB/s \n","\u001b[K |████████████████████████████████| 596 kB 45.0 MB/s \n","\u001b[K |████████████████████████████████| 750.6 MB 11 kB/s \n","\u001b[K |████████████████████████████████| 21.0 MB 1.3 MB/s \n","\u001b[K |████████████████████████████████| 57 kB 4.5 MB/s \n","\u001b[K |████████████████████████████████| 54 kB 3.0 MB/s \n","\u001b[K |████████████████████████████████| 212 kB 59.8 MB/s \n","\u001b[K |████████████████████████████████| 2.3 MB 44.1 MB/s \n","\u001b[K |████████████████████████████████| 1.1 MB 50.8 MB/s \n","\u001b[K |████████████████████████████████| 84 kB 2.7 MB/s \n","\u001b[K |████████████████████████████████| 140 kB 58.8 MB/s \n","\u001b[K |████████████████████████████████| 272 kB 43.8 MB/s \n","\u001b[K |████████████████████████████████| 84 kB 2.9 MB/s \n","\u001b[K |████████████████████████████████| 94 kB 2.9 MB/s \n","\u001b[K |████████████████████████████████| 144 kB 47.4 MB/s \n","\u001b[K |████████████████████████████████| 271 kB 41.4 MB/s \n","\u001b[K |████████████████████████████████| 63 kB 1.8 MB/s \n","\u001b[K |████████████████████████████████| 80 kB 8.1 MB/s \n","\u001b[K |████████████████████████████████| 68 kB 6.1 MB/s \n","\u001b[K |████████████████████████████████| 54 kB 2.9 MB/s \n","\u001b[K |████████████████████████████████| 43 kB 2.1 MB/s \n","\u001b[K |████████████████████████████████| 62 kB 804 kB/s \n","\u001b[K |████████████████████████████████| 856 kB 59.3 MB/s \n","\u001b[K |████████████████████████████████| 4.1 MB 42.2 MB/s \n","\u001b[?25h Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n"," Building wheel for python-multipart (setup.py) ... \u001b[?25l\u001b[?25hdone\n","\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n","torchtext 0.13.0 requires torch==1.12.0, but you have torch 1.11.0 which is incompatible.\n","torchaudio 0.12.0+cu113 requires torch==1.12.0, but you have torch 1.11.0 which is incompatible.\u001b[0m\n"]}]},{"cell_type":"code","source":["import gradio as gr\n","import torch\n","from PIL import Image\n","\n","# Images\n","#torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg', 'zidane.jpg')\n","#torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/bus.jpg', 'bus.jpg')\n","\n","# Model\n","#model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # force_reload=True to update\n","\n","model = torch.hub.load(cwd+'/yolov5', 'custom', path=cwd+'/saved_model/yolov5s.pt', source='local') # local model\n","\n","\n","\n","def yolo(im, size=640):\n"," g = (size / max(im.size)) # gain\n"," im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize\n","\n"," results = model(im) # inference\n"," results.render() # updates results.imgs with boxes and labels\n"," return Image.fromarray(results.imgs[0])\n","\n","\n","inputs = gr.inputs.Image(type='pil', label=\"Original Image\")\n","outputs = gr.outputs.Image(type=\"pil\", label=\"Output Image\")\n","\n","title = \"YOLOv5\"\n","description = \"YOLOv5 Gradio demo for object detection. Upload an image or click an example image to use.\"\n","article = \"
YOLOv5 is a family of compound-scaled object detection models trained on the COCO dataset, and includes \" \\\n"," \"simple functionality for Test Time Augmentation (TTA), model ensembling, hyperparameter evolution, \" \\\n"," \"and export to ONNX, CoreML and TFLite. Source code |\" \\\n"," \"iOS App | PyTorch Hub
\"\n","\n","examples = [[cwd+'/datasets/zidane.jpg'], [cwd+'/datasets/bus.jpg']]\n","gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(\n"," debug=True)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000},"id":"FwCb9Nmk1n6n","executionInfo":{"status":"ok","timestamp":1657788661371,"user_tz":-180,"elapsed":223749,"user":{"displayName":"orhan ekinci","userId":"08596083551140132378"}},"outputId":"219fa614-9dda-4870-fe54-dfce5b4e4b05"},"execution_count":10,"outputs":[{"output_type":"stream","name":"stderr","text":["\u001b[31m\u001b[1mrequirements:\u001b[0m PyYAML>=5.3.1 not found and is required by YOLOv5, attempting auto-update...\n","Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n","Requirement already satisfied: PyYAML>=5.3.1 in /usr/local/lib/python3.7/dist-packages (6.0)\n","\n","\u001b[31m\u001b[1mrequirements:\u001b[0m torch!=1.12.0,>=1.7.0 not found and is required by YOLOv5, attempting auto-update...\n","Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n","Requirement already satisfied: torch!=1.12.0,>=1.7.0 in /usr/local/lib/python3.7/dist-packages (1.11.0)\n","Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch!=1.12.0,>=1.7.0) (4.1.1)\n","\n","\u001b[31m\u001b[1mrequirements:\u001b[0m torchvision!=0.13.0,>=0.8.1 not found and is required by YOLOv5, attempting auto-update...\n","Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n","Requirement already satisfied: torchvision!=0.13.0,>=0.8.1 in /usr/local/lib/python3.7/dist-packages (0.12.0)\n","Requirement already satisfied: torch==1.11.0 in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (1.11.0)\n","Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (7.1.2)\n","Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (2.23.0)\n","Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (1.21.6)\n","Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torchvision!=0.13.0,>=0.8.1) (4.1.1)\n","Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (3.0.4)\n","Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (1.24.3)\n","Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (2022.6.15)\n","Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision!=0.13.0,>=0.8.1) (2.10)\n","\n","\u001b[31m\u001b[1mrequirements:\u001b[0m 3 packages updated per /root/.cache/torch/hub/ultralytics_yolov5_master/requirements.txt\n","\u001b[31m\u001b[1mrequirements:\u001b[0m ⚠️ \u001b[1mRestart runtime or rerun command for updates to take effect\u001b[0m\n","\n","YOLOv5 🚀 2022-7-14 Python-3.7.13 torch-1.11.0+cu102 CPU\n","\n","Fusing layers... \n","YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients\n","Adding AutoShape... \n","/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n"," warnings.warn(value)\n"]},{"output_type":"stream","name":"stdout","text":["Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n"]},{"output_type":"stream","name":"stderr","text":["Connected (version 2.0, client OpenSSH_7.6p1)\n","Authentication (publickey) successful!\n"]},{"output_type":"stream","name":"stdout","text":["Running on public URL: https://21879.gradio.app\n","\n","This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"]},{"output_type":"display_data","data":{"text/plain":["YOLOv5 is a family of compound-scaled object detection models trained on the COCO dataset, and includes \" \\\n"," \"simple functionality for Test Time Augmentation (TTA), model ensembling, hyperparameter evolution, \" \\\n"," \"and export to ONNX, CoreML and TFLite. Source code |\" \\\n"," \"iOS App | PyTorch Hub
\"\n","\n","path_folder = cwd+'/datasets/s1000/'\n","examples = [[path_folder+'s1000 (1).png'], [path_folder+'s1000 (2).png'],[path_folder+'s1000 (3).png'],[path_folder+'s1000 (4).png'],[path_folder+'s1000 (5).png']]\n","gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch(\n"," debug=True)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000},"id":"8kaOPNyz2cEr","executionInfo":{"status":"ok","timestamp":1657789820492,"user_tz":-180,"elapsed":541032,"user":{"displayName":"orhan ekinci","userId":"08596083551140132378"}},"outputId":"dd0ad238-a602-4bd0-e610-2f523a37d378"},"execution_count":5,"outputs":[{"output_type":"stream","name":"stderr","text":["\u001b[31m\u001b[1mrequirements:\u001b[0m PyYAML>=5.3.1 not found and is required by YOLOv5, attempting auto-update...\n","Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n","Requirement already satisfied: PyYAML>=5.3.1 in /usr/local/lib/python3.7/dist-packages (6.0)\n","\n","\u001b[31m\u001b[1mrequirements:\u001b[0m torch>=1.7.0 not found and is required by YOLOv5, attempting auto-update...\n","Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n","Requirement already satisfied: torch>=1.7.0 in /usr/local/lib/python3.7/dist-packages (1.11.0)\n","Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch>=1.7.0) (4.1.1)\n","\n","\u001b[31m\u001b[1mrequirements:\u001b[0m torchvision>=0.8.1 not found and is required by YOLOv5, attempting auto-update...\n","Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n","Requirement already satisfied: torchvision>=0.8.1 in /usr/local/lib/python3.7/dist-packages (0.12.0)\n","Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from torchvision>=0.8.1) (1.21.6)\n","Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/local/lib/python3.7/dist-packages (from torchvision>=0.8.1) (7.1.2)\n","Requirement already satisfied: torch==1.11.0 in /usr/local/lib/python3.7/dist-packages (from torchvision>=0.8.1) (1.11.0)\n","Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from torchvision>=0.8.1) (2.23.0)\n","Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torchvision>=0.8.1) (4.1.1)\n","Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision>=0.8.1) (2.10)\n","Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision>=0.8.1) (1.24.3)\n","Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision>=0.8.1) (3.0.4)\n","Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->torchvision>=0.8.1) (2022.6.15)\n","\n","\u001b[31m\u001b[1mrequirements:\u001b[0m 3 packages updated per /content/drive/My Drive/Colab Notebooks/Model Representation/yolov5/requirements.txt\n","\u001b[31m\u001b[1mrequirements:\u001b[0m ⚠️ \u001b[1mRestart runtime or rerun command for updates to take effect\u001b[0m\n","\n","YOLOv5 🚀 2022-6-24 Python-3.7.13 torch-1.11.0+cu102 CPU\n","\n","Fusing layers... \n","Model summary: 213 layers, 7012822 parameters, 0 gradients\n","Adding AutoShape... \n","/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n"," warnings.warn(value)\n"]},{"output_type":"stream","name":"stdout","text":["Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n"]},{"output_type":"stream","name":"stderr","text":["Connected (version 2.0, client OpenSSH_7.6p1)\n","Authentication (publickey) successful!\n"]},{"output_type":"stream","name":"stdout","text":["Running on public URL: https://32048.gradio.app\n","\n","This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"]},{"output_type":"display_data","data":{"text/plain":["