Briefly describe what this code sample all about does:

Github:
models
New commit
30 minutes ago
static
Add files via upload
46 minutes ago
templates
Add files via upload
45 minutes ago
DLOPs_Project_Code.ipynb
Add files via upload
last month
DLOps_Project_Report.pdf
Add files via upload
last month
README.md
Update README.md
last month
app.py
Add files via upload
49 minutes ago



README.md
Human-Face-Emotion-recognition
The FER2013 dataset, which contains 48x48 pixel grayscale images of faces labelled with seven emotion categories is used for facial expression recognition tasks.

We used PyTorch in this project to implement, train, and test various deep learning models. The dataset was divided into two parts: a training set and a test set, with data augmentation techniques like random cropping, rotation, and horizontal flipping applied to the training set to improve the model's generalisation capabilities. We tried out the following models:

Custom Model: A custom CNN architecture was implemented, consisting of four convolutional layers followed by max-pooling and dropout layers, and two fully connected layers.

-ResNet18: A pre-trained ResNet18 model was fine-tuned for the facial expression recognition task.

-MobileNetV2: A pre-trained MobileNetV2 model was fine-tuned for the facial expression recognition task.

-SqueezeNet1_0: A pre-trained SqueezeNet1_0 model was fine-tuned for the facial expression recognition task.

-ShuffleNetV2_x1_0: A pre-trained ShuffleNetV2_x1_0 model was fine-tuned for the facial expression recognition task.

Link where the model is deployed: https://huggingface.co/spaces/ruthvik2/DLOps_Project_1


app.py:
import os

import torch
#print(torch.version.cuda)

import torch.nn as nn

from torchvision.models import resnet18, mobilenet_v2, squeezenet1_0, shufflenet_v2_x1_0
from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize

from PIL import Image

import streamlit as st


# torchvision transforms
MEAN = [0.5, 0.5, 0.5]
STD = [0.5, 0.5, 0.5]

transform = Compose([
    Resize(256),
    CenterCrop(224),
    ToTensor(),
    Normalize(MEAN, STD)
])


# initializing models
device = "cuda" if torch.cuda.is_available() else "cpu"

resnet = resnet18()
resnet.fc = nn.Linear(resnet.fc.in_features, 7)
resnet.load_state_dict(torch.load("models/emotion_detection_model_resnet.pth", map_location=torch.device('cpu')))
resnet.to(device)
resnet.eval()

mobilenet = mobilenet_v2()
mobilenet.classifier[1] = nn.Linear(mobilenet.classifier[1].in_features, 7)
mobilenet.load_state_dict(torch.load("models/emotion_detection_model_mobilenet.pth", map_location=torch.device('cpu')))
mobilenet.to(device)
mobilenet.eval()

squeezenet = squeezenet1_0()
squeezenet.classifier[1] = nn.Conv2d(squeezenet.classifier[1].in_channels, 7, 1, 1)
squeezenet.load_state_dict(torch.load("models/emotion_detection_model_squeezenet.pth", map_location=torch.device('cpu')))
squeezenet.to(device)
squeezenet.eval()

shufflenet = shufflenet_v2_x1_0()
shufflenet.fc = nn.Linear(shufflenet.fc.in_features, 7)
shufflenet.load_state_dict(torch.load("models/emotion_detection_model_shufflenet.pth", map_location=torch.device('cpu')))
shufflenet.to(device)
shufflenet.eval()

UPLOAD_PATH = "static/temp/"
if not os.path.isdir(UPLOAD_PATH):
    os.mkdir(UPLOAD_PATH)

LABEL_MAP = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']


def predict(image_path, model_name="shufflenet"):
    # PIL Image load and predict
    image = Image.open(image_path)
    image = image.convert("RGB")

    image = transform(image)
    image = image.unsqueeze(0).to(device)

    with torch.no_grad():
        if model_name == "resnet":
            outs = resnet(image)
        elif model_name == "shufflenet":
            outs = shufflenet(image)
        elif model_name == "squeezenet":
            outs = squeezenet(image)
        elif model_name == "mobilenet":
            outs = mobilenet(image)

    outs = torch.argmax(outs, dim=-1)[0]
    outs = LABEL_MAP[outs]

    return outs


def main():
    st.title("Emotion Classifier App")

    model_name = st.selectbox("Select model", ("resnet", "shufflenet", "squeezenet", "mobilenet"))
    image_file = st.file_uploader("Upload an image here", type=["jpg", "jpeg", "png"])

    if image_file is not None:
        image = Image.open(image_file)
        st.image(image, caption="Uploaded Image", use_column_width=True)

        image_path = os.path.join(UPLOAD_PATH, image_file.name)
        image.save(image_path)

        pred = predict(image_path, model_name)
        st.header("Prediction")
        st.write("Predicted emotion:", pred)

if __name__ == "__main__":
    main()