-
-
Notifications
You must be signed in to change notification settings - Fork 422
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 #508 from bethgelab/mnist
added pretrained mnist zoo example and test model; fixed mnist samples
- Loading branch information
Showing
6 changed files
with
53 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env python3 | ||
import torch | ||
import torch.nn as nn | ||
import os | ||
from foolbox.models import PyTorchModel | ||
from foolbox.utils import accuracy, samples | ||
|
||
|
||
def create() -> PyTorchModel: | ||
model = nn.Sequential( | ||
nn.Conv2d(1, 32, 3), | ||
nn.ReLU(), | ||
nn.Conv2d(32, 64, 3), | ||
nn.ReLU(), | ||
nn.MaxPool2d(2), | ||
nn.Dropout2d(0.25), | ||
nn.Flatten(), # type: ignore | ||
nn.Linear(9216, 128), | ||
nn.ReLU(), | ||
nn.Dropout2d(0.5), | ||
nn.Linear(128, 10), | ||
) | ||
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "mnist_cnn.pth") | ||
model.load_state_dict(torch.load(path)) # type: ignore | ||
model.eval() | ||
preprocessing = dict(mean=0.1307, std=0.3081) | ||
fmodel = PyTorchModel(model, bounds=(0, 1), preprocessing=preprocessing) | ||
return fmodel | ||
|
||
|
||
if __name__ == "__main__": | ||
# test the model | ||
fmodel = create() | ||
images, labels = samples(fmodel, dataset="mnist", batchsize=20) | ||
print(accuracy(fmodel, images, labels)) |
Binary file not shown.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .zoo import get_model # noqa: F401 | ||
from .weights_fetcher import fetch_weights # noqa: F401 | ||
from .git_cloner import GitCloneError # noqa: F401 | ||
from .model_loader import ModelLoader # noqa: F401 |
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