main_lincls.py
def sanity_check(state_dict, pretrained_weights):
    """
    Linear classifier should not change any weights other than the linear layer.
    This sanity check asserts nothing wrong happens (e.g., BN stats updated).
    """
    print("=> loading '{}' for sanity check".format(pretrained_weights))
    checkpoint = torch.load(pretrained_weights, map_location="cpu")
    state_dict_pre = checkpoint['state_dict']

    for k in list(state_dict.keys()):
        # only ignore fc layer
        if 'fc.weight' in k or 'fc.bias' in k:
            continue

        # name in pretrained model
        k_pre = 'module.encoder_q.' + k[len('module.'):] \
            if k.startswith('module.') else 'module.encoder_q.' + k

        if k_pre not in state_dict_pre:
            k_pre = k_pre[len('module.'):]

        assert ((state_dict[k].cpu() == state_dict_pre[k_pre]).all()), \
            '{} is changed in linear classifier training.'.format(k)

    print("=> sanity check passed.")

    Linear classifier should not change any weights other than the linear layer.
    This sanity check asserts nothing wrong happens (e.g., BN stats updated).
    
Linear classifier should not change any weights other than the linear layer.
This sanity check asserts nothing wrong happens .
Linear classifier should not change any weights other than the linear layer . This sanity check asserts nothing wrong happens .
def sanity_check(state_dict, pretrained_weights):
    print("=> loading '{}' for sanity check".format(pretrained_weights))
    checkpoint = torch.load(pretrained_weights, map_location="cpu")
    state_dict_pre = checkpoint['state_dict']
    for k in list(state_dict.keys()):
        if 'fc.weight' in k or 'fc.bias' in k:
            continue
        k_pre = 'module.encoder_q.' + k[len('module.'):] \
            if k.startswith('module.') else 'module.encoder_q.' + k
        if k_pre not in state_dict_pre:
            k_pre = k_pre[len('module.'):]
        assert ((state_dict[k].cpu() == state_dict_pre[k_pre]).all()), \
            '{} is changed in linear classifier training.'.format(k)
    print("=> sanity check passed.")
def sanity_check ( state_dict , pretrained_weights ) : print ( "=> loading '{}' for sanity check" . format ( pretrained_weights ) ) checkpoint = torch . load ( pretrained_weights , map_location = "cpu" ) state_dict_pre = checkpoint [ 'state_dict' ] for k in list ( state_dict . keys ( ) ) : if 'fc.weight' in k or 'fc.bias' in k : continue k_pre = 'module.encoder_q.' + k [ len ( 'module.' ) : ] if k . startswith ( 'module.' ) else 'module.encoder_q.' + k if k_pre not in state_dict_pre : k_pre = k_pre [ len ( 'module.' ) : ] assert ( ( state_dict [ k ] . cpu ( ) == state_dict_pre [ k_pre ] ) . all ( ) ) , '{} is changed in linear classifier training.' . format ( k ) print ( "=> sanity check passed." )
Linear classifier should not change any weights other than the linear layer.
Linear classifier should not change any weights other than the linear layer .
"""
    Linear classifier should not change any weights other than the linear layer.
    This sanity check asserts nothing wrong happens (e.g., BN stats updated).
    """ # only ignore fc layer # name in pretrained model
state_dict
pretrained_weights