Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewilyas committed Aug 21, 2019
0 parents commit bc0ce85
Show file tree
Hide file tree
Showing 67 changed files with 8,004 additions and 0 deletions.
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/cox"]
path = src/cox
url = git@github.com:andrewilyas/cox.git
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Andrew Ilyas, Logan Engstrom

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
128 changes: 128 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
robustness package
==================
Install via ``pip``: ``pip install robustness``

Read the docs: https://robustness.readthedocs.io/en/latest/index.html

``robustness`` is a package we (students in the `MadryLab <http://madry-lab.ml>`_) created
to make training, evaluating, and exploring neural networks flexible and easy.
We use it in almost all of our projects (whether they involve
adversarial training or not!) and it will be a dependency in many of our
upcoming code releases. A few projects using the library include:

- `Code for "Learning Perceptually-Aligned Representations via Adversarial Robustness" <https://github.com/MadryLab/robust_representations>`_ (https://arxiv.org/abs/1906.00945)
- `Code for
"Image Synthesis with a Single (Robust) Classifier" <https://github.com/MadryLab/robustness_applications>`_ (https://arxiv.org/abs/1906.09453)

We
demonstrate how to use the library in a set of walkthroughs and our API
reference. Functionality provided by the library includes:

- Training and evaluating standard and robust models for a variety of
datasets/architectures using a `CLI interface
<https://robustness.readthedocs.io/en/latest/example_usage/cli_usage.html>`_. The library also provides support for adding
`custom datasets <https://robustness.readthedocs.io/en/latest/example_usage/training_lib_part_2.html#training-on-custom-datasets>`_ and `model architectures <https://robustness.readthedocs.io/en/latest/example_usage/training_lib_part_2.html#training-with-custom-architectures>`_.

.. code-block:: bash
python -m robustness.main --dataset cifar --data /path/to/cifar \
--adv-train 0 --arch resnet18 --out-dir /logs/checkpoints/dir/
- Performing `input manipulation
<https://robustness.readthedocs.io/en/latest/example_usage/input_space_manipulation.html>`_ using robust (or standard)
models---this includes making adversarial examples, inverting representations,
feature visualization, etc. The library offers a variety of optimization
options (e.g. choice between real/estimated gradients, Fourier/pixel basis,
custom loss functions etc.), and is easily extendable.

.. code-block:: python
import torch as ch
from robustness.datasets import CIFAR
from robustness.model_utils import make_and_restore_model
ds = CIFAR('/path/to/cifar')
model, _ = make_and_restore_model(arch='resnet50', dataset=ds,
resume_path='/path/to/model', state_dict_path='model')
model.eval()
attack_kwargs = {
'constraint': 'inf', # L-inf PGD
'eps': 0.05, # Epsilon constraint (L-inf norm)
'step_size': 0.01, # Learning rate for PGD
'iterations': 100, # Number of PGD steps
'targeted': True # Targeted attack
'custom_loss': None # Use default cross-entropy loss
}
_, test_loader = ds.make_loaders(workers=0, batch_size=10)
im, label = next(iter(test_loader))
target_label = (label + ch.randint_like(label, high=9)) % 10
adv_out, adv_im = model(im, target_label, make_adv, **attack_kwargs)
- Importing ``robustness`` as a package, which allows for easy training of
neural networks with support for custom loss functions, logging, data loading,
and more! A good introduction can be found in our two-part walkthrough
(`Part 1 <https://robustness.readthedocs.io/en/latest/example_usage/training_lib_part_1.html>`_,
`Part 2 <https://robustness.readthedocs.io/en/latest/example_usage/training_lib_part_2.html>`_).

.. code-block:: python
from robustness import model_utils, datasets, train, defaults
from robustness.datasets import CIFAR
# We use cox (http://github.com/MadryLab/cox) to log, store and analyze
# results. Read more at https//cox.readthedocs.io.
from cox.utils import Parameters
import cox.store
# Hard-coded dataset, architecture, batch size, workers
ds = CIFAR('/path/to/cifar')
m, _ = model_utils.make_and_restore_model(arch='resnet50', dataset=ds)
train_loader, val_loader = ds.make_loaders(batch_size=128, workers=8)
# Create a cox store for logging
out_store = cox.store.Store(OUT_DIR)
# Hard-coded base parameters
train_kwargs = {
'out_dir': "train_out",
'adv_train': 1,
'constraint': '2',
'eps': 0.5,
'attack_lr': 1.5,
'attack_steps': 20
}
train_args = Parameters(train_kwargs)
# Fill whatever parameters are missing from the defaults
train_args = defaults.check_and_fill_args(train_args,
defaults.TRAINING_ARGS, CIFAR)
train_args = defaults.check_and_fill_args(train_args,
defaults.PGD_ARGS, CIFAR)
# Train a model
train.train_model(train_args, m, (train_loader, val_loader), store=out_store)
Citation
--------
If you use this library in your research, cite it as
follows:

.. code-block:: bibtex
@misc{robustness,
title={Robustness (Python Library)},
author={Logan Engstrom and Andrew Ilyas and Shibani Santurkar and Dimitris Tsipras},
year={2019},
url={https://github.com/MadryLab/robustness}
}
*(Have you used the package and found it useful? Let us know!)*.

Contributors
-------------
- `Andrew Ilyas <https://twitter.com/andrew_ilyas>`_
- `Logan Engstrom <https://twitter.com/logan_engstrom>`_
- `Shibani Santurkar <https://twitter.com/ShibaniSan>`_
- `Dimitris Tsipras <https://twitter.com/tsiprasd>`_

20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
5 changes: 5 additions & 0 deletions docs/_static/all.min.css

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
API Reference
=============

.. toctree::
api/robustness.attack_steps
api/robustness.attacker
api/robustness.data_augmentation
api/robustness.datasets
api/robustness.defaults
api/robustness.loaders
api/robustness.main
api/robustness.model_utils
api/robustness.train
api/robustness.tools

7 changes: 7 additions & 0 deletions docs/api/robustness.attack_steps.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.attack\_steps module
===============================

.. automodule:: robustness.attack_steps
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/robustness.attacker.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.attacker module
==========================

.. automodule:: robustness.attacker
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/robustness.data_augmentation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.data\_augmentation module
====================================

.. automodule:: robustness.data_augmentation
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/robustness.datasets.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.datasets module
==========================

.. automodule:: robustness.datasets
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/robustness.defaults.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.defaults module
==========================

.. automodule:: robustness.defaults
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/robustness.loaders.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.loaders module
=========================

.. automodule:: robustness.loaders
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/robustness.main.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.main module
======================

.. automodule:: robustness.main
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/robustness.model_utils.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.model\_utils module
==============================

.. automodule:: robustness.model_utils
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/robustness.tools.constants.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.tools.constants module
=================================

.. automodule:: robustness.tools.constants
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/robustness.tools.folder.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.tools.folder module
==============================

.. automodule:: robustness.tools.folder
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/robustness.tools.helpers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.tools.helpers module
===============================

.. automodule:: robustness.tools.helpers
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/api/robustness.tools.label_maps.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
robustness.tools.label\_maps module
===================================

.. automodule:: robustness.tools.label_maps
:members:
:undoc-members:
:show-inheritance:

0 comments on commit bc0ce85

Please sign in to comment.