Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,65 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline, ViTForImageClassification, ViTImageProcessor
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import warnings
|
| 6 |
+
import logging
|
| 7 |
+
from pytorch_grad_cam import run_dff_on_image, GradCAM
|
| 8 |
+
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
| 9 |
+
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 10 |
+
import torch
|
| 11 |
+
from face_grab import FaceGrabber
|
| 12 |
+
from gradcam import GradCam
|
| 13 |
+
from torchvision import transforms
|
| 14 |
|
| 15 |
+
logging.basicConfig(level=logging.INFO)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
model = ViTForImageClassification.from_pretrained("ongkn/emikes-classifier")
|
| 19 |
+
processor = ViTImageProcessor.from_pretrained("ongkn/emikes-classifier")
|
| 20 |
+
|
| 21 |
+
faceGrabber = FaceGrabber()
|
| 22 |
+
gradCam = GradCam()
|
| 23 |
+
|
| 24 |
+
targetsForGradCam = [ClassifierOutputTarget(gradCam.category_name_to_index(model, "emi")),
|
| 25 |
+
ClassifierOutputTarget(gradCam.category_name_to_index(model, "kes"))]
|
| 26 |
+
targetLayerDff = model.vit.layernorm
|
| 27 |
+
targetLayerGradCam = model.vit.encoder.layer[-2].output
|
| 28 |
+
|
| 29 |
+
def classify_image(input):
|
| 30 |
+
face = faceGrabber.grab_faces(np.array(input))
|
| 31 |
+
if face is None:
|
| 32 |
+
return "No face detected", 0, input
|
| 33 |
+
face = Image.fromarray(face)
|
| 34 |
+
faceResized = face.resize((224, 224))
|
| 35 |
+
tensorResized = transforms.ToTensor()(faceResized)
|
| 36 |
+
dffImage = run_dff_on_image(model=model,
|
| 37 |
+
target_layer=targetLayerDff,
|
| 38 |
+
classifier=model.classifier,
|
| 39 |
+
img_pil=faceResized,
|
| 40 |
+
img_tensor=tensorResized,
|
| 41 |
+
reshape_transform=gradCam.reshape_transform_vit_huggingface,
|
| 42 |
+
n_components=5,
|
| 43 |
+
top_k=10
|
| 44 |
+
)
|
| 45 |
+
result = gradCam.get_top_category(model, tensorResized)
|
| 46 |
+
cls = result[0]["label"]
|
| 47 |
+
clsIdx = gradCam.category_name_to_index(model, cls)
|
| 48 |
+
clsTarget = ClassifierOutputTarget(clsIdx)
|
| 49 |
+
gradCamImage = gradCam.run_grad_cam_on_image(model=model,
|
| 50 |
+
target_layer=targetLayerGradCam,
|
| 51 |
+
targets_for_gradcam=[clsTarget],
|
| 52 |
+
input_tensor=tensorResized,
|
| 53 |
+
input_image=faceResized,
|
| 54 |
+
reshape_transform=gradCam.reshape_transform_vit_huggingface)
|
| 55 |
+
return result[0]["label"], result[0]["score"], face, dffImage, gradCamImage
|
| 56 |
+
|
| 57 |
+
iface = gr.Interface(
|
| 58 |
+
fn=classify_image,
|
| 59 |
+
inputs="image",
|
| 60 |
+
outputs=["text", "number", "image", "image", "image"],
|
| 61 |
+
title="Attraction Classifier - subjective",
|
| 62 |
+
description=f"Takes in a (224, 224) image and outputs a class: {'emi', 'kes'}, 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](https://huggingface.co/privacy) for details.
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
iface.launch()
|