当前位置: 首页 > news >正文

黑龙江省建设教育信息网网站网站做锚点

黑龙江省建设教育信息网网站,网站做锚点,钢筋网片理论重量表规格表,探测网站是什么程序做的Pytorch | 利用DTA针对CIFAR10上的ResNet分类器进行对抗攻击 CIFAR数据集DTA介绍算法流程 DTA代码实现DTA算法实现攻击效果 代码汇总dta.pytrain.pyadvtest.py 之前已经针对CIFAR10训练了多种分类器#xff1a; Pytorch | 从零构建AlexNet对CIFAR10进行分类 Pytorch | 从零构建… Pytorch | 利用DTA针对CIFAR10上的ResNet分类器进行对抗攻击 CIFAR数据集DTA介绍算法流程 DTA代码实现DTA算法实现攻击效果 代码汇总dta.pytrain.pyadvtest.py 之前已经针对CIFAR10训练了多种分类器 Pytorch | 从零构建AlexNet对CIFAR10进行分类 Pytorch | 从零构建Vgg对CIFAR10进行分类 Pytorch | 从零构建GoogleNet对CIFAR10进行分类 Pytorch | 从零构建ResNet对CIFAR10进行分类 Pytorch | 从零构建MobileNet对CIFAR10进行分类 Pytorch | 从零构建EfficientNet对CIFAR10进行分类 Pytorch | 从零构建ParNet对CIFAR10进行分类 也实现了一些攻击算法 Pytorch | 利用FGSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用BIM/I-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用MI-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用NI-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用PI-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用VMI-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用VNI-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用EMI-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用AI-FGTM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用I-FGSSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用SMI-FGRM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用VA-I-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用PC-I-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用IE-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用GRA针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用GNP针对CIFAR10上的ResNet分类器进行对抗攻击 Pytorch | 利用MIG针对CIFAR10上的ResNet分类器进行对抗攻击 本篇文章我们使用Pytorch实现DTA对CIFAR10上的ResNet分类器进行攻击. CIFAR数据集 CIFAR-10数据集是由加拿大高级研究所CIFAR收集整理的用于图像识别研究的常用数据集基本信息如下 数据规模该数据集包含60,000张彩色图像分为10个不同的类别每个类别有6,000张图像。通常将其中50,000张作为训练集用于模型的训练10,000张作为测试集用于评估模型的性能。图像尺寸所有图像的尺寸均为32×32像素这相对较小的尺寸使得模型在处理该数据集时能够相对快速地进行训练和推理但也增加了图像分类的难度。类别内容涵盖了飞机plane、汽车car、鸟bird、猫cat、鹿deer、狗dog、青蛙frog、马horse、船ship、卡车truck这10个不同的类别这些类别都是现实世界中常见的物体具有一定的代表性。 下面是一些示例样本 DTA介绍 DTADirection Tuning Attack, 方向调整攻击是一种新型迁移式对抗攻击方法旨在提高对抗样本的可迁移性。其核心思想是在每次更新迭代中通过小步长采样多个示例并使用这些采样示例的平均梯度来更新对抗样本从而减小实际更新方向与最速更新方向之间的角度同时减轻更新振荡。 算法流程 初始化设置扰动幅度 ϵ \epsilon ϵ、迭代次数 T T T、大步长 α ϵ / T \alpha\epsilon/T αϵ/T、动量衰减因子 μ 1 \mu_1 μ1​、内循环迭代次数 K K K和内循环动量衰减因子 μ 2 \mu_2 μ2​。初始化梯度 g 0 0 g_0 0 g0​0对抗样本 x 0 a d v x x^{adv}_0 x x0adv​x。外循环从 t 0 到 T - 1 内循环初始化对于第 t t t次外循环迭代设置 g t , 0 g t g_{t,0} g_t gt,0​gt​和 x t , 0 a d v x t a d v x^{adv}_{t,0} x^{adv}_t xt,0adv​xtadv​。内循环从 k 0 到 K - 1 前瞻操作look ahead operation计算前瞻对抗样本 x t , k n e s x^{nes}_{t,k} xt,knes​ x t , k n e s x t , k a d v α ⋅ μ 1 ⋅ g t , k x^{nes}_{t,k} x^{adv}_{t,k} \alpha \cdot \mu_1 \cdot g_{t,k} xt,knes​xt,kadv​α⋅μ1​⋅gt,k​。计算梯度计算第 k k k个采样示例的梯度 g t , k 1 g_{t,k1} gt,k1​ g t , k 1 μ 2 ⋅ g t , k ∇ x t , k a d v L ( x t , k n e s , y ; θ ) ∥ ∇ x t , k a d v L ( x t , k n e s , y ; θ ) ∥ 1 g_{t,k1}\mu _{2}\cdot g_{t,k}\frac {\nabla _{x_{t,k}^{a d v}} L\left(x_{t, k}^{nes },y ; \theta\right) }{\parallel \nabla _{x_{t, k}^{a d v}} L\left(x_{t, k}^{n e s}, y ; \theta\right) \parallel _{1}} gt,k1​μ2​⋅gt,k​∥∇xt,kadv​​L(xt,knes​,y;θ)∥1​∇xt,kadv​​L(xt,knes​,y;θ)​其中 μ 2 \mu_2 μ2​是内循环中 g t , k g_{t,k} gt,k​的衰减因子 x t , k a d v x^{adv}_{t,k} xt,kadv​表示第 k k k个采样示例 ∇ x t , k a d v L ( x t , k n e s , y ; θ ) \nabla_{x_{t,k}^{adv}} L\left(x_{t, k}^{nes },y ; \theta\right) ∇xt,kadv​​L(xt,knes​,y;θ)是在 x t , k n e s x^{nes}_{t,k} xt,knes​处的损失函数关于 x x x的梯度。更新采样示例使用小步长 α K \frac{\alpha}{K} Kα​更新采样示例 x t , k 1 a d v x^{adv}_{t,k1} xt,k1adv​ x t , k 1 a d v C l i p x ϵ { x t , k a d v α K ⋅ s i g n ( g t , k 1 ) } x_{t, k1}^{a d v}Clip_{x}^{\epsilon}\left\{x_{t, k}^{a d v}\frac{\alpha}{K} \cdot sign\left(g_{t, k1}\right)\right\} xt,k1adv​Clipxϵ​{xt,kadv​Kα​⋅sign(gt,k1​)}其中 C l i p x ϵ ( ⋅ ) Clip_{x}^{\epsilon}(\cdot) Clipxϵ​(⋅)函数将更新后的示例限制在以 x x x为中心、半径为 ϵ \epsilon ϵ的范围内。 计算平均梯度计算内循环中 K K K个采样示例的平均梯度 g t 1 μ 1 ⋅ g t ∑ k 1 K g t , k K g_{t1}\mu _{1}\cdot g_{t}\frac{\sum_{k1}^{K} g_{t, k}}{K} gt1​μ1​⋅gt​K∑k1K​gt,k​​。更新对抗样本使用大步长 α \alpha α和平均梯度 g t 1 g_{t1} gt1​更新对抗样本 x t 1 a d v x^{adv}_{t1} xt1adv​ x t 1 a d v C l i p x ϵ { x t a d v α ⋅ s i g n ( g t 1 ) } x_{t1}^{a d v}Clip_{x}^{\epsilon}\left\{x_{t}^{a d v}\alpha \cdot sign\left(g_{t1}\right)\right\} xt1adv​Clipxϵ​{xtadv​α⋅sign(gt1​)}。 输出经过 T T T次迭代后输出最终的对抗样本 x a d v x T a d v x^{adv}x^{adv}_T xadvxTadv​。 DTA代码实现 DTA算法实现 import torch import torch.nn as nndef DTA(model, criterion, original_images, labels, epsilon, num_iterations10, decay11.0, decay20.0, K10):DTA (Direction Tuning Attack)参数:- model: 要攻击的模型- criterion: 损失函数- original_images: 原始图像- labels: 原始图像的标签- epsilon: 最大扰动幅度- num_iterations: 外循环迭代次数- decay: 动量衰减因子- K: 内循环迭代次数# 计算大步长alphaalpha epsilon / num_iterations# 复制原始图像作为初始的对抗样本perturbed_images original_images.clone().detach().requires_grad_(True)momentum torch.zeros_like(original_images).detach().to(original_images.device)for t in range(num_iterations):inner_gradients []sample_gradient momentumfor k in range(K):# 计算前瞻对抗样本x_adv_lookahead perturbed_images alpha * decay1 * momentum# 计算损失x_adv_lookahead x_adv_lookahead.clone().detach().requires_grad_(True)outputs model(x_adv_lookahead)loss criterion(outputs, labels)model.zero_grad()loss.backward()# 计算梯度data_grad x_adv_lookahead.grad.datasample_gradient decay2 * sample_gradient data_grad / torch.sum(torch.abs(data_grad), dim(1, 2, 3), keepdimTrue)inner_gradients.append(sample_gradient)# 更新采样示例perturbed_images perturbed_images (alpha / K) * torch.sign(sample_gradient)perturbed_images torch.clamp(perturbed_images, original_images - epsilon, original_images epsilon)perturbed_images perturbed_images.detach().requires_grad_(True)# 更新动量momentum decay1 * momentum sum(inner_gradients) / K# 更新对抗样本perturbed_images perturbed_images alpha * momentum.sign()perturbed_images torch.clamp(perturbed_images, original_images - epsilon, original_images epsilon)perturbed_images perturbed_images.detach().requires_grad_(True)return perturbed_images攻击效果 为节省时间这里取 K1实际中可取 K10. 代码汇总 dta.py import torch import torch.nn as nndef DTA(model, criterion, original_images, labels, epsilon, num_iterations10, decay11.0, decay20.0, K10):DTA (Direction Tuning Attack)参数:- model: 要攻击的模型- criterion: 损失函数- original_images: 原始图像- labels: 原始图像的标签- epsilon: 最大扰动幅度- num_iterations: 外循环迭代次数- decay: 动量衰减因子- K: 内循环迭代次数# 计算大步长alphaalpha epsilon / num_iterations# 复制原始图像作为初始的对抗样本perturbed_images original_images.clone().detach().requires_grad_(True)momentum torch.zeros_like(original_images).detach().to(original_images.device)for t in range(num_iterations):inner_gradients []sample_gradient momentumfor k in range(K):# 计算前瞻对抗样本x_adv_lookahead perturbed_images alpha * decay1 * momentum# 计算损失x_adv_lookahead x_adv_lookahead.clone().detach().requires_grad_(True)outputs model(x_adv_lookahead)loss criterion(outputs, labels)model.zero_grad()loss.backward()# 计算梯度data_grad x_adv_lookahead.grad.datasample_gradient decay2 * sample_gradient data_grad / torch.sum(torch.abs(data_grad), dim(1, 2, 3), keepdimTrue)inner_gradients.append(sample_gradient)# 更新采样示例perturbed_images perturbed_images (alpha / K) * torch.sign(sample_gradient)perturbed_images torch.clamp(perturbed_images, original_images - epsilon, original_images epsilon)perturbed_images perturbed_images.detach().requires_grad_(True)# 更新动量momentum decay1 * momentum sum(inner_gradients) / K# 更新对抗样本perturbed_images perturbed_images alpha * momentum.sign()perturbed_images torch.clamp(perturbed_images, original_images - epsilon, original_images epsilon)perturbed_images perturbed_images.detach().requires_grad_(True)return perturbed_imagestrain.py import torch import torch.nn as nn import torchvision import torchvision.transforms as transforms from models import ResNet18# 数据预处理 transform_train transforms.Compose([transforms.RandomCrop(32, padding4),transforms.RandomHorizontalFlip(),transforms.ToTensor(),transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)) ])transform_test transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)) ])# 加载Cifar10训练集和测试集 trainset torchvision.datasets.CIFAR10(root./data, trainTrue, downloadFalse, transformtransform_train) trainloader torch.utils.data.DataLoader(trainset, batch_size128, shuffleTrue, num_workers2)testset torchvision.datasets.CIFAR10(root./data, trainFalse, downloadFalse, transformtransform_test) testloader torch.utils.data.DataLoader(testset, batch_size100, shuffleFalse, num_workers2)# 定义设备GPU或CPU device torch.device(cuda:0 if torch.cuda.is_available() else cpu)# 初始化模型 model ResNet18(num_classes10) model.to(device)# 定义损失函数和优化器 criterion nn.CrossEntropyLoss() optimizer torch.optim.Adam(model.parameters(), lr0.01)if __name__ __main__:# 训练模型for epoch in range(10): # 可以根据实际情况调整训练轮数running_loss 0.0for i, data in enumerate(trainloader, 0):inputs, labels data[0].to(device), data[1].to(device)optimizer.zero_grad()outputs model(inputs)loss criterion(outputs, labels)loss.backward()optimizer.step()running_loss loss.item()if i % 100 99:print(fEpoch {epoch 1}, Batch {i 1}: Loss {running_loss / 100})running_loss 0.0torch.save(model.state_dict(), fweights/epoch_{epoch 1}.pth)print(Finished Training)advtest.py import torch import torch.nn as nn import torchvision import torchvision.transforms as transforms from models import * from attacks import * import ssl import os from PIL import Image import matplotlib.pyplot as pltssl._create_default_https_context ssl._create_unverified_context# 定义数据预处理操作 transform transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.491, 0.482, 0.446), (0.247, 0.243, 0.261))])# 加载CIFAR10测试集 testset torchvision.datasets.CIFAR10(root./data, trainFalse,downloadFalse, transformtransform) testloader torch.utils.data.DataLoader(testset, batch_size128,shuffleFalse, num_workers2)# 定义设备GPU优先若可用 device torch.device(cuda if torch.cuda.is_available() else cpu)model ResNet18(num_classes10).to(device)criterion nn.CrossEntropyLoss()# 加载模型权重 weights_path weights/epoch_10.pth model.load_state_dict(torch.load(weights_path, map_locationdevice))if __name__ __main__:# 在测试集上进行FGSM攻击并评估准确率model.eval() # 设置为评估模式correct 0total 0epsilon 16 / 255 # 可以调整扰动强度for data in testloader:original_images, labels data[0].to(device), data[1].to(device)original_images.requires_grad Trueattack_name DTAif attack_name FGSM:perturbed_images FGSM(model, criterion, original_images, labels, epsilon)elif attack_name BIM:perturbed_images BIM(model, criterion, original_images, labels, epsilon)elif attack_name MI-FGSM:perturbed_images MI_FGSM(model, criterion, original_images, labels, epsilon)elif attack_name NI-FGSM:perturbed_images NI_FGSM(model, criterion, original_images, labels, epsilon)elif attack_name PI-FGSM:perturbed_images PI_FGSM(model, criterion, original_images, labels, epsilon)elif attack_name VMI-FGSM:perturbed_images VMI_FGSM(model, criterion, original_images, labels, epsilon)elif attack_name VNI-FGSM:perturbed_images VNI_FGSM(model, criterion, original_images, labels, epsilon)elif attack_name EMI-FGSM:perturbed_images EMI_FGSM(model, criterion, original_images, labels, epsilon)elif attack_name AI-FGTM:perturbed_images AI_FGTM(model, criterion, original_images, labels, epsilon)elif attack_name I-FGSSM:perturbed_images I_FGSSM(model, criterion, original_images, labels, epsilon)elif attack_name SMI-FGRM:perturbed_images SMI_FGRM(model, criterion, original_images, labels, epsilon)elif attack_name VA-I-FGSM:perturbed_images VA_I_FGSM(model, criterion, original_images, labels, epsilon)elif attack_name PC-I-FGSM:perturbed_images PC_I_FGSM(model, criterion, original_images, labels, epsilon)elif attack_name IE-FGSM:perturbed_images IE_FGSM(model, criterion, original_images, labels, epsilon)elif attack_name GRA:perturbed_images GRA(model, criterion, original_images, labels, epsilon)elif attack_name GNP:perturbed_images GNP(model, criterion, original_images, labels, epsilon)elif attack_name MIG:perturbed_images MIG(model, original_images, labels, epsilon)elif attack_name DTA:perturbed_images DTA(model, criterion, original_images, labels, epsilon)perturbed_outputs model(perturbed_images)_, predicted torch.max(perturbed_outputs.data, 1)total labels.size(0)correct (predicted labels).sum().item()accuracy 100 * correct / total# Attack Success RateASR 100 - accuracyprint(fLoad ResNet Model Weight from {weights_path})print(fepsilon: {epsilon:.4f})print(fASR of {attack_name} : {ASR :.2f}%)
http://www.w-s-a.com/news/943187/

