Download model_loader.py from FrezzyI/GarmentAttributePrediction: direct link, hf CLI and curl.
- Browser
- Download file 926 Bytes
-
https://huggingface.co/spaces/FrezzyI/GarmentAttributePrediction/resolve/main/model_loader.py
- Command line
-
hf download hf://spaces/FrezzyI/GarmentAttributePrediction/model_loader.py
-
curl -L -o model_loader.py https://huggingface.co/spaces/FrezzyI/GarmentAttributePrediction/resolve/main/model_loader.py
926 Bytes
| import torch | |
| import torch.nn as nn | |
| import torchvision.models as models | |
| def load_model(model_path): | |
| # Architektur aufbauen | |
| model = models.resnet50(pretrained=False) | |
| model.fc = nn.Linear(2048, 228) | |
| # State Dict laden | |
| state_dict = torch.load(model_path, map_location=torch.device("cpu")) | |
| # Keys ggf. anpassen | |
| new_state_dict = {} | |
| for k, v in state_dict.items(): | |
| if k.startswith("predictor."): | |
| new_k = k.replace("predictor.", "") | |
| else: | |
| new_k = k | |
| new_state_dict[new_k] = v | |
| model.load_state_dict(new_state_dict) | |
| model.eval() | |
| return model | |
| def predict_attributes(model, input_tensor): | |
| with torch.no_grad(): | |
| output = model(input_tensor) | |
| prediction = torch.sigmoid(output).squeeze().numpy() | |
| threshold = 0.5 | |
| predicted_indices = [i for i, p in enumerate(prediction) if p > threshold] | |
| return predicted_indices | |