Skip to content

Fix blob_loss_weights cache attribute name typo (cache never hits)#7104

Open
Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Chessing234:fix/blob-loss-weights-cache-attr-typo
Open

Fix blob_loss_weights cache attribute name typo (cache never hits)#7104
Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Chessing234:fix/blob-loss-weights-cache-attr-typo

Conversation

@Chessing234
Copy link
Copy Markdown

Bug

_Net_blob_loss_weights in python/caffe/pycaffe.py is intended to memoize the OrderedDict of per-blob loss weights on self, but the guard and the set/return reference two different attribute names:

https://github.com/BVLC/caffe/blob/9b89154/python/caffe/pycaffe.py#L35-L44

@property
def _Net_blob_loss_weights(self):
    ...
    if not hasattr(self, '_blobs_loss_weights_dict'):
        self._blob_loss_weights_dict = OrderedDict(zip(self._blob_names,
                                                       self._blob_loss_weights))
    return self._blob_loss_weights_dict

The guard is _blobs_loss_weights_dict (plural "blobs"), while the store/return is _blob_loss_weights_dict (singular "blob"). So hasattr always returns False, a fresh OrderedDict is rebuilt on every access, and net.blob_loss_weights is net.blob_loss_weights is False. Any mutation of the returned dict is silently lost on the next access.

Root cause

Typo in the cache-check attribute name; the sibling properties (_Net_blobs, _Net_layer_dict, _Net_params, _Net_inputs, _Net_outputs) all use the same name for the guard and the stored attribute.

Fix

Change the hasattr check to _blob_loss_weights_dict so the memoization actually hits and net.blob_loss_weights returns the same OrderedDict on subsequent calls, matching the pattern of the neighbouring properties.

_Net_blob_loss_weights guards its memoization with hasattr(self,
'_blobs_loss_weights_dict') but writes/reads self._blob_loss_weights_dict
(no 's' after 'blob'). The guard therefore always evaluates to False and
a fresh OrderedDict is built on every access, defeating the cache and
making the returned dict a different object on each call. Mutations to
the returned dict are also lost on the next access.

Align the hasattr name with the attribute that is actually set/returned,
matching the pattern of the sibling _Net_blobs / _Net_layer_dict /
_Net_params properties.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant