Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timing may not be accurate #1

Closed
YiqunChen1999 opened this issue Apr 25, 2022 · 1 comment
Closed

Timing may not be accurate #1

YiqunChen1999 opened this issue Apr 25, 2022 · 1 comment

Comments

@YiqunChen1999
Copy link

Timing in Tracker may not be accurate enough.

As pointed out in Pytorch Forums post, CUDA is asynchronous so you will need some tools to measure time.

I then give a simple example which may be helpful:

from typing import Union
import torch

class Tracker(object):
    # original code here.
    @property
    def is_using_cuda(self):
        return self.device != torch.device("cpu")

    def _init_timer(self):
        if self.is_using_cuda:
            self._timer_start = torch.cuda.Event(enable_timing=True)
            self._timer_stop = torch.cuda.Event(enable_timing=True)
        self._timestamp = None

    def _start_timing(self) -> Union[float, None]:
        if self.is_using_cuda:
            self._timer_start.record()
            timestamp = None
        else:
            timestamp = time.time()
            self._timestamp = timestamp
        return timestamp

    def _stop_timing(self) -> float:
        if self.is_using_cuda:
            self._timer_stop.record()
            torch.cuda.synchronize()
            # cuda event record return duration in milliseconds.
            duration = self._timer_start.elapsed_time(
                self._timer_stop
            )
            duration /= 1000.0
        else:
            duration = time.time() - self._timestamp
        return duration

    def track(...): # args are omitted here.
        self._init_timer()
        # code for initialization is omitted.
        for f, img_file in enumerate(img_files):
            # code for data preparation is omitted.
            self._start_timing()
            if f == 0: 
                self.init(image, box)
                times[f] = self._stop_timing()
            if fail_count >= 10 and method == 'restart' and f in restart_flag:
                # the tracker will be restarted when the cumulative number of failures reaches 10
                print('init again in %s' % f)                
                init_positions.append(f)
                self.init(image, anno[f,:])
                fail_count = 0
            else:
                frame_box = self.update(image) 
                frame_box = np.rint(frame_box)
                times[f] = self._stop_timing()
            # the rest code is omitted.

If you find this code snippet is helpful, you may consider update your toolkit.

huuuuusy added a commit that referenced this issue Apr 25, 2022
@huuuuusy
Copy link
Owner

Thanks a lot; I have updated the codes based on your suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants