commit files to HF hub
Browse files- .gitattributes +1 -0
- __pycache__/pipeline.cpython-310.pyc +3 -0
- config.json +20 -1
- pipeline.py +72 -0
- pytorch_model.bin +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
__pycache__/pipeline.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
__pycache__/pipeline.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:46de4d880f08624a93de2c8ad10f5b4e7194112faf429e0ab1879f4c50d2bfff
|
| 3 |
+
size 2926
|
config.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
{
|
| 2 |
"DEVICE": "cpu",
|
|
|
|
| 3 |
"architectures": [
|
| 4 |
"DeepFakeModel"
|
| 5 |
],
|
|
@@ -7,7 +8,25 @@
|
|
| 7 |
"AutoConfig": "deepfakeconfig.DeepFakeConfig",
|
| 8 |
"AutoModelForImageClassification": "deepfakemodel.DeepFakeModel"
|
| 9 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"model_type": "ResNet",
|
| 11 |
"torch_dtype": "float32",
|
| 12 |
-
"transformers_version": "4.
|
| 13 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"DEVICE": "cpu",
|
| 3 |
+
"_name_or_path": "not-lain/deepfake",
|
| 4 |
"architectures": [
|
| 5 |
"DeepFakeModel"
|
| 6 |
],
|
|
|
|
| 8 |
"AutoConfig": "deepfakeconfig.DeepFakeConfig",
|
| 9 |
"AutoModelForImageClassification": "deepfakemodel.DeepFakeModel"
|
| 10 |
},
|
| 11 |
+
"custom_pipelines": {
|
| 12 |
+
"deepfake": {
|
| 13 |
+
"default": {
|
| 14 |
+
"model": {
|
| 15 |
+
"pt": [
|
| 16 |
+
"not-lain/deepfake",
|
| 17 |
+
"main"
|
| 18 |
+
]
|
| 19 |
+
}
|
| 20 |
+
},
|
| 21 |
+
"impl": "pipeline.DeepFakePipeline",
|
| 22 |
+
"pt": [
|
| 23 |
+
"AutoModelForImageClassification"
|
| 24 |
+
],
|
| 25 |
+
"tf": [],
|
| 26 |
+
"type": "multimodal"
|
| 27 |
+
}
|
| 28 |
+
},
|
| 29 |
"model_type": "ResNet",
|
| 30 |
"torch_dtype": "float32",
|
| 31 |
+
"transformers_version": "4.26.1"
|
| 32 |
}
|
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)
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:55b70cf572d8fcd290f26b474c9b1a0c8379f324df3f6932555ea0a633b89c6a
|
| 3 |
+
size 94273065
|