Пример #1
0
    sys.path.append(MODULE_PATH)

import tensorflow as tf
import iat.nets

ResNetDenoiseModel = iat.nets.ResNetDenoiseModel

from tensorpack import TowerContext
from tensorpack.tfutils import get_model_loader

from ares import ClassifierWithLogits
from ares.utils import get_res_path, download_res

import argparse

MODEL_PATH = get_res_path('./imagenet/R152-Denoise.npz')


def load(session):
    model = ResNet152_Denoising()
    model.load(session, MODEL_PATH)
    return model


def download(model_path):
    url = 'https://github.com/facebookresearch/ImageNet-Adversarial-Training/releases/download/v0.1/R152-Denoise.npz'
    if not os.path.exists(model_path):
        if not os.path.exists(os.path.dirname(model_path)):
            os.makedirs(os.path.dirname(model_path), exist_ok=True)
        download_res(url, model_path)
Пример #2
0
''' This file provides a wrapper class for AT_HE (https://github.com/ShawnXYang/AT_HE) model for CIFAR-10 dataset. '''

import sys
import os
import math

import torch
import torch.nn as nn
import torch.nn.functional as F
import tensorflow as tf

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path, download_res

MODEL_PATH = get_res_path('./cifar10/model-wideres-pgdHE-wide10.pt')


def load(_):
    model = AT_HE()
    model.load()
    return model


def download(model_path):
    if not os.path.exists(model_path):
        if not os.path.exists(os.path.dirname(model_path)):
            os.makedirs(os.path.dirname(model_path), exist_ok=True)
        url = 'http://ml.cs.tsinghua.edu.cn/~xiaoyang/downloads/weights/model-wideres-pgdHE-wide10.pt'
        download_res(url, model_path)

Пример #3
0
''' This file provides a wrapper class for AWP (https://github.com/csdongxian/AWP) model for CIFAR-10 dataset. '''

import sys
import os

import torch
import torch.nn as nn
import torch.nn.functional as F
import tensorflow as tf

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path

MODEL_PATH = get_res_path('./cifar10/RST-AWP_cifar10_linf_wrn28-10.pt')


def load(_):
    model = AWP()
    model.load()
    return model


def filter_state_dict(state_dict):
    from collections import OrderedDict

    if 'state_dict' in state_dict.keys():
        state_dict = state_dict['state_dict']
    new_state_dict = OrderedDict()
    for k, v in state_dict.items():
        if 'sub_block' in k:
            continue
Пример #4
0
import sys
import os

THIRD_PARTY_PATH = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../third_party'))
if THIRD_PARTY_PATH not in sys.path:
    sys.path.append(THIRD_PARTY_PATH)

from bayesian_defense.models.vgg_rse import VGG

import torch
import tensorflow as tf

from ares.utils import get_res_path, download_res
from ares.model.pytorch_wrapper import pytorch_classifier_with_logits

MODEL_PATH = get_res_path('./cifar10/rse')


def load(_):
    model = VGG_RSE()
    model.load(MODEL_PATH)
    return model


