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

perf: shallow copy instead of deepcopy #197

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions perception_eval/perception_eval/common/deepcopy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from copy import deepcopy
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what's the copyright procedure here? I could be either stackoverlow, Autonomous Systems or TIER4, or all together.



# https://stackoverflow.com/a/24621200/4732868
def deepcopy_with_sharing(obj, shared_attribute_names, memo=None):
"""
Deepcopy an object, except for a given list of attributes, which should
be shared between the original object and its copy.

obj is some object
shared_attribute_names: A list of strings identifying the attributes that
should be shared between the original and its copy.
memo is the dictionary passed into __deepcopy__. Ignore this argument if
not calling from within __deepcopy__.
"""
assert isinstance(shared_attribute_names, (list, tuple))
shared_attributes = {k: getattr(obj, k) for k in shared_attribute_names}

if hasattr(obj, "__deepcopy__"):
# Do hack to prevent infinite recursion in call to deepcopy
deepcopy_method = obj.__deepcopy__
obj.__deepcopy__ = None

for attr in shared_attribute_names:
del obj.__dict__[attr]

clone = deepcopy(obj)

for attr, val in shared_attributes.items():
setattr(obj, attr, val)
setattr(clone, attr, val)

if hasattr(obj, "__deepcopy__"):
# Undo hack
obj.__deepcopy__ = deepcopy_method
del clone.__deepcopy__

return clone
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from copy import copy
from typing import Dict
from typing import List
from typing import Optional
Expand Down Expand Up @@ -123,7 +124,7 @@ def filter_object_results(
is_target = False

if is_target:
filtered_object_results.append(object_result)
filtered_object_results.append(copy(object_result))

return filtered_object_results

Expand Down Expand Up @@ -198,7 +199,7 @@ def filter_objects(
transforms=transforms,
)
if is_target:
filtered_objects.append(object_)
filtered_objects.append(copy(object_))
return filtered_objects


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from perception_eval.common import DynamicObject
from perception_eval.common import DynamicObject2D
from perception_eval.common import ObjectType
from perception_eval.common.deepcopy import deepcopy_with_sharing
from perception_eval.common.evaluation_task import EvaluationTask
from perception_eval.common.label import LabelType
from perception_eval.common.label import TrafficLightLabel
Expand Down Expand Up @@ -106,6 +107,9 @@ def __init__(
self.iou_3d = None
self.plane_distance = None

def __deepcopy__(self, memo):
return deepcopy_with_sharing(self, shared_attribute_names = ['estimated_object', 'ground_truth_object'], memo=memo)

def get_status(
self,
matching_mode: MatchingMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

from __future__ import annotations

from copy import copy, deepcopy
from typing import Dict
from typing import List
from typing import Optional
Expand Down
1 change: 0 additions & 1 deletion perception_eval/perception_eval/tool/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ def filter_frame_by_distance(
PerceptionFrameResult: Filtered frame results.
"""
ret_frame = deepcopy(frame)

if min_distance is not None:
min_distance_list = [min_distance] * len(ret_frame.target_labels)
else:
Expand Down
Loading