Foolbox предсказывает изображения и запускает атаки на изображения с сохраненной моделью тензорного потока - PullRequest
0 голосов
/ 05 июня 2018

У меня есть предварительно обученная модель в Tensorflow (полностью обученная предварительно обученная модель imagenet) с сетевой архитектурой inception_resnet_v2.Теперь я использую foolbox , чтобы использовать состязательные атаки на медицинских изображениях и получать прогнозы.

Но каждый раз, когда модель возвращает другое предсказание (другой класс с другой вероятностью) на исходном изображении. Я как-то повторно инициализирую веса?Более того, предсказания для изображений соперников не работают, и каждый раз выдается ошибка NoneType , и я не могу использовать любую атаку.

import tensorflow as tf
import numpy as np
from PIL import Image
from nets import nets_factory
import matplotlib.pyplot as plt

image = np.asarray(Image.open('1.jpg'))
data_tf = tf.convert_to_tensor(image, np.float32)
data_tf = tf.reshape(data_tf, [1, 299, 299, 3])
arg_scope = nets_factory.arg_scopes_map['inception_resnet_v2']()

graph = tf.get_default_graph()
with slim.arg_scope(arg_scope):
(logits, end_points) = \
    nets_factory.networks_map['inception_resnet_v2'](inputs=data_tf, num_classes=2, is_training=False)

with foolbox.models.TensorFlowModel(data_tf, logits, (0, 255)) as model:
    init_op = tf.global_variables_initializer()
    model.session.run(init_op)
    attack = foolbox.attacks.FGSM(model)
    saver = tf.train.import_meta_graph(
    '/local-scratch/arkadeep/1_June/tf_classification/experiment/final_model/model.ckpt-30589.meta',
    clear_devices=True)
    saver.restore(model.session,
              '/local-scratch/arkadeep/1_June/tf_classification/experiment/final_model/model.ckpt-30589')

    example_label = np.argmax(model.predictions(image))
    print(example_label)
    print(foolbox.utils.softmax(model.predictions(image))[0])

    adversarial = attack(image, example_label, unpack=False)
    print(np.argmax(model.predictions(adversarial.image)))
    print(foolbox.utils.softmax(model.predictions(adversarial))[0])

    print(adversarial.distance)

Вот трассировка стека :


[ada77@cs-mial-ts06 tf_classification]$ CUDA_VISIBLE_DEVICES=0 python example_code.py 
(1, 299, 299, 3)
2018-06-05 10:53:49.097397: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
0  //the class predicted
0.944639 // probability of the class
/home/ada77/.local/lib/python3.5/site-packages/foolbox/attacks/base.py:103: UserWarning: GradientSignAttack did not find an adversarial, maybe the model or the criterion is not supported by this attack.
  ' attack.'.format(self.name()))
Traceback (most recent call last):
  File "example_code.py", line 54, in 
    print(np.argmax(model.predictions(adversarial.image)))
  File "/home/ada77/.local/lib/python3.5/site-packages/foolbox/models/base.py", line 122, in predictions
    return np.squeeze(self.batch_predictions(image[np.newaxis]), axis=0)
TypeError: 'NoneType' object is not subscriptable
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...