1. 远程连接:Remote - SSH
1.1 安装 Remote - SSH 插件
在Extensions中搜索插件
Remote - SSH
1.2 配置SSH密钥
1.2.1 检查是否已经存在密钥
if (dir "$env:USERPROFILE\.ssh\id_*") { "SSH keys exist" } `
else { "No SSH keys found" }
1.2.2 创建密钥
ssh-keygen
1.2.3 查看公钥内容
cat ~/.ssh/id_rsa.pub
2. 服务器系统设置
2.1 VSCode提示“Visual Studio Code is unable to watch for file changes in this large workspace”的问题 [official solution]
2.1.1 sudo sysctl -p
:设置从配置文件加载系统参数
sysctl
:用于查看和修改内核参数
“sysctl"的全称是"system control”,对应其功能:控制系统参数;
-p
:指示从配置文件加载系统参数
全称是“preload”,即 “load from file”;
3. 环境配置
conda create -n transferattack python=3.10
conda create --name py312_transferattack python
3.1 安装PyTorch
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.6 -c pytorch -c conda-forge
3.2 安装numpy
pip install numpy==1.23.2
3.3 安装timm
pip install timm==0.6.12
4. 攻击图像生成
python main.py --input_dir /home/rslab/data/cbr/attackoriimg --output_dir adv_data/dra_rs/resnet101 --attack dra_rs --model=resnet101
4.2 训练模型
python classification_model/rs_train_drapre.py --model=resnet152
5. 常用模型分析
5.1 DRA
5.1.1 Alibaba/easyrobust定义的ResNet与torchvision的FC层的命名不同
alibaba/easyrobust
def _forward_impl(self, x: Tensor) -> Tensor:
# ...
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.last_linear(x)
torchvision/models/resnet
def _forward_impl(self, x: Tensor) -> Tensor:
# ...
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
所以在载入权重时,需要进行键值替换
6. 自定义attack模型
5.1 拷贝模板文件
5.2 自定义模型代码
5.3 载入模型权重
5.3.1 TransferAttack的载入方式使用的是“弹性载入”,并不推荐
仅更新相同键值的权重
model_dict = model.state_dict()
pre_dict = net.state_dict()
state_dict = {k:v for k,v in pre_dict.items() if k in model_dict.keys()}
state_dict['module.fc.weight'] = pre_dict['module.last_linear.weight']
state_dict['module.fc.bias'] = pre_dict['module.last_linear.bias']
这部分代码进行了权重匹配的操作:
- 仅保留新模型中存在的键
- 手动处理了
last_linear
到fc
的映射
5.4 转换为比赛提交格式
python big2samples.py
6. 成绩记录
Time | Method | Command | Score |
---|---|---|---|
2024/09/14 14:15 | DRA-rs + ResNet152-rs + dra-pretrain | python main.py --input_dir /home/rslab/data/cbr/attackoriimg --output_dir adv_data/dra_rs/resnet152 --attack dra_rs --model=resnet152 | 42.8 |
2024/09/09 20:21 | DRA-rs + ResNet101-rs | - | 39 |
2024/09/04 14:49 | DRA-rs + ResNet50-rs | python main.py --input_dir /home/rslab/data/cbr/attackoriimg --output_dir adv_data/dra_rs/resnet50 --attack dra_rs --model=resnet50 | 51.5 |
2024/09/03 13:11 | DRA + ResNet50-imagenet | python main.py --input_dir /home/rslab/data/cbr/attackoriimg --output_dir adv_data/dra/resnet50 --attack dra --model=resnet50 | 33.7 |
2024/09/03 11:10 | BSR + ResNet18-rs | - | 28.5 |
2024/08/27 12:29 | MI-FGSM + ResNet18-imagenet | - | 28.5 |
2024/08/21 23:52 | BSR + ResNet18-imagenet | - | 30.2 |