Package: advt
Attack & Defence on Video TasksOverviewADVT(Attack Defence on Video Tasks) is an adversarial attack/defence toolbox open source library based on Pytorch.
This repository mainly implements some adversarial attack & defence algorithms and provides some video processing apis.FeaturesThe ADVT library has five functional features, which cover the whole process:PreprocessAttackDefenceRecordVisualizationAttackThis module implements attack methods. All attack methods are from top computer conferences in recent 5 years:FGSM [Explaining and harnessing adversarial examples]BIM [Adversarial examples in the physical world]MIM [Boosting adversarial attacks with momentum(CVPR-18)]DeepFool [DeepFool: A simple and accurate method to fool deep neural networks(CVPR-16)]DIM [Improving Transferability of Adversarial Examples with Input Diversity(CVPR-18)]C&W [Towards evaluating the robustness of neural networks(IEEE SP-17)]Universal [Universal adversarial perturbations(CVPR-17)]ZOO [ZOO: Zeroth Order Optimization Based Black-box Attacks to Deep Neural Networks without Training Substitute Models(ACMAIS)]Sparse ADV [Sparse Adversarial Perturbations for Videos(AAAI-19)]DefenceThis module implements defence methods.Bit-depth ReductionTotal Variance MinimizationImage QuiltingComDefend [ComDefend: An Efficient Image Compression Model to Defend Adversarial Examples(CVPR-18)]Randomization [Mitigating Adversarial Effects Through Randomization(ICLR-18)]Evaluation & VisualizationADVT provides some useful evaluation & visualization tools.Evaluation Metric:PSNRSSMImAPVisualization:Video-to-FramesFrames-to-VideoChanged-PixelInstallationBefore installation, make sure you install fellow requirements.numpy==1.18.5
opencv-python==4.4.0.42torch==1.7.0urllib==1.26.4You can install advt through pypi or build from source code.1. from pypipipinstalladvt2. from source codegitclonehttps://github.com/WindFantasy98/ADVT.git
pipinstall-e.Usageattack example# python3.7 torch1.7importtorchimporttorchvision.transformsastransformsimporttorchvision.datasetsasdatasetsfromtorch.utils.dataimportDataLoaderfromadvt.model.cnnimportCNNfromadvt.attackimportFGSMPATH_PARAMETERS='tests/cnn_model.pth'defmain():transform=transforms.Compose([transforms.ToTensor()])t=transforms.Compose([transforms.ToPILImage()])test_dataset=datasets.CIFAR10(root='/data',train=False,transform=transform,download=True)test_loader=DataLoader(dataset=test_dataset,batch_size=1,shuffle=False)device=torch.device("cuda:0"iftorch.cuda.is_available()else"cpu")net=CNN()net.load_state_dict(torch.load(PATH_PARAMETERS))net=net.to(device)fgsm=FGSM(net,device)attack_succ=0total_num=0fori,(img,lbl)inenumerate(test_loader):img,lbl=img.to(device),lbl.to(device)adv_img=fgsm.attack(img,lbl)output=net(adv_img)_,pred_indice=output.max(1)total_num+=len(lbl)attack_succ+=(pred_indice==lbl).sum().item()if(i+1)%20==0:print('batch{}:'.format((i+1)//20),'total tested number:{}, correct number:{}'.format(total_num,attack_succ))if__name__=='__main__':main()defence example# python3.7 torch1.7importtorchimporttorchvision.transformsastransformsimporttorchvision.datasetsasdatasetsfromtorch.utils.dataimportDataLoaderfromadvt.model.cnnimportCNNfromadvt.attackimportDeepFoolfromadvt.defenceimportRandomizationPATH_PARAMETERS='tests/cnn_model.pth'defmain():# initialize datasettransform=transforms.Compose([transforms.ToTensor()])t=transforms.Compose([transforms.ToPILImage()])test_dataset=datasets.CIFAR10(root='/data',train=False,transform=transform,download=True)test_loader=DataLoader(dataset=test_dataset,batch_size=1,shuffle=False)device=torch.device("cuda:0"iftorch.cuda.is_available()else"cpu")# load victim modelnet=CNN()net.load_state_dict(torch.load(PATH_PARAMETERS))net=net.to(device)# initialize attack methoddf=DeepFool(net,device)# initialize defend methodrand_defend=Randomization(net,device)# initialize indicatorattack_succ=0total_num=0# start attackfori,(img,lbl)inenumerate(test_loader):img,lbl=img.to(device),lbl.to(device)adv_img=df.attack(img,lbl)# get adv sampleoutput=rand_defend.defend(adv_img)# get processed sample_,pred_indice=output.max(1)total_num+=len(lbl)attack_succ+=(pred_indice==lbl).sum().item()if(i+1)%20==0:print('batch{}:'.format((i+1)//20),'total tested number:{}, correct number:{}'.format(total_num,attack_succ))if__name__=='__main__':main()This repo is still under maintenance. For more information, please contact with me.