Update README.md
Browse files
README.md
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
|
|
|
|
|
| 1 |
VGG model taken finetuned on AffectNet data for prediction of the 7 basic emotions. The model architecture can be described in code as follows:
|
| 2 |
|
| 3 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
class CustomVGG(nn.Module):
|
| 5 |
def __init__(self):
|
| 6 |
super(CustomVGG, self).__init__()
|
|
@@ -12,12 +19,28 @@ class CustomVGG(nn.Module):
|
|
| 12 |
self.vgg.classifier[6] = nn.Linear(in_features=4096, out_features=7)
|
| 13 |
|
| 14 |
def forward(self, x):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 19 |
classifier = CustomVGG()
|
|
|
|
| 20 |
classifier = classifier.to(device)
|
|
|
|
| 21 |
```
|
| 22 |
|
| 23 |
Here is the loss plot for this training run. This checkpoint is taken from the epoch with the best validation loss. At it's peak it acheievd 59% accuracy on the validation set. It was trained on 7 basic emotion classes with no face cropping, but a bunch of augmentations including the standard ImageNet normalization.
|
|
|
|
| 1 |
+
# VGG Finetuned on AffectNet
|
| 2 |
+
|
| 3 |
VGG model taken finetuned on AffectNet data for prediction of the 7 basic emotions. The model architecture can be described in code as follows:
|
| 4 |
|
| 5 |
```
|
| 6 |
+
from torchvision import models
|
| 7 |
+
import torch.nn as nn
|
| 8 |
+
import torch
|
| 9 |
+
from huggingface_hub import hf_hub_download
|
| 10 |
+
|
| 11 |
class CustomVGG(nn.Module):
|
| 12 |
def __init__(self):
|
| 13 |
super(CustomVGG, self).__init__()
|
|
|
|
| 19 |
self.vgg.classifier[6] = nn.Linear(in_features=4096, out_features=7)
|
| 20 |
|
| 21 |
def forward(self, x):
|
| 22 |
+
# Get features up to classifier[4] (second-to-last layer)
|
| 23 |
+
features = self.vgg.features(x)
|
| 24 |
+
features = self.vgg.avgpool(features)
|
| 25 |
+
features = torch.flatten(features, 1)
|
| 26 |
+
|
| 27 |
+
# Pass through first 5 classifier layers
|
| 28 |
+
for i in range(5):
|
| 29 |
+
features = self.vgg.classifier[i](features)
|
| 30 |
+
|
| 31 |
+
second_to_last = features # Features before final layer
|
| 32 |
+
pred = self.vgg.classifier[6](features) # Final prediction
|
| 33 |
+
|
| 34 |
+
return pred, second_to_last
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# Load model
|
| 38 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 39 |
+
weights_path = hf_hub_download(repo_id="harveymannering/VGG_AffectNet7", filename="vgg_7_best.pth")
|
| 40 |
classifier = CustomVGG()
|
| 41 |
+
classifier.load_state_dict(torch.load(weights_path, map_location=device))
|
| 42 |
classifier = classifier.to(device)
|
| 43 |
+
|
| 44 |
```
|
| 45 |
|
| 46 |
Here is the loss plot for this training run. This checkpoint is taken from the epoch with the best validation loss. At it's peak it acheievd 59% accuracy on the validation set. It was trained on 7 basic emotion classes with no face cropping, but a bunch of augmentations including the standard ImageNet normalization.
|