def download(model_path):
    pth_path = os.path.join(model_path, 'cifar10_vgg_rse.pth')
    if not os.path.exists(os.path.dirname(pth_path)):
        os.makedirs(os.path.dirname(pth_path), exist_ok=True)
    if not os.path.exists(pth_path):
        print('Please download "cifar10_vgg_rse.pth" from ' +
              '"https://drive.google.com/drive/folders/1uAP6q-fSADhlkx-YNBipPzv4KCvC_3_J?usp=sharing", ' +
Пример #5
0
''' This file provides a wrapper class for Hydra (https://github.com/inspire-group/hydra) model for CIFAR-10 dataset. '''

import sys
import os

import torch
import torch.nn as nn
import torch.nn.functional as F
import tensorflow as tf
import math

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path

MODEL_PATH = get_res_path('./cifar10/model_best_dense.pth.tar')


def load(_):
    model = Hydra()
    model.load()
    return model


@pytorch_classifier_with_logits(n_class=10,
                                x_min=0.0,
                                x_max=1.0,
                                x_shape=(32, 32, 3),
                                x_dtype=tf.float32,
                                y_dtype=tf.int32)
class Hydra(torch.nn.Module):
    def __init__(self):
Пример #6
0
    sys.path.append(THIRD_PARTY_PATH)
MODULE_PATH = os.path.join(THIRD_PARTY_PATH, 'models/research/slim')
if MODULE_PATH not in sys.path:
    sys.path.append(MODULE_PATH)

import tensorflow as tf
import numpy as np

import models.research.slim.nets.resnet_v2 as resnet_v2

from ares import ClassifierWithLogits
from ares.utils import get_res_path, download_res

slim = tf.contrib.slim

MODEL_PATH = get_res_path('./imagenet/imagenet64_alp025_2018_06_26.ckpt')


def load(session):
    model = ResnetV2ALP()
    model.load(session, MODEL_PATH)
    return model


def download(model_path):
    url = 'http://download.tensorflow.org/models/adversarial_logit_pairing/imagenet64_alp025_2018_06_26.ckpt.tar.gz'
    if not os.path.exists(model_path + '.meta'):
        if not os.path.exists(os.path.dirname(model_path)):
            os.makedirs(os.path.dirname(model_path), exist_ok=True)
        import tarfile
        download_res(url, model_path + '.tar.gz')
Пример #7
0
if THIRD_PARTY_PATH not in sys.path:
    sys.path.append(THIRD_PARTY_PATH)
MODULE_PATH = os.path.join(THIRD_PARTY_PATH, 'models/research/slim')
if MODULE_PATH not in sys.path:
    sys.path.append(MODULE_PATH)

import tensorflow as tf

import models.research.slim.nets.inception_v3 as inception_v3

from ares import ClassifierWithLogits
from ares.utils import get_res_path, download_res

slim = tf.contrib.slim

MODEL_PATH = get_res_path('./imagenet/inception_v3.ckpt')


def load(session):
    model = InceptionV3()
    model.load(session, MODEL_PATH)
    return model


def download(model_path):
    if not os.path.exists(model_path):
        if not os.path.exists(os.path.dirname(model_path)):
            os.makedirs(os.path.dirname(model_path), exist_ok=True)
        gz_path = os.path.join(os.path.dirname(model_path),
                               'inception_v3_2016_08_28.tar.gz')
        if not os.path.exists(gz_path):
Пример #8
0
(https://github.com/tensorflow/models/blob/master/research/adv_imagenet_models/README.md) for ImageNet dataset.
'''

import sys
import os

import tensorflow as tf

from inception_v3 import inception_v3

from ares import ClassifierWithLogits
from ares.utils import get_res_path, download_res

slim = tf.contrib.slim

MODEL_PATH = get_res_path('./imagenet/ens4_adv_inception_v3')


def load(session):
    model = Ens4AdvInceptionV3()
    model.load(session, MODEL_PATH)
    return model


def download(model_path):
    if not os.path.exists(model_path):
        import tarfile

        os.makedirs(model_path, exist_ok=True)
        download_res(
            'http://download.tensorflow.org/models/ens4_adv_inception_v3_2017_08_18.tar.gz',
Пример #9
0
''' ImageNet dataset (ILSVRC 2012). '''

import os
import tensorflow as tf
import numpy as np
from PIL import Image

from ares.utils import get_res_path, download_res

PATH_IMGS = get_res_path('./imagenet/ILSVRC2012_img_val')
PATH_VAL_TXT = get_res_path('./imagenet/val.txt')
PATH_TARGET_TXT = get_res_path('./imagenet/target.txt')


def load_dataset_for_classifier(classifier,
                                offset=0,
                                load_target=False,
                                target_label=None,
                                clip=True):
    ''' Get an ImageNet dataset in tf.data.Dataset format.

    The first element of the dataset is the filename, the second one is the image tensor with shape of the classifier's
    ``x_shape`` in the classifier's ``x_dtype``, the third one is the label in the classifier's ``y_dtype``. If
    ``load_target`` is true, the target label would be returned as the fourth element of the dataset. It would
    automatically handle ``n_class == 1000`` and ``n_class == 1001`` case (assume the empty class is labeled 0).

    :param offset: Ignore the first ``offset`` images.
    :param load_target: Whether to load the target label.
    :param target_label: If it is a integer, the returned dataset would only include data points with this label.
    :param clip: If it is true, the images would be clipped towards center.
    :return: A ``tf.data.Dataset`` instance.
Пример #10
0
''' This file provides a wrapper class for MMC (https://github.com/P2333/Max-Mahalanobis-Training) model for CIFAR-10 dataset. '''

import os
import numpy as np
import tensorflow as tf
import keras
from keras.layers import Dense, Conv2D, BatchNormalization, Activation, GlobalAveragePooling2D
from keras.layers import Input, Lambda
from keras.regularizers import l2
from keras.models import Model
from scipy.io import loadmat

from ares.model import ClassifierWithLogits
from ares.utils import get_res_path, download_res

MODEL_PATH = get_res_path('./cifar10/mmc')


def load(session):
    model = MMC()
    model.load(session, MODEL_PATH)
    return model


def download(model_path):
    if not os.path.exists(model_path):
        os.makedirs(model_path, exist_ok=True)
    h5_name = 'model.180.h5'
    h5_path = os.path.join(model_path, h5_name)
    if not os.path.exists(h5_path):
        url = 'http://ml.cs.tsinghua.edu.cn/~tianyu/MMC/pretrained_models/MMC_mom_advtrain_cifar10/' + h5_name
Пример #11
0
''' This file provides a wrapper class for Fast_AT (https://github.com/locuslab/fast_adversarial) model for CIFAR-10 dataset. '''

import sys
import os

import torch
import torch.nn as nn
import torch.nn.functional as F
import tensorflow as tf

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path

MODEL_PATH = get_res_path('./cifar10/cifar_model_weights_30_epochs.pth')


def load(_):
    model = Fast_AT()
    model.load()
    return model


@pytorch_classifier_with_logits(n_class=10, x_min=0.0, x_max=1.0,
                                x_shape=(32, 32, 3), x_dtype=tf.float32, y_dtype=tf.int32)
class Fast_AT(torch.nn.Module):
    def __init__(self):
        torch.nn.Module.__init__(self)
        self.model = PreActResNet18().cuda()
        self._mean_torch = torch.tensor((0.4914, 0.4822, 0.4465)).view(3,1,1).cuda()
        self._std_torch = torch.tensor((0.2471, 0.2435, 0.2616)).view(3,1,1).cuda()
Пример #12
0
''' This file provides a wrapper class for Free_AT (https://github.com/mahyarnajibi/FreeAdversarialTraining) model for ImageNet dataset. '''

import sys
import os

import torch
import torchvision.models as models
import tensorflow as tf

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path

MODEL_PATH = get_res_path(
    './imagenet/free_at/resnet50_free_adv_step4_eps4_repeat4_bs256/model_best.pth.tar'
)


def load(_):
    model = Free_AT()
    model.load()
    return model


@pytorch_classifier_with_logits(n_class=1000,
                                x_min=0.0,
                                x_max=1.0,
                                x_shape=(288, 288, 3),
                                x_dtype=tf.float32,
                                y_dtype=tf.int32)
class Free_AT(torch.nn.Module):
    def __init__(self):
Пример #13
0
''' This file provides a wrapper class for Robust_Overfitting (https://github.com/locuslab/robust_overfitting) model for CIFAR-10 dataset. '''

import sys
import os

import torch
import torch.nn as nn
import torch.nn.functional as F
import tensorflow as tf

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path

MODEL_PATH = get_res_path('./cifar10/cifar10_wide10_linf_eps8.pth')


def load(_):
    model = Robust_Overfitting()
    model.load()
    return model


@pytorch_classifier_with_logits(n_class=10,
                                x_min=0.0,
                                x_max=1.0,
                                x_shape=(32, 32, 3),
                                x_dtype=tf.float32,
                                y_dtype=tf.int32)
class Robust_Overfitting(torch.nn.Module):
    def __init__(self):
        torch.nn.Module.__init__(self)
Пример #14
0
THIRD_PARTY_PATH = os.path.abspath(
    os.path.join(os.path.dirname(os.path.abspath(__file__)),
                 '../../third_party'))
if THIRD_PARTY_PATH not in sys.path:
    sys.path.append(THIRD_PARTY_PATH)

import torch
import tensorflow as tf

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path

from trades.models.wideresnet import WideResNet

MODEL_PATH = get_res_path('./cifar10/wrn.pt')


def load(_):
    model = WideResNet_TRADES()
    model.load()
    return model


@pytorch_classifier_with_logits(n_class=10,
                                x_min=0.0,
                                x_max=1.0,
                                x_shape=(32, 32, 3),
                                x_dtype=tf.float32,
                                y_dtype=tf.int32)
class WideResNet_TRADES(torch.nn.Module):
Пример #15
0
THIRD_PARTY_PATH = os.path.abspath(
    os.path.join(os.path.dirname(os.path.abspath(__file__)),
                 '../../third_party'))
if THIRD_PARTY_PATH not in sys.path:
    sys.path.append(THIRD_PARTY_PATH)

import torch
import tensorflow as tf

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path

from trades.models.wideresnet import WideResNet

MODEL_PATH = get_res_path('./cifar10/label_smoothing/model_best.pth')


def load(_):
    model = Label_Smoothing()
    model.load()
    return model


@pytorch_classifier_with_logits(n_class=10,
                                x_min=0.0,
                                x_max=1.0,
                                x_shape=(32, 32, 3),
                                x_dtype=tf.float32,
                                y_dtype=tf.int32)
class Label_Smoothing(torch.nn.Module):
Пример #16
0
                 '../../third_party'))
