From 791152918ca931f8bb751f65081e68328717d719 Mon Sep 17 00:00:00 2001 From: liukuikun <641417025@qq.com> Date: Mon, 6 Mar 2023 11:54:30 +0800 Subject: [PATCH 1/2] [Fix] fix pytorch2.0 not support sys._getframe to remove private key --- mmengine/structures/base_data_element.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mmengine/structures/base_data_element.py b/mmengine/structures/base_data_element.py index 7be1ef9044..3aa4db291c 100644 --- a/mmengine/structures/base_data_element.py +++ b/mmengine/structures/base_data_element.py @@ -1,6 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. import copy -import sys from typing import Any, Iterator, Optional, Tuple, Type, Union import numpy as np @@ -309,7 +308,15 @@ def keys(self) -> list: Returns: list: Contains all keys in data_fields. """ - return list(self._data_fields) + # We assume that the name of the attribute related to property is + # '_' + the name of the property. We use this rule to filter out + # private keys. + private_keys = { + '_' + key + for key in self._data_fields + if isinstance(getattr(type(self), key, None), property) + } + return list(self._data_fields - private_keys) def metainfo_keys(self) -> list: """ @@ -466,12 +473,7 @@ def set_field(self, raise AttributeError( f'Cannot set {name} to be a field of data ' f'because {name} is already a metainfo field') - # The name only added to `data_fields` when it is not the - # attribute related to property(methods decorated by @property). - if not isinstance( - getattr(type(self), - sys._getframe(1).f_code.co_name, None), property): - self._data_fields.add(name) + self._data_fields.add(name) super().__setattr__(name, value) # Tensor-like methods From a8380ffc1d7f3c6939ba8dd28b80f64344133b82 Mon Sep 17 00:00:00 2001 From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> Date: Tue, 14 Mar 2023 19:49:50 +0800 Subject: [PATCH 2/2] Update mmengine/structures/base_data_element.py --- mmengine/structures/base_data_element.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mmengine/structures/base_data_element.py b/mmengine/structures/base_data_element.py index 3aa4db291c..46c4c886e6 100644 --- a/mmengine/structures/base_data_element.py +++ b/mmengine/structures/base_data_element.py @@ -311,6 +311,7 @@ def keys(self) -> list: # We assume that the name of the attribute related to property is # '_' + the name of the property. We use this rule to filter out # private keys. + # TODO: Use a more robust way to solve this problem private_keys = { '_' + key for key in self._data_fields