Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,16 @@ import numpy as np
|
|
4 |
from PIL import Image
|
5 |
import cv2 as cv
|
6 |
import dlib
|
|
|
7 |
import logging
|
8 |
from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
11 |
logging.basicConfig(level=logging.INFO)
|
@@ -71,19 +79,49 @@ processor = ViTImageProcessor.from_pretrained("ongkn/attraction-classifier")
|
|
71 |
|
72 |
pipe = pipeline("image-classification", model=model, feature_extractor=processor)
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
def classify_image(input):
|
75 |
-
face = grab_faces(np.array(input))
|
76 |
if face is None:
|
77 |
return "No face detected", 0, input
|
78 |
face = Image.fromarray(face)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
result = pipe(face)
|
80 |
-
return result[0]["label"], result[0]["score"], face
|
81 |
|
82 |
iface = gr.Interface(
|
83 |
fn=classify_image,
|
84 |
inputs="image",
|
85 |
-
outputs=["text", "number", "image"],
|
86 |
title="Attraction Classifier - subjective",
|
87 |
-
description=f"Takes in a (224, 224) image and outputs an attraction class: {'pos', 'neg'}. Face detection, cropping, and resizing are done internally. Uploaded images are not stored by us, but may be stored by HF. Refer to their privacy policy for details."
|
88 |
)
|
89 |
iface.launch()
|
|
|
4 |
from PIL import Image
|
5 |
import cv2 as cv
|
6 |
import dlib
|
7 |
+
import warnings
|
8 |
import logging
|
9 |
from typing import Optional
|
10 |
+
from pytorch_grad_cam import run_dff_on_image, GradCAM
|
11 |
+
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
12 |
+
from pytorch_grad_cam.utils.image import show_cam_on_image
|
13 |
+
import torch
|
14 |
+
from face_grab import FaceGrabber
|
15 |
+
from gradcam import GradCam
|
16 |
+
from torchvision import transforms
|
17 |
|
18 |
|
19 |
logging.basicConfig(level=logging.INFO)
|
|
|
79 |
|
80 |
pipe = pipeline("image-classification", model=model, feature_extractor=processor)
|
81 |
|
82 |
+
faceGrabber = FaceGrabber()
|
83 |
+
gradCam = GradCam()
|
84 |
+
|
85 |
+
targetsForGradCam = [ClassifierOutputTarget(gradCam.category_name_to_index(model, "pos")),
|
86 |
+
ClassifierOutputTarget(gradCam.category_name_to_index(model, "neg"))]
|
87 |
+
targetLayerDff = model.vit.layernorm
|
88 |
+
targetLayerGradCam = model.vit.encoder.layer[-2].output
|
89 |
+
|
90 |
def classify_image(input):
|
91 |
+
face = faceGrabber.grab_faces(np.array(input))
|
92 |
if face is None:
|
93 |
return "No face detected", 0, input
|
94 |
face = Image.fromarray(face)
|
95 |
+
imgTensor = transforms.ToTensor()(face)
|
96 |
+
tensor = transforms.ToTensor()(face)
|
97 |
+
dffImage = run_dff_on_image(model=model,
|
98 |
+
target_layer=targetLayerDff,
|
99 |
+
classifier=model.classifier,
|
100 |
+
img_pil=face,
|
101 |
+
img_tensor=tensor,
|
102 |
+
reshape_transform=gradCam.reshape_transform_vit_huggingface,
|
103 |
+
n_components=5,
|
104 |
+
top_k=10,
|
105 |
+
threshold=0,
|
106 |
+
)
|
107 |
+
gradCamImage = gradCam.run_grad_cam_on_image(model=model,
|
108 |
+
target_layer=targetLayerGradCam,
|
109 |
+
classifier=model.classifier,
|
110 |
+
img_pil=face,
|
111 |
+
img_tensor=tensor,
|
112 |
+
reshape_transform=gradCam.reshape_transform_vit_huggingface,
|
113 |
+
n_components=5,
|
114 |
+
top_k=10,
|
115 |
+
threshold=0,
|
116 |
+
)
|
117 |
result = pipe(face)
|
118 |
+
return result[0]["label"], result[0]["score"], face, dffImage, gradCamImage
|
119 |
|
120 |
iface = gr.Interface(
|
121 |
fn=classify_image,
|
122 |
inputs="image",
|
123 |
+
outputs=["text", "number", "image", "image", "image"],
|
124 |
title="Attraction Classifier - subjective",
|
125 |
+
description=f"Takes in a (224, 224) image and outputs an attraction class: {'pos', 'neg'}, along with a GradCam/DFF explanation. Face detection, cropping, and resizing are done internally. Uploaded images are not stored by us, but may be stored by HF. Refer to their privacy policy for details."
|
126 |
)
|
127 |
iface.launch()
|