if THIRD_PARTY_PATH not in sys.path:
    sys.path.append(THIRD_PARTY_PATH)

from deepdefense_pytorch.models.cifar10 import ConvNet

from ares.model import ClassifierWithLogits
from ares.utils import get_res_path, download_res
from ares.model.pytorch_wrapper import pytorch_classifier_with_logits

import torch
import scipy.io
import numpy as np
import tensorflow as tf

MODEL_PATH = get_res_path('./cifar10/deepdefense')


def load(_):
    model = ConvNet_DeepDefense()
    model.load(MODEL_PATH)
    return model


def download(model_path):
    mat_name = 'cifar10-convnet-15742544.mat'
    mat_path = os.path.abspath(os.path.join(model_path, mat_name))
    tar_path = os.path.abspath(
        os.path.join(model_path, 'mnist-cifar10-data-model.tar'))
    if not os.path.exists(mat_path):
        if not os.path.exists(os.path.dirname(tar_path)):
Пример #17
0
import sys
import os

THIRD_PARTY_PATH = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../third_party'))
if THIRD_PARTY_PATH not in sys.path:
    sys.path.append(THIRD_PARTY_PATH)

from functools import partial
import tensorflow as tf

from ares.model import ClassifierWithLogits
from ares.utils import get_res_path

