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.

  • From GitHub Basics to Publishing Your Code Alongside a Paper

    Here are the slides from a talk I gave on how to use Git and GitHub.

    The slides cover everything from hands-on operations in GitHub to the steps needed to make your program code available alongside a published paper.

    Sharing paper data and analysis code is increasingly expected these days.
    Beyond that, managing code with GitHub has become an essential part of working on collaborative research.

    [Updated January 15, 2024] https://www.slideshare.net/slideshow/embed_code/key/vzfmHD9i6I15wI

    From GitHub basics and code management to publishing your program code with a paper from Hayato Yamanouchi

    The same material is also posted on my personal site.

    Hayato M. Yamanouchi’s personal site

  • 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.

  • My elif conditional branching isn’t working!! [Python]

    My elif conditional branching isn’t working!! [Python]

    Have you ever wanted to branch on multiple conditions with an if statement, tried using elif, and found that although no error was raised the results still weren’t right?
    If so, the situation described below might be what’s happening.
    I made this mistake myself, so I’m writing it down here for reference.

    The problematic program

    The problem arises when you write two conditions in the if and elif statements of a program that includes elif.

    def trable(a, b):
        if a >= 10 & b >= 10:
            print("patern A")
        elif a >= 10 & b < 10:
            print("patern B")
        elif a < 10 & b >= 10:
            print("patern C")
        else:
            print("patern D")
    
    trable(11, 11)
    trable(11, 9)
    trable(9, 11)
    trable(9, 9)

    When you run the program above, you might expect the cases to be sorted into patterns A, B, C, and D in order from the top, but what you actually get is the output below.

    patern A
    patern B
    patern C
    patern B

    So what happens if we swap the order?

    def trable(a, b):
        if a >= 10 & b >= 10:
            print("patern A")
        elif a < 10 & b < 10:
            print("patern B")
        elif a >= 10 & b >= 10:
            print("patern C")
        else:
            print("patern D")
    
    trable(11, 11)
    trable(9, 11)
    trable(11, 9)
    trable(9, 9)

    If you swap the code for patterns B and C, expecting the output to come out as A, B, C, D, what you actually get is the output below.

    patern A
    patern D
    patern D
    patern D

    elif itself is used as follows when you want to define multiple conditions.

    if 条件式A:
      条件式Aが真(True)となった場合の処理
    elif 条件式B:
      条件式Aが偽(False)で、条件式Bが真(True)となった場合の処理
    else:
      条件式Aが偽(False)で、条件式Bも偽(False)となった場合の処理

    However, once the conditional expressions in the if and elif statements contain two conditions, the problem described above is likely to occur.
    As a result, you don’t get the output you intended.

    The tricky part is that no error is raised, so you can’t tell whether things are working until you actually look at the results.

    How to fix it

    When you want to branch into multiple cases using compound conditions, avoid specifying multiple conditions in the elif statement.

    def resolve(a, b):
        if a>= 10:
            if b >= 10:
                print("patern A")
            else:
                print("patern B")
    
        else:
            if b >= 10:
                print("patern C")
            else:
                print("patern D")
    
    resolve(11, 11)
    resolve(11, 9)
    resolve(9, 11)
    resolve(9, 9)

    It’s more cumbersome, but doing it this way gives you exactly the output you expect.

    Programming has unexpected pitfalls like this, so it’s a good lesson in carefully checking the code you write.

    Addendum (August 2022)

    It turns out the problem was that I hadn’t wrapped the conditions in parentheses.

    def trable(a, b):
        if (a >= 10) & (b >= 10):
            print("patern A")
        elif (a < 10) & (b < 10):
            print("patern B")
        elif (a >= 10) & (b >= 10):
            print("patern C")
        else:
            print("patern D")

    With this change, the branching worked correctly.
    Alternatively, you can also fix it by writing and instead of &.

    def trable(a, b):
        if a >= 10 and b >= 10:
            print("patern A")
        elif a < 10 and b < 10:
            print("patern B")
        elif a >= 10 and b >= 10:
            print("patern C")
        else:
            print("patern D")

    & and and may seem equivalent, but & also acts as a bitwise AND, so in the original program

    a >= 10 & b < 10

    this apparently means a >= (10 & b) < 10, and
    with a=9, b=9 the inequality becomes
    9>=8<10, which evaluates to True.

    Tricky stuff…

  • [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.

  • Changing Pulse Wave Frequency and Duty Cycle Using PWM Output [Arduino UNO]

    Changing Pulse Wave Frequency and Duty Cycle Using PWM Output [Arduino UNO]

    What is PWM output?

    PWM stands for pulse width modulation. It is a method of modulating a signal by changing the duty cycle of the waveform.

    For details, see my previous post.

    PWM output on Arduino

    The Arduino UNO uses a Microchip microcontroller called the ATmega328 as its main chip.
    Once you start digging deep into Arduino programming, you inevitably end up having to read the microcontroller’s datasheet, so it is worth taking a look at least once. (Which is exactly the situation I’m in now.)

    The Arduino has three timers (Timer/Counter).
    These timers govern timing in Arduino programs.
    Functions such as delay() and tone() rely on them for their measurements.

    Timer/CounterPin numberBit widthRolePWM frequency
    Timer05, 68 bitManages Arduino timing
    delay(), millis(), micros(), etc.
    977 Hz
    Timer19, 1016 bitServo library, etc.490 Hz
    TImer23, 118 bittone(), etc.490 Hz

    This time we will change the PWM output frequency by manipulating these timers.
    Incidentally, since this alters the timers at their core, you might also be able to tweak functions like delay() in useful ways. (Though it seems more likely that they would simply be thrown off and behave erratically.)

    Because TImer0 generally affects the system as a whole, I recommend using TImer1.

    Useful references
    https://playground.arduino.cc/Main/TimerPWMCheatsheet/
    https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM
    https://atooshi-note.com/arduino-1hz-pwm/
    http://blog.kts.jp.net/arduino-pwm-change-freq/
    http://garretlab.web.fc2.com/arduino/inside/hardware/arduino/avr/cores/arduino/wiring_analog.c/analogWrite.html

    Overview of the program

     The overall approach is to change the Arduino timer’s register settings so that the PWM output frequency can be set freely.

    The goal is to be able to output low frequencies, so the program is written to produce 10 Hz.

    Here I connect an LED to pin 10 and write a program that lets you freely change the frequency and duty cycle of its blinking.
    Since we are using pin 10, we work with TImer1.

    Program code

    //レジスタの設定を変えるためのもの
    #include <avr/io.h>
    int PWMPin = 10;
    
    //関数の定義
    //frq:周波数 (1Hz~指定できる)
    //duty:指定したいduty比
    void HzWrite(int frq, float duty) { 
    
        // モード指定
      TCCR1A = 0b00100001;
      TCCR1B = 0b00010100; //分周比256を用いる
    
      // TOP値指定
      OCR1A = (unsigned int)(31250 / frq);
    
      // Duty比指定
      OCR1B = (unsigned int)(31250 / frq * duty);
    }
    
    
    void setup() {
      pinMode(PWMPin, OUTPUT);
    }
    
    void loop() {
      HzWrite(10, 0.5);
      delay(5000);
      digitalWrite(PWMPin, LOW);
      delay(5000);
    
    }

    Explanation of the program

    First, include <avr/io.h> in order to change the register settings.

    #include <avr/io.h>

    Next, to build a function that works together with delay() to repeat a 10 Hz output every five seconds, we define a function called HzWrite. Its arguments allow you to specify the frequency and duty cycle.

    void HzWrite(int frq, float duty) { 
    
    }

    Next comes setting the mode.
    The registers used here are TCCR1A/TCCR1B. (TCCR: Timer/Counter Control Register)
    The “1” indicates TImer1; if you want to use Timer2, use TCCR2A/TCCR2B instead.

    To set the PWM frequency to a specific value in Hz, you need to specify the TOP value yourself.
    The larger the TOP value, the lower the output frequency.
    Here I use 10 Hz as an example. Since this is very slow compared with the 16 MHz system clock, a large TOP value and a large prescaler are required. For this reason, we use Timer1, which offers the largest range.


    With the 8-bit Timer0 and Timer2, the maximum TOP value is 255 (2^8 – 1), while with the 16-bit Timer1 it is 65535 (2^16 – 1).
    (The reason for the -1 is that the range is 0–255 or 0–65535: the number of values is 2^x, but the maximum value must be one less.)

    In terms of how the system works, the counter increments up to the TOP value (counting 0, 1, 2, …), and when it matches OCRxA/OCRxB (where x is the timer number; each timer has two output pins, A and B, assigned to it), the pin output changes state (e.g., LOW→HIGH). Once the counter reaches the TOP value, it then decrements back down to 0 (counting down 65535, 65534, 65533, …), and just as during the increment phase, the pin output changes state when the count matches OCRxA/OCRxB.

    On the Arduino UNO, the speed at which this counter increments can be changed to some extent by changing the prescaler setting. (“To some extent” means you can choose from 1/8/64/256/1024.)
    The prescaler is the ratio (n) used when dividing the frequency (multiplying the frequency by 1/n).
    In other words, dividing 1000 Hz by a prescaler of 10 gives 100 Hz.

    Incidentally, with a prescaler of 1 the timer runs at 16 MHz, the system clock of the Arduino UNO (ATmega328).

    In short, by changing the TOP value, the OCRxA/OCRxB values, and the prescaler, you can freely set the points at which the output switches.

    Since we want 10 Hz here, we use a prescaler of 256 to leave plenty of margin.
    On the Uno, the clock is 16 MHz, so one count takes 1 / 16 MHz = 62.5 ns (prescaler 1).
    With a prescaler of 256, counting all the way to TOP takes 62.5 ns x 256 x 65535 = 1.04856 s, so frequencies as low as 1 Hz can be specified.

    This program can generate frequencies from 1 Hz up to 31250 Hz.
    However, as you approach 31250 Hz it becomes impossible to set the duty cycle precisely.
    If you want fine control over the duty cycle, you can only go up to about 300 Hz.

    By changing the prescaler setting in this program, you can build a version that produces the frequency range suited to your own application.

    Next, we write what to set in TCCR1A/TCCR1B.
    The fine details here are rather complicated, so let’s just work through the datasheet and set them roughly.

    Here the values are given in binary, so they begin with 0b. In TCCR1A you set COM1A1, COM1A0, COM1B, unused, unused, WGM11, WGM10 to 1 or 0.
    In TCCR1B you set unused (ICNC1), unused (ICES1), unused, WGM13, WGM12, CS12, CS11, CS10, respectively.

    TCCR1Aの指定(ATmega328データシートより)
    TCCR1Bの指定(ATmega328データシートより)

    First, here we select Mode 9, whose PWM mode is Phase and Frequency Correct.
    In this case the TOP value is set in OCR1A.

    モードの指定(ATmega328データシートより)

    Therefore, WGM13 / WGM12 / WGM11 / WGM10 are 1, 0, 0, 1, respectively.

    出力の指定(ATmega328データシートより)

    For COM1B1 / COM1B0: 0, 0 means no output; 0, 1 means toggle operation (the output is inverted on a compare match);
    1, 0 outputs LOW while the counter is between OCR1A/B and TOP, and HIGH while it is between 0 and OCR1A/B;
    1, 1 is the inverse of 1, 0.

    Here, since we want to switch the output LED between LOW and HIGH to produce the frequency, we set COM1B1 / COM1B0 to 1, 0.

    We choose 1, 0 because it makes the sketch easier to follow.

    分周比の指定(ATmega328データシートより)

    Since we are using a prescaler of 256 here, CS12/CS11/CS10 are set to 1, 0, 0.

    To summarize, we get the following.

    TCCR1A = 0b00100001;
    TCCR1B = 0b00010010;

    Next we set OCR1A and OCR1B so that the output is generated with the specified frequency and duty cycle.

      // TOP値指定
      OCR1A = (unsigned int)(31250 / frq);
    
      // Duty比指定
      OCR1B = (unsigned int)(31250 / frq * duty);

    When using Phase and Frequency Correct PWM, the counter both increments and decrements, so the output frequency can be expressed as follows.

    Frequency frq = IC clock frequency / (prescaler * TOP value * 2)

    Conversely, to obtain the TOP value:

    TOP value = IC clock frequency / (prescaler x frq x 2)

    For the Arduino UNO with a prescaler of 256:

    TOP value = OCR1A = 16,000,000 / (256 x frq x 2) = 31250 / frq

    Since we want the timing at which LOW and HIGH switch to correspond to OCR1A/OCR1B = duty cycle,

    OCR1B = 31250 / frq x duty

    An unsigned int is used to prevent overflow.

    Here is the main part of the output code.

    void setup() {
      pinMode(PWMPin, OUTPUT);
    }
    
    void loop() {
      HzWrite(10, 0.5);
      delay(5000);
      digitalWrite(PWMPin, LOW);
      delay(5000);
    
    }

    PWMPin, i.e. pin 10, is set as OUTPUT, and the frequency and duty cycle are specified with HzWrite().
    After waiting with delay(), the output is turned off with digitalWrite(PWMPin, LOW), followed by another delay().

    That covers the full program and its explanation.

    Afterword

    When looking for blog posts on how to change the PWM output frequency, I found many more results by searching for AVR, ATmega328, or 328P than by searching for Arduino.

    This post was only a rough overview, so if you want to dig deeper, I encourage you to look into it yourself.

  • How to generate continuous pulse waves with Arduino

    How to generate continuous pulse waves with Arduino

    Introduction

    There are times when you want to output a continuous pulse wave with an Arduino: blinking an LED, generating a sound, using it as a timer, and so on.

    It comes up often and seems simple at first glance, but once you dig into it you find it is surprisingly deep.

    In this post, I introduce several ways to output a continuous pulse wave with an Arduino.

    Changing the timing with delay

    The simplest and easiest approach is to switch the output ON and OFF using the delay function.

    //pinはピン番号
    void loop(){
        digitalWrite(pin, HIGH);
        delay(1000);
        digitalWrite(pin, LOW);
        delay(1000);
    }

    In the program above, the output is toggled between HIGH and LOW.
    Since delay is specified in milliseconds, delay(1000) waits for one second.

    In other words, this program turns the output on and off at 1 Hz.

    However, using the delay() function to set the frequency has a number of drawbacks.
    With this approach, changing the output duration—for example, outputting a 60 Hz signal for five seconds—requires a for loop, which is inconvenient.

    That said, because it is so simple, it is a good choice when you just want to try something out.

    Using tone()

    The tone() function is commonly used to generate buzzer sounds and the like.
    Official Arduino reference

    This function lets you specify the frequency and the duration.
    So, unlike the delay approach, you can specify the frequency directly without calculating it.

    //pinはピン番号
    void loop() {
         tone(pin,60);
    }

    You can write it as tone(pin, frequency) or tone(pin, frequency, duration).

    The duration is given in milliseconds, so it is written the same way as delay.

    The limitation of this function is that you cannot specify frequencies of 31 Hz or below.
    In other words, you cannot generate an output such as 1 Hz.

    For frequencies above 31 Hz, such as audio tones, it makes specifying the frequency extremely easy, and the code is far shorter and more accurate than using the delay function.

    Using PWM output and changing its frequency

    Using PWM output offers the greatest flexibility—and it is also why this topic gets so deep.

    PWM stands for pulse width modulation.
    For details, see Wikipedia.

    The term alone does not tell you much, but simply put, PWM is a way of modulating an output by changing the duty ratio.
    The official Arduino explanation is here.

    Normally, you would set the brightness of an LED by changing the current. But if the current is fixed and you still want to change the brightness, you can blink the LED at a very high frequency (30–60 Hz is said to be the point where people can no longer perceive the flicker).

    Normally the ON and OFF periods are 1:1 (a duty ratio of 50%), but what happens if you make it 4:1 (80% duty) or 1:4 (20% duty)?
    The former will look bright, and the latter will look dim.

    Modulating the output by changing the pulse width in this way is what PWM output is all about.

    On the Arduino you can not only generate this output but also change the frequency of the PWM output.
    The idea here is that by tweaking the register settings—essentially the low-level guts of the Arduino—you can change the PWM output frequency.
    Incidentally, the default output frequency is 490 Hz, or 980 Hz on some pins.

    I will explain how to do this in detail in a future post.
    Searching for “PWM Arduino frequency change” turns up plenty of explanations.

    After reading them about four times, it starts to make sense.

    Basically, I suggest looking through these methods to find the one that best fits your own goal.

  • How to Install Python Video Annotator

    How to Install Python Video Annotator

    What is Python Video Annotator?

    Python Video Annotator is an application that lets you analyze recorded videos and annotate events within them along a timeline.

    Researchers in neuroscience and ethology can use it to record videos of animals and then analyze and quantify their behavior.
    For example, suppose you are recording mouse behavior and want to score actions such as sticking out the tongue, wagging the tail, or moving the ears.
    When in the video does each action occur, and how long does it last?
    Watching the video and logging everything in Excel each time becomes overwhelming once you have defined many behaviors.
    With a tool like this, you can record annotations directly on the video and export the timing and event information.

    There used to be an open-source application called VCode for recording animal behavior.
    The problem, however, was that it no longer runs on current computer operating systems.

    Python Video Annotator works on modern PCs and retains the essential features, while also allowing you to combine it with external sensor data (such as pressure gauges) and behavior-quantification tools like DeepLabCut, making it an extremely useful tool for researchers.

    How to install

    The official website describes the installation procedure in detail, but I could not get it to install properly on my computer. (Installing it directly may have caused conflicts with packages that were already installed.)
    So instead I followed the approach described on the GitHub page, building a virtual environment with Anaconda and installing it there.

    It sounds complicated when described in words, but the steps are very simple.
    As of now (October 20, 2021) it does not appear to support the latest macOS (Big Sur 11.6), though this will probably be fixed soon.
    For that reason, I will use Windows as the example here.

    That said, on macOS the steps are basically the same once you have installed Anaconda and can use the conda command, so please refer to this guide once support is available. (For details, see my previous post.)

    Install Anaconda and open the Anaconda prompt.
    Then create a virtual environment and activate it.

    conda create -n videoannotator python=3.6
    conda activate videoannotator

    Next, install the required packages.

    pip install opencv-python-headless pyqt5==5.14.1 pyqtwebengine==5.14.0

    Then install Python Video Annotator.

    pip install python-video-annotator

    Once all the processing finishes, the installation is complete.

    To launch it, activate the virtual environment first and then run it.

    conda activate videoannotator
    start-video-annotator

    If the software starts up, you are all set.