-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from RobustBench/add_models_2
Add models from Singh2023Revisting
- Loading branch information
Showing
11 changed files
with
308 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
model_info/imagenet/Linf/Singh2023Revisiting_ConvNeXt-B-ConvStem.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"link": "https://arxiv.org/abs/2303.01870", | ||
"name": "Revisiting Adversarial Training for ImageNet: Architectures, Training and Generalization across Threat Models", | ||
"authors": "Naman D Singh, Francesco Croce, Matthias Hein", | ||
"additional_data": false, | ||
"number_forward_passes": 1, | ||
"dataset": "imagenet", | ||
"venue": "arXiv, Mar 2023", | ||
"architecture": "ConvNeXt-B + ConvStem", | ||
"eps": "4/255", | ||
"clean_acc": "75.90", | ||
"reported": "56.14", | ||
"autoattack_acc": "56.14", | ||
"unreliable": false | ||
} |
15 changes: 15 additions & 0 deletions
15
model_info/imagenet/Linf/Singh2023Revisiting_ConvNeXt-L-ConvStem.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"link": "https://arxiv.org/abs/2303.01870", | ||
"name": "Revisiting Adversarial Training for ImageNet: Architectures, Training and Generalization across Threat Models", | ||
"authors": "Naman D Singh, Francesco Croce, Matthias Hein", | ||
"additional_data": false, | ||
"number_forward_passes": 1, | ||
"dataset": "imagenet", | ||
"venue": "arXiv, Mar 2023", | ||
"architecture": "ConvNeXt-L + ConvStem", | ||
"eps": "4/255", | ||
"clean_acc": "77.00", | ||
"reported": "57.70", | ||
"autoattack_acc": "57.70", | ||
"unreliable": false | ||
} |
15 changes: 15 additions & 0 deletions
15
model_info/imagenet/Linf/Singh2023Revisiting_ConvNeXt-S-ConvStem.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"link": "https://arxiv.org/abs/2303.01870", | ||
"name": "Revisiting Adversarial Training for ImageNet: Architectures, Training and Generalization across Threat Models", | ||
"authors": "Naman D Singh, Francesco Croce, Matthias Hein", | ||
"additional_data": false, | ||
"number_forward_passes": 1, | ||
"dataset": "imagenet", | ||
"venue": "arXiv, Mar 2023", | ||
"architecture": "ConvNeXt-S + ConvStem", | ||
"eps": "4/255", | ||
"clean_acc": "74.10", | ||
"reported": "52.42", | ||
"autoattack_acc": "52.42", | ||
"unreliable": false | ||
} |
15 changes: 15 additions & 0 deletions
15
model_info/imagenet/Linf/Singh2023Revisiting_ConvNeXt-T-ConvStem.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"link": "https://arxiv.org/abs/2303.01870", | ||
"name": "Revisiting Adversarial Training for ImageNet: Architectures, Training and Generalization across Threat Models", | ||
"authors": "Naman D Singh, Francesco Croce, Matthias Hein", | ||
"additional_data": false, | ||
"number_forward_passes": 1, | ||
"dataset": "imagenet", | ||
"venue": "arXiv, Mar 2023", | ||
"architecture": "ConvNeXt-T + ConvStem", | ||
"eps": "4/255", | ||
"clean_acc": "72.72", | ||
"reported": "49.46", | ||
"autoattack_acc": "49.46", | ||
"unreliable": false | ||
} |
15 changes: 15 additions & 0 deletions
15
model_info/imagenet/Linf/Singh2023Revisiting_ViT-B-ConvStem.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"link": "https://arxiv.org/abs/2303.01870", | ||
"name": "Revisiting Adversarial Training for ImageNet: Architectures, Training and Generalization across Threat Models", | ||
"authors": "Naman D Singh, Francesco Croce, Matthias Hein", | ||
"additional_data": false, | ||
"number_forward_passes": 1, | ||
"dataset": "imagenet", | ||
"venue": "arXiv, Mar 2023", | ||
"architecture": "ViT-B + ConvStem", | ||
"eps": "4/255", | ||
"clean_acc": "76.30", | ||
"reported": "54.66", | ||
"autoattack_acc": "54.66", | ||
"unreliable": false | ||
} |
15 changes: 15 additions & 0 deletions
15
model_info/imagenet/Linf/Singh2023Revisiting_ViT-S-ConvStem.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"link": "https://arxiv.org/abs/2303.01870", | ||
"name": "Revisiting Adversarial Training for ImageNet: Architectures, Training and Generalization across Threat Models", | ||
"authors": "Naman D Singh, Francesco Croce, Matthias Hein", | ||
"additional_data": false, | ||
"number_forward_passes": 1, | ||
"dataset": "imagenet", | ||
"venue": "arXiv, Mar 2023", | ||
"architecture": "ViT-S + ConvStem", | ||
"eps": "4/255", | ||
"clean_acc": "72.56", | ||
"reported": "48.08", | ||
"autoattack_acc": "48.08", | ||
"unreliable": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
"""Definition of ConvStem models as in https://arxiv.org/abs/2303.01870.""" | ||
|
||
import torch | ||
import torch.nn as nn | ||
from collections import OrderedDict | ||
from typing import Tuple | ||
from torch import Tensor | ||
import torch.nn as nn | ||
|
||
import timm | ||
from timm.models import create_model | ||
import torch.nn.functional as F | ||
import math | ||
|
||
from robustbench.model_zoo.architectures.utils_architectures import normalize_model | ||
|
||
|
||
IMAGENET_MEAN = [c * 1. for c in (0.485, 0.456, 0.406)] | ||
IMAGENET_STD = [c * 1. for c in (0.229, 0.224, 0.225)] | ||
|
||
|
||
class LayerNorm(nn.Module): | ||
r""" LayerNorm that supports two data formats: channels_last (default) or channels_first. | ||
The ordering of the dimensions in the inputs. channels_last corresponds to inputs with | ||
shape (batch_size, height, width, channels) while channels_first corresponds to inputs | ||
with shape (batch_size, channels, height, width). | ||
From https://github.com/facebookresearch/ConvNeXt/blob/main/models/convnext.py. | ||
""" | ||
def __init__(self, normalized_shape, eps=1e-6, data_format="channels_first"): | ||
super().__init__() | ||
self.weight = nn.Parameter(torch.ones(normalized_shape)) | ||
self.bias = nn.Parameter(torch.zeros(normalized_shape)) | ||
self.eps = eps | ||
self.data_format = data_format | ||
if self.data_format not in ["channels_last", "channels_first"]: | ||
raise NotImplementedError | ||
self.normalized_shape = (normalized_shape, ) | ||
|
||
def forward(self, x): | ||
if self.data_format == "channels_last": | ||
return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps) | ||
elif self.data_format == "channels_first": | ||
u = x.mean(1, keepdim=True) | ||
s = (x - u).pow(2).mean(1, keepdim=True) | ||
x = (x - u) / torch.sqrt(s + self.eps) | ||
x = self.weight[:, None, None] * x + self.bias[:, None, None] | ||
return x | ||
|
||
|
||
class ConvBlock(nn.Module): | ||
expansion = 1 | ||
def __init__(self, siz=48, end_siz=8, fin_dim=384): | ||
super(ConvBlock, self).__init__() | ||
self.planes = siz | ||
fin_dim = self.planes * end_siz if fin_dim != 432 else 432 | ||
# self.bn = nn.BatchNorm2d(planes) if self.normaliz == "bn" else nn.GroupNorm(num_groups=1, num_channels=planes) | ||
self.stem = nn.Sequential(nn.Conv2d(3, self.planes, kernel_size=3, stride=2, padding=1), | ||
LayerNorm(self.planes, data_format="channels_first"), | ||
nn.GELU(), | ||
nn.Conv2d(self.planes, self.planes*2, kernel_size=3, stride=2, padding=1), | ||
LayerNorm(self.planes*2, data_format="channels_first"), | ||
nn.GELU(), | ||
nn.Conv2d(self.planes*2, self.planes*4, kernel_size=3, stride=2, padding=1), | ||
LayerNorm(self.planes*4, data_format="channels_first"), | ||
nn.GELU(), | ||
nn.Conv2d(self.planes*4, self.planes*8, kernel_size=3, stride=2, padding=1), | ||
LayerNorm(self.planes*8, data_format="channels_first"), | ||
nn.GELU(), | ||
nn.Conv2d(self.planes*8, fin_dim, kernel_size=1, stride=1, padding=0) | ||
) | ||
def forward(self, x): | ||
out = self.stem(x) | ||
# out = self.bn(out) | ||
return out | ||
|
||
|
||
class ConvBlock3(nn.Module): | ||
# expansion = 1 | ||
def __init__(self, siz=64): | ||
super(ConvBlock3, self).__init__() | ||
self.planes = siz | ||
|
||
self.stem = nn.Sequential(nn.Conv2d(3, self.planes, kernel_size=3, stride=2, padding=1), | ||
LayerNorm(self.planes, data_format="channels_first"), | ||
nn.GELU(), | ||
nn.Conv2d(self.planes, int(self.planes*1.5), kernel_size=3, stride=2, padding=1), | ||
LayerNorm(int(self.planes*1.5), data_format="channels_first"), | ||
nn.GELU(), | ||
nn.Conv2d(int(self.planes*1.5), self.planes*2, kernel_size=3, stride=1, padding=1), | ||
LayerNorm(self.planes*2, data_format="channels_first"), | ||
nn.GELU() | ||
) | ||
|
||
def forward(self, x): | ||
out = self.stem(x) | ||
# out = self.bn(out) | ||
return out | ||
|
||
|
||
class ConvBlock1(nn.Module): | ||
def __init__(self, siz=48, end_siz=8, fin_dim=384): | ||
super(ConvBlock1, self).__init__() | ||
self.planes = siz | ||
|
||
fin_dim = self.planes*end_siz if fin_dim == None else 432 | ||
self.stem = nn.Sequential(nn.Conv2d(3, self.planes, kernel_size=3, stride=2, padding=1), | ||
LayerNorm(self.planes, data_format="channels_first"), | ||
nn.GELU(), | ||
nn.Conv2d(self.planes, self.planes*2, kernel_size=3, stride=2, padding=1), | ||
LayerNorm(self.planes*2, data_format="channels_first"), | ||
nn.GELU() | ||
) | ||
|
||
def forward(self, x): | ||
out = self.stem(x) | ||
# out = self.bn(out) | ||
return out | ||
|
||
|
||
def get_convstem_models(modelname, pretrained=False): | ||
"""Initialize models with ConvStem.""" | ||
|
||
if modelname == 'convnext_t_cvst': | ||
model = timm.models.convnext.convnext_tiny(pretrained=pretrained) | ||
model.stem = ConvBlock1(48, end_siz=8) | ||
|
||
elif modelname == "convnext_s_cvst": | ||
model = timm.models.convnext.convnext_small(pretrained=pretrained) | ||
model.stem = ConvBlock1(48, end_siz=8) | ||
|
||
elif modelname == "convnext_b_cvst": | ||
model_args = dict(depths=[3, 3, 27, 3], dims=[128, 256, 512, 1024]) | ||
model = timm.models.convnext._create_convnext( | ||
'convnext_base.fb_in1k', pretrained=pretrained, **model_args) | ||
model.stem = ConvBlock3(64) | ||
|
||
elif modelname == "convnext_l_cvst": | ||
model = timm.models.convnext_large(pretrained=pretrained) | ||
model.stem = ConvBlock3(96) | ||
|
||
elif modelname == 'vit_s_cvst': | ||
model = create_model('deit_small_patch16_224', pretrained=pretrained) | ||
model.patch_embed.proj = ConvBlock(48, end_siz=8) | ||
model = normalize_model(model, IMAGENET_MEAN, IMAGENET_STD) | ||
|
||
elif modelname == 'vit_b_cvst': | ||
model = timm.models.vision_transformer.vit_base_patch16_224(pretrained=pretrained) | ||
model.patch_embed.proj = ConvBlock(48, end_siz=16, fin_dim=None) | ||
|
||
else: | ||
raise ValueError(f'Invalid model name: {modelname}.') | ||
|
||
return model | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.