Deployed backend
Browse files- Dockerfile +14 -0
- backend.py +91 -0
- model.py +55 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Get the image of python
|
| 2 |
+
FROM python:3.9
|
| 3 |
+
|
| 4 |
+
# Copy all the files from local-dir to machine dir
|
| 5 |
+
COPY . .
|
| 6 |
+
|
| 7 |
+
# Set the current directory as working dir
|
| 8 |
+
WORKDIR /
|
| 9 |
+
|
| 10 |
+
# Install the requirements
|
| 11 |
+
RUN pip install --no-cache-dir -r ./requirements.txt
|
| 12 |
+
|
| 13 |
+
# Launch the server
|
| 14 |
+
CMD ["uvicorn", "backend:app", "--host", "0.0.0.0", "--port", "7860"]
|
backend.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from model import get_model
|
| 4 |
+
|
| 5 |
+
import torch as T
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
from torchvision.transforms import v2
|
| 8 |
+
from fastapi import FastAPI, UploadFile, File
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
import json
|
| 12 |
+
import numpy as np
|
| 13 |
+
from PIL import Image
|
| 14 |
+
from io import BytesIO
|
| 15 |
+
|
| 16 |
+
MODEL_IMAGE_WIDTH = 224
|
| 17 |
+
MODEL_IMAGE_HEIGHT = 224
|
| 18 |
+
|
| 19 |
+
transform = v2.Compose([
|
| 20 |
+
v2.Resize((MODEL_IMAGE_HEIGHT, MODEL_IMAGE_WIDTH)),
|
| 21 |
+
v2.ToTensor()
|
| 22 |
+
])
|
| 23 |
+
|
| 24 |
+
######### Utilities #########
|
| 25 |
+
def load_image(image_data):
|
| 26 |
+
image = Image.open(BytesIO(image_data))
|
| 27 |
+
return image
|
| 28 |
+
|
| 29 |
+
def preprocess(image):
|
| 30 |
+
image = image.resize((MODEL_IMAGE_WIDTH, MODEL_IMAGE_HEIGHT))
|
| 31 |
+
image = transform(image)
|
| 32 |
+
|
| 33 |
+
return image
|
| 34 |
+
|
| 35 |
+
def get_prediction(image, model):
|
| 36 |
+
image = T.from_numpy(np.array(image))
|
| 37 |
+
print("image shape: ", image.shape)
|
| 38 |
+
|
| 39 |
+
image = image.unsqueeze(0)
|
| 40 |
+
# image = image.permute(0, 3, 1, 2)
|
| 41 |
+
print("batch size shape: ", image.shape)
|
| 42 |
+
|
| 43 |
+
pred_probs = model(image)
|
| 44 |
+
pred_probs = F.softmax(pred_probs, dim=-1)
|
| 45 |
+
pred_probs = pred_probs.detach().numpy()[0]
|
| 46 |
+
label = np.argmax(pred_probs, axis=-1)
|
| 47 |
+
|
| 48 |
+
return {
|
| 49 |
+
'pred_probs': pred_probs.tolist(),
|
| 50 |
+
'label': int(label)
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
####################################
|
| 54 |
+
|
| 55 |
+
############## Backend #############
|
| 56 |
+
app = FastAPI()
|
| 57 |
+
model = get_model(6, [-1], 0.1)
|
| 58 |
+
|
| 59 |
+
@app.get("/")
|
| 60 |
+
def foo():
|
| 61 |
+
return {
|
| 62 |
+
"status": "Face Expression Classifier"
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
@app.post("/")
|
| 66 |
+
def bar():
|
| 67 |
+
return {
|
| 68 |
+
"status": "Response"
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
@app.post("/get_prediction")
|
| 72 |
+
async def predict(face_img: UploadFile = File(...)):
|
| 73 |
+
image = load_image(await face_img.read())
|
| 74 |
+
|
| 75 |
+
image = preprocess(image)
|
| 76 |
+
|
| 77 |
+
result = get_prediction(image, model)
|
| 78 |
+
print("Model Predicted: \n", result)
|
| 79 |
+
|
| 80 |
+
return {
|
| 81 |
+
'result': json.dumps(result)
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
@app.post("/test")
|
| 85 |
+
def test():
|
| 86 |
+
return {
|
| 87 |
+
'result': {
|
| 88 |
+
'pred_probs': [0.5, 0.2, 0.1],
|
| 89 |
+
'label': 0
|
| 90 |
+
}
|
| 91 |
+
}
|
model.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch as T
|
| 2 |
+
from torch import nn, optim
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import torchvision.models as models
|
| 5 |
+
|
| 6 |
+
from typing import Union, List
|
| 7 |
+
|
| 8 |
+
def get_model(num_classes:int, unfreeze_layers:Union[None, List[int]] = None, drop_rate: Union[None, float] = None):
|
| 9 |
+
|
| 10 |
+
model = models.efficientnet_b7(weights=models.EfficientNet_B7_Weights.DEFAULT)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
for param in model.parameters():
|
| 15 |
+
|
| 16 |
+
param.requires_grad = False
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
if unfreeze_layers is not None and len(unfreeze_layers) > 0:
|
| 21 |
+
|
| 22 |
+
# Now unfreeze the layers in the unfreeze layer/ list
|
| 23 |
+
|
| 24 |
+
for layer_num in unfreeze_layers:
|
| 25 |
+
|
| 26 |
+
for name, child in model.features[layer_num].named_modules():
|
| 27 |
+
|
| 28 |
+
if not isinstance(child, nn.BatchNorm2d) and \
|
| 29 |
+
not isinstance(child, nn.Sequential) and \
|
| 30 |
+
not hasattr(child, 'block'):
|
| 31 |
+
|
| 32 |
+
for param in child.parameters():
|
| 33 |
+
|
| 34 |
+
param.requires_grad = True
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
if drop_rate is not None:
|
| 39 |
+
|
| 40 |
+
model.classifier[0] = nn.Dropout(drop_rate)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# Chagne the classifier head as per our need
|
| 45 |
+
|
| 46 |
+
model.classifier[1] = nn.Linear(2560, num_classes)
|
| 47 |
+
|
| 48 |
+
return model
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
model = get_model(
|
| 52 |
+
6,
|
| 53 |
+
[-1],
|
| 54 |
+
0.1
|
| 55 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
fastapi
|
| 3 |
+
uvicorn[standard]
|
| 4 |
+
tensorflow==2.15.0
|
| 5 |
+
pillow
|
| 6 |
+
numpy
|