Skip to content

Need explaination for using deepcopy function when Automatically Generating Object Masks with SAM #573

@Yi-Chen-Research

Description

@Yi-Chen-Research

Hello, I want to thank you for your impressive work!

I have a question regarding the use of the deepcopy function when concatenating mask results during the process of "Automatically Generating Object Masks" with SAM. It appears to me that replacing deepcopy with copy could not only yield the same mask results but also potentially reduce the overall time required for automatic generation. Some other ways for copying mask results might also work.

So I wonder if there is a specific reason for the necessity of using the "deepcopy" function. The original code snippets used and possible changes are shown below.

data = MaskData()
        for (points,) in batch_iterator(self.points_per_batch, points_for_image):
            batch_data = self._process_batch(points, cropped_im_size, crop_box, orig_size)
            data.cat(batch_data)
            del batch_data
        self.predictor.reset_image()
def cat(self, new_stats: "MaskData") -> None:
        for k, v in new_stats.items():
            if k not in self._stats or self._stats[k] is None:
                self._stats[k] = deepcopy(v)
                # self._stats[k] = copy(v)
            elif isinstance(v, torch.Tensor):
                self._stats[k] = torch.cat([self._stats[k], v], dim=0)
            elif isinstance(v, np.ndarray):
                self._stats[k] = np.concatenate([self._stats[k], v], axis=0)
            elif isinstance(v, list):
                self._stats[k] = self._stats[k] + deepcopy(v)
                # self._stats[k] = self._stats[k] + copy(v)
            else:
                raise TypeError(f"MaskData key {k} has an unsupported type {type(v)}.")

Your help on this matter would be greatly appreciated. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions