WZMIAOMIAO/deep-learning-for-image-processing · error · AttributeError
'{}' object has no attribute '{}'
Error message
'{}' object has no attribute '{}' What it means
SmoothedValue/MetricLogger implements __getattr__ that looks up `attr` first in self.meters, then in self.__dict__; if found in neither it raises AttributeError. This occurs when accessing a metric/attribute that was never registered or recorded.
Source
Thrown at pytorch_object_detection/faster_rcnn/train_utils/distributed_utils.py:161
class MetricLogger(object):
def __init__(self, delimiter="\t"):
self.meters = defaultdict(SmoothedValue)
self.delimiter = delimiter
def update(self, **kwargs):
for k, v in kwargs.items():
if isinstance(v, torch.Tensor):
v = v.item()
assert isinstance(v, (float, int))
self.meters[k].update(v)
def __getattr__(self, attr):
if attr in self.meters:
return self.meters[attr]
if attr in self.__dict__:
return self.__dict__[attr]
raise AttributeError("'{}' object has no attribute '{}'".format(
type(self).__name__, attr))
def __str__(self):
loss_str = []
for name, meter in self.meters.items():
loss_str.append(
"{}: {}".format(name, str(meter))
)
return self.delimiter.join(loss_str)
def synchronize_between_processes(self):
for meter in self.meters.values():
meter.synchronize_between_processes()
def add_meter(self, name, meter):
self.meters[name] = meter
def log_every(self, iterable, print_freq, header=None):View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Register the meter first via meters[key] = SmoothedValue() or call update(key, value) before accessing it
- Print the available keys (logger.meters.keys()) to find the correct attribute name
- Fix typos in the attribute/metric name
Example fix
// before print(logger.loss.global_avg) # never updated // after logger.meters['loss'] = SmoothedValue() # or update first: logger.update(loss=loss_value) print(logger.loss.global_avg)
Defensive patterns
Strategy: type-guard
Validate before calling
attr = "loss"
assert attr in logger.meters or attr in logger.__dict__, f"{attr} not registered; available: {list(logger.meters)}" Type guard
def has_meter(logger, attr: str) -> bool:
return attr in logger.meters or attr in logger.__dict__ Try / catch
try:
value = logger.loss
except AttributeError as e:
value = None
print(f"metric missing: {e}; registered meters: {list(logger.meters)}") Prevention
- Call update(key, value) before reading the meter
- List logger.meters.keys() when unsure of metric names
- Centralize metric key names in constants to avoid typos
When it happens
Trigger: Accessing logger.some_metric before any update(some_metric, ...) call registered it in meters; typo in meter name; accessing an instance attribute on the object when __getattr__ intercepts missing attributes.
Common situations: Reading loss values from the MetricLogger before the first training iteration; renaming a metric key in one place but not the logging code; pickling issues that bypass meters initialization.
Related errors
- '{}' object has no attribute '{}'
- '{}' object has no attribute '{}'
- '{}' object has no attribute '{}'
- '{}' object has no attribute '{}'
- '{}' object has no attribute '{}'
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/c5e886022aea79a0.
Report an issue: GitHub.