File size: 3,191 Bytes
fa61e0a
 
bf4a3d3
 
 
 
c2a0c92
fa61e0a
 
bf4a3d3
 
 
 
 
 
fa61e0a
 
 
 
9fe2003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc1a7f4
9fe2003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc1a7f4
b86c1ba
9fe2003
 
 
 
 
 
 
 
 
 
f32ae5c
 
9fe2003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
---
library_name: transformers
tags:
- deepfake
datasets:
- elsaEU/ELSA_D3
license: mit
---

# Contrasting Deepfakes Diffusion via Contrastive Learning and Global-Local Similarities (ECCV 2024)

[Project page](https://aimagelab.github.io/CoDE/)

[Source code](https://github.com/aimagelab/CoDE)

# Model Card for Model ID

<!-- Provide the basic links for the model. -->

```python
import transformers
from huggingface_hub import hf_hub_download
from PIL import Image
import faiss
import timm
import torch
import torch.nn as nn
import joblib
from torchvision import transforms

if torch.cuda.is_available():
    device = torch.device('cuda')
else: device = torch.device('cpu')

'''
linear - knn --> 0 Real - 1 Fake
svm --> -1 Real - 1 Fake
'''
class VITContrastiveHF(nn.Module):
    def __init__(self, repo_name, classificator_type):
        super(VITContrastiveHF, self).__init__()
        self.model = transformers.AutoModel.from_pretrained(repo_name)
        self.model.pooler= nn.Identity()

        self.processor = transformers.AutoProcessor.from_pretrained(repo_name)
        self.processor.do_resize= False
        # define the correct classifier /// consider to use the `cache_dir`` parameter
        if classificator_type == 'svm':
            file_path = hf_hub_download(repo_id=repo_name, filename='sklearn/ocsvm_kernel_poly_gamma_auto_nu_0_1_crop.joblib')
            self.classifier = joblib.load(file_path)

        elif classificator_type == 'linear':
            file_path = hf_hub_download(repo_id=repo_name, filename='sklearn/linear_tot_classifier_epoch-32.sav')
            self.classifier = joblib.load(file_path)

        elif classificator_type == 'knn':
            file_path = hf_hub_download(repo_id=repo_name, filename='sklearn/knn_tot_classifier_epoch-32.sav')
            self.classifier = joblib.load(file_path)
            
        else:
            raise ValueError('Selected an invalid classifier')

    def forward(self, x, return_feature=False):
        features = self.model(x)
        if return_feature:
            return features
        features = features.last_hidden_state[:,0,:].cpu().detach().numpy()
        # features.last_hidden_state[:,0,:].shape
        predictions = self.classifier.predict(features)
        return torch.from_numpy(predictions)
    
# HF inference code
classificator_type = 'linear'
model = VITContrastiveHF(repo_name='aimagelab/CoDE', classificator_type=classificator_type)

transform = transforms.Compose([
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

model.eval()
model.model.to(device)
y_pred= []
# Put your image to tes
img = Image.open('206496010652.png').convert('RGB')

with torch.no_grad():
    # in_tens = model.processor(img, return_tensors='pt')['pixel_values']
    in_tens = transform(img).unsqueeze(0)

    in_tens= in_tens.to(device)
    y_pred.extend(model(in_tens).flatten().tolist())

# check the correct label of the predict image
for el in y_pred:
    if el == 1:
        print('Fake')
    elif el == 0:
        print('Real')
    elif el == -1:
        print('Real')
    else:
        print('Error')

```