Commit
·
f3f8c30
1
Parent(s):
4ead16c
Model Iploaded
Browse files- .gitattributes +1 -0
- README.md +19 -1
- __pycache__/pipeline.cpython-310.pyc +3 -0
- config.json +32 -0
- deepfakeconfig.py +7 -0
- deepfakemodel.py +18 -0
- model.safetensors +3 -0
- pipeline.py +72 -0
- pytorch_model.bin +3 -0
- requirements.txt +6 -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
|
README.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
| 1 |
---
|
| 2 |
-
license:
|
|
|
|
|
|
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
base_model: aaronespasa/deepfake-detection-resnetinceptionv1
|
| 4 |
+
library_name: transformers
|
| 5 |
---
|
| 6 |
+
# original model repo :
|
| 7 |
+
📖 this is a cutomized version of the following model [aaronespasa/deepfake-detection-resnetinceptionv1](https://huggingface.co/aaronespasa/deepfake-detection-resnetinceptionv1)
|
| 8 |
+
# how to use
|
| 9 |
+
```python
|
| 10 |
+
from transformers import pipeline
|
| 11 |
+
pipe = pipeline(model="not-lain/deepfake",trust_remote_code=True)
|
| 12 |
+
pipe.predict("img_path.jpg")
|
| 13 |
+
```
|
| 14 |
+
```python
|
| 15 |
+
>> {"confidences":confidences,"face_with_mask": face_with_mask}
|
| 16 |
+
```
|
| 17 |
+
# dependencies
|
| 18 |
+
to install related dependencies simply use the command
|
| 19 |
+
```
|
| 20 |
+
!wget https://huggingface.co/not-lain/deepfake/resolve/main/requirements.txt && pip install -r requirements.txt
|
| 21 |
+
```
|
__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
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"DEVICE": "cpu",
|
| 3 |
+
"_name_or_path": "not-lain/deepfake",
|
| 4 |
+
"architectures": [
|
| 5 |
+
"DeepFakeModel"
|
| 6 |
+
],
|
| 7 |
+
"auto_map": {
|
| 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 |
+
}
|
deepfakeconfig.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
import torch
|
| 3 |
+
class DeepFakeConfig(PretrainedConfig):
|
| 4 |
+
model_type = "ResNet"
|
| 5 |
+
def __init__(self,**kwargs):
|
| 6 |
+
super().__init__(**kwargs)
|
| 7 |
+
self.DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
deepfakemodel.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PreTrainedModel
|
| 2 |
+
from facenet_pytorch import MTCNN, InceptionResnetV1
|
| 3 |
+
from .deepfakeconfig import DeepFakeConfig
|
| 4 |
+
|
| 5 |
+
class DeepFakeModel(PreTrainedModel):
|
| 6 |
+
config_class = DeepFakeConfig
|
| 7 |
+
def __init__(self, config):
|
| 8 |
+
super().__init__(config)
|
| 9 |
+
self.model = InceptionResnetV1(
|
| 10 |
+
pretrained="vggface2",
|
| 11 |
+
classify=True,
|
| 12 |
+
num_classes=1,
|
| 13 |
+
device=config.DEVICE
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
DeepFakeConfig.register_for_auto_class()
|
| 18 |
+
DeepFakeModel.register_for_auto_class("AutoModelForImageClassification")
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f1300ca17461156f633c87852ca294a257260ab7b965cff81b448f1c295dacd1
|
| 3 |
+
size 94126980
|
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
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Pillow
|
| 2 |
+
facenet-pytorch==2.5.2
|
| 3 |
+
torch==1.11.0
|
| 4 |
+
opencv-python
|
| 5 |
+
grad-cam
|
| 6 |
+
transformers
|