Commit 17ca41a0 authored by Senatilaka T.S.'s avatar Senatilaka T.S.

Upload New File

parent ee11011a
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "Ie5uLDH4uzAp"
},
"source": [
"# clone YOLOv5 repository\n",
"!git clone https://github.com/ultralytics/yolov5 # clone repo\n",
"%cd yolov5\n",
"!git reset --hard fbe67e465375231474a2ad80a4389efc77ecff99"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "wbvMlHd_QwMG"
},
"source": [
"# install dependencies as necessary\n",
"!pip install -qr requirements.txt # install dependencies (ignore errors)\n",
"import torch\n",
"\n",
"from IPython.display import Image, clear_output # to display images\n",
"from utils.downloads import attempt_download # to download models/datasets\n",
"\n",
"# clear_output()\n",
"print('Setup complete. Using torch %s %s' % (torch.__version__, torch.cuda.get_device_properties(0) if torch.cuda.is_available() else 'CPU'))"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Knxi2ncxWffW"
},
"source": [
"#follow the link below to get the download code from from Roboflow\n",
"!pip install -q roboflow\n",
"from roboflow import Roboflow\n",
"rf = Roboflow(model_format=\"yolov5\", notebook=\"roboflow-yolov5\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Ug_PhK1oqwQA"
},
"source": [
"%cd /content/yolov5\n",
"!pip install roboflow\n",
"\n",
"from roboflow import Roboflow\n",
"rf = Roboflow(api_key=\"UHsNPABfqFybAyjVrBXT\")\n",
"project = rf.workspace(\"agroengine\").project(\"diseases-detection\")\n",
"dataset = project.version(2).download(\"yolov5\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "ZZ3DmmGQztJj"
},
"source": [
"# this is the YAML file Roboflow wrote for that i am loading into this notebook with our data\n",
"%cat {dataset.location}/data.yaml"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# define number of classes based on YAML\n",
"import yaml\n",
"with open(dataset.location + \"/data.yaml\", 'r') as stream:\n",
" num_classes = str(yaml.safe_load(stream)['nc'])"
],
"metadata": {
"id": "YN1eri9udgKg"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#this is the model configuration i will use for this model.\n",
"%cat /content/yolov5/models/yolov5s.yaml"
],
"metadata": {
"id": "qFkK2SyZdtQX"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#customize iPython writefile so i can write variables\n",
"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": "J2Mwtj4VXQ5W"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%%writetemplate /content/yolov5/models/custom_yolov5s.yaml\n",
"\n",
"# parameters\n",
"nc: {num_classes} # 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": "JsjNhEHOXUIj"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "1NcFxRcFdJ_O"
},
"source": [
"# train yolov5s on custom data for 100 epochs\n",
"# time its performance\n",
"%%time\n",
"%cd /content/yolov5/\n",
"!python train.py --img 416 --batch 16 --epochs 100 --data {dataset.location}/data.yaml --cfg ./models/custom_yolov5s.yaml --weights '' --name yolov5s_results --cache"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "C60XAsyv6OPe"
},
"source": [
"from utils.plots import plot_results # plot results.txt as results.png\n",
"Image(filename='/content/yolov5/runs/train/yolov5s_results/results.png', width=1000) # view results.png"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "W40tI99_7BcH"
},
"source": [
"# print out an augmented training example\n",
"print(\"GROUND TRUTH AUGMENTED TRAINING DATA:\")\n",
"Image(filename='/content/yolov5/runs/train/yolov5s_results/train_batch0.jpg', width=900)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "yIEwt5YLeQ7P"
},
"source": [
"# trained weights are saved by default in our weights folder\n",
"%ls runs/"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "4SyOWS80qR32"
},
"source": [
"%ls runs/train/yolov5s_results/weights"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "9nmZZnWOgJ2S"
},
"source": [
"%cd /content/yolov5/\n",
"!python detect.py --weights runs/train/yolov5s_results/weights/best.pt --img 416 --conf 0.4 --source /content/yolov5/diseases-detection-2/test/images"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "odKEqYtTgbRc"
},
"source": [
"#display inference on ALL test images\n",
"#this looks much better with longer training above\n",
"\n",
"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\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "YH4CTzDRh00g"
},
"source": [
"from google.colab import drive\n",
"drive.mount('/content/gdrive')"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "1x_wg3VeiXMW"
},
"source": [
"%cp /content/yolov5/runs/train/yolov5s_results/weights/best.pt /content/gdrive/My\\ Drive"
],
"execution_count": null,
"outputs": []
}
]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment