Create pipeline.py
Browse files- pipeline.py +72 -0
pipeline.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers.pipelines import PIPELINE_REGISTRY
|
| 2 |
+
from transformers import Pipeline, AutoModelForImageClassification
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import cv2
|
| 6 |
+
from pytorch_grad_cam import GradCAM
|
| 7 |
+
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
| 8 |
+
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 9 |
+
from facenet_pytorch import MTCNN
|
| 10 |
+
import torch.nn.functional as F
|
| 11 |
+
|
| 12 |
+
class DeepFakePipeline(Pipeline):
|
| 13 |
+
def __init__(self,**kwargs):
|
| 14 |
+
Pipeline.__init__(self,**kwargs)
|
| 15 |
+
def _sanitize_parameters(self, **kwargs):
|
| 16 |
+
return {}, {}, {}
|
| 17 |
+
def preprocess(self, inputs):
|
| 18 |
+
return inputs
|
| 19 |
+
def _forward(self,input):
|
| 20 |
+
return input
|
| 21 |
+
def postprocess(self,confidences,face_with_mask):
|
| 22 |
+
out = {"confidences":confidences,
|
| 23 |
+
"face_with_mask": face_with_mask}
|
| 24 |
+
return out
|
| 25 |
+
|
| 26 |
+
def predict(self,input_image:str):
|
| 27 |
+
DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
| 28 |
+
mtcnn = MTCNN(
|
| 29 |
+
select_largest=False,
|
| 30 |
+
post_process=False,
|
| 31 |
+
device=DEVICE)
|
| 32 |
+
mtcnn.to(DEVICE)
|
| 33 |
+
model = self.model.model
|
| 34 |
+
model.to(DEVICE)
|
| 35 |
+
|
| 36 |
+
input_image = Image.open(input_image)
|
| 37 |
+
face = mtcnn(input_image)
|
| 38 |
+
if face is None:
|
| 39 |
+
raise Exception('No face detected')
|
| 40 |
+
|
| 41 |
+
face = face.unsqueeze(0) # add the batch dimension
|
| 42 |
+
face = F.interpolate(face, size=(256, 256), mode='bilinear', align_corners=False)
|
| 43 |
+
|
| 44 |
+
# convert the face into a numpy array to be able to plot it
|
| 45 |
+
prev_face = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
|
| 46 |
+
prev_face = prev_face.astype('uint8')
|
| 47 |
+
|
| 48 |
+
face = face.to(DEVICE)
|
| 49 |
+
face = face.to(torch.float32)
|
| 50 |
+
face = face / 255.0
|
| 51 |
+
face_image_to_plot = face.squeeze(0).permute(1, 2, 0).cpu().detach().int().numpy()
|
| 52 |
+
|
| 53 |
+
target_layers=[model.block8.branch1[-1]]
|
| 54 |
+
cam = GradCAM(model=model, target_layers=target_layers)
|
| 55 |
+
targets = [ClassifierOutputTarget(0)]
|
| 56 |
+
grayscale_cam = cam(input_tensor=face, targets=targets,eigen_smooth=True)
|
| 57 |
+
grayscale_cam = grayscale_cam[0, :]
|
| 58 |
+
visualization = show_cam_on_image(face_image_to_plot, grayscale_cam, use_rgb=True)
|
| 59 |
+
face_with_mask = cv2.addWeighted(prev_face, 1, visualization, 0.5, 0)
|
| 60 |
+
|
| 61 |
+
with torch.no_grad():
|
| 62 |
+
output = torch.sigmoid(model(face).squeeze(0))
|
| 63 |
+
prediction = "real" if output.item() < 0.5 else "fake"
|
| 64 |
+
|
| 65 |
+
real_prediction = 1 - output.item()
|
| 66 |
+
fake_prediction = output.item()
|
| 67 |
+
|
| 68 |
+
confidences = {
|
| 69 |
+
'real': real_prediction,
|
| 70 |
+
'fake': fake_prediction
|
| 71 |
+
}
|
| 72 |
+
return self.postprocess(confidences, face_with_mask)
|