from cifar10_challenge.model import Model

MODEL_PATH = get_res_path('./cifar10/free_at/c10seed1cifar10_m8_eps8.0_b128')


def load(session):
    model = Free_AT()
    model.load(session, MODEL_PATH)
    return model


def convert_model(model_path):
    session = tf.Session(config=tf.ConfigProto(device_count={'GPU': 0}))
    reader = tf.train.load_checkpoint(os.path.join(model_path, 'checkpoint-79999'))
    var_list = list(reader.get_variable_to_dtype_map().keys())
    for var in var_list:
        tf.Variable(reader.get_tensor(var), name=var.replace(var, 'free_at/'+var))
    session.run(tf.global_variables_initializer())
Пример #18
0
from ares.utils import get_res_path
from ares.defense.jpeg_compression import jpeg_compression

import resnet56

MODEL_PATH = get_res_path('./cifar10/resnet56.ckpt')


def load(session):
    model = ResNet56_JPEG()
    model.load(MODEL_PATH, session)
    return model


@jpeg_compression()
class ResNet56_JPEG(resnet56.ResNet56):
    pass


if __name__ == '__main__':
    resnet56.download(MODEL_PATH)
Пример #19
0
''' This file provides a wrapper class for Feature_Scatter (https://github.com/Haichao-Zhang/FeatureScatter) model for CIFAR-10 dataset. '''

