Skip to content

Commit

Permalink
Merge branch 'master' into switch_mnist_mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
tejuafonja authored Mar 16, 2021
2 parents d4aa1dd + 7ab42b9 commit 2f0ad4a
Show file tree
Hide file tree
Showing 227 changed files with 42,821 additions and 37,226 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ The following authors contributed 100 lines or more (ordered according to the Gi
* Erfan Noury (UMBC)
* Robert Wagner (Case Western Reserve University)
* Erh-Chung Chen (National Tsing Hua University)
* Joel Frank (Ruhr-University Bochum)

## Copyright

Expand Down
2 changes: 1 addition & 1 deletion cleverhans/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

# If possible attach a hex digest to the version string to keep track of
# changes in the development branch
__version__ = append_dev_version('4.0.0')
__version__ = append_dev_version("4.0.0")
4 changes: 2 additions & 2 deletions cleverhans/devtools/autopep8_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

for f in list_files(".py"):

command = ["autopep8", "-i", "--indent-size", "2", f]
shell_call(command)
command = ["autopep8", "-i", "--indent-size", "2", f]
shell_call(command)
25 changes: 13 additions & 12 deletions cleverhans/devtools/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@

import numpy as np


class CleverHansTest(unittest.TestCase):
"""TestCase with some extra features"""
"""TestCase with some extra features"""

def setUp(self):
self.test_start = time.time()
# seed the randomness
np.random.seed(1234)
def setUp(self):
self.test_start = time.time()
# seed the randomness
np.random.seed(1234)

def tearDown(self):
print(self.id(), "took", time.time() - self.test_start, "seconds")
def tearDown(self):
print(self.id(), "took", time.time() - self.test_start, "seconds")

def assertClose(self, x, y, *args, **kwargs):
"""Assert that `x` and `y` have close to the same value"""
# self.assertTrue(np.allclose(x, y)) doesn't give a useful message
# on failure
assert np.allclose(x, y, *args, **kwargs), (x, y)
def assertClose(self, x, y, *args, **kwargs):
"""Assert that `x` and `y` have close to the same value"""
# self.assertTrue(np.allclose(x, y)) doesn't give a useful message
# on failure
assert np.allclose(x, y, *args, **kwargs), (x, y)
112 changes: 59 additions & 53 deletions cleverhans/devtools/list_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,74 @@


def list_files(suffix=""):
"""
Returns a list of all files in CleverHans with the given suffix.
"""
Returns a list of all files in CleverHans with the given suffix.
Parameters
----------
suffix : str
Parameters
----------
suffix : str
Returns
-------
Returns
-------
file_list : list
A list of all files in CleverHans whose filepath ends with `suffix`.
"""
file_list : list
A list of all files in CleverHans whose filepath ends with `suffix`.
"""

cleverhans_path = os.path.abspath(cleverhans.__path__[0])
# In some environments cleverhans_path does not point to a real directory.
# In such case return empty list.
if not os.path.isdir(cleverhans_path):
return []
repo_path = os.path.abspath(os.path.join(cleverhans_path, os.pardir))
file_list = _list_files(cleverhans_path, suffix)
cleverhans_path = os.path.abspath(cleverhans.__path__[0])
# In some environments cleverhans_path does not point to a real directory.
# In such case return empty list.
if not os.path.isdir(cleverhans_path):
return []
repo_path = os.path.abspath(os.path.join(cleverhans_path, os.pardir))
file_list = _list_files(cleverhans_path, suffix)

extra_dirs = ['cleverhans_tutorials', 'examples', 'scripts', 'tests_tf', 'tests_pytorch']
extra_dirs = [
"cleverhans_tutorials",
"examples",
"scripts",
"tests_tf",
"tests_pytorch",
]

for extra_dir in extra_dirs:
extra_path = os.path.join(repo_path, extra_dir)
if os.path.isdir(extra_path):
extra_files = _list_files(extra_path, suffix)
extra_files = [os.path.join(os.pardir, path) for path in extra_files]
file_list = file_list + extra_files
for extra_dir in extra_dirs:
extra_path = os.path.join(repo_path, extra_dir)
if os.path.isdir(extra_path):
extra_files = _list_files(extra_path, suffix)
extra_files = [os.path.join(os.pardir, path) for path in extra_files]
file_list = file_list + extra_files

return file_list
return file_list


def _list_files(path, suffix=""):
"""
Returns a list of all files ending in `suffix` contained within `path`.
"""
Returns a list of all files ending in `suffix` contained within `path`.
Parameters
----------
path : str
a filepath
suffix : str
Parameters
----------
path : str
a filepath
suffix : str
Returns
-------
l : list
A list of all files ending in `suffix` contained within `path`.
(If `path` is a file rather than a directory, it is considered
to "contain" itself)
"""
if os.path.isdir(path):
incomplete = os.listdir(path)
complete = [os.path.join(path, entry) for entry in incomplete]
lists = [_list_files(subpath, suffix) for subpath in complete]
flattened = []
for one_list in lists:
for elem in one_list:
flattened.append(elem)
return flattened
else:
assert os.path.exists(path), "couldn't find file '%s'" % path
if path.endswith(suffix):
return [path]
return []
Returns
-------
l : list
A list of all files ending in `suffix` contained within `path`.
(If `path` is a file rather than a directory, it is considered
to "contain" itself)
"""
if os.path.isdir(path):
incomplete = os.listdir(path)
complete = [os.path.join(path, entry) for entry in incomplete]
lists = [_list_files(subpath, suffix) for subpath in complete]
flattened = []
for one_list in lists:
for elem in one_list:
flattened.append(elem)
return flattened
else:
assert os.path.exists(path), "couldn't find file '%s'" % path
if path.endswith(suffix):
return [path]
return []
104 changes: 61 additions & 43 deletions cleverhans/devtools/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,70 @@


def random_feed_dict(rng, placeholders):
"""
Returns random data to be used with `feed_dict`.
:param rng: A numpy.random.RandomState instance
:param placeholders: List of tensorflow placeholders
:return: A dict mapping placeholders to random numpy values
"""
"""
Returns random data to be used with `feed_dict`.
:param rng: A numpy.random.RandomState instance
:param placeholders: List of tensorflow placeholders
:return: A dict mapping placeholders to random numpy values
"""

output = {}
output = {}

for placeholder in placeholders:
if placeholder.dtype != 'float32':
raise NotImplementedError()
value = rng.randn(*placeholder.shape).astype('float32')
output[placeholder] = value
for placeholder in placeholders:
if placeholder.dtype != "float32":
raise NotImplementedError()
value = rng.randn(*placeholder.shape).astype("float32")
output[placeholder] = value

return output

return output

class SimpleDataset(Dataset):
"""
A dataset containing random values.
Values are uniformly distributed, either in [0, max_val] or [-1, max_val].
"""
"""
A dataset containing random values.
Values are uniformly distributed, either in [0, max_val] or [-1, max_val].
"""

def __init__(self, dim=2, train_start=0, train_end=3, test_start=0, test_end=5, center=False, max_val=1.,
nb_classes=5):
kwargs = copy.copy(locals())
del kwargs['self']
if "__class__" in kwargs:
del kwargs["__class__"]
super(SimpleDataset, self).__init__(kwargs)
self.__dict__.update(kwargs)
train_x_rng = np.random.RandomState([2018, 11, 9, 1])
# Even if train_start is not 0, we should still generate the first training examples from the rng.
# This way the dataset looks like it is an array of deterministic data that we index using train_start.
self.x_train = train_x_rng.uniform(- center * max_val, max_val, (train_end, dim))[train_start:]
# Use a second rng for the test set so that it also looks like an array of deterministic data that we
# index into, unaffected by the number of training examples.
test_x_rng = np.random.RandomState([2018, 11, 9, 2])
self.x_test = test_x_rng.uniform(- center * max_val, max_val, (test_end, dim))[test_start:]
# Likewise, to keep the number of examples read from the rng affecting the values of the labels, we
# must generate the labels from a different rng
train_y_rng = np.random.RandomState([2018, 11, 9, 3])
self.y_train = train_y_rng.randint(low=0, high=nb_classes, size=(train_end, 1))[train_start:]
test_y_rng = np.random.RandomState([2018, 11, 9, 4])
self.y_test = test_y_rng.randint(low=0, high=nb_classes, size=(test_end, 1))[test_start:]
assert self.x_train.shape[0] == self.y_train.shape[0]
assert self.x_test.shape[0] == self.y_test.shape[0]
self.y_train = np_utils.to_categorical(self.y_train, nb_classes)
self.y_test = np_utils.to_categorical(self.y_test, nb_classes)
def __init__(
self,
dim=2,
train_start=0,
train_end=3,
test_start=0,
test_end=5,
center=False,
max_val=1.0,
nb_classes=5,
):
kwargs = copy.copy(locals())
del kwargs["self"]
if "__class__" in kwargs:
del kwargs["__class__"]
super(SimpleDataset, self).__init__(kwargs)
self.__dict__.update(kwargs)
train_x_rng = np.random.RandomState([2018, 11, 9, 1])
# Even if train_start is not 0, we should still generate the first training examples from the rng.
# This way the dataset looks like it is an array of deterministic data that we index using train_start.
self.x_train = train_x_rng.uniform(
-center * max_val, max_val, (train_end, dim)
)[train_start:]
# Use a second rng for the test set so that it also looks like an array of deterministic data that we
# index into, unaffected by the number of training examples.
test_x_rng = np.random.RandomState([2018, 11, 9, 2])
self.x_test = test_x_rng.uniform(-center * max_val, max_val, (test_end, dim))[
test_start:
]
# Likewise, to keep the number of examples read from the rng affecting the values of the labels, we
# must generate the labels from a different rng
train_y_rng = np.random.RandomState([2018, 11, 9, 3])
self.y_train = train_y_rng.randint(low=0, high=nb_classes, size=(train_end, 1))[
train_start:
]
test_y_rng = np.random.RandomState([2018, 11, 9, 4])
self.y_test = test_y_rng.randint(low=0, high=nb_classes, size=(test_end, 1))[
test_start:
]
assert self.x_train.shape[0] == self.y_train.shape[0]
assert self.x_test.shape[0] == self.y_test.shape[0]
self.y_train = np_utils.to_categorical(self.y_train, nb_classes)
self.y_test = np_utils.to_categorical(self.y_test, nb_classes)
Loading

0 comments on commit 2f0ad4a

Please sign in to comment.