IndexError: индекс 3 выходит за пределы оси 0 с размером 1 - PullRequest
0 голосов
/ 25 сентября 2019

Я пытаюсь запустить проект Github 'cleverhans', скрипт находится в папке cleverhans/example/face_adversarial_faces/facenet_fgsm.py, все зависимости установлены, но когда я запускаю python facenet_fgsm.py в виртуальной среде, терминал выдает эту ошибку 'IndexError: index3 выходит за пределы оси 0 с размером 1 ', как избавиться от этой проблемы?

Сценарий facenet_fgsm.py выглядит следующим образом:

import tensorflow.python.util.deprecation as deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False

import cv2
from skimage.transform import resize
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from cleverhans.cleverhans.model import Model
from cleverhans.cleverhans.attacks.fast_gradient_method import FastGradientMethod
from cleverhans.examples.facenet_adversarial_faces.set_loader import load_testset
from facenet.src.facenet import load_model

class InceptionResnetV1Model(Model):
    model_path = "~/Documents/fnatk/facenet/src/models/20190702-103304"

    def __init__(self):
        super(InceptionResnetV1Model, self).__init__(scope='model')

        # Load Facenet CNN
        load_model(self.model_path)
        # Save input and output tensors references
        graph = tf.get_default_graph()
        self.face_input = graph.get_tensor_by_name("input:0")
        self.embedding_output = graph.get_tensor_by_name("embeddings:0")

    def convert_to_classifier(self):
        # Create victim_embedding placeholder
        self.victim_embedding_input = tf.placeholder(
            tf.float32,
            shape=(None, 128))

        # Squared Euclidean Distance between embeddings
        distance = tf.reduce_sum(
            tf.square(self.embedding_output - self.victim_embedding_input),
            axis=1)

        # Convert distance to a softmax vector
        # 0.99 out of 4 is the distance threshold for the Facenet CNN
        threshold = 0.99
        score = tf.where(
            distance > threshold,
            0.5 + ((distance - threshold) * 0.5) / (4.0 - threshold),
            0.5 * distance / threshold)
        reverse_score = 1.0 - score
        self.softmax_output = tf.transpose(tf.stack([reverse_score, score]))

        # Save softmax layer
        self.layer_names = []
        self.layers = []
        self.layers.append(self.softmax_output)
        self.layer_names.append('logits')

        # define logits
        def calc(self, image):
            model_def = "models.inception_resnet_v1"
            network = importlib.import_module(model_def)
            prelogits, _ = network.inference(images_placeholder, 1.0,
                                             phase_train=False, weight_decay=0.0, reuse=False)
            batch_norm_params = {
                # Decay for the moving averages
                'decay': 0.995,
                # epsilon to prevent 0s in variance
                'epsilon': 0.001,
                # force in-place updates of mean and variance estimates
                'updates_collections': None,
                # Moving averages ends up in the trainable variables collection
                'variables_collections': [tf.GraphKeys.TRAINABLE_VARIABLES],
                # Only update statistics during training mode
                'is_training': False
            }
            bottleneck = slim.fully_connected(prelogits, 128, activation_fn=None,
                                              weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
                                              weights_regularizer=slim.l2_regularizer(0.0),
                                              normalizer_fn=slim.batch_norm,
                                              normalizer_params=batch_norm_params,
                                              scope='Bottleneck', reuse=False)
            logits = slim.fully_connected(bottleneck, 2, activation_fn=None,
                                          weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
                                          weights_regularizer=slim.l2_regularizer(0.),
                                          scope='Logits', reuse=False)
            return logits

    def fprop(self, x, set_ref=False):
        return dict(zip(self.layer_names, self.layers))


