Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mid Eval PR #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
337 changes: 337 additions & 0 deletions Pigs_Detection.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,337 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Pigs_:).ipynb",
"private_outputs": true,
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyMJu0a2m6nGa7n9sUb8Y6WN",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"gpuClass": "standard",
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/rohanjha04/IITISoC-22-Auto2-Aerial-Surveying-IIT-Indore/blob/main/Pigs_Detection.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"metadata": {
"id": "qsxk5RIcAhyr"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!git clone https://github.com/ultralytics/yolov5 \n",
"%cd yolov5"
],
"metadata": {
"id": "5gNDSpldZHgq"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!pip install -qr requirements.txt"
],
"metadata": {
"id": "QnJr5c5pZsrg"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!pip install -q roboflow"
],
"metadata": {
"id": "-bTZHz2wCuEz"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from roboflow import Roboflow\n",
"rf= Roboflow(model_format=\"yolov5\", notebook= \"Pigs_:)\")"
],
"metadata": {
"id": "MI2HcRdqC8iB"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from roboflow import Roboflow\n",
"rf = Roboflow(api_key=\"KFImbK0bKcJbFEdhKs5B\")\n",
"project = rf.workspace(\"jhar\").project(\"pigs-p5ltl\")\n",
"dataset = project.version(2).download(\"yolov5\")"
],
"metadata": {
"id": "Z5rUOKjXDd9E"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%cat {dataset.location}/data.yaml"
],
"metadata": {
"id": "qFq-1bMfEiJp"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import yaml\n",
"with open(dataset.location + \"/data.yaml\", 'r') as stream:\n",
" num_classes = str(yaml.safe_load(stream)['nc'])"
],
"metadata": {
"id": "cJeuus0cEQmt"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%cd yolov5\n",
"import torch\n",
"from yolov5 import utils\n",
"import torch\n",
"import utils\n",
"from IPython import display\n",
"from IPython.display import clear_output\n",
"from pathlib import Path\n",
"import yaml\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.image as mpimg\n",
"import glob"
],
"metadata": {
"id": "zb7dTTKraOMk"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%cat /content/yolov5/models/yolov5s.yaml"
],
"metadata": {
"id": "8WWuMGREbjR8"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from IPython.core.magic import register_line_cell_magic\n",
"\n",
"@register_line_cell_magic\n",
"def writetemplate(line, cell):\n",
" with open(line, 'w') as f:\n",
" f.write(cell.format(**globals()))"
],
"metadata": {
"id": "Nepe7DTwhbN4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%%writetemplate /content/yolov5/models/custom_yolov5s.yaml\n",
"\n",
"# parameters\n",
"nc: 1 # number of classes\n",
"depth_multiple: 0.33 # model depth multiple\n",
"width_multiple: 0.50 # layer channel multiple\n",
"\n",
"# anchors\n",
"anchors:\n",
" - [10,13, 16,30, 33,23] # P3/8\n",
" - [30,61, 62,45, 59,119] # P4/16\n",
" - [116,90, 156,198, 373,326] # P5/32\n",
"\n",
"# YOLOv5 backbone\n",
"backbone:\n",
" # [from, number, module, args]\n",
" [[-1, 1, Focus, [64, 3]], # 0-P1/2\n",
" [-1, 1, Conv, [128, 3, 2]], # 1-P2/4\n",
" [-1, 3, BottleneckCSP, [128]],\n",
" [-1, 1, Conv, [256, 3, 2]], # 3-P3/8\n",
" [-1, 9, BottleneckCSP, [256]],\n",
" [-1, 1, Conv, [512, 3, 2]], # 5-P4/16\n",
" [-1, 9, BottleneckCSP, [512]],\n",
" [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32\n",
" [-1, 1, SPP, [1024, [5, 9, 13]]],\n",
" [-1, 3, BottleneckCSP, [1024, False]], # 9\n",
" ]\n",
"\n",
"# YOLOv5 head\n",
"head:\n",
" [[-1, 1, Conv, [512, 1, 1]],\n",
" [-1, 1, nn.Upsample, [None, 2, 'nearest']],\n",
" [[-1, 6], 1, Concat, [1]], # cat backbone P4\n",
" [-1, 3, BottleneckCSP, [512, False]], # 13\n",
"\n",
" [-1, 1, Conv, [256, 1, 1]],\n",
" [-1, 1, nn.Upsample, [None, 2, 'nearest']],\n",
" [[-1, 4], 1, Concat, [1]], # cat backbone P3\n",
" [-1, 3, BottleneckCSP, [256, False]], # 17 (P3/8-small)\n",
"\n",
" [-1, 1, Conv, [256, 3, 2]],\n",
" [[-1, 14], 1, Concat, [1]], # cat head P4\n",
" [-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium)\n",
"\n",
" [-1, 1, Conv, [512, 3, 2]],\n",
" [[-1, 10], 1, Concat, [1]], # cat head P5\n",
" [-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large)\n",
"\n",
" [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)\n",
" ]"
],
"metadata": {
"id": "j_WFhT6EheSx"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%%time\n",
"%cd /content/yolov5/\n",
"!python train.py --img 32 --batch 25 --epochs 10 --data {dataset.location}/data.yaml --cfg ./models/custom_yolov5s.yaml --weights '' --name yolov5_results --cache"
],
"metadata": {
"id": "4tpHP2nbbxz3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from IPython.display import Image"
],
"metadata": {
"id": "iGpygX7sd4ZQ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%ls runs"
],
"metadata": {
"id": "1cjWxYWoexRD"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%ls runs/train/yolov5_results/weights"
],
"metadata": {
"id": "fb6ZxhJKe0O6"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%cd /content/yolov5/\n",
"!python detect.py --img 416 --conf 0.4 --source /content/yolov5/Pigs-2/test/images --weights runs/train/yolov5_results/weights/best.pt\n"
],
"metadata": {
"id": "96v-EH3de7WM"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import glob\n",
"from IPython.display import Image, display\n",
"\n",
"for imageName in glob.glob('/content/yolov5/runs/detect/exp/*.jpg'): #assuming JPG\n",
" display(Image(filename=imageName))\n",
" print(\"\\n\")"
],
"metadata": {
"id": "-EO6zhmXgmQ3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"Image(filename='/content/yolov5/runs/train/yolov5_results/train_batch0.jpg', width=500)"
],
"metadata": {
"id": "jQjqAnvihPnv"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%cd /content/yolov5/\n",
"!python detect.py --img 32 --conf 0.4 --source https://www.youtube.com/watch?v=u1emPEApVlo --weights runs/train/yolov5_reults3/weights/best.pt"
],
"metadata": {
"id": "RIwPrM4LMMF4"
},
"execution_count": null,
"outputs": []
}
]
}
Loading