Skip to content

Commit

Permalink
docs demos updated to work with v6 and v5
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniela Ruiz committed Oct 5, 2024
1 parent 27fb15d commit 176cc84
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 61 deletions.
3 changes: 1 addition & 2 deletions demo/video_detection_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import numpy as np\n",
"import supervision as sv\n",
"import torch\n",
Expand Down Expand Up @@ -140,7 +139,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "pytorch-wildlife",
"language": "python",
"name": "python3"
},
Expand Down
60 changes: 27 additions & 33 deletions docs/demo/image_detection_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,9 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from PIL import Image\n",
"import os\n",
"import torch\n",
"from torch.utils.data import DataLoader\n",
"from PytorchWildlife.models import detection as pw_detection\n",
"from PytorchWildlife.data import transforms as pw_trans\n",
"from PytorchWildlife.data import datasets as pw_data \n",
"from PytorchWildlife import utils as pw_utils"
]
},
Expand All @@ -51,8 +46,8 @@
"id": "3a23982f",
"metadata": {},
"source": [
"## Setting GPU\n",
"If you are using a GPU for this exercise, please specify which GPU to use for the computations. By default, GPU number 0 is used. Adjust this as per your setup. You don't need to run this cell if you are using a CPU."
"## Setting Device\n",
"If you are using a GPU for this exercise, please specify which GPU to use for the computations. By default, GPU number 0 is used."
]
},
{
Expand All @@ -62,7 +57,9 @@
"metadata": {},
"outputs": [],
"source": [
"torch.cuda.set_device(0) # Only use if you are running on GPU."
"DEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"if DEVICE == \"cuda\":\n",
" torch.cuda.set_device(0)"
]
},
{
Expand All @@ -71,7 +68,7 @@
"metadata": {},
"source": [
"## Model Initialization\n",
"We will initialize the MegaDetectorV5 model for image detection. This model is designed for detecting animals in images."
"We will initialize the MegaDetectorV6 model for image detection. This model is designed for detecting animals in images."
]
},
{
Expand All @@ -81,37 +78,34 @@
"metadata": {},
"outputs": [],
"source": [
"DEVICE = \"cuda\" # Use \"cuda\" if GPU is available \"cpu\" if no GPU is available\n",
"detection_model = pw_detection.MegaDetectorV5(device=DEVICE, pretrained=True, version=\"a\")"
"detection_model = pw_detection.MegaDetectorV6(device=DEVICE, pretrained=True, version=\"yolov9c\")"
]
},
{
"cell_type": "markdown",
"id": "1e57dcca",
"id": "00ed2c42",
"metadata": {},
"source": [
"## Single Image Detection\n",
"We will first perform detection on a single image. Make sure to verify that you have the image in the specified path."
"If you want to use MegaDetectorV5, run this line."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc41c371",
"id": "9046937d",
"metadata": {},
"outputs": [],
"source": [
"# Get the current working directory \n",
"current_working_dir = os.getcwd() \n",
" \n",
"# Construct the folder path relative to the parent directory \n",
"demo_path = os.path.join(\"/\".join(current_working_dir.split(\"/\")[:-2]), \"demo\") \n",
" \n",
"# Verify the path exists \n",
"if os.path.exists(demo_path): \n",
" print(\"Path exists:\", demo_path) \n",
"else: \n",
" print(\"Path does not exist:\", demo_path) "
"detection_model = pw_detection.MegaDetectorV5(device=DEVICE, pretrained=True, version=\"a\")"
]
},
{
"cell_type": "markdown",
"id": "1e57dcca",
"metadata": {},
"source": [
"## Single Image Detection\n",
"We will first perform detection on a single image. Make sure to verify that you have the image in the specified path."
]
},
{
Expand All @@ -121,7 +115,7 @@
"metadata": {},
"outputs": [],
"source": [
"tgt_img_path = os.path.join(demo_path, \"demo_data\", \"imgs\", \"10050028_0.JPG\")\n",
"tgt_img_path = os.path.join(\".\", \"demo_data\", \"imgs\", \"10050028_0.JPG\")\n",
"results = detection_model.single_image_detection(tgt_img_path)\n",
"pw_utils.save_detection_images(results, os.path.join(demo_path,\"demo_output\"), overwrite=False)\n",
"pw_utils.save_crop_images(results, os.path.join(\".\",\"crop_output\"), overwrite=False)"
Expand All @@ -143,7 +137,7 @@
"metadata": {},
"outputs": [],
"source": [
"tgt_folder_path = os.path.join(demo_path, \"demo_data\", \"imgs\")\n",
"tgt_folder_path = os.path.join(\".\", \"demo_data\", \"imgs\")\n",
"results = detection_model.batch_image_detection(tgt_folder_path, batch_size=16, extension=\"JPG\")"
]
},
Expand All @@ -166,7 +160,7 @@
"metadata": {},
"outputs": [],
"source": [
"pw_utils.save_detection_images(results, os.path.join(demo_path,\"batch_output\"), overwrite=False)"
"pw_utils.save_detection_images(results, \"batch_output\", overwrite=False)"
]
},
{
Expand All @@ -185,7 +179,7 @@
"metadata": {},
"outputs": [],
"source": [
"pw_utils.save_crop_images(results, os.path.join(demo_path,\"crop_output\"), overwrite=False)"
"pw_utils.save_crop_images(results, \"crop_output\", overwrite=False)"
]
},
{
Expand All @@ -204,7 +198,7 @@
"metadata": {},
"outputs": [],
"source": [
"pw_utils.save_detection_json(results, os.path.join(demo_path,\"batch_output.json\"),\n",
"pw_utils.save_detection_json(results, os.path.join(\".\",\"batch_output.json\"),\n",
" categories=detection_model.CLASS_NAMES,\n",
" exclude_category_ids=[], # Category IDs can be found in the definition of each model.\n",
" exclude_file_path=None)"
Expand All @@ -230,7 +224,7 @@
" categories=detection_model.CLASS_NAMES,\n",
" exclude_category_ids=[], # Category IDs can be found in the definition of each model.\n",
" exclude_file_path=tgt_folder_path,\n",
" info={\"detector\": \"MegaDetectorV5\"})"
" info={\"detector\": \"MegaDetectorV6\"})"
]
},
{
Expand All @@ -245,7 +239,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "pytorch-wildlife",
"language": "python",
"name": "python3"
},
Expand Down
50 changes: 24 additions & 26 deletions docs/demo/video_detection_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,22 @@
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image\n",
"import os\n",
"import numpy as np\n",
"import supervision as sv\n",
"import torch\n",
"from PytorchWildlife.models import detection as pw_detection\n",
"from PytorchWildlife.models import classification as pw_classification\n",
"from PytorchWildlife.data import transforms as pw_trans\n",
"from PytorchWildlife import utils as pw_utils\n",
"import os"
"from PytorchWildlife import utils as pw_utils"
]
},
{
"cell_type": "markdown",
"id": "a1d72019",
"metadata": {},
"source": [
"## Setting GPU\n",
"If you are using a GPU for this exercise, please specify which GPU to use for the computations. By default, GPU number 0 is used. Adjust this as per your setup. You don't need to run this cell if you are using a CPU."
"## Setting Device\n",
"If you are using a GPU for this exercise, please specify which GPU to use for the computations. By default, GPU number 0 is used."
]
},
{
Expand All @@ -62,7 +60,9 @@
"metadata": {},
"outputs": [],
"source": [
"torch.cuda.set_device(0) # Use only if you are running on GPU"
"DEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"if DEVICE == \"cuda\":\n",
" torch.cuda.set_device(0)"
]
},
{
Expand All @@ -77,35 +77,33 @@
{
"cell_type": "code",
"execution_count": null,
"id": "f5401c20",
"id": "dd069110",
"metadata": {},
"outputs": [],
"source": [
"# Get the current working directory \n",
"current_working_dir = os.getcwd() \n",
" \n",
"# Construct the folder path relative to the parent directory \n",
"demo_path = os.path.join(\"/\".join(current_working_dir.split(\"/\")[:-2]), \"demo\") \n",
" \n",
"# Verify the path exists \n",
"if os.path.exists(demo_path): \n",
" print(\"Path exists:\", demo_path) \n",
"else: \n",
" print(\"Path does not exist:\", demo_path) "
"SOURCE_VIDEO_PATH = os.path.join(\".\",\"demo_data\",\"videos\",\"opossum_example.MP4\")\n",
"TARGET_VIDEO_PATH = os.path.join(\".\",\"demo_data\",\"videos\",\"opossum_example_processed.MP4\")\n",
"\n",
"detection_model = pw_detection.MegaDetectorV6(device=DEVICE, pretrained=True, version=\"yolov9c\")\n",
"classification_model = pw_classification.AI4GOpossum(device=DEVICE, pretrained=True)"
]
},
{
"cell_type": "markdown",
"id": "f63b5af1",
"metadata": {},
"source": [
"If you want to use MegaDetectorV5, run this line."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd069110",
"id": "c5a0461e",
"metadata": {},
"outputs": [],
"source": [
"DEVICE = \"cuda\" # Use \"cuda\" if you are running on GPU. Use \"cpu\" if you are running on CPU\n",
"SOURCE_VIDEO_PATH = os.path.join(demo_path,\"demo_data/videos/opossum_example.MP4\")\n",
"TARGET_VIDEO_PATH = os.path.join(demo_path,\"demo_data/videos/opossum_example_processed.MP4\")\n",
"detection_model = pw_detection.MegaDetectorV5(device=DEVICE, pretrained=True)\n",
"classification_model = pw_classification.AI4GOpossum(device=DEVICE, pretrained=True)"
"detection_model = pw_detection.MegaDetectorV5(device=DEVICE, pretrained=True, version=\"a\")"
]
},
{
Expand Down Expand Up @@ -162,7 +160,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "pytorch-wildlife",
"language": "python",
"name": "python3"
},
Expand Down

0 comments on commit 176cc84

Please sign in to comment.