Download inference.py from IIITH-25-27/Image_to_emotion_model: direct link, hf CLI and curl.
- Browser
- Download file 709 Bytes
-
https://huggingface.co/IIITH-25-27/Image_to_emotion_model/resolve/main/inference.py
- Command line
-
hf download hf://IIITH-25-27/Image_to_emotion_model/inference.py
-
curl -L -o inference.py https://huggingface.co/IIITH-25-27/Image_to_emotion_model/resolve/main/inference.py
709 Bytes
| import torch | |
| import torchvision.models as models | |
| from torchvision import transforms | |
| from PIL import Image | |
| model = models.resnet50() | |
| model.fc = torch.nn.Linear(model.fc.in_features, 6) | |
| model_path = "resnet50_c3_lr3e-04_bs32_aug_heavy_opt_adam_drop0.5_ls0.1_6class.pth" | |
| model.load_state_dict(torch.load(model_path, map_location="cpu")) | |
| model.eval() | |
| labels = ["angry","fear","happy","sad","surprise","neutral"] | |
| transform = transforms.Compose([ | |
| transforms.Resize((224,224)), | |
| transforms.ToTensor() | |
| ]) | |
| def predict(image): | |
| image = transform(image).unsqueeze(0) | |
| with torch.no_grad(): | |
| output = model(image) | |
| pred = torch.argmax(output,1).item() | |
| return {"emotion": labels[pred]} |