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 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, YOLO detects and classifies objects the way a human would judge them—deciding whether something is a person, a pet, or a car.

    The classification algorithm itself is complex, so I will not go into it here, but a quick search will turn up a great many sites that explain it.

    New versions of YOLO have been appearing at a pace of roughly once every two years. So far we have had 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 as a rule accuracy improves with each new release.

    In this post I will walk through the steps for installing YOLO v5 and verifying that it works—a version that offers sufficient accuracy and, now that plenty of information is available, is easy to get up and running.

    Installation environment

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

    How to install

    Creating a virtual environment

    We will 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 screen that appears, use Archive of Previous CUDA Releases under Resources near the bottom of the page.

    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 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 select the installation options that match your environment.
    One thing to watch out for is the Package field: be sure to choose pip here.
    If you install with conda, you will run into errors later on.

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

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

    pip list

    If torch appears in the list, you are all set!
    As a quick sanity check, launch python and run the following commands.

    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 on 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 will work as is; 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 necessary 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 will probably see that quite a few packages were installed.
    With that, the YOLOv5 installation is complete 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 into 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 to run detection on.
    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 is done 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 don’t work and you’re not sure why, start Python, import PyTorch, and check whether it is usable.

    import torch
    torch.cuda.is_acailable()

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

    One possible cause of the 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 arise when you have mixed up pip and conda when installing.
    Here I have used pip throughout, but it’s a good idea to always keep track of which command you used to install something.

    Links

    Anaconda
    CUDA
    PyTorch
    YOLOv5 GitHub
    YOLO homepage

    Bonus

    For those who aren’t sure which computer to buy, I’ve decided to offer 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.
    Inspired by that, I figured I’d give it a try!

    I’ll suggest a purchase that fits how you plan to use the computer and your budget.
    In particular, I’ve chosen and used many computers for machine learning.

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

    Computers I’ve picked out so far include analysis machines for the lab, everyday-use machines, PCs for game streaming, PCs for incoming university students, PCs capable of running CAD for architecture students, and simple stopgap 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, there are times when you want to use not only the pre-existing trained models but also a model you built yourself.

    YOLO is a well-known tool for object clustering, and labelImg (written in all caps as LABELIMG) 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)

    Installation

    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 recognize it, but here we’ll use “labelimg_env”.
    Run the following command in Anaconda Prompt.

    conda create -n labelimg_env python=3.9

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

    Next, let’s update pip and setuptools.
    Doing this prevents 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, you’re all set.

    Launching

    First, activate the virtual environment.

    conda activate labelimg_env

    Then launch labelImg.

    labelImg

    If a separate window opens, you’re good to go!

    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.

    Generally, the latest version should work without any problems.

  • 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 specific reason otherwise, this is the one to use.

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

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

    Configuring the command line environment

    After installing on macOS, all you get is Anaconda-Navigator in your applications list — as is, you can’t use Anaconda or Python from the terminal.

    If you want to use the conda command to install packages and so on, you need to activate Anaconda.
    To activate Anaconda (i.e., 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 シェル名

    Run the above.
    Use the name of the shell you actually use. If you have no idea what that means and have never changed it, you’re probably 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 above 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 have probably heard of GitHub before you ever heard of Git.
    When looking for a program or a piece of software to use as a reference, you often land on a GitHub page, and I suspect many of you have simply downloaded something without really understanding how the site works.

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

    Git is a tool for version control of files.
    By modifying files and recording those changes, you can return to any recorded version at any time.

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

    The link to Git is here

    The three areas 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 the place that holds the change history.
    A repository on your own computer is called a “local repository”, while one hosted remotely, such as on GitHub, is called a “remote repository”.

    When you want to manage files with Git, you specify the folder that Git will manage.
    Within that folder there are three areas:
    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 never be altered.
    In principle, the content recorded by a commit cannot be changed or deleted afterward.
    Once something is committed to the Git directory, it has been recorded as part of the change history.

    Basic use of Git

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

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

    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 beginning.
    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 Git to manage in the terminal.
    Then,

    git init

    Entering this gets you ready to use Git for version control.

    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 file is currently in which state,

    git status

    run this.
    The commit history can be

    git log

    checked by running this.

    Basically, the workflow is to run git add, then git commit to create a change record, and to 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 want to study this in more depth, I recommend it.

  • I Don’t Really Know What I’m Doing, But I Want to Program in Python! (Part 1)

    I Don’t Really Know What I’m Doing, But I Want to Program in Python! (Part 1)

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

    I struggled with exactly that myself.

    There are broadly 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 technically correct terminology…)

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

    The script-based type is more like writing a whole essay and then getting feedback on it—you run the entire program at once.
    What most people picture when they think of a program is the script-based type, which looks like a long list of incomprehensible code.

    But when you decide to write a script-based program, the problem becomes: which software do you actually use?

    On top of that, beginner-level programming lessons often use the interactive style, and then at the intermediate level you’re suddenly writing scripts—yet what to write them in is often 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-based programs in?

    Most script-based programs are written in an IDE (integrated development environment). (When you’re stuck, searching for “Python IDE recommendations” turns up plenty of articles.)

    The ones I normally use for writing script-based programs are
    Jupyter Notebook and PyCharm.
    (Apparently some people pronounce “Jupyter” as “joo-pih-ter” and others as “joo-py-ter”.)

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

    Let me explain 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.

    Then a listing of the files on your computer appears.
    Open the folder you want, and click “New” in the upper right.
    Then click Python3, and a screen like the image below opens.
    Saving this file gives you a *.ipynb file.

    You write code into individual cells, and by clicking the RUN button at the top you can execute the program one cell at a time.

    Being able to run code cell by cell is a huge advantage when working with graphs and statistics.
    That’s why I use it so often.

    I use PyCharm for opening *.py files.
    Note that the two tools open different types of files.

    For PyCharm, please refer to other sites.
    (I haven’t used it much yet, so I’d like 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 downloading that one to start with.

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