import sys
import os
import math

import torch
import torch.nn as nn
import torch.nn.functional as F
import tensorflow as tf

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path

MODEL_PATH = get_res_path('./cifar10/checkpoint-199-ipot')


def load(_):
    model = Feature_Scatter()
    model.load()
    return model


@pytorch_classifier_with_logits(n_class=10, x_min=0.0, x_max=1.0,
                                x_shape=(32, 32, 3), x_dtype=tf.float32, y_dtype=tf.int32)
class Feature_Scatter(torch.nn.Module):
    def __init__(self):
        torch.nn.Module.__init__(self)
        self.model = WideResNet(depth=28,
                                num_classes=10,
                                widen_factor=10).cuda()
Пример #20
0
''' CIFAR-10 dataset. '''

import tensorflow as tf
import numpy as np

from keras.datasets.cifar10 import load_data

from ares.utils import get_res_path

PATH_TARGET = get_res_path('cifar10/target.npy')


def load_dataset_for_classifier(classifier,
                                offset=0,
                                load_target=False,
                                target_label=None):
    ''' Get an CIFAR-10 dataset in tf.data.Dataset format.
    
    The first element of the dataset is the index, the second one is the image tensor with shape of the classifier's
    ``x_shape`` in the classifier's ``x_dtype``, the third one is the label in the classifier's ``y_dtype``. If
    ``load_target`` is true, the target label would be returned as the fourth element of the dataset.

    :param offset: Ignore the first ``offset`` images.
    :param load_target: Whether to load the target label.
    :param target_label: If it is a integer, the returned dataset would only include data points with this label.
    :return: A ``tf.data.Dataset`` instance.
    '''
    label_dtype = classifier.y_dtype
    x_shape, x_dtype, x_min, x_max = classifier.x_shape, classifier.x_dtype, classifier.x_min, classifier.x_max
    dataset = load_dataset(offset=offset,
                           label_dtype=label_dtype,
Пример #21
0
''' This file provides a wrapper class for Pre_Training (https://github.com/hendrycks/pre-training) model for CIFAR-10 dataset. '''

import sys
import os
import math

import torch
import torch.nn as nn
import torch.nn.functional as F
import tensorflow as tf

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path

MODEL_PATH = get_res_path('./cifar10/cifar10wrn_baseline_epoch_4.pt')


def load(_):
    model = Pre_Training()
    model.load()
    return model


@pytorch_classifier_with_logits(n_class=10,
                                x_min=0.0,
                                x_max=1.0,
                                x_shape=(32, 32, 3),
                                x_dtype=tf.float32,
                                y_dtype=tf.int32)
class Pre_Training(torch.nn.Module):
    def __init__(self):
Пример #22
0
''' This file provides a wrapper class for Fast_AT (https://github.com/locuslab/fast_adversarial) model for ImageNet dataset. '''

import sys
import os

import torch
import torchvision.models as models
import tensorflow as tf

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path

MODEL_PATH = get_res_path('./imagenet/imagenet_model_weights_4px.pth')


def load(_):
    model = Fast_AT()
    model.load()
    return model


@pytorch_classifier_with_logits(n_class=1000,
                                x_min=0.0,
                                x_max=1.0,
                                x_shape=(288, 288, 3),
                                x_dtype=tf.float32,
                                y_dtype=tf.int32)
class Fast_AT(torch.nn.Module):
    def __init__(self):
        torch.nn.Module.__init__(self)
        self.model = models.__dict__['resnet50']()
Пример #23
0
import sys
import os

THIRD_PARTY_PATH = os.path.abspath(
    os.path.join(os.path.dirname(os.path.abspath(__file__)),
                 '../../third_party'))
if THIRD_PARTY_PATH not in sys.path:
    sys.path.append(THIRD_PARTY_PATH)

from functools import partial
import tensorflow as tf

from ares.model import ClassifierWithLogits
from ares.utils import get_res_path, download_res

MODEL_PATH = get_res_path('./cifar10/pgd_at')

from cifar10_challenge.model import Model


def load(session):
    model = PgdAT()
    model.load(session, MODEL_PATH)
    return model


def download(model_path):
    if not os.path.exists(
            os.path.join(model_path, 'models/adv_trained/checkpoint')):
        zip_path = os.path.join(model_path, 'adv_trained.zip')
        if not os.path.exists(zip_path):
Пример #24
0
import os
import numpy as np
import tensorflow as tf
import keras
from keras.layers import Dense, Conv2D, BatchNormalization, Activation
from keras.layers import AveragePooling2D, Input, Flatten
from keras.regularizers import l2
from keras.models import Model

from ares.model import ClassifierWithLogits
from ares.utils import get_res_path, download_res

MODEL_PATH = get_res_path('./cifar10/adp')


def load(session):
    model = ADP()
    model.load(session, MODEL_PATH)
    return model


def download(model_path):
    if not os.path.exists(model_path):
        os.makedirs(model_path, exist_ok=True)
    h5_name = 'cifar10_ResNet110v2_model.200.h5'
    h5_path = os.path.join(model_path, h5_name)
    if not os.path.exists(h5_path):
        url = 'http://ml.cs.tsinghua.edu.cn/~tianyu/ADP/pretrained_models/ADP_standard_3networks/' + h5_name
        download_res(url, h5_path)
    npy_path = os.path.join(model_path, 'mean.npy')
    if not os.path.exists(npy_path):
Пример #25
0
''' This file provides a wrapper class for RST (https://github.com/yaircarmon/semisup-adv) model for CIFAR-10 dataset. '''

import sys
import os
import math

import torch
import torch.nn as nn
import torch.nn.functional as F
import tensorflow as tf

from ares.model.pytorch_wrapper import pytorch_classifier_with_logits
from ares.utils import get_res_path

MODEL_PATH = get_res_path('./cifar10/rst_adv.pt.ckpt')


def load(_):
    model = RST()
    model.load()
    return model


@pytorch_classifier_with_logits(n_class=10, x_min=0.0, x_max=1.0,
                                x_shape=(32, 32, 3), x_dtype=tf.float32, y_dtype=tf.int32)
class RST(torch.nn.Module):
    def __init__(self):
        torch.nn.Module.__init__(self)
        self.model = WideResNet(depth=28,
                                num_classes=10,
                                widen_factor=10)
Пример #26
0
if MODULE_PATH not in sys.path:
    sys.path.append(MODULE_PATH)

from convex_adversarial_pytorch.examples.problems import cifar_model_resnet, Flatten
from convex_adversarial.utils import Dense, DenseSequential

sys.path.remove(MODULE_PATH)

import torch
import numpy as np
import tensorflow as tf

from ares.utils import get_res_path, download_res
from ares.model.pytorch_wrapper import pytorch_classifier_with_logits

MODEL_PATH = get_res_path('./cifar10/convex')


def load(_):
    model = ResNet_Convex()
    model.load(MODEL_PATH)
    return model


def download(model_path):
    pth_path = os.path.join(model_path, 'cifar_resnet_2px.pth')
    if not os.path.exists(pth_path):
        if not os.path.exists(os.path.dirname(pth_path)):
            os.makedirs(os.path.dirname(pth_path), exist_ok=True)
        url = 'https://github.com/locuslab/convex_adversarial/blob/master/models_scaled/cifar_resnet_2px.pth?raw=true'
        download_res(url, pth_path)