library_name: transformers
tags:
- 深度伪造
datasets:
- elsaEU/ELSA_D3
license: mit
基于对比学习与全局-局部相似性的深度伪造扩散对比方法(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')
'''
线性分类器 - knn --> 0真实 1伪造
svm分类器 --> -1真实 1伪造
'''
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('选择了无效的分类器')
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('伪造')
elif el == 0:
print('真实')
elif el == -1:
print('真实')
else:
print('错误')