I don’t think I’ve talked about idTracker here before. I presented the original IdTracker paper at a journal club in 2015 or so, shortly after it came out. The original method is bafflingly beautiful. Using a bit of clever feature selection, they made a rotationally invariant representation that captured fine details of fish appearance while reducing it down to a small matrix, perfect for classic Machine Learning algorithms. They paired this with a very slick algorithm for progressively building representations, and they were able to do miracles: reliably distinguishing between a hundred fish that (to us) look completely identical. That was in my second year of grad school, and it was one of the papers that got me really excited for the potential of computer vision in quantifying animal behavior.

Given how much of an impression it left, it maybe won’t surprise you that we use idTracker in the lab, and it’s one of 3 programs I immediately recommend people consider. The learning algorithm has actually improved a lot since then. It now uses contrastive learning, which is magical (maybe I’ll talk about that in a future blog post). That said, there are some major limitations. Importantly, idTracker struggles on videos with inconsistent backgrounds. Maybe you have a little ripple in the water that reflects a bit of light. Maybe you have to introduce some food stimulus halfway through your recording. Maybe you study an animal that poops. Maybe you just have a lot of videos, and don’t have the time to manually check segmentation. While the algorithm to distinguish between detected individuals is deep learning, the detection itself still relies on extremely basic computer vision approaches (thresholding or background subtraction), which are very sensitive to any visual noise. A lot of the work we do involves long-term recordings with enriched experimental arenas that can make background subtraction challenging, especially over extended time windows. But there are so many good approaches for detecting individuals, surely we could just pipe CNN-based detections into contrastive learning and be done, right?

Well, yes, actually. This is what I did.

As I mentioned in an earlier blog post, I’ve been really enjoying BehaveAI. It works great on very messy backgrounds, but even just as a convenient GUI for training YOLO models, it’s great. And with just a little bit of adjustment, I’ve been able to feed those detections into an extremely powerful contrastive learning pipeline.

Step 1 : export a list of crops that behaveAI detected. This was easy. I had already written a little script to work as a simple CLI for behaveAI. This was just a little one-line change to export the crops.

Step 2 : Pass them into idTracker-inspired contrastive learning. This part was obviously slightly more complex. But in practice it was just copying the approach idTracker uses and adjusting it to fit the required input and output. I’ve going to fess up at this point: this project was also an opportunity to try out this whole LLM-based coding business. I’ll talk a little bit more on that in a bit, but for all of AI’s flaws “Take this thing and tweak it slightly in a specific way” is very much in its wheelhouse. Within literally a few hours of prompts, tests, and improvements, I had a working pipeline. A couple weeks of intermittent piloting and adjustment later, it’s working great, and I’m in the final stages of testing before we run it on a couple of large datasets filled with all sorts of crazy nonsense.

Building this custom code also opened up some flexibility that was really important for us: For example sometimes individual fish get removed and/or replaced mid experiment. Because idTracker assumes the individuals are consistent across videos, it behaves unpredictably if the individuals change between videos. With this framework, we can take changes in the number and identities of fish into account when predicting individuals. So that’s cool. Gold star for ChatGPT I guess. That said, I have some thoughts on vibe coding:

I won’t try to speak to the pros/cons of LLM-coding locally or globally (especially this week), but in this case, it did something that would have taken me long enough I probably wouldn’t have tried it myself. And as best as I can tell, it did a solid job. This is, to my knowledge, the most effective algorithm for identifying unmarked individuals over imperfect backgrounds, especially over extended periods of time.

This puts me in a somewhat interesting position though. If this code had existed even a few years ago, I would have said this was extremely important and should be a Nature Methods paper. And I shouldn’t completely minimize my contribution, I do have a deep understanding of both the problem and the details of the methods involved that helped ensure this worked. But realistically you, dear reader, could just get a $20 subscription and ask it to do the same thing and it would probably work. Stochastic parrot that it is, it might even work better. So, is spending a few days getting an LLM to combine some existing code for you noteworthy?

So, I settled on a blog post. Regardless of the intellectual merit, this code itself is pretty handy. If you want to track specific individuals but you don’t have a perfectly clean background, I think this is currently the best approach out there. I’m also on working to see if we can add to BehaveAI proper, so stay tuned for that, but in the mean time, check out the repo, if it is helpful to it, give it a whirl.

~ap