### Quick Start: Detection and Classification Source: https://github.com/microsoft/pytorch-wildlife/blob/main/README.md Demonstrates how to load and use detection and classification models. Weights are downloaded automatically. Ensure you have numpy installed. ```python import numpy as np from PytorchWildlife.models import detection as pw_detection from PytorchWildlife.models import classification as pw_classification # Detection — weights download automatically detection_model = pw_detection.MegaDetectorV6() detection_result = detection_model.single_image_detection("path/to/image.jpg") # Classification classification_model = pw_classification.AI4GAmazonRainforest() classification_result = classification_model.single_image_classification("path/to/image.jpg") ``` -------------------------------- ### Initialize and Use Detection and Classification Models Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/index.md Quick start example demonstrating how to initialize MegaDetector V6 for detection and AI4GAmazonRainforest for classification. Weights are downloaded automatically. Use this for single image inference. ```python import numpy as np from PytorchWildlife.models import detection as pw_detection from PytorchWildlife.models import classification as pw_classification # Detection — MegaDetector V6, weights download automatically detection_model = pw_detection.MegaDetectorV6() detection_result = detection_model.single_image_detection("path/to/image.jpg") # Classification classification_model = pw_classification.AI4GAmazonRainforest() classification_result = classification_model.single_image_classification("path/to/image.jpg") ``` -------------------------------- ### Install Dependencies from requirements.txt Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/build_mkdocs.md Installs MkDocs and its plugins using a requirements file. This is an alternative to the global installation. ```bash pip install -r docs-requirements.txt ``` -------------------------------- ### Install PyTorchWildlife Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/video_detection_colabdemo.ipynb Installs the PyTorchWildlife library using pip. After installation, the runtime session must be restarted. ```bash #Install PytorchWildlife !pip install pytorchwildlife ``` -------------------------------- ### Install PytorchWildlife Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo_herdnet.ipynb Installs PytorchWildlife using conda and pip. Ensure you have a CUDA-capable GPU for optimal performance. ```bash conda create -n pytorch_wildlife python=3.10 -y conda activate pytorch_wildlife pip install PytorchWildlife ``` -------------------------------- ### Install PyTorch-Wildlife Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/index.md Install the PyTorch-Wildlife library using pip. This command installs the core package. ```bash pip install PytorchWildlife ``` -------------------------------- ### Install PyTorchWildlife and Dependencies Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/custom_weight_loading.ipynb Install PyTorchWildlife and necessary libraries using conda and pip. Ensure CUDA is available for GPU acceleration. ```bash conda create -n pytorch_wildlife python=3.8 -y conda activate pytorch_wildlife pip install PytorchWildlife pip install wget ``` -------------------------------- ### Install System Dependencies with Homebrew Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/build_mkdocs.md Installs MkDocs and its plugins globally using Homebrew. Ensure a new terminal is opened after running `pipx ensurepath`. ```bash brew bundle ``` ```bash pipx install mkdocs-material --include-deps pipx inject mkdocs-material pymdown-extensions mkdocstrings mkdocstrings-python pipx ensurepath ``` -------------------------------- ### Install Dependencies Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_colabdemo.ipynb Installs necessary Python packages for image detection. It's recommended to use a virtual environment. ```python # install pip Installing collected packages: wcwidth, pure-eval, ptyprocess, pickleshare, ipython_genutils, backcall, zipp, typing-extensions, traitlets, tornado, pyzmq, python-dateutil, pyparsing, pygments, psutil, prompt_toolkit, platformdirs, pexpect, parso, packaging, nest-asyncio, executing, decorator, debugpy, asttokens, astor, stack-data, matplotlib-inline, jupyter-core, jedi, importlib-metadata, httplib2, comm, jupyter-client, ipython, ipykernel, jupyter_console Successfully installed astor-0.8.1 asttokens-2.4.1 backcall-0.2.0 comm-0.2.1 debugpy-1.8.0 decorator-5.1.1 executing-2.0.1 httplib2-0.22.0 importlib-metadata-7.0.1 ipykernel-6.28.0 ipython-8.12.3 ipython_genutils-0.2.0 jedi-0.19.1 jupyter-client-8.6.0 jupyter-core-5.7.1 jupyter_console-6.6.3 matplotlib-inline-0.1.6 nest-asyncio-1.5.8 packaging-23.2 parso-0.8.3 pexpect-4.9.0 pickleshare-0.7.5 platformdirs-4.1.0 prompt_toolkit-3.0.43 psutil-5.9.7 ptyprocess-0.7.0 pure-eval-0.2.2 pygments-2.17.2 pyparsing-3.1.1 python-dateutil-2.8.2 pyzmq-25.1.2 stack-data-0.6.3 tornado-6.4 traitlets-5.14.1 typing-extensions-4.9.0 wcwidth-0.2.13 zipp-3.17.0 [33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv [0m [33m [0m ``` ```python ``` -------------------------------- ### Install Dependencies Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_colabdemo.ipynb Installs required Python packages for the image detection demo. This includes libraries like ipython, ipykernel, and others necessary for the environment. ```bash pip install ipython ipykernel importlib-metadata>=4.8.3 python-dateutil>=2.8.2 platformdirs>=2.5 ptyprocess>=0.5 executing>=1.2.0 asttokens>=2.1.0 pure-eval zipp>=0.5 ipython ipykernel prompt_toolkit comm debugpy jedi jupyter_client jupyter_core pexpect pygments pyparsing pyzmq tornado traitlets nest_asyncio packaging psutil stack_data typing_extensions wcwidth asttokens executing importlib_metadata platformdirs zipp ``` -------------------------------- ### Install PytorchWildlife Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_separation_demo.ipynb Install PytorchWildlife and its dependencies using conda and pip. Ensure you have a CUDA-capable GPU for optimal performance, though CPU is also supported. ```bash conda create -n pytorch_wildlife python=3.8 -y conda activate pytorch_wildlife pip install PytorchWildlife ``` -------------------------------- ### Install Pip Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_colabdemo.ipynb Downloads and installs or upgrades pip to the latest version. It's recommended to use a virtual environment for managing packages. ```bash !curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py !python3 get-pip.py --force-reinstall ``` -------------------------------- ### Install Python 3.8 and Dependencies Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/video_detection_colabdemo.ipynb Installs Python 3.8, its development files, and distutils. It then updates system alternatives to prioritize Python 3.8. ```bash !sudo apt-get update -y !sudo apt-get install python3.8 python3.8-dev python3.8-distutils libpython3.8-dev #change alternatives !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2 #Check that it points at the right location !python3 --version ``` -------------------------------- ### Install Python 3.8 and Dependencies Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_colabdemo.ipynb Installs Python 3.8 and necessary development libraries on the system. This is a prerequisite for running the image detection demo. ```bash debconf: falling back to frontend: Readline debconf: unable to initialize frontend: Readline debconf: (This frontend requires a controlling tty.) debconf: falling back to frontend: Teletype dpkg-preconfigure: unable to re-open stdin: Selecting previously unselected package libpython3.8-minimal:amd64. (Reading database ... 121654 files and directories currently installed.) Preparing to unpack .../00-libpython3.8-minimal_3.8.18-1+jammy1_amd64.deb ... Unpacking libpython3.8-minimal:amd64 (3.8.18-1+jammy1) ... Selecting previously unselected package python3.8-minimal. Preparing to unpack .../01-python3.8-minimal_3.8.18-1+jammy1_amd64.deb ... Unpacking python3.8-minimal (3.8.18-1+jammy1) ... Selecting previously unselected package mailcap. Preparing to unpack .../02-mailcap_3.70+nmu1ubuntu1_all.deb ... Unpacking mailcap (3.70+nmu1ubuntu1) ... Selecting previously unselected package mime-support. Preparing to unpack .../03-mime-support_3.66_all.deb ... Unpacking mime-support (3.66) ... Selecting previously unselected package libpython3.8-stdlib:amd64. Preparing to unpack .../04-libpython3.8-stdlib_3.8.18-1+jammy1_amd64.deb ... Unpacking libpython3.8-stdlib:amd64 (3.8.18-1+jammy1) ... Selecting previously unselected package libpython3.8:amd64. Preparing to unpack .../05-libpython3.8_3.8.18-1+jammy1_amd64.deb ... Unpacking libpython3.8:amd64 (3.8.18-1+jammy1) ... Selecting previously unselected package libpython3.8-dev:amd64. Preparing to unpack .../06-libpython3.8-dev_3.8.18-1+jammy1_amd64.deb ... Unpacking libpython3.8-dev:amd64 (3.8.18-1+jammy1) ... Selecting previously unselected package python3.8. Preparing to unpack .../07-python3.8_3.8.18-1+jammy1_amd64.deb ... Unpacking python3.8 (3.8.18-1+jammy1) ... Selecting previously unselected package python3.8-dev. Preparing to unpack .../08-python3.8-dev_3.8.18-1+jammy1_amd64.deb ... Unpacking python3.8-dev (3.8.18-1+jammy1) ... Selecting previously unselected package python3.8-lib2to3. Preparing to unpack .../09-python3.8-lib2to3_3.8.18-1+jammy1_all.deb ... Unpacking python3.8-lib2to3 (3.8.18-1+jammy1) ... Selecting previously unselected package python3.8-distutils. Preparing to unpack .../10-python3.8-distutils_3.8.18-1+jammy1_all.deb ... Unpacking python3.8-distutils (3.8.18-1+jammy1) ... Setting up libpython3.8-minimal:amd64 (3.8.18-1+jammy1) ... Setting up python3.8-lib2to3 (3.8.18-1+jammy1) ... Setting up python3.8-minimal (3.8.18-1+jammy1) ... Setting up python3.8-distutils (3.8.18-1+jammy1) ... Setting up mailcap (3.70+nmu1ubuntu1) ... Setting up mime-support (3.66) ... Setting up libpython3.8-stdlib:amd64 (3.8.18-1+jammy1) ... Setting up python3.8 (3.8.18-1+jammy1) ... Setting up libpython3.8:amd64 (3.8.18-1+jammy1) ... Setting up libpython3.8-dev:amd64 (3.8.18-1+jammy1) ... Setting up python3.8-dev (3.8.18-1+jammy1) ... Processing triggers for man-db (2.10.2-1) ... update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python3 (python3) in auto mode update-alternatives: error: alternative path /usr/bin/python3.9 doesn't exist Python 3.8.18 ``` -------------------------------- ### Install pip and Colab Dependencies Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/video_detection_colabdemo.ipynb Downloads and installs pip for Python 3, then installs essential Colab dependencies like ipython and jupyter. ```bash # install pip !curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py !python3 get-pip.py --force-reinstall #install colab's dependencies !python3 -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor ``` -------------------------------- ### Install OpenCV for Ubuntu Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/installation.md Install the python3-opencv package on Ubuntu systems, which is a dependency for certain functionalities. ```bash sudo apt-get update sudo apt-get install -y python3-opencv ``` -------------------------------- ### Install ffmpeg on macOS Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/installation.md Install ffmpeg using Homebrew on macOS for video decoding capabilities required by PyTorch-Wildlife. ```bash brew install ffmpeg ``` -------------------------------- ### Install GPU-enabled PyTorch for CUDA 12.1 Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/installation.md Install or reinstall PyTorch, torchvision, and torchaudio specifically for CUDA 12.1 if CUDA is not detected. This is followed by installing PyTorch-Wildlife. ```bash pip uninstall torch torchvision torchaudio pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 pip install PytorchWildlife ``` -------------------------------- ### Run PyTorch-Wildlife Docker container Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/installation.md Pull the PyTorch-Wildlife Docker image and run a container to execute the Gradio demo. This allows trying the library without local installation. ```bash docker pull andreshdz/pytorchwildlife:1.0.2.3 docker run -p 80:80 andreshdz/pytorchwildlife:1.0.2.3 python demo/gradio_demo.py ``` -------------------------------- ### Set up Conda environment and install PyTorch-Wildlife Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/installation.md Create a dedicated conda environment for PyTorch-Wildlife with Python 3.10 and then install the library. This helps manage dependencies. ```bash conda create -n pytorch-wildlife python=3.10 -y conda activate pytorch-wildlife pip install PytorchWildlife ``` -------------------------------- ### Verify PyTorch-Wildlife installation Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/installation.md Load the MegaDetectorV6 model from PyTorch-Wildlife to confirm that the library has been installed correctly. Model weights download automatically on first use. ```python from PytorchWildlife.models import detection as pw_detection model = pw_detection.MegaDetectorV6() print("PyTorch-Wildlife loaded successfully.") ``` -------------------------------- ### Install Colab Dependencies Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_colabdemo.ipynb Installs a suite of Python packages required for interactive computing and development within a Colab environment. This includes IPython for enhanced interactivity and other utilities. ```bash !python3 -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor ``` -------------------------------- ### Install Jupyter kernel for PyTorch-Wildlife Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/installation.md Install the ipykernel and register a new Jupyter kernel for the PyTorch-Wildlife conda environment. This enables using the environment within Jupyter notebooks. ```bash conda install ipykernel python -m ipykernel install --user --name pytorch-wildlife --display-name "Python (PytorchWildlife)" ``` -------------------------------- ### Preview MkDocs Site Locally Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/build_mkdocs.md Serves the generated documentation site locally, making it available at `http://127.0.0.1:8000/` for preview. ```bash mkdocs serve ``` -------------------------------- ### Build the MkDocs Site Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/build_mkdocs.md Generates the static site files for the documentation in the `site/` directory. ```bash mkdocs build ``` -------------------------------- ### Detection + Classification Pipeline Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/model_zoo.md Demonstrates setting up a pipeline that first performs detection and then classification on an image. Requires separate detection and classification models. ```python from PytorchWildlife.models import detection as pw_detection from PytorchWildlife.models import classification as pw_classification detection_model = pw_detection.MegaDetectorV6() classification_model = pw_classification.AI4GAmazonRainforest() # Detect, then classify crops detection_result = detection_model.single_image_detection("image.jpg") classification_result = classification_model.single_image_classification("image.jpg") ``` -------------------------------- ### Deploy MkDocs Site to GitHub Pages Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/build_mkdocs.md Deploys the documentation site to GitHub Pages. The `--force` flag is used for manual deployment. ```bash mkdocs gh-deploy --force ``` -------------------------------- ### Initialize Detection and Classification Models Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/video_detection_colabdemo.ipynb Sets the device to CUDA if available, otherwise CPU. Initializes MegaDetectorV6 for detection and AI4GOpossum for classification, loading pretrained weights. ```python DEVICE = "cuda" if torch.cuda.is_available() else "cpu" if DEVICE == "cuda": torch.cuda.set_device(0) SOURCE_VIDEO_PATH = "/content/CameraTraps-main/demo/demo_data/videos/opossum_example.MP4" TARGET_VIDEO_PATH = "/content/CameraTraps-main/demo/demo_data/videos/opossum_processed.MP4" # Verify the checkpoints directory exists to save the model weights if pretrained=True os.makedirs(os.path.join(torch.hub.get_dir(), "checkpoints"), exist_ok=True) # Initializing the MegaDetectorV6 model for image detection # Valid versions are MDV6-yolov9-c, MDV6-yolov9-e, MDV6-yolov10-c, MDV6-yolov10-e or MDV6-rtdetr-c detection_model = pw_detection.MegaDetectorV6(device=DEVICE, pretrained=True, version="MDV6-yolov10-e") # Uncomment the following line to use MegaDetectorV5 instead of MegaDetectorV6 #detection_model = pw_detection.MegaDetectorV5(device=DEVICE, pretrained=True, version="a") # Initializing the AI4GOpossum model for image classification classification_model = pw_classification.AI4GOpossum(device=DEVICE, pretrained=True) ``` -------------------------------- ### Import Libraries Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_separation_demo.ipynb Import necessary libraries including torch, PytorchWildlife detection models, and utility functions. ```python import os import torch from PytorchWildlife.models import detection as pw_detection from PytorchWildlife import utils as pw_utils ``` -------------------------------- ### Initialize OWL Detection Model Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo_owl.ipynb Initialize the OWL detection model (OWL-C or OWL-T) for overhead image detection. Set the device to 'cuda' if available, otherwise 'cpu'. ```python # Setting the device to use for computations ('cuda' indicates GPU) DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Model arguments and initialization # OWL-C (CNN-only): General or Caribou version #detection_model = pw_detection.OWLC(device=DEVICE) #detection_model = pw_detection.OWLC(device=DEVICE, version="caribou") # OWL-T (Hybrid CNN + Transformer): detection_model = pw_detection.OWLT(device=DEVICE) ``` -------------------------------- ### Initialize Detection and Classification Models Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/video_detection_demo.ipynb Set the computation device (GPU or CPU) and initialize the MegaDetectorV6 for detection and AI4GOpossum for classification models. Specify model versions and pretrained weights. ```python # Setting the device to use for computations ('cuda' indicates GPU) DEVICE = "cuda" if torch.cuda.is_available() else "cpu" if DEVICE == "cuda": torch.cuda.set_device(0) SOURCE_VIDEO_PATH = os.path.join(".","demo_data","videos","opossum_example.MP4") TARGET_VIDEO_PATH = os.path.join(".","demo_data","videos","opossum_example_processed.MP4") # Initializing the MegaDetectorV6 model for image detection # Valid versions are MDV6-yolov9-c, MDV6-yolov9-e, MDV6-yolov10-c, MDV6-yolov10-e or MDV6-rtdetr-c detection_model = pw_detection.MegaDetectorV6(device=DEVICE, pretrained=True, version="MDV6-yolov10-e") # Uncomment the following line to use MegaDetectorV5 instead of MegaDetectorV6 #detection_model = pw_detection.MegaDetectorV5(device=DEVICE, pretrained=True, version="a") # Initializing the AI4GOpossum model for image classification classification_model = pw_classification.AI4GOpossum(device=DEVICE, pretrained=True) ``` -------------------------------- ### Load HerdNet Model Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/model_zoo.md Instantiate HerdNet, a point-based localization model suitable for overhead and aerial imagery. ```python detector = pw_detection.HerdNet() ``` -------------------------------- ### Initialize Detection Model Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/custom_weight_loading.ipynb Initialize the MegaDetectorV6 model for image detection, specifying the device (GPU or CPU) and model version. Ensure the correct version string is used. ```python # Setting the device to use for computations ('cuda' indicates GPU) DEVICE = "cuda" if torch.cuda.is_available() else "cpu" if DEVICE == "cuda": torch.cuda.set_device(0) # Initializing the MegaDetectorV6 model for image detection # Valid versions are MDV6-yolov9c, MDV6-yolov9e, MDV6-yolov10n, MDV6-yolov10x or MDV6-rtdetrl detection_model = pw_detection.MegaDetectorV6(device=DEVICE, pretrained=True, version="MDV6-yolov10x") # Uncomment the following line to use MegaDetectorV5 instead of MegaDetectorV6 #detection_model = pw_detection.MegaDetectorV5(device=DEVICE, pretrained=True, version="a") ``` -------------------------------- ### Prepare Dataset for Classification Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/custom_weight_loading.ipynb Create a dataset of cropped images from detection results for classification inference. Apply `Classification_Inference_Transform` to resize images to the target size. ```python clf_dataset = pw_data.DetectionCrops( det_results, transform=pw_trans.Classification_Inference_Transform(target_size=224), path_head="" ) clf_loader = DataLoader(clf_dataset, batch_size=32, shuffle=False, pin_memory=True, num_workers=0, drop_last=False) clf_results = classification_model.batch_image_classification(clf_loader, id_strip=tgt_folder_path) ``` -------------------------------- ### Download CameraTraps Repository Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/video_detection_colabdemo.ipynb Downloads the CameraTraps GitHub repository, which contains demo data and potentially other utilities needed for the video detection process. ```bash !wget -O repo.zip https://github.com/microsoft/CameraTraps/archive/refs/heads/main.zip !unzip -q repo.zip -d ./ ``` -------------------------------- ### Import Libraries for Video Detection Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/video_detection_demo.ipynb Import necessary libraries including numpy, supervision, torch, and PyTorchWildlife modules for detection and classification. ```python import os import numpy as np import supervision as sv import torch from PytorchWildlife.models import detection as pw_detection from PytorchWildlife.models import classification as pw_classification from PytorchWildlife import utils as pw_utils ``` -------------------------------- ### Set Computation Device (GPU/CPU) Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_colabdemo.ipynb Configures the computation device, prioritizing GPU if available, otherwise falling back to CPU. Adjust the GPU index as needed. ```python # Setting the device to use for computations ('cuda' indicates GPU) DEVICE = "cuda" if torch.cuda.is_available() else "cpu" if DEVICE == "cuda": torch.cuda.set_device(0) ``` -------------------------------- ### Load Bioacoustic Model Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/model_zoo.md Instantiate a bioacoustic classification model for audio analysis in wildlife monitoring. ```python from PytorchWildlife.models import bioacoustics as pw_bioacoustics model = pw_bioacoustics.BioacousticsResnetClassifier() ``` -------------------------------- ### Initialize HerdNet Detection Model Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo_herdnet.ipynb Initializes the HerdNet model, specifying the device for computation (GPU if available, otherwise CPU). Optionally, load weights from the 'ennedi' dataset. ```python # Setting the device to use for computations ('cuda' indicates GPU) DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Model arguments and initialization detection_model = pw_detection.HerdNet(device=DEVICE) # If you want to use ennedi dataset weights, you can use the following line: # detection_model = pw_detection.HerdNet(device=DEVICE, version="ennedi") ``` -------------------------------- ### Process Video with Annotations Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/video_detection_demo.ipynb Process the source video using the defined callback function and save the annotated output to a target video path with a specified frames per second (FPS). ```python pw_utils.process_video(source_path=SOURCE_VIDEO_PATH, target_path=TARGET_VIDEO_PATH, callback=callback, target_fps=5) ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/custom_weight_loading.ipynb Import essential libraries for data handling, model definition, and utility functions from PyTorchWildlife and other standard Python packages. ```python import os import numpy as np import os from PIL import Image import torch from torch.utils.data import DataLoader from PytorchWildlife.models import detection as pw_detection from PytorchWildlife.models import classification as pw_classification from PytorchWildlife.data import transforms as pw_trans from PytorchWildlife.data import datasets as pw_data from PytorchWildlife import utils as pw_utils import wget ``` -------------------------------- ### Load Species Classification Models Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/model_zoo.md Instantiate various species classification models. These can be paired with detection models for a complete pipeline. ```python from PytorchWildlife.models import classification as pw_classification classifier = pw_classification.AI4GAmazonRainforest() classifier = pw_classification.AI4GSnapshotSerengeti() classifier = pw_classification.DeepfauneClassifier() classifier = pw_classification.DFNE() ``` -------------------------------- ### Initialize MegaDetector Model Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_separation_demo.ipynb Initialize the MegaDetectorV6 model for image detection, specifying the device (GPU or CPU) and model version. MegaDetectorV5 is also available. ```python # Setting the device to use for computations ('cuda' indicates GPU) DEVICE = "cuda" if torch.cuda.is_available() else "cpu" if DEVICE == "cuda": torch.cuda.set_device(0) # Initializing the MegaDetectorV6 model for image detection # Valid versions are MDV6-yolov9-c, MDV6-yolov9-e, MDV6-yolov10-c, MDV6-yolov10-e or MDV6-rtdetr-c detection_model = pw_detection.MegaDetectorV6(device=DEVICE, pretrained=True, version="MDV6-yolov10-e") # Uncomment the following line to use MegaDetectorV5 instead of MegaDetectorV6 #detection_model = pw_detection.MegaDetectorV5(device=DEVICE, pretrained=True, version="a") ``` -------------------------------- ### Load Deepfaune Detector Model Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/model_zoo.md Instantiate the Deepfaune Detector, trained for European ecosystems and the first third-party detection model integrated into PyTorch-Wildlife. ```python detector = pw_detection.DeepfauneDetector() ``` -------------------------------- ### Load MegaDetector V6 Models Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/model_zoo.md Instantiate different MegaDetector V6 architecture variants. Choose based on accuracy, speed, and licensing requirements. ```python from PytorchWildlife.models import detection as pw_detection # Default (AGPL, YOLOv10) detector = pw_detection.MegaDetectorV6() # MIT-licensed YOLO detector = pw_detection.MegaDetectorV6MIT(version="MDV6-mit-yolov9-e") # Apache RT-DETR detector = pw_detection.MegaDetectorV6Apache(version="MDV6-apa-rtdetr-e") ``` -------------------------------- ### Initialize MegaDetectorV6 Model Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_colabdemo.ipynb Initializes the MegaDetectorV6 model for animal detection. Supports different versions like yolov9, yolov10, and rtdetr. The MegaDetectorV5 model can be used by uncommenting the provided line. ```python # Initializing the MegaDetectorV6 model for image detection # Valid versions are MDV6-yolov9-c, MDV6-yolov9-e, MDV6-yolov10-c, MDV6-yolov10-e or MDV6-rtdetr-c detection_model = pw_detection.MegaDetectorV6(device=DEVICE, pretrained=True, version="MDV6-yolov10-e") # Uncomment the following line to use MegaDetectorV5 instead of MegaDetectorV6 #detection_model = pw_detection.MegaDetectorV5(device=DEVICE, pretrained=True, version="a") ``` -------------------------------- ### Load Custom Classification Weights Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/custom_weight_loading.ipynb Download and load custom weights for the classification model using `CustomWeights`. Provide the URL to the weights file and a dictionary mapping class IDs to names. ```python # Defining the classification model example_weights = "https://zenodo.org/records/13357337/files/AI4GAmazonClassification_v0.0.0.ckpt?download=1" filename = wget.download(example_weights) class_names = { 0: 'Dasyprocta', 1: 'Bos', 2: 'Pecari', 3: 'Mazama', 4: 'Cuniculus', 5: 'Leptotila', 6: 'Human', 7: 'Aramides', 8: 'Tinamus', 9: 'Eira', 10: 'Crax', 11: 'Procyon', 12: 'Capra', 13: 'Dasypus', 14: 'Sciurus', 15: 'Crypturellus', 16: 'Tamandua', 17: 'Proechimys', 18: 'Leopardus', 19: 'Equus', 20: 'Columbina', 21: 'Nyctidromus', 22: 'Ortalis', 23: 'Emballonura', 24: 'Odontophorus', 25: 'Geotrygon', 26: 'Metachirus', 27: 'Catharus', 28: 'Cerdocyon', 29: 'Momotus', 30: 'Tapirus', 31: 'Canis', 32: 'Furnarius', 33: 'Didelphis', 34: 'Sylvilagus', 35: 'Unknown' } classification_model = pw_classification.CustomWeights(weights=filename, class_names=class_names, device=DEVICE) ``` -------------------------------- ### Perform Batch Image Detection Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_separation_demo.ipynb Process multiple images efficiently by performing batch detection using the initialized model. Adjust the batch_size for optimal performance. ```python results = detection_model.batch_image_detection(tgt_folder_path, batch_size=16) ``` -------------------------------- ### Check CUDA availability with PyTorch Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/installation.md Verify if your system's CUDA is accessible by PyTorch. This is crucial for GPU acceleration. ```python import torch print(torch.cuda.is_available()) ``` -------------------------------- ### Batch Image Detection Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo_owl.ipynb Process multiple images in batches from a specified folder for efficient processing. For OWL models, a batch size of 1 is recommended due to internal image patching. ```python folder_path = os.path.join(".","demo_data","owl_imgs") results = detection_model.batch_image_detection(folder_path, batch_size=1) ``` -------------------------------- ### Define Variables for Batch Detection Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_separation_demo.ipynb Define the input image directory, confidence threshold, and output directory for separating detections. Customize these variables for your own data. ```python tgt_folder_path = os.path.join(".","demo_data","imgs") output_path = "folder_separation" threshold = 0.2 ``` -------------------------------- ### Save Batch Detection Results as Annotated Images Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo_owl.ipynb Save the batch detection results as annotated images with dots drawn around detected animals. Specify the output directory and whether to overwrite existing files. ```python pw_utils.save_detection_images_dots(results, "owl_demo_batch_output", folder_path, overwrite=False) ``` -------------------------------- ### Load MegaDetector V5 Model Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/model_zoo.md Instantiate the MegaDetector V5 model, the previous generation widely used by conservation organizations. For older versions, refer to the archive branch. ```python detector = pw_detection.MegaDetectorV5() ``` -------------------------------- ### Perform Single Image Detection and Save Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/custom_weight_loading.ipynb Detect objects in a single image using the initialized detection model and save the annotated image to a specified output directory. The `overwrite` parameter controls whether existing files are replaced. ```python tgt_img_path = os.path.join(".","demo_data","imgs","10050028_0.JPG") img = np.array(Image.open(tgt_img_path).convert("RGB")) results = detection_model.single_image_detection(img, tgt_img_path) pw_utils.save_detection_images(results, os.path.join(".","demo_output"), overwrite=False) ``` -------------------------------- ### Save Detection Results and Separate Detections Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_separation_demo.ipynb Save detection results to a JSON file and then use `detection_folder_separation` to copy images into 'Animal' and 'No-animal' subdirectories based on a confidence threshold. ```python os.makedirs(output_path, exist_ok=True) json_file = os.path.join(output_path, "detection_results.json") pw_utils.save_detection_json(results, json_file, categories=detection_model.CLASS_NAMES, exclude_category_ids=[], # Category IDs can be found in the definition of each model. exclude_file_path=tgt_folder_path) # Separate the positive and negative detections through file copying: pw_utils.detection_folder_separation(json_file, tgt_folder_path, output_path, threshold) ``` -------------------------------- ### Link Google Colab Packages and Patch IPython Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/video_detection_colabdemo.ipynb Creates a symbolic link for Google Colab packages to Python 3.8 and patches specific IPython files to ensure compatibility. ```bash # link to the old google package !ln -s /usr/local/lib/python3.10/dist-packages/google \ /usr/local/lib/python3.8/dist-packages/google !sed -i "s/from IPython.utils import traitlets as _traitlets/import traitlets as _traitlets/" /usr/local/lib/python3.8/dist-packages/google/colab/*.py !sed -i "s/from IPython.utils import traitlets/import traitlets/" /usr/local/lib/python3.8/dist-packages/google/colab/*.py ``` -------------------------------- ### Perform Batch Image Detection with HerdNet Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo_herdnet.ipynb Processes multiple images from a specified folder in batches using HerdNet. A batch size of 1 is recommended for HerdNet due to its patch-based processing. ```python folder_path = os.path.join(".","demo_data","herdnet_imgs") results = detection_model.batch_image_detection(folder_path, batch_size=1) ``` -------------------------------- ### Single Image Detection Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo_owl.ipynb Perform detection on a single image using the initialized model. Ensure the image path is correct. The results are then saved as annotated images with dots. ```python img_path = os.path.join(".","demo_data","owl_imgs","S_11_05_16_DSC01556_cut.JPG") results = detection_model.single_image_detection(img=img_path) pw_utils.save_detection_images_dots(results, os.path.join(".","owl_demo_output"), overwrite=False) ``` -------------------------------- ### Save Annotated Detection Images Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo.ipynb Saves images with bounding boxes drawn around detected objects to a specified output directory. Set overwrite=True to replace existing files. ```python pw_utils.save_detection_images(results, "batch_output", overwrite=False) ``` -------------------------------- ### Save HerdNet Batch Detection Results as Annotated Images Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo_herdnet.ipynb Saves the detection results from batch processing as images with dots marking detected animals. Specify the output directory and original folder path. ```python pw_utils.save_detection_images_dots(results, "herdnet_demo_batch_output", folder_path, overwrite=False) ``` -------------------------------- ### Cite PyTorch-Wildlife (BibTeX) Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/cite.md Use this BibTeX entry to cite the PyTorch-Wildlife summary paper in your research. It was presented at CVPR 2024. ```bibtex @misc{hernandez2024pytorchwildlife, title={Pytorch-Wildlife: A Collaborative Deep Learning Framework for Conservation}, author={Andres Hernandez and Zhongqi Miao and Luisa Vargas and Sara Beery and Rahul Dodhia and Juan Lavista}, year={2024}, eprint={2405.12930}, archivePrefix={arXiv}, } ``` -------------------------------- ### Perform Single Image Detection with HerdNet Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo_herdnet.ipynb Performs object detection on a single image using the initialized HerdNet model and saves the annotated image. Ensure the image path is correct. ```python img_path = os.path.join(".","demo_data","herdnet_imgs","S_11_05_16_DSC01556.JPG") results = detection_model.single_image_detection(img=img_path) pw_utils.save_detection_images_dots(results, os.path.join(".","herdnet_demo_output"), overwrite=False) ``` -------------------------------- ### Save Detection Results in Timelapse JSON Format Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo.ipynb Outputs detection results in a JSON format compatible with Timelapse for visualizing camera trap data. Includes optional metadata like detector information. ```python pw_utils.save_detection_timelapse_json(results, os.path.join(".","batch_output_timelapse.json"), categories=detection_model.CLASS_NAMES, exclude_category_ids=[], # Category IDs can be found in the definition of each model. exclude_file_path=tgt_folder_path, info={"detector": "MegaDetectorV6"}) ``` -------------------------------- ### Perform Single Image Detection Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo.ipynb Performs object detection on a single image file. Ensure the image path is correct and the output directory exists. ```python tgt_img_path = os.path.join(".","demo_data","imgs","10050028_0.JPG") results = detection_model.single_image_detection(tgt_img_path) pw_utils.save_detection_images(results, os.path.join(".","demo_output"), overwrite=False) ``` -------------------------------- ### Save Batch Detection Results in JSON Format Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo_owl.ipynb Save the batch detection results in JSON format, using dot coordinates instead of bounding boxes. Include class names and optionally exclude specific categories or file paths. ```python pw_utils.save_detection_json_as_dots(results, os.path.join(".","owl_demo_batch_output.json"), categories=detection_model.CLASS_NAMES, exclude_category_ids=[], # Category IDs can be found in the definition of each model. exclude_file_path=None) ``` -------------------------------- ### Save Cropped Detection Images Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo.ipynb Saves cropped images of detected objects to a specified output directory. Cropping is based on the bounding box coordinates. ```python pw_utils.save_crop_images(results, "crop_output", overwrite=False) ``` -------------------------------- ### Save Detection and Classification Results Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/custom_weight_loading.ipynb Save the combined detection and classification results to a JSON file. This function requires both detection and classification results, along with their respective category names. ```python pw_utils.save_detection_classification_json(det_results=det_results, clf_results=clf_results, det_categories=detection_model.CLASS_NAMES, clf_categories=classification_model.CLASS_NAMES, output_path="results.json") ``` -------------------------------- ### Save Detection Results in JSON Format Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo.ipynb Saves detection results to a JSON file, including class names and optional exclusion criteria. Useful for further data processing. ```python pw_utils.save_detection_json(results, os.path.join(".","batch_output.json"), categories=detection_model.CLASS_NAMES, exclude_category_ids=[], # Category IDs can be found in the definition of each model. exclude_file_path=None) ``` -------------------------------- ### Define Video Processing Callback Function Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/video_detection_demo.ipynb Define a callback function to process each video frame. It applies detection, crops detected regions, performs classification, and annotates the frame with results. ```python box_annotator = sv.BoxAnnotator(thickness=4) lab_annotator = sv.LabelAnnotator(text_color=sv.Color.BLACK, text_thickness=4, text_scale=2) def callback(frame: np.ndarray, index: int) -> np.ndarray: results_det = detection_model.single_image_detection(frame, img_path=index) labels = [] for xyxy in results_det["detections"].xyxy: cropped_image = sv.crop_image(image=frame, xyxy=xyxy) results_clf = classification_model.single_image_classification(cropped_image) labels.append("{} {:.2f}".format(results_clf["prediction"], results_clf["confidence"])) annotated_frame = lab_annotator.annotate( scene=box_annotator.annotate( scene=frame, detections=results_det["detections"], ), detections=results_det["detections"], labels=labels, ) return annotated_frame ``` -------------------------------- ### Save HerdNet Batch Detection Results as JSON Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_demo_herdnet.ipynb Saves the detection results from batch processing into a JSON file. This format includes dot coordinates instead of bounding boxes for HerdNet. You can specify categories to include or exclude. ```python pw_utils.save_detection_json_as_dots(results, os.path.join(".","herdnet_demo_batch_output.json"), categories=detection_model.CLASS_NAMES, exclude_category_ids=[], # Category IDs can be found in the definition of each model. exclude_file_path=None) ``` -------------------------------- ### Execute Python Code Source: https://github.com/microsoft/pytorch-wildlife/blob/main/demo/image_detection_colabdemo.ipynb A placeholder for executing Python code within the Colab environment. This snippet is intended to be followed by specific image detection logic. ```python ``` -------------------------------- ### Cite MegaDetector (BibTeX) Source: https://github.com/microsoft/pytorch-wildlife/blob/main/docs/cite.md If using MegaDetector specifically, cite this original paper. This BibTeX entry is for academic referencing. ```bibtex @misc{beery2019efficient, title={Efficient Pipeline for Camera Trap Image Review}, author={Sara Beery and Dan Morris and Siyu Yang}, year={2019}, eprint={1907.06772}, archivePrefix={arXiv}, } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.