sam-brause commited on
Commit
f83cef1
·
1 Parent(s): 318c78b

Added model and handler

Browse files
Files changed (4) hide show
  1. .DS_Store +0 -0
  2. handler.py +108 -0
  3. model_scripted_efficientnet.pt +3 -0
  4. requirements.txt +3 -0
.DS_Store ADDED
Binary file (6.15 kB). View file
 
handler.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision.transforms as transforms
3
+ from PIL import Image
4
+ import json
5
+ import os
6
+
7
+ # Load Model
8
+ MODEL_PATH = "model_scripted_efficientnet.pt"
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+
11
+ model = torch.jit.load(MODEL_PATH, map_location=device)
12
+ model.eval()
13
+
14
+ # Define Transform
15
+ transform = transforms.Compose([
16
+ transforms.Resize((224, 224)),
17
+ transforms.ToTensor(),
18
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
19
+ ])
20
+
21
+ # Mappings
22
+ issue_name_to_area_mapping = {
23
+ "Forehead Wrinkles": "Skin",
24
+ "Crow's Feet Wrinkles": "Skin",
25
+ "Glabella Wrinkles": "Skin",
26
+ "Under Eye Wrinkles": "Skin",
27
+ "Perioral Wrinkles": "Skin",
28
+ "Bunny Lines": "Skin",
29
+ "Neck Lines": "Skin",
30
+ "Dark Spots": "Skin",
31
+ "Red Spots": "Skin",
32
+ "Scars": "Skin",
33
+ "Dry Skin": "Skin",
34
+ "Whiteheads": "Skin",
35
+ "Blackheads": "Skin",
36
+ "Under Eye Dark Circles": "Skin",
37
+ "Crepey Skin": "Skin",
38
+ "Nasolabial Folds": "Skin",
39
+ "Marionette Lines": "Skin",
40
+ "Temporal Hollow": "Forehead",
41
+ "Brow Asymmetry": "Forehead",
42
+ "Flat Forehead": "Forehead",
43
+ "Brow Ptosis": "Forehead",
44
+ "Excess Upper Eyelid Skin": "Eyes",
45
+ "Lower Eyelid - Excess Skin": "Eyes",
46
+ "Upper Eyelid Droop": "Eyes",
47
+ "Lower Eyelid Sag": "Eyes",
48
+ "Lower Eyelid Bags": "Eyes",
49
+ "Under Eye Hollow": "Eyes",
50
+ "Upper Eye Hollow": "Eyes",
51
+ "Mid Cheek Flattening": "Cheeks",
52
+ "Cheekbone - Not Prominent": "Cheeks",
53
+ "Heavy Lateral Cheek": "Cheeks",
54
+ "Crooked Nose": "Nose",
55
+ "Droopy Tip": "Nose",
56
+ "Dorsal Hump": "Nose",
57
+ "Tip Droop When Smiling": "Nose",
58
+ "Thin Lips": "Lips",
59
+ "Lacking Philtral Column": "Lips",
60
+ "Long Philtral Column": "Lips",
61
+ "Gummy Smile": "Lips",
62
+ "Asymmetric Lips": "Lips",
63
+ "Dry Lips": "Lips",
64
+ "Lip Thinning When Smiling": "Lips",
65
+ "Retruded Chin": "Jawline",
66
+ "Over-Projected Chin": "Jawline",
67
+ "Asymmetric Chin": "Jawline",
68
+ "Jowls": "Jawline",
69
+ "Lower Cheeks - Volume Depletion": "Jawline",
70
+ "Ill-Defined Jawline": "Jawline",
71
+ "Asymmetric Jawline": "Jawline",
72
+ "Masseter Hypertrophy": "Jawline",
73
+ "Prejowl Sulcus": "Jawline",
74
+ "Loose Neck Skin": "Jawline",
75
+ "Platysmal Bands": "Jawline",
76
+ "Excess/Submental Fullness": "Jawline"
77
+ }
78
+
79
+ supported_issues = [
80
+ "Dark Spots",
81
+ "Dry Lips",
82
+ "Forehead Wrinkles",
83
+ "Jowls",
84
+ "Nasolabial Folds",
85
+ "Prejowl Sulcus",
86
+ "Thin Lips",
87
+ "Under Eye Hollow",
88
+ "Under Eye Wrinkles",
89
+ "Brow Asymmetry"
90
+ ]
91
+
92
+ def predict(image_path):
93
+ image = Image.open(image_path).convert("RGB")
94
+ image = transform(image).unsqueeze(0).to(device)
95
+
96
+ with torch.no_grad():
97
+ outputs = model(image)
98
+
99
+ predictions = outputs.squeeze().tolist()
100
+ output_labels = [label for label, prob in zip(supported_issues, predictions) if prob > 0.5]
101
+
102
+ return {"predictions": output_labels}
103
+
104
+ # Hugging Face API Inference Handler
105
+ def handle_request(data):
106
+ image_path = data["image_path"]
107
+ results = predict(image_path)
108
+ return json.dumps(results)
model_scripted_efficientnet.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d57cded4178789d368e9825e646e25806447a8526c60388a28304fe043ac2d03
3
+ size 19175250
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ torchvision
3
+ pillow