facebookresearch / facebookresearch/sam2
Is it possible to calculate all roi objects on an image at one time?
- Dominant language
- Jupyter Notebook
- Stars
- 19.9k
- Forks
- 2.5k
- PR merge metrics
- No merged PRs in 30d
Description
I tried to use sam2 for people tracking, and I found that under a single camera, as the number of people increases, the tracking time increases linearly.
After checking the contact of propagate_in_video:
```python
for obj_idx in range(batch_size):
t0 = time.time()
obj_output_dict = inference_state["output_dict_per_obj"][obj_idx]
# We skip those frames already in consolidated outputs (these are frames
# that received input clicks or mask). Note that we cannot directly run
# batched forward on them via `_run_single_frame_inference` because the
# number of clicks on each object might be different.
if frame_idx in obj_output_dict["cond_frame_outputs"]:
storage_key = "cond_frame_outputs"
current_out = obj_output_dict[storage_key][frame_idx]
device = inference_state["device"]
pred_masks = current_out["pred_masks"].to(device, non_blocking=True)
if self.clear_non_cond_mem_around_input:
# clear non-conditioning memory of the surrounding frames
self._clear_obj_non_cond_mem_around_input(
inference_state, frame_idx, obj_idx
)
else:
storage_key = "non_cond_frame_outputs"
current_out, pred_masks = self._run_single_frame_inference(
inference_state=inference_state,
output_dict=obj_output_dict,
frame_idx=frame_idx,
batch_size=1, # run on the slice of a single object
is_init_cond_frame=False,
point_inputs=None,
mask_inputs=None,
reverse=reverse,
run_mem_encoder=True,
)
obj_output_dict[storage_key][frame_idx] = current_out
```
Here self._run_single_frame_inference is executed for each target on a single image, 我进行了简单测速,使用设备3090:
```
2025-04-22 05:40:47.935 | INFO | sam2.sam2_video_predictor:propagate_in_video:627 - frame idx: 97 single object tracker time 0.015321731567382812
2025-04-22 05:40:47.949 | INFO | sam2.sam2_video_predictor:propagate_in_video:627 - frame idx: 97 single object tracker time 0.013638019561767578
2025-04-22 05:40:47.961 | INFO | sam2.sam2_video_predictor:propagate_in_video:627 - frame idx: 97 single object tracker time 0.01247262954711914
2025-04-22 05:40:47.977 | INFO | sam2.sam2_video_predictor:propagate_in_video:627 - frame idx: 97 single object tracker time 0.015761852264404297
2025-04-22 05:40:47.993 | INFO | sam2.sam2_video_predictor:propagate_in_video:627 - frame idx: 97 single object tracker time 0.01570725440979004
2025-04-22 05:40:48.009 | INFO | sam2.sam2_video_predictor:propagate_in_video:627 - frame idx: 97 single object tracker time 0.01572585105895996
2025-04-22 05:40:48.024 | INFO | sam2.sam2_video_predictor:propagate_in_video:627 - frame idx: 97 single object tracker time 0.0156097412109375
2025-04-22 05:40:48.039 | INFO | sam2.sam2_video_predictor:propagate_in_video:627 - frame idx: 97 single object tracker time 0.014842748641967773
2025-04-22 05:40:48.055 | INFO | sam2.sam2_video_predictor:propagate_in_video:627 - frame idx: 97 single object tracker time 0.015426397323608398
2025-04-22 05:40:48.071 | INFO | sam2.sam2_video_predictor:propagate_in_video:627 - frame idx: 97 single object tracker time 0.015554666519165039
2025-04-22 05:40:48.085 | INFO | sam2.sam2_video_predictor:propagate_in_video:627 - frame idx: 97 single object tracker time 0.013868331909179688
2025-04-22 05:40:48.085 | INFO | sam2.sam2_video_predictor:propagate_in_video:629 - frame idx: 97 muti object tracker time 0.16500067710876465
```
For each target, it takes about 0.015 seconds to execute the forward pass.
I hope to improve the reasoning speed of a single image with many targets to be tracked by parallel reasoning, but this is very difficult. The code is encapsulated layer by layer.
I noticed that in the comment on line 598 of sam2_video_predictor.py:
```
# We skip those frames already in consolidated outputs (these are frames
# that received input clicks or mask). Note that we cannot directly run
# batched forward on them via `_run_single_frame_inference` because the
# number of clicks on each object might be different.
```
Does this mean that parallel reasoning is difficult to implement?
But I think that in theory parallel execution can be performed at the network stage, but it requires rewriting the pre-processing before sending it to the network and restoring the required format after the network inference is completed, which is too difficult.
I want to know how I can achieve my needs for parallel inference acceleration, thank you very much
Contributor guide
Assessment
This issue has not been assessed yet.