with tf.Graph().as_default():
  with tf.Session() as sess:
    # Load model
    model = InceptionResnetV1Model()
    # Convert to classifier
    model.convert_to_classifier()

    # Load pairs of faces and their labels in one-hot encoding
    faces1, faces2, labels = load_testset(10)

    # Create victims' embeddings using Facenet itself
    graph = tf.get_default_graph()
    phase_train_placeholder = graph.get_tensor_by_name("phase_train:0")
    feed_dict = {model.face_input: faces2,
                 phase_train_placeholder: False}
    victims_embeddings = sess.run(
        model.embedding_output, feed_dict=feed_dict)

    def fgsm_attack(eps, steps):
        # Define FGSM for the model
        alpha = eps / steps
        fgsm = FastGradientMethod(model)
        fgsm_params = {'eps': alpha,
                       'clip_min': 0.,
                       'clip_max': 1.}
        adv_x = fgsm.generate(model.face_input, **fgsm_params)

        # Run FGSM
        adv = faces1
        for i in range(steps):
          print("FGSM step " + str(i + 1))
          feed_dict = {model.face_input: adv,
                       model.victim_embedding_input: victims_embeddings,
                       phase_train_placeholder: False}
          adv = sess.run(adv_x, feed_dict=feed_dict)

        # Test accuracy of the model
        batch_size = graph.get_tensor_by_name("batch_size:0")

        feed_dict = {model.face_input: faces1,
                     model.victim_embedding_input: victims_embeddings,
                     phase_train_placeholder: False,
                     batch_size: 64}
        real_labels = sess.run(model.softmax_output, feed_dict=feed_dict)

        accuracy = np.mean(
            (np.argmax(labels, axis=-1)) == (np.argmax(real_labels, axis=-1))
        )
        print('Accuracy: ' + str(accuracy * 100) + '%')

        # Test accuracy against adversarial examples
        feed_dict = {model.face_input: adv,
                     model.victim_embedding_input: victims_embeddings,
                     phase_train_placeholder: False,
                     batch_size: 64}
        adversarial_labels = sess.run(
            model.softmax_output, feed_dict=feed_dict)

        same_faces_index = np.where((np.argmax(labels, axis=-1) == 0))
        different_faces_index = np.where((np.argmax(labels, axis=-1) == 1))

        dodge_acc = np.mean(
            (np.argmax(labels[same_faces_index], axis=-1)) ==
            (np.argmax(adversarial_labels[same_faces_index], axis=-1))
        )
        print('Accuracy against adversarial examples for '
              + 'same person faces (dodging): '
              + str(dodge_acc * 100)
              + '%')

        impersonate_acc = np.mean(
            (np.argmax(labels[different_faces_index], axis=-1)) == (
                np.argmax(adversarial_labels[different_faces_index], axis=-1))
        )
        print('Accuracy against adversarial examples for '
              + 'different people faces (impersonation): '
              + str(impersonate_acc * 100)
              + '%')

        return dodge_acc, impersonate_acc

    # Doesn't work atm
    def plot_graph(eps, dodge_accs, impersonate_accs):
        plt.plot(eps, dodge_accs, label = "dodge")
        plt.plot(eps, impersonate_accs, label = "impersonation")

        plt.xlabel("Epsilon")
        plt.ylabel("Accuracy (%)")
        plt.title("Accuracy against Epsilon graph")
        plt.legend()
        plt.show()


    #all_eps = [0.01, 0.05, 0.1, 0.2, 0.3, 0.5]
    all_eps = [0.01]
    all_steps = [1]
    dodge_accs = []
    impersonate_accs = []
    #steps = 10
    # eps = 0.3
    for eps in all_eps:
        for steps in all_steps:
            print("eps: " + str(eps) + "    steps: " + str(steps))
            dodge_acc, impersonate_acc = fgsm_attack(eps, steps)
            dodge_accs.append(dodge_acc)
            impersonate_accs.append(impersonate_acc)
    #plot_graph(np.array(eps), np.array(dodge_accs), np.array(impersonate_accs))

И терминал броситьпоявляется следующая ошибка:

Traceback (most recent call last):
  File "facenet_fgsm.py", line 191, in <module>
    dodge_acc, impersonate_acc = fgsm_attack(eps, steps)
  File "facenet_fgsm.py", line 151, in fgsm_attack
    (np.argmax(adversarial_labels[same_faces_index], axis=-1))
IndexError: index 3 is out of bounds for axis 0 with size 1

Я ожидаю успешного запуска этого скрипта.

...