Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Clip Values #940

Merged
merged 3 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions armory/baseline_models/keras/densenet121_resisc45.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,5 @@ def get_art_model(
if weights_path:
model.load_weights(weights_path)

mean, std = mean_std()
wrapped_model = KerasClassifier(
model, clip_values=((0.0 - mean) / std, (1.0 - mean) / std), **wrapper_kwargs
)
wrapped_model = KerasClassifier(model, clip_values=(0.0, 1.0), **wrapper_kwargs)
return wrapped_model
2 changes: 1 addition & 1 deletion armory/baseline_models/keras/inception_resnet_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ def get_art_model(
if weights_path:
model.load_weights(weights_path)

wrapped_model = KerasClassifier(model, clip_values=(-1.0, 1.0), **wrapper_kwargs)
wrapped_model = KerasClassifier(model, clip_values=(0.0, 1.0), **wrapper_kwargs)
return wrapped_model
22 changes: 1 addition & 21 deletions armory/baseline_models/keras/resnet50.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
from typing import Optional

import numpy as np
import tensorflow as tf
from art.classifiers import KerasClassifier
from tensorflow.keras.applications.resnet50 import ResNet50
Expand Down Expand Up @@ -38,24 +37,5 @@ def get_art_model(
if weights_path:
model.load_weights(weights_path)

wrapped_model = KerasClassifier(
model,
clip_values=(
np.array(
[
0.0 - IMAGENET_MEANS[0],
0.0 - IMAGENET_MEANS[1],
0.0 - IMAGENET_MEANS[2],
]
),
np.array(
[
255.0 - IMAGENET_MEANS[0],
255.0 - IMAGENET_MEANS[1],
255.0 - IMAGENET_MEANS[2],
]
),
),
**wrapper_kwargs,
)
wrapped_model = KerasClassifier(model, clip_values=(0.0, 1.0), **wrapper_kwargs,)
return wrapped_model
58 changes: 23 additions & 35 deletions armory/baseline_models/pytorch/resnet50.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Optional

from art.classifiers import PyTorchClassifier
import numpy as np
import torch
from torchvision import models

Expand All @@ -14,57 +13,46 @@

DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")

IMAGENET_MEANS = [0.485, 0.456, 0.406]
IMAGENET_STDEV = [0.229, 0.224, 0.225]

class OuterModel(torch.nn.Module):
def __init__(
self, weights_path: Optional[str], **model_kwargs,
):
super().__init__()
self.inner_model = models.resnet50(**model_kwargs)
self.inner_model.to(DEVICE)

def preprocessing_fn(img: np.ndarray):
"""
Standardize, then normalize imagenet images
"""
# Standardize images to [0, 1]
img /= 255.0
if weights_path:
checkpoint = torch.load(weights_path, map_location=DEVICE)
self.inner_model.load_state_dict(checkpoint)

# Normalize images ImageNet means
for i, (mean, std) in enumerate(zip(IMAGENET_MEANS, IMAGENET_STDEV)):
img[i] -= mean
img[i] /= std
self.imagenet_means = torch.tensor(
[0.485, 0.456, 0.406], dtype=torch.float32, device=DEVICE
)
self.imagenet_stdev = torch.tensor(
[0.229, 0.224, 0.225], dtype=torch.float32, device=DEVICE
)

return img
def forward(self, x: torch.Tensor) -> torch.Tensor:
x_norm = ((x - self.imagenet_means) / self.imagenet_stdev).permute(0, 3, 1, 2)
output = self.inner_model(x_norm)

return output


# NOTE: PyTorchClassifier expects numpy input, not torch.Tensor input
def get_art_model(
model_kwargs: dict, wrapper_kwargs: dict, weights_path: Optional[str] = None
) -> PyTorchClassifier:
model = models.resnet50(**model_kwargs)
model.to(DEVICE)

if weights_path:
checkpoint = torch.load(weights_path, map_location=DEVICE)
model.load_state_dict(checkpoint)
model = OuterModel(weights_path=weights_path, **model_kwargs)

wrapped_model = PyTorchClassifier(
model,
loss=torch.nn.CrossEntropyLoss(),
optimizer=torch.optim.Adam(model.parameters(), lr=0.003),
input_shape=(224, 224, 3),
**wrapper_kwargs,
clip_values=(
np.array(
[
0.0 - IMAGENET_MEANS[0] / IMAGENET_STDEV[0],
0.0 - IMAGENET_MEANS[1] / IMAGENET_STDEV[1],
0.0 - IMAGENET_MEANS[2] / IMAGENET_STDEV[2],
]
),
np.array(
[
1.0 - IMAGENET_MEANS[0] / IMAGENET_STDEV[0],
1.0 - IMAGENET_MEANS[1] / IMAGENET_STDEV[1],
1.0 - IMAGENET_MEANS[2] / IMAGENET_STDEV[2],
]
),
),
clip_values=(0.0, 1.0),
)
return wrapped_model
4 changes: 4 additions & 0 deletions scenario_configs/asr_deepspeech_baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
},
"weights_file": null,
"wrapper_kwargs": {
"clip_values": [
-1,
1
],
"pretrained_model": "librispeech"
}
},
Expand Down
4 changes: 4 additions & 0 deletions scenario_configs/asr_deepspeech_baseline_fgsm.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
},
"weights_file": null,
"wrapper_kwargs": {
"clip_values": [
-1,
1
],
"pretrained_model": "librispeech"
}
},
Expand Down