Example #1
0
def create():
    weights_path = zoo.fetch_weights(
        'https://github.com/MadryLab/mnist_challenge_models/raw/master/secret.zip',
        unzip=True
    )
    weights_path = os.path.join(weights_path, 'models/secret')

    model = Model()

    sess = tf.Session().__enter__()
    saver = tf.train.Saver()
    checkpoint = tf.train.latest_checkpoint(weights_path)
    saver.restore(sess, checkpoint)

    images = model.x_input
    logits = model.pre_softmax

    fmodel = foolbox.models.TensorFlowModel(images, logits, bounds=(0, 1))

    return fmodel
Example #2
0
def test_fetch_weights_unzipped() -> None:
    weights_uri = "http://localhost:8080/weights.zip"
    raw_body = _random_body(zipped=False)

    # mock server
    responses.add(responses.GET,
                  weights_uri,
                  body=raw_body,
                  status=200,
                  stream=True)

    expected_path = _expected_path(weights_uri)

    if os.path.exists(expected_path):
        shutil.rmtree(expected_path)  # make sure path does not exist already

    file_path = fetch_weights(weights_uri)

    exists_locally = os.path.exists(expected_path)
    assert exists_locally
    assert expected_path in file_path
def create():
    # fetch weights
    weights_path = zoo.fetch_weights(
        'https://github.com/facebookresearch/ImageNet-Adversarial-Training/releases/download/v0.1/R152-Denoise.npz',
        unzip=False)

    # model constructer expects an ArgumentParser as argument
    parser = argparse.ArgumentParser()
    parser.add_argument('-d',
                        '--depth',
                        help='ResNet depth',
                        type=int,
                        default=152,
                        choices=[50, 101, 152])
    parser.add_argument('--arch',
                        help='Name of architectures defined in nets.py',
                        default='ResNetDenoise')
    args = parser.parse_args([])

    model = getattr(nets, args.arch + 'Model')(args)

    image = tf.placeholder(tf.float32, shape=(None, 3, 224, 224))

    with TowerContext(tower_name='', is_training=False):
        logits = model.get_logits(image)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.Session(config=config)
    with session.as_default():
        model = get_model_loader(weights_path).init(session)

        fmodel = TensorFlowModel(image,
                                 logits,
                                 channel_axis=1,
                                 bounds=[0, 255.],
                                 preprocessing=(127.5, 127.5))

    return fmodel
Example #4
0
def test_fetch_weights_zipped():
    weights_uri = 'http://localhost:8080/weights.zip'

    # mock server
    raw_body = _random_body(zipped=True)
    responses.add(responses.GET,
                  weights_uri,
                  body=raw_body,
                  status=200,
                  stream=True,
                  content_type='application/zip',
                  headers={'Accept-Encoding': 'gzip, deflate'})

    expected_path = _expected_path(weights_uri)

    if path_exists(expected_path):
        shutil.rmtree(expected_path)  # make sure path does not exist already

    file_path = fetch_weights(weights_uri, unzip=True)

    exists_locally = path_exists(expected_path)
    assert exists_locally
    assert expected_path in file_path
Example #5
0
def create():
    tf.enable_eager_execution()
    # load pretrained weights
    weights_path = zoo.fetch_weights(
        'http://download.tensorflow.org/models/adversarial_logit_pairing/imagenet64_alp025_2018_06_26.ckpt.tar.gz',
        unzip=True)
    with tf.get_default_graph().as_default():

        checkpoint = os.path.join(weights_path,
                                  'imagenet64_alp025_2018_06_26.ckpt')

        # load model
        input_ = tf.keras.layers.Input(dtype=tf.float32, shape=(64, 64, 3))
        model_fn_two_args = get_model('resnet_v2_50', 1001)
        model_fn = lambda x: model_fn_two_args(x, is_training=False)
        preprocessed = _normalize(input_)
        logits = model_fn(preprocessed)[:, 1:]

        # load pretrained weights into model
        variables_to_restore = tf.contrib.framework.get_variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)
        sess = tf.Session().__enter__()

        saver.restore(sess, checkpoint)

        def __call__(inputs):
            sess = tf.get_default_session()
            return sess.run(logits, feed_dict={input_: inputs})

    # create foolbox model
    preprocessing = None
    bounds = (0, 255)
    fmodel = foolbox.models.TensorFlowModel(__call__,
                                            bounds=bounds,
                                            preprocessing=preprocessing)

    return fmodel
Example #6
0
def test_no_uri_given():
    assert fetch_weights(None) is None