WZMIAOMIAO/deep-learning-for-image-processing · error · AttributeError
'{}' object has no attribute '{}'
Error message
'{}' object has no attribute '{}' What it means
Identical family to error 150 but in the FCN project's distributed_utils.py: a MetricLogger/SmoothedValue container raises AttributeError when an accessed attribute is neither a registered meter nor in instance __dict__. The __getattr__ fallback is the last resort before failing, so the requested metric simply does not exist.
Source
Thrown at pytorch_segmentation/fcn/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
- Compare the missing attribute name with keys passed to `metric_logger.update(...)` in train_one_epoch/evaluate
- Register the meter at logger creation (`self.meters[name] = SmoothedValue(...)`) or ensure update() is called before access
- Use `hasattr`/`getattr(..., None)` for optional metrics
- Check for typos and copy-paste drift between project variants
Example fix
// before print(metric_logger.lr.global_avg) # AttributeError: never registered // after metric_logger.update(lr=optimizer.param_groups[0]['lr']) print(metric_logger.lr.global_avg)
Defensive patterns
Strategy: try-catch
Validate before calling
registered = set(metric_logger.meters) | set(metric_logger.__dict__)
assert 'loss' in registered, f"'loss' missing; registered: {registered}" Type guard
def meter_exists(logger, name):
return name in getattr(logger, 'meters', {}) or name in logger.__dict__ Try / catch
try:
value = getattr(metric_logger, name).global_avg
except AttributeError as e:
logging.warning("meter %s missing: %s", name, e)
value = float('nan') Prevention
- Register all meters before the training loop starts
- Update meters with the same keys every iteration
- Grep for metric_logger.update to enumerate valid names
- Avoid copy-pasting meter names between project variants
When it happens
Trigger: Reading `metric_logger.<name>` for a key never passed to `update()`; mistyped meter name; accessing meters after `reset()` cleared them; code assuming FCN train loop registers meters that only the detection loop registers (e.g. 'mask_loss').
Common situations: Adapting the FCN train_one_epoch to log extra metrics without creating them; summary/print code copied from another project; running evaluation loop expecting train-only meters.
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/860999e6135d0060.
Report an issue: GitHub.