Category: Research Tools

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

  • A Roundup of Drosophila Research Tools and Databases

    Here I’ve compiled a memo-style list of research tools useful for working with the fruit fly (Drosophila).
    If anything is missing, or if there is a tool you would like me to add, please leave a comment on the site or contact me at haya.m.yamano.neuro@gmail.com.

    This page is still under construction, so the information will keep growing over time.

    Last updated: December 1, 2023

    Databases

    FlyBase

    URL: https://flybase.org

    A comprehensive database for Drosophila. In addition to gene sequences and expression data, it provides access to research papers and information about the Drosophila research community.

    FlyWire

    URL: https://flywire.ai

    NeuronBridge

    URL: https://neuronbridge.janelia.org

    FlyLight

    URL: https://www.janelia.org/project-team/flylight

    FlyCircuit

    URL: https://www.flycircuit.tw

    neuPrint

    URL: https://neuprint.janelia.org

    SCope

    URL: https://scope.aertslab.org

    Fly Cell Atlas

    URL: https://flycellatlas.org

    Paper:

    Stock Centers

    Bloomington Drosophila Stock Center

    URL: https://bdsc.indiana.edu

     The Drosophila stock center at Indiana University in Bloomington, USA.
    You can search not only by genotype but also by stock number (the numbers beginning with BL).

    A wide variety of lines are available here, including GAL4, UAS, and RNAi lines.

    Vienna Drosophila Resource Center

    URL: https://shop.vbc.ac.at/vdrc_store/

     The Drosophila stock center at the Institute of Molecular Biotechnology (IMBA) in Austria. It holds an especially large collection of RNAi lines.

    KYOTO Drosophila Stock Center

    URL: https://kyotofly.kit.jp/cgi-bin/stocks/index.cgi

     A stock center at the Kyoto Institute of Technology in Kyoto, Japan. It is the largest Drosophila stock center in Japan; you can download the stock list from the site and search collectively by line.

    KYORIN-Fly : Drosophila species stock center

    URL: https://shigen.nig.ac.jp/fly/kyorin/

     A Drosophila stock center at Kyorin University in Japan. It maintains a wide range of Drosophila species, and mutants of species closely related to Drosophila melanogaster can also be obtained here.

    Behavioral Analysis Tools

    DeepLabCut

    URL: http://www.mackenziemathislab.org/deeplabcut

    Paper: Mathis A, Mamidanna P, Cury KM, et al. DeepLabCut: markerless pose estimation of user-defined body parts with deep learning. Nat Neurosci. 2018;21(9):1281-1289. DOI:10.1038/s41593-018-0209-y

     

    SLEAP

    URL: https://sleap.ai

    Paper: Pereira TD, Tabris N, Matsliah A, et al. SLEAP: A deep learning system for multi-animal pose tracking. Nat Methods. 2022;19(4):486-495.
    DOI:10.1038/s41592-022-01426-1

    UMATracker

    URL: https://ymnk13.github.io/UMATracker/

    Paper: Yamanaka O, Takeuchi R. UMATracker: An intuitive image-based tracking platform. J Exp Biol. 2018;221(16):1-5.
    DOI:10.1242/jeb.182469

    Ctrax

    URL: https://ctrax.sourceforge.net

    Paper: https://www.nature.com/articles/nmeth.1328

    FlyTracker

    URL:

    Paper:

    ID Tracker

    URL:

    Paper:

    TRex

    URL:

    Paper: https://elifesciences.org/articles/64000

    JAABA

    URL:

    Paper:

    Other Resources

    Brain and VNC template (JRC 2018 Brain templates)

    URL: https://www.janelia.org/open-science/jrc-2018-brain-templates

    Color-Depth MIP

    URL: https://www.janelia.org/open-science/color-depth-mip

    Dissection and Immunostaining Protocols

    URL: https://www.janelia.org/project-team/flylight/protocols

    Here you can find videos on dissecting adult and larval Drosophila, along with immunostaining protocols.

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

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

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

  • Arithmetic Operations in Bonsai

    Arithmetic Operations in Bonsai

    Bonsai is widely used for designing experiments in neuroscience and beyond.
    With Bonsai you can build sophisticated programs without writing code, and run them with precise synchronization.

    Because it is so simple, however, operations that would be trivial in ordinary programming—such as basic arithmetic—have to be done in a somewhat unintuitive way.
    Here I explain how to perform arithmetic operations, using MouseMove as an example.

    Below are the arithmetic operations in action: from top to bottom, addition, subtraction, multiplication, and division.

    For clarity, I used the X and Y values from MouseMove as an example here.
    In practice, though, you can perform calculations on values obtained from other sensors and the like.

    The key point in Bonsai is that you need to combine the two values with “Zip” before performing the operation.

    The contents of “Zip” take the form (value 1, value 2); its role is to bundle the two values at that moment into one.
    Each operation then computes value 1 and value 2 together.

    It is hard to explain in words, so try to get a rough intuition for it.
    Without “Zip”, the program would not know which value to multiply with which, so the calculation could not be performed. That is why the X and Y values must first be combined so that they correspond to each other.

    The names of the operators for each arithmetic operation are listed below.

    • Addition => Add
    • Subtraction => Subtract
    • Multiplication => Multiply
    • Division => Divide

    I have included a demonstration below for your reference.

    <Example in action>

  • Batch Image Processing with ImageJ (Fiji)

    In biological research, you will often image samples such as antibody-stained preparations with a confocal laser scanning microscope.

    Here I explain how to use ImageJ to turn that imaging data into a stack image (Stack) and to merge images acquired at different wavelengths into a merged image (Merge).
    I will finish by showing how to add a scale bar.

    First, with a confocal laser scanning microscope you obtain images acquired at different wavelengths (different lasers) for each single slice, and these slices span the thickness of the sample along the z axis.

    Here, combining images of different wavelengths taken at the same z position is called merging, and superimposing the images along the z axis is called stacking.
    ImageJ uses these terms in the same sense, so keep in mind what you actually want to do as you work.

    I will use Fiji in this explanation, but the procedure is largely the same in ImageJ.
    That said, Fiji can open confocal microscope image data without any format conversion, which is why I use Fiji here.

    For this example I used a confocal microscope image of a fly from flylight.
    The image used as an example

    As you can see from the files downloadable at that site, confocal microscope images use manufacturer-specific formats such as *.lsm, and these files contain a variety of information.
    Scale information is included as well, so you can insert an accurate scale bar without having to look up the microscope’s scale yourself.

    First, open Fiji and drag your imaging data onto the menu bar–like window that appears.

    When it opens, a new window appears showing something blackish or greenish.
    This is the image you are currently working on.
    You can move along the z axis by dragging the scroll bar at the bottom.

    Merge the images via Image<Color<Make Composite in the top menu.
    Then open the Channels Tool via Image<Color<Channels Tool.

    In the Channels Tool, you can toggle the display of each channel using the Channel checkboxes.

    To stack the images, click Image<Stacks<Z project and the images will be stacked.
    Unless you have a particular preference, set Projection Type to Sum Slices.
    A new window will then open showing the stacked image.

    The Channels Tool works here too, so you can view the stacked image for each channel.

    You can add a scale bar via Analyze<Tools<Scale Bar.
    By changing the value of “Width in 〇〇:”, you can set it to whatever length you like.

    To save the processed image, use File<Save as and specify the file format.
    For some reason images come out white when saved as Tiff, so I recommend PNG or JPEG.

    And here is the finished image.

    You can find the link to Fiji here.

  • How to Install DeepLabCut 2.3 [Updated December 2023]

    How to Install DeepLabCut 2.3 [Updated December 2023]

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

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

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

    DeepLabCut homepage
    GitHub

    How to install

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

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

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

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

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

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

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

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

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

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

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

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

    conda activate DEEPLABCUT

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

    Next, install CUDA and cuDNN.

    conda install -c conda-forge cudnn

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

    Then enter the following four commands to complete the installation.

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

    Update DeepLabCut, and the installation is complete.

    pip install --upgrade deeplabcut

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

    conda activate DEEPLABCUT
    python -m deeplabcut

    Entering this will launch the DeepLabCut GUI.

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

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

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

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

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

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

    Bonus

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

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

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

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

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

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

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

    Please feel free to make use of it.

    Addendum 1

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

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

  • DeepLabCut: A Deep Learning-Based Behavior Analysis Tool

    DeepLabCut: A Deep Learning-Based Behavior Analysis Tool

    (The image above links to the paper.)

    Behavioral analysis is a crucial experimental approach in biology, and accurate quantification of behavior is especially indispensable for understanding the brain.

    Traditionally, high-accuracy pose estimation has been achieved by attaching markers (such as sensors) to the subject (a person or a laboratory animal).
    However, sensors get in the subject’s way, which can alter its behavior or restrict its movements.

    One marker-free alternative is to fit a skeleton model, but developing such models is time-consuming and has to be done on a large scale.
    There are also image-based pose estimation systems, but these generate enormous amounts of data, which again makes them very difficult for a single laboratory to run given the equipment required.

    With DeepLabCut, anyone with a single computer can perform pose estimation very easily.
    Using deep learning-based image recognition, you can track any body parts you choose without markers.

    Because the labeling is learned by a deep network, you can analyze recorded videos without correcting them beforehand.
    In other words, tracking works even if the lighting across the field of view is uneven or the image is somewhat distorted depending on the camera angle.

    The page of the laboratory that developed DeepLabCut is here.
    The GitHub repository is here.

    From actually using DeepLabCut, I found that the computer used for analysis needs a GPU (graphics card) with at least 8 GB of memory.
    In gaming PC terms, that means a mid- to high-end GPU, so comfortable analysis is not possible on an everyday laptop.

    That said, even without such a high-spec machine, you can run the analysis on a virtual GPU using Google Colab.
    I haven’t tried it yet, though…

    In practice, I found the plotting accuracy to be very high.
    However, analysis often fails when the background differs from that of the training data, so it is best to assume the exact setting you want to analyze and use training data recorded under conditions as close to it as possible.

    The paper shows that plotting accuracy improves further when you label not only the few points you are interested in but the whole subject, including a rough skeleton, even for parts you will not use in the analysis.
    In addition, if the results are unsatisfactory after training and analysis, you can remove the problematic frames or add new training data, allowing you to actively refine the model.

    DeepLabCut itself is very user-friendly, and anyone can use it easily.
    Why not give it a try in your own analyses?

    I’d like to write about how to install and use DeepLabCut in future posts.