portugueslab / stytra

A modular package to control stimulation and track behaviour
http://www.portugueslab.com/stytra/
GNU General Public License v3.0
41 stars 26 forks source link

Tracking rate slower than camera acquisition above 80Hz #19

Closed EricThomson closed 4 years ago

EricThomson commented 4 years ago

I am running a simple protocol to track fish, without showing any stimuli. Below 80Hz, the tracking and acquisition are in sync: when I go above 80 video capture, the tracking rate stays at 80. Someone else using Stytra in our lab uses them together at 150Hz.

My cpu seems to be running pretty hot during this: I have a Xeon Quad Core W-2102 (2.9 GHz). Am I hitting a known cpu-induced limit, or perhaps doing something silly in my code?

Code:

from stytra import Stytra
from stytra.stimulation.stimuli import Pause
from stytra.stimulation import Protocol

class FreeTrackingProtocol(Protocol):
    name = "fishtrack"
    stytra_config = dict(
        display=dict(min_framerate=50),
        tracking=dict(method="fish", embedded=False, estimator="position"),
        camera=dict(type="spinnaker", max_buffer_length = 5, rotation = 2,  roi =  [400, 400, 1300, 1300]),
        min_framerate=50, )

    def __init__(self):
        super().__init__()

    def get_stim_sequence(self):
        return [Pause(duration=10)]  # protocol does not do anything

if __name__ == "__main__":
    s = Stytra(protocol = FreeTrackingProtocol())
vilim commented 4 years ago

The freely-swimming fish tracking can be computationally quite demanding, especially at the resolution you have (1300x1300 px), so if the processor is slightly older and not optimized for single core performance (same-generation Xeons usually are worse compared to the i7-i9 series), this can happen. There is a parameter in fish tracking called bg_downsample, which makes a part of the fish tracking procedure run on a lower-resolution image, usually enough to find the fish if set to 2, or even 3. Setting it to 2 improves the processing speed ~3 times.

EricThomson commented 4 years ago

Increasing bg_downsample worked -- useful to know going forward as we order computers for this -- thank you!