WZMIAOMIAO/deep-learning-for-image-processing · error · AttributeError
'{}' object has no attribute '{}'
Error message
'{}' object has no attribute '{}' What it means
AttributeError thrown by the SmoothedValue/MeterDictionary-style class's custom __getattr__. When attribute lookup fails on the instance, __getattr__ checks self.meters and self.__dict__; if the name is found in neither, it raises AttributeError naming the class and the missing attribute. This is the standard torch.utils.data distrbuted-utils pattern from maskrcnn-benchmark.
Source
Thrown at pytorch_segmentation/lraspp/train_utils/distributed_utils.py:142
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
- Fix the attribute name typo to match a registered meter key
- Ensure the meter was created before access: any use of loss_meter.update(...) registers it via __setattr__
- Use getattr(obj, 'attr', default) or check `attr in obj.meters` before access
Example fix
// before mean_loss = torch.stack([m.global_avg for m in metric_logger.meters.values()]) print(metric_logger.losss) // after print(metric_logger.loss) # correct meter name registered via metric_logger.loss = SmoothedValue()
Defensive patterns
Strategy: validation
Validate before calling
if attr not in logger.meters and attr not in logger.__dict__:
print(f"available meters: {list(logger.meters)}") Type guard
def has_meter(logger, name: str) -> bool:
return name in logger.meters or hasattr(logger, name) Try / catch
try:
value = logger.loss.global_avg
except AttributeError as e:
value = float('nan') # or fall back to default metric Prevention
- Register every meter with metric_logger.name = SmoothedValue() before reading
- Use IDE autocomplete to avoid typos on MetricLogger attributes
- Keep meter names consistent between train and eval loops
When it happens
Trigger: Accessing an attribute like logger.meters['loss'] misspelled (e.g. .los), or accessing a meter key that was never registered via __setattr__ on the MetricLogger before reading it, e.g. calling meter loss without a prior update().
Common situations: Typos in training scripts (meterr vs meters), reading a loss key before any training step registered it, or copy-pasting code between projects where the MetricLogger was populated with different meter names.
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/8d5befa845df134c.
Report an issue: GitHub.