Month: March 2021

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

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

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

    I struggled with exactly that myself.

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

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

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

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

    On top of that, beginner-level programming lessons often use the interactive style, and then at the intermediate level you’re suddenly writing scripts—yet what to write them in is often left unstated, or differs completely from book to book.

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

    But what do you write script-based programs in?

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

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

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

    Let me explain what each one is like.

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

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

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

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

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

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

    The Anaconda site is here

    The Jupyter Notebook site is here

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

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

  • Can’t remember where the screws go?! Solved with a clever idea ☆彡

    Can’t remember where the screws go?! Solved with a clever idea ☆彡

    When you swap a PC’s HDD for an SSD, reapply thermal paste, or try to fix a broken appliance or piece of furniture yourself, it’s all too easy to lose the screws.

    Once you lose a screw, you have to buy a replacement at a hardware store, and finding one that fits exactly is no easy task.
    I lost a screw myself when I tried to repair a Switch Pro Controller, and ended up having to go buy one.

    In recent years, electronic components circulating in the Chinese market have become available through AliExpress, Wish, and similar sites, and junk items are now easy to pick up on flea market apps like Mercari. As a result, far more people are trying their hand at repairs themselves.
    With COVID keeping people at home for longer stretches, I think this trend has only accelerated.

    Still, keeping track of screws is a basic part of fixing anything—and, I’d argue, one of the most important.
    So I’d like to share how I manage screws when repairing junk items.

    You can make this with a magnetic sheet with an adhesive backing and a clear tray, both available at a hundred-yen shop.
    When I bought mine, the magnetic sheets were in the stationery section and the clear trays were in the storage section.

    Just cut the magnetic sheet to a suitable size and stick it on—that’s all there is to it.

    Stackable trays work even better: stacking them in the order you disassembled the device makes it easy to tell which screws came from where.
    Being able to keep small parts alongside the screws is another selling point.

    It’s easy to put together, so give it a try.

  • Reflections After a Month of Studying Programming

    The thing I feel most strongly while learning to program is that I have no idea whether I’m actually getting any good at it.

    For a long time I thought programming was something like English: that as I studied, I would gradually be able to write it as fluently as a language.
    In practice, though, it never really felt like that was happening.

    Programming feels like something you study when you’re forced to by necessity, which is to say you never really pick it up unless you use it.
    In my own case, I had analysis results from DeepLabCut, but the data were so massive that I needed to find some way to turn them into easy-to-read graphs.

    You can certainly make graphs in Excel, but with data this large it took forever just to open the file.
    So I decided to use Python to process the data and plot it, and set about writing the program while learning as I went.

    Once I started writing code, I found that writing the code itself wasn’t all that hard—what was hard was searching Google for the specific operation I wanted to do.
    It was a constant cycle of figuring out what processing I wanted, looking up code that would do it, and typing it in.

    Given all this, going back to my earlier point that programming isn’t quite like a spoken language: it’s more like always talking with a Japanese-English dictionary in hand. You can do just about anything if you spend enough time on it, but experience is what changes how fast you can look things up and how elegant your code becomes.

    Also, the code you write yourself becomes an asset—I often find myself copying and pasting my own code and tweaking it slightly when I need to do something similar.

    I’ve only just started programming and I’m something of a bandwagon type, so maybe I’m not really in a position to hold forth on this, but I hope it’s useful to anyone thinking of starting out.

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


  • Bonsai as a Visual Programming Language

    Bonsai as a Visual Programming Language

    Bonsai is a visual programming language introduced in a 2015 paper.
    It lets you acquire data from sensors and process it at the same time.

    One of Bonsai’s strengths is that you can build complex processing pipelines much like assembling a puzzle, and then check the results of that processing in real time.

    Bonsai can also be combined with other analysis software (DeepLabCut, Open Ephys, BonVision, and so on) and with hardware (cameras, controllers, microcontrollers, etc.), which makes it highly extensible.

    Because you can perform complex analysis and processing intuitively without having to learn a seemingly daunting programming language, anyone can use it easily, and it broadens the range of experiments you can run.

    The paper on Bonsai is available here.
    You can install Bonsai from here.

    Image taken from the paper.
    Each circle is called a node and represents an individual processing step.

    This is what the screen looks like when Bonsai is actually running (taken from the paper).
    On the left you can search for nodes and add the one you want; in the center you arrange nodes to build your pipeline while visualizing how it works; and on the right are the detailed settings for each node.

    The pop-up windows on the main screen show the execution status of each node — for a camera capture node, for example, it can display the live camera image.

    It may look difficult at first glance, but seen this way it turns out to be surprisingly simple.

    Now let’s actually install it.
    That said, all you have to do is download it from this site.
    Just open the downloaded “Bonsai-*.*.*.exe” file and run the installer.
    Note that security software such as Virus Buster may sometimes prevent the installation.

    Once Bonsai is installed, both Bonsai and Bonsai (x86) will appear in your list of applications.
    Either one is fine for normal use, but occasionally one of them will not run; when that happens, trying the other one often works.

    One drawback is that Bonsai currently runs only on Windows and does not support Linux or macOS.
    Support may come in the future, but this is something to keep in mind.

    From next time, I’ll start actually using Bonsai.