Download utils/utils.py from KalbeDigitalLab/phbreast-bilateral-view-mammography-classifier: direct link, hf CLI and curl.
- Browser
- Download file 596 Bytes
-
https://huggingface.co/spaces/KalbeDigitalLab/phbreast-bilateral-view-mammography-classifier/resolve/main/utils/utils.py
- Command line
-
hf download hf://spaces/KalbeDigitalLab/phbreast-bilateral-view-mammography-classifier/utils/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/KalbeDigitalLab/phbreast-bilateral-view-mammography-classifier/resolve/main/utils/utils.py
596 Bytes
| import torch | |
| def mean_activations(tensor): | |
| """Computes mean of activation maps tensor.""" | |
| # squeeze to remove batch dimension | |
| return torch.mean(tensor.detach().cpu(), dim=1).squeeze(dim=0) | |
| def load_weights(model, weights): | |
| """Loads the weights of only the layers present in the given model.""" | |
| pretrained_dict = torch.load(weights, map_location='cpu') | |
| model_dict = model.state_dict() | |
| pretrained_dict = {k: v for k, | |
| v in pretrained_dict.items() if k in model_dict} | |
| model_dict.update(pretrained_dict) | |
| model.load_state_dict(model_dict) | |