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 would like to explain what kind of tool it is.

    YORU preprint

    YORU documentation

    What is YORU?

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

    YORUのサイトより引用


    Many deep learning-based tools, such as DeepLabCut and SLEAP, have already been released.
    These tools use deep learning to track animal body parts and estimate posture (here I will call them tracking tools).

    By estimating posture, they reveal what posture an animal is in and use this 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 behaving animal.

    When we distinguish an apple from an orange, we 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, this is referred to as 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 even as the number of individuals increases

    ・Less affected by the animal’s orientation

    ・Fast analysis speed

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

    and so on.

    It overcomes a weakness of tracking tools by making multi-animal behavior analysis simple and far less computationally costly.
    It also makes it easy to analyze behaviors that are difficult to define from body part coordinates.

    Consider, for example, the actions 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 still make an accurate call even when part of the hand is occluded.

    As for robustness to increasing numbers of individuals: with a tracking tool, as the number of animals grows you must assign each body part to the correct individual in order to capture each animal’s behavior accurately, whereas YORU analyzes only the shape of each individual, so no identity assignment or part-to-individual matching is required.

    Because analysis is also fast, real-time analysis is possible—you can trigger device control when a particular behavior occurs, enabling automation of behavioral experiments.

    Disadvantages of defining behavior with object detection

    There are, of course, drawbacks as well:

    ・Cannot capture a behavioral sequence

    ・Cannot identify individuals

    ・Cannot capture unknown behaviors

    ・Cannot capture behaviors that are not visually distinctive

    ・Provides no fine-grained detail about the defined behavior

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

    Experimenters therefore need to choose the approach that best suits their situation.

    What makes YORU remarkable?

    Having covered the advantages and disadvantages of the object detection-based behavior analysis that underlies YORU, what exactly makes YORU itself stand out?

    Everything runs through a GUI

    YORU lets you perform behavior analysis without writing any code.

    The same is true of tools such as 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 look like that of 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 is not that hard if you can program to some degree, but otherwise the barrier is quite high.

    With YORU, however, you can carry out real-time analysis—and even fire external triggers—entirely without 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 that lets you choose the program used to control your external devices.

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

    You can also write your own plugins: simply turning your device’s control program into a plugin makes it easy to link that hardware to YORU’s real-time analysis.

    This makes for a remarkably user-friendly system that no other tool currently offers.

    Closing thoughts

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

    That said, as these are addressed over time, I am very much looking forward to seeing how people put it to use.

  • How to Install SLEAP [Updated February 2024]

    How to Install SLEAP [Updated February 2024]

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

    DeepLabCut is the best-known tracking tool, and I’ve written about how to install it myself in the past, while many others have posted articles about it as well.

    SLEAP, however, has relatively few articles written about it 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 covers everything from installation to usage very clearly, complete 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, a note about 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

    the one-line command that is supposed to handle environment creation, package installation, and everything else at once. I tried it on several computers, but in every case it hung indefinitely during the environment creation step and never completed.

    So in this post I’ll introduce the alternative installation method that the developers also suggest.

    Installation

    We’ll basically follow the installation instructions on the official SLEAP site.

    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’re using Git, run the following command.

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

    If the git command isn’t available, either install git or download the files directly from GitHub.

    Installing the GPU driver

    Install the NVIDIA driver.
    (Skip this step if it’s 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’ll 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’ll use it to run the commands below to create the virtual environment, install the packages, and finally launch SLEAP.

    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

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

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

    That completes the installation.

    Checking that the installation worked

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

    conda activate sleap
    sleap-label

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

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

    If it launches, the installation was successful.

    Checking version information for reporting in a paper

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

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

    This outputs:

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

    which shows the SLEAP version and related information.

    To check whether SLEAP can use the GPU, run:

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

    If the GPU is available, you’ll see output like the following.

    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 who aren’t sure which computer to buy, I’ve started offering PC purchase consultations on Coconala!

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

    I often pick out computers and give advice about 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 intended 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 that can run CAD for architecture students, and simple entry-level machines.

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

    Please feel free to make use of it.

  • [Updated December 2022] How to Install YOLOv5 [Object Detection Tool]

    [Updated December 2022] How to Install YOLOv5 [Object Detection Tool]

    What is YOLOv5?

    YOLO is an object detection algorithm. The name stands for You Only Look Once.
    It differs somewhat from object tracking: it detects objects and identifies what they are.

    YOLOのHPより引用(https://pjreddie.com/darknet/yolo/)

    In applications such as autonomous driving, objects are detected and classified the way a human would judge them—as a person, a pet, or a car, for example.

    The classification algorithm itself is complex, so I will skip the explanation here, but a quick search turns up a great many sites that describe it.

    New versions of YOLO are currently released roughly every other year. We now have YOLO v1 through YOLOv5, and in August 2021 a new version, YOLO X, was announced.
    The differences between versions involve substantial changes in the details, but generally speaking accuracy improves with each new release.

    Here I will walk through the steps for installing YOLO v5—which offers ample accuracy and, thanks to the wealth of information now available, is easy to set up—and verifying that it works.

    Installation environment

    OS: Windows 10 (this also worked on macOS Monterey)
    Python: 3.9.7 (3.6 or later)
    CUDA: 11.3

    How to install

    Creating a virtual environment

    Create a virtual environment using Anaconda.
    If you do not have Anaconda installed, install it first.

    Installing YOLOv5 pulls in quite a few packages, so I recommend installing it inside a virtual environment.
    You can name the environment anything you like; here we will call it “Yolov5_env”.
    Enter the following command in Anaconda Prompt.

    conda create -n Yolov5_env python=3.9

    This should create the virtual environment.
    Activate it with the following command.

    conda activate Yolov5_env

    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.)
    CUDA can be downloaded from the official site.

    As for which CUDA version to use, I suggest checking which CUDA versions PyTorch supports and installing that version.
    PyTorch official site

    To install an older version, click Download now and then, on the resulting page, use Archive of Previous CUDA Releases under Resources further down the page.

    The latest version may well work too, but for now, matching the version PyTorch expects 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 look for a path beginning with CUDA_PATH.
    The number after the V indicates the version.

    Installing PyTorch

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

    python -m pip install --upgrade pip

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

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

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

    pip list

    If torch appears in the list, you are good to go!
    As a quick check, start Python and run the following command.

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

    If the output is True, the installation succeeded and torch is ready to run on the GPU.
    (On a Mac or a computer without a GPU you will see False, but as long as the import goes through, you are fine.)
    If you get an error, go back and review the installation.

    Installing YOLOv5

    Download (clone) the YOLOv5 files from GitHub.

    git clone https://github.com/ultralytics/yolov5

    If you cannot use the git command, either install git or download the files from the YOLOv5 GitHub repository.

    Next, run the following command in your virtual environment in Anaconda Prompt.

    cd yolov5
    pip install -r requirements.txt

    The first command moves you into the yolov5 folder you downloaded from GitHub. If you installed it with the git command, this is all you need; if you downloaded it directly from the website, you will have to navigate to that directory yourself.

    The downloaded yolov5 folder 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.

    Once the installation is finished, let’s check which packages were installed.

    pip list 

    You should see that quite a few packages were installed.
    That completes the installation of YOLOv5 for now.

    Running It

    To check that everything works, let’s try it with the data that comes with the yolov5 folder.
    Open Anaconda Prompt, activate the virtual environment, and use the cd command to move to the yolov5 folder.

    Then run the following command.

    python detect.py --source ./data/images/ --weights yolov5s.pt --conf 0.4

    detect.py is the program that performs object detection using YOLO.
    With source, you specify the path to the folder containing the material you want analyzed.
    Here I used the images included in yolov5.

    With weights, you choose which model to use.
    Here we use yolov5.pt.

    conf sets the likelihood threshold at which an object is recognized.
    This is a term you will run into often when studying machine learning.
    If you are not sure, leaving it as is should be fine.

    After running it, you should find the annotated images in runsdetectexp inside the yolov5 folder.
    This is how object detection works in YOLOv5.

    Errors I Ran Into

    ・ModuleNotFoundError: No module named ‘torch’

    You may see this error when trying to run YOLO.
    It means PyTorch is not available, i.e., the module cannot be found.

    If you get this error, first check whether torch is installed in your virtual environment.

    pip list

    If torch is not listed, install PyTorch.
    If it is there but things still do not work and you are not sure why, launch python, import PyTorch, and check whether it can actually be used.

    import torch
    torch.cuda.is_acailable()

    If it is usable, True will be returned; if not, you will likely see the error above.

    One possible cause of this error is that PyTorch was installed with the conda command.
    If PyTorch is installed via conda while the other packages are installed via pip, the error above can occur.

    In that case, uninstall torch with the conda command and reinstall it with pip.

    Module-not-found problems often come from mixing up whether you installed something with pip or with conda.
    Here we use pip throughout, but it is a good habit to always keep track of which command you used to install a package.

    Links

    Anaconda
    CUDA
    PyTorch
    YOLOv5 GitHub
    YOLO homepage

    Bonus

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

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

    I often pick out computers and give advice, 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 based on 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 analysis machines for the lab, everyday machines, PCs for game streaming, PCs for incoming university students, CAD-capable PCs for architecture students, and simple all-purpose machines.

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

    Please give it a try!

  • How to Install labelImg [Updated December 2022]

    How to Install labelImg [Updated December 2022]

    When doing object clustering, you may sometimes want to use your own trained model rather than just the models that already exist.

    YOLO is a well-known tool for object clustering, and labelImg (written LABELIMG in capitals) is extremely useful when creating the training data for it.

    Here I introduce how to install labelImg.

    Environment

    OS: Windows 10 (it also worked on macOS Monterey)
    Python: 3.9.7 (3.6 or later)

    How to install

    I recommend creating a virtual environment with Anaconda and installing it there.
    That way you can install it without affecting your other environments.

    The name of the virtual environment can be anything as long as you can recognize it, but here we will use “labelimg_env”.
    Run the following command in the Anaconda prompt.

    conda create -n labelimg_env python=3.9

    Here I am installing with Python 3.9, but any version from 3.6 onward should be fine.

    Next, update pip and setuptools.
    Doing this should prevent mysterious errors (probably).

    python -m pip install --upgrade pip setuptools

    Then install labelImg.

    pip install labelImg

    You can check whether it was installed with the following command.

    pip list

    If labelImg appears in the list, the installation was successful.

    Launching

    First, activate the virtual environment.

    conda activate labelimg_env

    Then launch labelImg.

    labelImg

    If a separate window opens, you’re all set!!

    Afterword

    LabelImg’s GUI is built with PyQt5.
    The reason Python 3.6 or later is preferable is that the current version of PyQt5 requires Python 3.6 or later.

    In general, the latest version should work fine.

  • Using Conda Commands on macOS

    Using Conda Commands on macOS

    Here I’ll walk through installing Anaconda on macOS and setting up the terminal environment.

    Installing Anaconda

    Go to the Anaconda homepage and scroll down to find the downloads.

    Choose the installer that matches your environment.
    Since this guide covers macOS, select the 64-bit Graphical Installer.

    Launch the installer and follow the instructions to complete the installation.

    There are two installers for Mac. The 64-bit Graphical Installer installs Anaconda through a GUI, which is the more familiar approach, and unless you have a reason to do otherwise, you can install Anaconda this way.

    The 64-bit Command Line Installer downloads a *.sh file.
    This is a shell script installer, used when you want to install from the terminal.

    Since installing software on Linux is normally done from the terminal, people who are used to Linux may find this route easier. (Probably.)

    Setting up the command line

    When you install on macOS, all that appears in your applications list is Anaconda-Navigator, and as it stands you can’t use Anaconda or Python from the terminal.

    If you want to use the conda command, for example to install packages, you need to activate Anaconda.
    To activate Anaconda (that is, to make the conda command available):

    conda activate

    Run the above.
    The conda command should now work.

    If activating the conda environment every time is a hassle, you can have it activate automatically.
    To do so, run the following in the terminal:

    ~/opt/anaconda3/bin/conda init シェル名

    Use the name of the shell you’re actually using. If you have no idea and have never touched this setting, you’re most likely on the default shell (zsh on macOS), so substitute that for the shell name.

    ~/opt/anaconda3/bin/conda init zsh

    Now, when you restart the terminal, the conda environment will be activated automatically and you won’t need to type conda activate.

    To turn off automatic activation,

    conda config --set auto_activate_base false

    run the following in the terminal.

  • Git: The Ultimate Tool for File Version Control — A Beginner’s Guide

    Git: The Ultimate Tool for File Version Control — A Beginner’s Guide

    What is Git?

    Many of you probably knew about GitHub before you ever heard of Git.
    When looking for a program or piece of software to reference, GitHub pages often come up in search results, and I suspect plenty of people have simply downloaded something from there without really understanding how it works.

    GitHub is indeed a place where programs are shared, but it is more than just a sharing platform—it is a tool for using something called Git online.

    Git is a tool for version control of files.
    By recording the changes you make to your files, it lets you return to any recorded version at any time.

    When you have someone review a presentation manuscript, you probably save the file before review, the reviewed file, and the file you revised based on the comments as separate files.
    With Git, you can handle all of those changes easily, within a single file.

    You can find Git here

    The three places in Git

    A record made with Git—and the act of making that record—is called a “commit”.

    The place where commits accumulate is called a “repository”.
    In other words, a repository is where the change history is kept.
    A repository on your own computer is called a “local repository”, while one hosted remotely, such as on GitHub, is called a “remote repository”.

    To manage files with Git, you designate a folder to be managed by Git.
    Within that folder there are three places:
    the working tree, the staging area, and the Git directory.

    The working tree is where your files live, and simply editing a file here does not yet save the change history as a commit.

    The staging area is where you register the files to be committed.
    Files on the stage likewise have not had their change history saved yet; think of it as the place where you declare, “I am going to commit this file.”

    The Git directory is where commits are stored.
    Files committed here are stored as files that will not be altered.
    In principle, the content recorded by a commit cannot be changed or deleted afterward.
    Once committed to the Git directory, a change is officially recorded in the history.

    Basic usage of Git

    I will leave the detailed usage for you to look up, but here I will give a rough overview of the actual operations.

    Git is generally operated through Git Bash on Windows (installed together with Git) and through the terminal on macOS and Linux. (I will refer to both simply as the terminal here.)

    There are GUI tools that may feel more familiar, but once you get used to it, using Git from the CUI is extremely convenient, so I recommend starting with the CUI from the outset.
    You should get the hang of it within an hour.

    First, to start managing files with Git, create a local repository.
    Begin by moving to the directory you want to manage with Git in the terminal.
    Then,

    git init

    Entering this sets you up for version control with Git.

    Next, to record changes in the staging area,

    git add ファイルパスもしくはディレクトリパス

    Running this registers the specified file in the working tree to the staging area.

    Next,

    git commit

    This commits the files in the working tree.

    To check which files are currently in which state, run

    git status

    Run this command.
    The commit history can be checked by

    git log

    running this command.

    Basically, the workflow is to run git add, then git commit to create a change record, and use git status to check the current state whenever you run into trouble.

    Below is a reference.
    It is a book that is very easy to follow even for beginners, and I referred to it while writing this article.
    If you would like to study this in more depth, I recommend it.

  • I Don’t Really Know Much, But I Want to Program in Python! (1)

    I Don’t Really Know Much, But I Want to Program in Python! (1)

    When you start learning programming, I think the first hurdle is figuring out where to begin.
    You search online, but then what? What software are you even supposed to use to write your code?

    I struggled with exactly that myself.

    Broadly speaking, there are two kinds of tools for writing programs.
    One is interactive, and the other is script-based. (I’m putting it this way for clarity. I’m not entirely sure it’s the correct terminology, though…)

    With the interactive type, you type one line and get one response back
    —you write and run the program one line at a time, over and over.

    With the script type, it’s like writing a whole essay and then getting feedback on it
    —you run the entire program at once.
    This script type is what most people picture when they think of programming: page after page of cryptic-looking code.

    The problem is, when you set out to write a script-type program, what software should you actually use?

    On top of that, beginner-level programming lessons often use the interactive style, but at the intermediate level you’re suddenly writing script code, and what to write it in is either left unstated or differs completely from book to book.

    For the interactive style, you use something already built into your computer, like Command Prompt or Terminal, and get started by typing “Python”.

    But what do you write script-type programs in?

    Most script-type programs are written in an IDE (integrated development environment). (When in doubt, searching “Python IDE recommended” will turn up plenty of articles.)

    The tools I normally use for writing script-type programs are
    Jupyter Notebook and PyCharm.
    (Apparently people are split on whether “Jupyter” is pronounced “joo-pih-ter” or “joo-py-ter”.)

    I use Jupyter Notebook for statistical analysis and plotting graphs, and PyCharm for driving hardware or writing more complex programs.

    Let me describe what each one is like.

    First, Jupyter Notebook.
    It comes bundled when you download Anaconda.
    When you launch Jupyter Notebook, a browser such as Safari or Firefox opens first.

    A list of the files on your computer is then displayed.
    Open the folder you want, and click “New” in the upper right.
    Then click “Python 3” and a screen like the image below will open.
    Saving this file gives you a *.ipynb file.

    You write your code in individual cells, and by clicking the RUN button at the top you can execute the program cell by cell.

    Being able to run code one cell at a time is a huge advantage when making graphs or doing statistics.
    That’s why I use it so often.

    I use PyCharm when opening *.py files.
    Note that the two tools open different file types, so be careful.

    For PyCharm, please refer to other sites.
    (I haven’t used it much yet, so I plan to cover how to use it in detail in a future post.)

    The Anaconda site is here

    The Jupyter Notebook site is here

    The PyCharm site is here
    For the download, choose the gray Community edition rather than Professional—that’s the free one.
    I’d recommend starting with that.

    I’ve been using Jupyter Notebook a lot lately, so I plan to keep posting updates, both as notes on my own learning and for anyone who wants to learn to program.