Views
No views yet
@inproceedings{pham2021facial,
title={Facial expression recognition using residual masking network},
author={Pham, Luan and Vu, The Huynh and Tran, Tuan Anh},
booktitle={2020 25th International Conference on Pattern Recognition (ICPR)},
pages={4513--4519},
year={2021},
organization={IEEE}
}1import numpy as np
2import torch
3import torch.nn as nn
4from feat.emo_detectors.ResMaskNet.resmasknet_test import ResMasking
5from huggingface_hub import hf_hub_download
6
7# Load Configs
8emotion_config_file = hf_hub_download(repo_id= "py-feat/resmasknet", filename="config.json", cache_dir=get_resource_path())
9with open(emotion_config_file, "r") as f:
10 emotion_config = json.load(f)
11
12device = 'cpu'
13emotion_detector = ResMasking("", in_channels=emotion_config['in_channels'])
14emotion_detector.fc = nn.Sequential(nn.Dropout(0.4), nn.Linear(512, emotion_config['num_classes']))
15emotion_model_file = hf_hub_download(repo_id='py-feat/resmasknet', filename="ResMaskNet_Z_resmasking_dropout1_rot30.pth")
16emotion_checkpoint = torch.load(emotion_model_file, map_location=device)["net"]
17emotion_detector.load_state_dict(emotion_checkpoint)
18emotion_detector.eval()
19emotion_detector.to(device)
20
21
22# Test model
23face_image = "path/to/your/test_image.jpg" # Replace with your extracted face image that is [224, 224]
24
25# Classification - [angry, disgust, fear, happy, sad, surprise, neutral]
26emotions = emotion_detector.forward(face_image)
27emotion_probabilities = torch.softmax(emotions, 1)
28