🚀 對比式深度偽造擴散模型(ECCV 2024)
本項目通過對比學習和全局 - 局部相似度來處理深度偽造擴散問題,提供了模型的相關代碼和使用示例,可用於判斷圖像的真偽。
🚀 快速開始
項目鏈接
代碼示例
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
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()
predictions = self.classifier.predict(features)
return torch.from_numpy(predictions)
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= []
img = Image.open('206496010652.png').convert('RGB')
with torch.no_grad():
in_tens = transform(img).unsqueeze(0)
in_tens= in_tens.to(device)
y_pred.extend(model(in_tens).flatten().tolist())
for el in y_pred:
if el == 1:
print('Fake')
elif el == 0:
print('Real')
elif el == -1:
print('Real')
else:
print('Error')
📄 許可證
本項目採用 MIT 許可證。
📦 相關信息
屬性 |
詳情 |
庫名稱 |
transformers |
標籤 |
deepfake |
數據集 |
elsaEU/ELSA_D3 |
許可證 |
MIT |