thu-ml / ares

A Python library for adversarial machine learning focusing on benchmarking adversarial robustness.
https://thu-ml-ares.rtfd.io
Apache License 2.0
476 stars 88 forks source link

how to integrate other models into realsafe? Like tensorflow hub models #8

Closed mayfly227 closed 3 years ago

mayfly227 commented 3 years ago

i want use other models to generate adversarial samples, but something wrong with it.can you tell me how to do it? This is my code

import tensorflow as tf
import tensorflow_hub as hub

from realsafe.model.base import ClassifierWithLogits
from realsafe.utils import get_res_path

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

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

class VGGDEEP(ClassifierWithLogits):
    def __init__(self):
        ClassifierWithLogits.__init__(self,
                                      x_min=0.0, x_max=1.0, x_shape=(32, 32, 3,), x_dtype=tf.float32,
                                      y_dtype=tf.int32, n_class=10)
        self.model = hub.Module("https://hub.tensorflow.google.cn/deepmind/ganeval-cifar10-convnet/1")

    def _logits_and_labels(self, xs_ph):
        logits = self.model(xs_ph)
        predicts = tf.nn.softmax(logits)
        predicted_labels = tf.argmax(predicts, 1, output_type=tf.int32)
        return logits, predicted_labels

    def load(self, model_path, session):
        # var_list_pre = set(tf.global_variables())
        # x_input = tf.placeholder(tf.float32, shape=(None,) + self.x_shape)
        # self.model.inference(x_input, self.num_residual_blocks, reuse=tf.AUTO_REUSE)
        # var_list_post = set(tf.global_variables())
        # var_list = list(var_list_post - var_list_pre)
        # if len(var_list) > 0:
        #     saver = tf.train.Saver(var_list=var_list)
        #     saver.restore(session, model_path)
        pass

as you see ,i want to use this model(https://hub.tensorflow.google.cn/deepmind/ganeval-cifar10-convnet/1) to genernate adv samples.

mayfly227 commented 3 years ago

i am not familiar with tensorflow 1.x

Fugoes commented 3 years ago

You are quite closed to the answer. :+1:

import tensorflow as tf
import tensorflow_hub as hub

from realsafe.model.base import ClassifierWithLogits
from realsafe.utils import get_res_path

def load(session):
    model = VGGDEEP()
    model.load(session)
    return model

class VGGDEEP(ClassifierWithLogits):
    def __init__(self):
        ClassifierWithLogits.__init__(self,
                                      x_min=0.0, x_max=1.0, x_shape=(32, 32, 3,), x_dtype=tf.float32,
                                      y_dtype=tf.int32, n_class=10)
        self.model = hub.Module("https://hub.tensorflow.google.cn/deepmind/ganeval-cifar10-convnet/1")

    def _logits_and_labels(self, xs_ph):
        logits = self.model(xs_ph)
        predicts = tf.nn.softmax(logits)
        predicted_labels = tf.argmax(predicts, 1, output_type=tf.int32)
        return logits, predicted_labels

    def load(self, session):
        session.run(tf.variables_initializer(self.model.variables))

Save it to ganeval.py. To run some basic tests:

python3 -m realsafe.benchmark.prediction_cli --dataset cifar10 --offset 0 --count 1000 --output x.npy --batch-size 100 ganeval.py
mayfly227 commented 3 years ago

You are quite closed to the answer. 👍

import tensorflow as tf
import tensorflow_hub as hub

from realsafe.model.base import ClassifierWithLogits
from realsafe.utils import get_res_path

def load(session):
    model = VGGDEEP()
    model.load(session)
    return model

class VGGDEEP(ClassifierWithLogits):
    def __init__(self):
        ClassifierWithLogits.__init__(self,
                                      x_min=0.0, x_max=1.0, x_shape=(32, 32, 3,), x_dtype=tf.float32,
                                      y_dtype=tf.int32, n_class=10)
        self.model = hub.Module("https://hub.tensorflow.google.cn/deepmind/ganeval-cifar10-convnet/1")

    def _logits_and_labels(self, xs_ph):
        logits = self.model(xs_ph)
        predicts = tf.nn.softmax(logits)
        predicted_labels = tf.argmax(predicts, 1, output_type=tf.int32)
        return logits, predicted_labels

    def load(self, session):
        session.run(tf.variables_initializer(self.model.variables))

Save it to ganeval.py. To run some basic tests:

python3 -m realsafe.benchmark.prediction_cli --dataset cifar10 --offset 0 --count 1000 --output x.npy --batch-size 100 ganeval.py

very thanks!