Views
No views yet
fastvit_sa24 (Apple's SOTA efficient Transformer/CNN hybrid) for visual texture analysis.| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| Angry | 0.89 | 0.95 | 0.92 | 1797 |
| Disgust | 1.00 | 1.00 | 1.00 | 1798 |
| Fear | 0.88 | 0.93 | 0.90 | 1798 |
| Happy | 0.92 | 0.89 | 0.91 | 1798 |
| Neutral | 0.83 | 0.84 | 0.84 | 1798 |
| Sad | 0.88 | 0.76 | 0.82 | 1798 |
| Surprise | 0.95 | 0.97 | 0.96 | 1798 |
| Accuracy | 0.91 | 12585 |

timm, mediapipe, and torch.1import torch
2import timm
3import mediapipe as mp
4import numpy as np
5from PIL import Image
6from torchvision import transforms
7
8# 1. Define Model Architecture (Same as training)
9class MultimodalFERModel(torch.nn.Module):
10 def __init__(self, num_classes=7):
11 super().__init__()
12 self.vision_backbone = timm.create_model('fastvit_sa24.apple_in1k', num_classes=0)
13 self.landmark_encoder = torch.nn.Sequential(
14 torch.nn.Linear(478*3, 512), torch.nn.BatchNorm1d(512), torch.nn.ReLU(),
15 torch.nn.Linear(512, 256), torch.nn.BatchNorm1d(256), torch.nn.ReLU()
16 )
17 self.classifier = torch.nn.Sequential(
18 torch.nn.Linear(self.vision_backbone.num_features + 256, 512),
19 torch.nn.BatchNorm1d(512), torch.nn.ReLU(),
20 torch.nn.Linear(512, num_classes)
21 )
22 def forward(self, pixel_values, landmarks):
23 v = self.vision_backbone(pixel_values)
24 l = self.landmark_encoder(landmarks)
25 return self.classifier(torch.cat((v, l), dim=1))
26
27# 2. Load Model
28model = MultimodalFERModel()
29# Download weights from Hub (manually or via API) and load
30# model.load_state_dict(torch.hub.load_state_dict_from_url('...'))
31model.eval()
32
33# 3. Preprocessing (MediaPipe + Transforms)
34mp_face = mp.solutions.face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1)
35val_tf = transforms.Compose([
36 transforms.Resize((256, 256)),
37 transforms.ToTensor(),
38 transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
39])
40
41def predict(image_path):
42 img = Image.open(image_path).convert('RGB')
43 # Vision Input
44 pixel_values = val_tf(img).unsqueeze(0)
45
46 # Landmark Input
47 results = mp_face.process(np.array(img))
48 if results.multi_face_landmarks:
49 lm = np.array([[l.x, l.y, l.z] for l in results.multi_face_landmarks[0].landmark]).flatten()
50 else:
51 lm = np.zeros(478*3)
52 landmarks = torch.tensor(lm, dtype=torch.float32).unsqueeze(0)
53
54 with torch.no_grad():
55 logits = model(pixel_values, landmarks)
56 pred = logits.argmax(1).item()
57 return pred