Beispiel #1
0
def preprocess_data(X, Y, model, GPU_ID):
    X = np.array(keras.applications.resnet.preprocess_input(X))
    x_test_o = np.copy(X)
    loss = keras.losses.CategoricalCrossentropy()
    epsilon = .01 * 255
    pert = .001 * 255
    for i in range(int(len(X) / 100)):
        Z = tf.cast(X[i * 100:(100 * i + 100)], tf.float32)
        Y_next = Y[i * 100:(100 * i + 100)]
        for j in range((int)(math.floor(epsilon / pert))):
            with tf.GradientTape() as grad_tracker:
                grad_tracker.watch(Z)
                prediction = model(Z)
                next_loss = loss(Y_next, prediction)
            grad = grad_tracker.gradient(next_loss, Z)
            signed_grad = tf.sign(grad)
            Z = Z + pert * signed_grad
            Z1 = tf.clip_by_value(Z[:, :, :, 0], -103.939, 151.061)
            Z2 = tf.clip_by_value(Z[:, :, :, 1], -116.779, 138.221)
            Z3 = tf.clip_by_value(Z[:, :, :, 2], -123.68, 131.32)
            Z = tf.stack([Z1, Z2, Z3], axis=3)
        for k in range(100):
            X[100 * i + k, :, :, 0] = tf.math.add(Z[k, :, :, 0], 103.939) / 255
            X[100 * i + k, :, :, 1] = tf.math.add(Z[k, :, :, 1], 116.779) / 255
            X[100 * i + k, :, :, 2] = tf.math.add(Z[k, :, :, 2], 123.68) / 255
    X_p = X
    first, second, third, fourth = get_processed_data(X_p, img_size2, GPU_ID)
    first[:, :, :, 0] = tf.math.add(first[:, :, :, 0] * 255, -103.939)
    first[:, :, :, 1] = tf.math.add(first[:, :, :, 1] * 255, -116.779)
    first[:, :, :, 2] = tf.math.add(first[:, :, :, 2] * 255, -123.68)

    second[:, :, :, 0] = tf.math.add(second[:, :, :, 0] * 255, -103.939)
    second[:, :, :, 1] = tf.math.add(second[:, :, :, 1] * 255, -116.779)
    second[:, :, :, 2] = tf.math.add(second[:, :, :, 2] * 255, -123.68)

    third[:, :, :, 0] = tf.math.add(third[:, :, :, 0] * 255, -103.939)
    third[:, :, :, 1] = tf.math.add(third[:, :, :, 1] * 255, -116.779)
    third[:, :, :, 2] = tf.math.add(third[:, :, :, 2] * 255, -123.68)

    fourth[:, :, :, 0] = tf.math.add(fourth[:, :, :, 0] * 255, -103.939)
    fourth[:, :, :, 1] = tf.math.add(fourth[:, :, :, 1] * 255, -116.779)
    fourth[:, :, :, 2] = tf.math.add(fourth[:, :, :, 2] * 255, -123.68)

    X[:, :, :, 0] = tf.math.add(X[:, :, :, 0] * 255, -103.939)
    X[:, :, :, 1] = tf.math.add(X[:, :, :, 1] * 255, -116.779)
    X[:, :, :, 2] = tf.math.add(X[:, :, :, 2] * 255, -123.68)
    # encode to one-hot
    return x_test_o, X, first, second, third, fourth
Beispiel #2
0
def preprocess_data(X, Y, model, epsilon, pert):
    epsilon = epsilon * 255
    pert = pert * 255
    X = np.array(keras.applications.resnet.preprocess_input(X))
    loss = keras.losses.CategoricalCrossentropy()
    for i in range(int(len(X) / 100)):
        Z = tf.cast(X[i * 100:(100 * i + 100)], tf.float32)
        Y_next = Y[i * 100:(100 * i + 100)]
        if (epsilon != 0):
            for j in range((int)(math.floor(epsilon / pert))):
                with tf.GradientTape() as grad_tracker:
                    grad_tracker.watch(Z)
                    prediction = model(Z)
                    next_loss = loss(Y_next, prediction)
                grad = grad_tracker.gradient(next_loss, Z)
                signed_grad = tf.sign(grad)
                Z = Z + pert * signed_grad
                Z1 = tf.clip_by_value(Z[:, :, :, 0], -103.939, 151.061)
                Z2 = tf.clip_by_value(Z[:, :, :, 1], -116.779, 138.221)
                Z3 = tf.clip_by_value(Z[:, :, :, 2], -123.68, 131.32)
                Z = tf.stack([Z1, Z2, Z3], axis=3)
        for k in range(100):
            X[100 * i + k, :, :, 0] = tf.math.add(Z[k, :, :, 0], 103.939) / 255
            X[100 * i + k, :, :, 1] = tf.math.add(Z[k, :, :, 1], 116.779) / 255
            X[100 * i + k, :, :, 2] = tf.math.add(Z[k, :, :, 2], 123.68) / 255
    X_p = X
    X_p = get_processed_data(X_p, img_size2)
    X_p[:, :, :, 0] = tf.math.add(X_p[:, :, :, 0] * 255, -103.939)
    X_p[:, :, :, 1] = tf.math.add(X_p[:, :, :, 1] * 255, -116.779)
    X_p[:, :, :, 2] = tf.math.add(X_p[:, :, :, 2] * 255, -123.68)

    X[:, :, :, 0] = tf.math.add(X[:, :, :, 0] * 255, -103.939)
    X[:, :, :, 1] = tf.math.add(X[:, :, :, 1] * 255, -116.779)
    X[:, :, :, 2] = tf.math.add(X[:, :, :, 2] * 255, -123.68)
    # encode to one-hot
    return X_p, X