相关文章:

  • 打扑克网站推广软件设计类专业哪个最好
  • 网站设计首页网站建设意向书
  • 做网站要学那些angularjs后台管理系统网站
  • 广州白云手机网站建设学做点心上哪个网站
  • 哈尔滨网站建设步骤百度青岛代理公司
  • 怎么利用代码做网站军队 网站备案
  • 百度手机版网址免费广州seo
  • 军博做网站公司wordpress评论插件
  • 如何申请一个网站 做视频网站报错解析
  • 徐州高端网站建设无锡找做网站
  • 网站如何不需要备案百度的宣传视频广告
  • 如何用易语言做网站采购系统有哪些
  • 建一个网站容易吗浙江省城乡建设厅官网
  • 奇点网站建设黄骅贴吧百度贴吧
  • 站长爱it如何分析网站设计
  • 服装公司网站定位seo网站关键词
  • 电商网站开发流程文档南京 seo 价格
  • 网站建设任务分解张家港网站制作服务
  • 化州+网站建设网络营销怎么做推广
  • 贵阳网站设计方案阿里云 wordpress 数据库
  • 如何做购物返佣金网站高校 网站建设实施方案
  • 网站如何连接微信支付网页制作与网站开发
  • 地名网站建设方案营销型网站策划书
  • 网站优化排名查询网站图片怎么做的高级
  • 官方网站建设调研报告小程序短链接生成
  • 专做耐克阿迪鞋网站免费微信网站模板下载
  • 视频在线制作网站wordpress怎么调用友情链接
  • 做微商什么是官方网站温州阀门外贸网站建设
  • 申请主机网站网站建设平台合同模板
  • 如何做ps4的游戏视频网站海口网红图书馆