Tag: インストール方法

  • What Exactly Is YORU, a New Behavior Analysis Tool Powered by Object Detection?

    What Exactly Is YORU, a New Behavior Analysis Tool Powered by Object Detection?

    In November 2024, a preprint was posted on bioRxiv describing YORU, a behavior analysis tool that takes a different approach from existing behavior analysis tools.
    In this post, I’d like to explain what kind of tool it is.

    The YORU preprint

    The YORU documentation

    What is YORU?

    YORU is a deep learning-based tool for analyzing animal behavior.

    YORUのサイトより引用


    A number of deep learning-based tools, such as DeepLabCut and SLEAP, have already been released.
    These tools use deep learning to track body parts of animals and estimate their posture (here I’ll refer to them as tracking tools).

    By estimating posture, they reveal what posture an animal is in and use that for behavior analysis.

    YORU, by contrast, recognizes behavior using an object detection algorithm.
    In short, it does not track body parts; instead, it defines behavior directly from the appearance of the animal as it behaves.

    When people tell an apple from a mandarin orange, they rely on cues such as shape and color.
    Object detection excels at classifying objects, so it makes exactly this kind of appearance-based distinction.

    Applying this to animal behavior analysis means classifying animal behavior from its appearance.
    Within YORU, we call this a “behavior object.”

    Whereas tracking tools represent behavior as points and lines, YORU analyzes behavior by enclosing it in a bounding box.

    Advantages of defining behavior with object detection

    The advantages of object detection-based behavior analysis include:

    ・Errors are less likely as the number of individuals increases

    ・Less sensitive to the orientation of the animal

    ・Fast analysis speed

    ・Can capture behaviors that are hard to capture by tracking (such as a mouse crouching)

    and so on.

    It makes it easy to analyze the behavior of multiple individuals—a weak point of tracking tools—at a considerably lower computational cost.
    It can also easily analyze behaviors that are difficult to define from the positional information of body parts.

    Consider, for example, the act of opening and closing a hand.
    With a tracking tool, you would track the fingertips with a camera and define the hand as open or closed, but if the fingertips become hidden when the hand closes, tracking fails and the positional information can no longer be computed accurately.

    With object detection, however, the model looks at the overall shape of the open and closed hand, so it can capture the state accurately even when part of the hand is hidden.

    The same logic explains its robustness to larger numbers of individuals: with a tracking tool, as the number of individuals grows, you must assign each body part to the correct individual to capture each individual’s behavior accurately, whereas YORU analyzes only the shape of each individual and therefore requires no individual identification or assignment.

    Because analysis is fast, real-time analysis is possible, allowing you to control equipment when a particular behavior occurs and thus automate behavioral experiments.

    Disadvantages of defining behavior with object detection

    Of course, there are disadvantages as well:

    ・Cannot capture a sequence of behaviors

    ・Cannot identify individuals

    ・Cannot capture unknown behaviors

    ・Cannot capture behaviors that are hard to distinguish by appearance

    ・Provides no detailed information about the defined behavior

    These are precisely the strengths of tracking tools; rather than one approach being superior, the two complement each other’s advantages and disadvantages.

    Experimenters therefore need to choose the method appropriate to their situation.

    What makes YORU special?

    Having described the advantages and disadvantages of the object detection-based behavior analysis that underlies YORU, what exactly makes YORU special?

    Everything runs through a GUI

    YORU lets you perform behavior analysis without any programming.

    The same is true of tools like DeepLabCut and SLEAP, and it goes a long way toward making these analyses accessible to biologists.

    Design


    Unlike previous tools, YORU’s design doesn’t really feel like a research tool.

    Built-in real-time analysis

    Unusually for tools of this kind, YORU comes with a full GUI for real-time analysis.

    Building a real-time analysis system isn’t that difficult if you can program to some extent, but otherwise the barrier is quite high.

    With YORU, however, real-time analysis and even external triggering can all be done without any programming.

    Adaptable to a wide range of experimental setups

    The biggest hurdle in real-time analysis is figuring out how to combine the code that drives your own experimental apparatus with the analysis itself.

    YORU adopts a plugin system, letting you choose the program that controls your external device.

    In other words, you simply select the plugin that matches your own system.

    You can also write your own plugins: by wrapping the control program for your own apparatus in a plugin, you can easily link it to YORU’s real-time analysis.

    This makes for a remarkably easy-to-use system that no other tool offers.

    Closing remarks

    YORU has only just been released, and there are still rough edges in usability as well as gaps in the documentation.

    That said, as these are addressed over time, I am very much looking forward to seeing how it comes to be used.

  • How to Install SLEAP [Updated February 2024]

    How to Install SLEAP [Updated February 2024]

    Here I’d like to describe how to install SLEAP, a machine learning-based tool for tracking animal body parts.

    DeepLabCut is the most widely used tracking tool, and I have written about how to install it in the past, as have many other people.

    For SLEAP, however, there are currently few articles available in Japanese.
    SLEAP has many advantages over DeepLabCut, so I decided to write up the installation procedure to make it a viable option for more people.

    SLEAP’s documentation, from installation to usage, is extremely well organized and comes with videos, so anyone comfortable with English should find it easy to get started.

    The SLEAP paper is available here

    The official SLEAP website is here

    The SLEAP GitHub repository is here

    Introduction

    First of all, regarding the approach described in the official SLEAP installation guide,

    mamba create -y -n sleap -c conda-forge -c nvidia -c sleap -c anaconda sleap=1.3.3

    which sets up the environment, installs the packages, and handles everything else in a single command: I tried this on several computers, but in every case it stalled indefinitely during the environment setup and never completed successfully.

    So here I introduce an alternative installation method, also suggested by the developers.

    Installation

    We will basically follow the installation instructions on the official SLEAP website.

    Environment

    ・Windows 11 Pro
    ・Verified on an NVIDIA 3080
    ・SLEAP 1.3.3
    ・Python 3.7.12

    Downloading the files

    First, download the files from the SLEAP GitHub repository to your computer.

    If you are using Git, run the following command.

    git clone https://github.com/talmolab/sleap

    If the git command is not available, either install git or download the files directly from GitHub.

    Installing the GPU driver

    Install the NVIDIA driver.
    (Skip this step if it is already installed.)

    Installing Anaconda

    Next, download and install the Windows 10 64-bit version from the Anaconda website.
    Click Free Download on the Anaconda site and scroll down; you should see a screen like the one below.
    Install the Windows installer on the far left.

    Once Anaconda is installed, you should find “Anaconda Prompt” in your Windows app list.
    We will use it to run the commands below to create the virtual environment, install SLEAP, and launch it.

    Creating the virtual environment and installing

    Next, run the following commands to create the virtual environment and install SLEAP.
    The environment will be named “sleap”.

    cd sleap
    conda env create -f environment.yml -n sleap

    Note that the commands above work on computers with a GPU;
    on computers without one, use the following commands instead.

    cd sleap
    conda env create -f environment_no_cuda.yml -n sleap

    That completes the installation.

    Verifying the installation

    Activate the virtual environment and launch SLEAP with sleap-label.

    conda activate sleap
    sleap-label

    You need to activate the virtual environment (the conda activate sleap command) every time you reopen Anaconda Prompt.

    After activation, the prompt should change from (base) to (sleap).

    If SLEAP launches, the installation was successful.

    Checking version information for reporting in a paper

    To check the version, activate the sleap environment and run the following command.

    python -c "import sleap; sleap.versions()"

    This will output

    SLEAP: 1.3.3
    TensorFlow: 2.7.0
    Numpy: 1.21.5
    Python: 3.7.12
    OS: Windows-10-10.0.22621-SP0

    showing the SLEAP version information and related details.

    You can also check whether SLEAP can use the GPU with

    python -c "import sleap; sleap.system_summary()"

    which should produce output like the following when the GPU is available.

    GPUs: 1/1 available
      Device: /physical_device:GPU:0
             Available: True
           Initialized: False
         Memory growth: None

    In future posts, I hope to walk through how to actually use SLEAP.

    Bonus

    For those unsure which computer to buy, I’ve started offering PC purchase consultations on Coconala!

    あなたの要望に合わせてパソコンを選び、提案します パソコン選びに困っている方々へ!様々な目的に対応できます!

    I often pick out computers and give advice on them, and many friends have told me I could make money doing PC consultations.
    That inspired me to give it a try!

    I’ll recommend a machine that fits both what you want to use it for and your budget.
    In particular, I’ve chosen and used many computers for machine learning.

    And if you’d like, I’m happy to advise you on what to look for the next time you buy a computer.

    Machines I’ve picked out so far include lab analysis computers, everyday-use computers, computers for game streaming, computers for incoming university students, computers that can run CAD for architecture students, and simple stopgap machines.

    I use both Mac and Windows, so I can talk about and recommend either one!

    Please feel free to make use of it.

  • [Updated December 2022] How to install YOLOX

    [Updated December 2022] How to install YOLOX

    What is YOLOX?

    Among the many releases in the YOLO series, the newest one, YOLOX, came out in July 2021.

    The YOLOX paper is available here (currently a preprint).
    The GitHub repository is here.

    YOLO is an object detection algorithm.
    It is designed to run in real time, so it is extremely lightweight (inference alone can even run on a smartphone), it is easy to build systems around, and it has been adopted in many IoT devices.

    “Object detection algorithm” may sound complicated, but an easy example to picture is a camera’s automatic face or eye focus.
    A marker appears telling you “here is a face”; the same idea applies to other objects as well, and each one can be classified as to what it is.

    There are many versions in the YOLO series: YOLO, YOLOv2, YOLOv3, YOLOv4, YOLOv5, and YOLOX.
    It also runs not only on Python but on MATLAB as well, so you can adapt it to whichever platform you use.

    Unlike the system used from YOLOv3 onward, YOLOX is based on the system of the original YOLO (the first published method), and it is said to be both faster and considerably more accurate.

    Other blogs cover the details as well.

    How to install YOLOX

    Installation instructions are given on the GitHub page, but they are in English and do not cover using a virtual environment, so here I explain how to do it with an Anaconda virtual environment.

    On both Windows and Mac, following the YOLOX manual exactly produced a string of errors.
    The errors will likely keep changing with future versions, so look them up as they come up.

    See below for how to use the conda command in the macOS Terminal.

    On Windows, I recommend using Anaconda Prompt.

    Open Terminal (Anaconda Prompt), and a black window filled with text will appear.
    To the left of the last line you should see (base).
    At this point you are not inside a virtual environment.

    YOLOX will run without a virtual environment, but since environments can conflict with other packages, I recommend creating one.

    First, enter the following code to create the virtual environment.

    conda create -n YoloX python=3.8

    YoloX is the name of the virtual environment; you can use any other name.
    You can also specify the Python version with python=3.8.

    Then enter the virtual environment with the following code.

    conda activate YoloX

    If the (base) on the left has changed to (YoloX), you have succeeded.

    Installing CUDA

    First, if this is your first time installing machine learning libraries and you plan to train YOLO on a GPU, install CUDA.
    (Training on a CPU takes an absurdly long time, so I do not recommend it. Note, however, that GPUs cannot currently be used on Apple Silicon machines.)
    CUDA can be downloaded from the official site.

    As for the CUDA version, I suggest checking which CUDA version PyTorch supports and then installing that version.
    PyTorch official site

    To install an older version, click Download now and then, on the following screen, scroll down to Resources and use Archive of Previous CUDA Releases.

    The latest version may well work too, but for now, matching the version to PyTorch should let you run everything without trouble.

    To check whether it is installed on Windows, open “Edit the system environment variables,” go to Advanced > Environment Variables, and
    check whether there is a path beginning with CUDA_PATH.
    The number after the V indicates the version.

    Installing PyTorch

    Before installing, update pip.
    Enter the virtual environment in Anaconda Prompt and run the following command.

    python -m pip install --upgrade pip

    Go to the PyTorch official site and select the installation method that matches your environment.
    One thing to watch out for is the Package field: here, choose pip.
    Installing with conda will cause errors later on.

    Copy and paste the command shown under “Run this Command” to install it in your virtual environment.

    Once the installation finishes, use the following command to check that PyTorch is installed.

    pip list

    As long as torch appears in this list, you’re good to go!!!
    Just to make sure everything works, launch python and run the following command.

    import torch
    print(torch.cuda.is_available())

    If the output is True, the installation went through without problems and torch is ready to run on the GPU.
    (On a Mac or on a computer without a GPU, you will get False, but as long as the import works you’re fine.)
    If you get an error, go back and check the installation again.

    Installing the packages required for YOLOX

    Download (clone) the various YOLOX files from GitHub.
    If you can use Git commands, you can clone it with the code below.

    git clone git@github.com:Megvii-BaseDetection/YOLOX.git

    Next, navigate into the cloned folder and install the required packages.

    First, edit the contents of requirements.txt in the cloned YOLOX folder as follows.

    # TODO: Update with exact module version
    numpy
    #torch>=1.7
    opencv_python
    loguru
    tqdm
    #torchvision
    thop
    ninja
    tabulate
    
    # verified versions
    # pycocotools corresponds to https://github.com/ppwwyyxx/cocoapi
    #pycocotools>=2.0.2
    #onnx==1.8.1
    onnxruntime==1.8.0
    onnx-simplifier==0.3.5

    It seems that packages such as pycocotools cannot be installed with this command.

    Once you’ve done that, save requirements.txt and run the command below in the terminal (Anaconda prompt).

    cd YOLOX
    pip3 install -r requirements.txt
    pip3 install cython pycocotools

    The YOLOX folder you downloaded contains a file called “requirements.txt” that lists the required packages. The second command opens this file in read-only mode and installs the packages listed in it.

    pip3 install cython pycocotools

    This command installs cython and pycocotools.
    It appears these need to be installed separately.

    When you run this command, some of you may see the following error.

          building 'pycocotools._mask' extension
          error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

    Apparently pycocotools requires Microsoft C++ Build Tools. (Even though there is no Mac version, the error still shows up on Mac.)
    If you’re on Windows, it’s a good idea to install it just in case.

    Install it, then try installing again with the code below.

    pip3 install "git+https://github.com/philferriere/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI"

    If that still doesn’t work, install it with the following command.

    conda install -c conda-forge pycocotools

    In general we’ve been installing these packages with pip, but since this one simply won’t install that way, we fall back on the last resort of installing it with conda.

    Next, download yolox_x.pth.
    The download starts as soon as you click the link.

    Move the downloaded file into the YOLOX folder you cloned from GitHub.
    This gives you the model used for detection in the demo, placed wherever you like.

    Add the following lines near the top of tools/demo.py so that the script can access the yolox folder.

    #demo.pyの中身に書き込みましょう。
    import sys
    import os
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

    Without this, you will get an error saying that the yolox package cannot be found.

    Then run the following command in the terminal.
    This lets you check whether the demo runs correctly.

    python tools/demo.py image -n yolox-x -c yolox_x.pth --path assets/dog.jpg --conf 0.25 --nms 0.45 --tsize 640 --save_result --device gpu 
    #gpuで動かさない場合には、gpuをcpuに書き換えてください。

    Change –device gpu to –device cpu as appropriate for your setup.

    You should now find the following image inside YOLOX/YOLOX_outputs/yolox_x/vis_res/20……..

    Bonus

    For those who aren’t sure which computer to buy, I’ve started offering computer purchase consultations on Coconala!

    あなたの要望に合わせてパソコンを選び、提案します パソコン選びに困っている方々へ!様々な目的に対応できます!

    I often pick out computers and give advice on them, and many friends told me I could make money doing computer consultations.
    Inspired by that, I decided to give it a try!

    I’ll recommend a computer that suits what you plan to use it for and your budget.
    In particular, I’ve chosen and used many computers for machine learning.

    And if you’d like, I can also advise you on what to look for the next time you buy a computer.

    Computers I’ve picked out so far include lab analysis machines, everyday-use machines, game-streaming machines, machines for incoming university students, machines for architecture students that can run CAD, and basic no-frills machines.

    I use both Mac and Windows, so I can talk about and recommend either!

    Please feel free to make use of it.

  • How to Install DeepLabCut 2.3 [Updated December 2023]

    How to Install DeepLabCut 2.3 [Updated December 2023]

    In this post, I’ll walk through how to install DeepLabCut.

    Reference sites
    ・Japanese-language pages
    https://qiita.com/auditorycortex/items/1b3a55101cddf09553b2
    ↑Very clear. Following this alone should get you there.

    https://note.com/sakulab/n/n9caeb32d74d6
    ↑Includes screenshots

    DeepLabCut homepage
    GitHub

    How to install

    Environment
    ・Windows 10
    ・Confirmed working on NVIDIA 2060 SUPER, NVIDIA 1080 Ti, and NVIDIA 3080
    (did not work on NVIDIA 1060 SUPER)
    ・DeepLabCut 2.1
    ・Latest NVIDIA driver
    ・Anaconda3
    ・Python 3.8
    ・CUDA 11.8
    ・tensorflow-gpu 2.5

    Steps
    First, download the master files from the GitHub page.

    You can download it via “Download ZIP” under Code.

    Next, download and install the Windows 10 64-bit version from the Anaconda site.

    Install the NVIDIA driver. (Skip this if it is already installed.)

    Scroll down on the DeepLabCut site, click “DOWNLOAS CONDA FILE” on the right, and
    download DEEPLABCUT.yaml.

    Create a DeepLabCut folder somewhere on your PC (the Desktop or a directory on the C drive is recommended), and create an environment folder inside it.
    Then place the DEEPLABCUT.yaml file you just downloaded into that folder.
    This is a matter of personal preference, but I like to keep environment files in one fixed place, so this is how I do it.

    Launch Anaconda Prompt as administrator. (A terminal-like window full of white text on black, similar to the command prompt, will appear.)
    Anaconda Prompt is installed together with Anaconda, so you should be able to find it in your list of installed applications.

    Now build a virtual environment using the DEEPLABCUT.yaml file you downloaded.
    In Anaconda Prompt, type

    conda env create -f C:(DLC-GPU.yamlファイルの場所)DEEPLABCUT.yaml

    and run it.
    You can check the location of the DEEPLABCUT.yaml file in the file’s properties, so look it up there and enter it.

    When you run this command, it will download various packages.
    After that, activate the virtual environment you created.

    conda activate DEEPLABCUT

    The prompt should change from (base) to (DEEPLABCUT).
    If you get that far, you’re good for now.

    Next, install CUDA and cuDNN.

    conda install -c conda-forge cudnn

    This will download suitable versions of the CUDA toolkit and cuDNN for you.

    Then enter the following four commands to complete the installation.

    pip install numpy
    pip install deeplabcut
    pip install imgaug
    pip install torch

    Update DeepLabCut, and the installation is complete.

    pip install --upgrade deeplabcut

    Once you’ve made it this far, try launching it.

    conda activate DEEPLABCUT
    python -m deeplabcut

    Entering this will launch the DeepLabCut GUI.

    What to do when DeepLabCut won’t run on the GPU

    Sometimes you start training and think, “Huh? Isn’t this slow?”
    That’s because DeepLabCut is running on the CPU when you meant to run it on the GPU.

    A common cause is a version mismatch among CUDA, cuDNN, and tensorflow.

    Packages like tensorflow keep getting updated to newer versions,
    so things occasionally stop working.

    For the latest version information, please check DeepLabCut’s GitHub page.

    That covers the installation process.
    In upcoming posts, I’ll explain how to actually use it.

    Bonus

    For those who aren’t sure which computer to buy, I’ve started offering PC purchase consultations on Coconala!

    あなたの要望に合わせてパソコンを選び、提案します パソコン選びに困っている方々へ!様々な目的に対応できます!

    I’ve picked out and advised on computers so often that many friends told me I should charge for it.
    That inspired me to give it a try!

    I’ll recommend a machine that fits both your intended use and your budget.
    I have particular experience choosing and working with computers for machine learning.

    And if you’d like, I can also advise you on what to look for when buying a computer in the future.

    Machines I’ve helped choose so far include lab analysis workstations, everyday work computers, game-streaming rigs, laptops for incoming university students, CAD-capable machines for architecture students, and simple all-purpose computers.

    I use both Mac and Windows, so I can discuss and recommend either!

    Please feel free to make use of it.

    Addendum 1

    I’ve also written an article on how to install “SLEAP,” another behavior-tracking tool like DeepLabCut.

    SLEAP is every bit as capable as DeepLabCut, so do give it a try.