Exemple #1
0
    def test_strided_slice(self):

        config = tfe.LocalConfig([
            'server0',
            'server1',
            'crypto_producer'
        ])

        with tf.Session() as sess:
            t = tf.constant([[[1, 1, 1], [2, 2, 2]],
                             [[3, 3, 3], [4, 4, 4]],
                             [[5, 5, 5], [6, 6, 6]]])
            out = tf.strided_slice(t, [1, 0, 0], [2, 1, 3], [1, 1, 1])

            actual = sess.run(out)

        tf.reset_default_graph()

        with tfe.protocol.Pond(*config.get_players('server0, server1, crypto_producer')) as prot:
            x = np.array([[[1, 1, 1], [2, 2, 2]],
                          [[3, 3, 3], [4, 4, 4]],
                          [[5, 5, 5], [6, 6, 6]]])

            out = prot.define_private_variable(x)

            out = prot.strided_slice(out, [1, 0, 0], [2, 1, 3], [1, 1, 1])

            with tfe.Session() as sess:
                sess.run(tf.global_variables_initializer())
                final = sess.run(out.reveal())

        np.testing.assert_array_equal(final, actual)
Exemple #2
0
    def test_forward(self):
        input_shape = [4]
        input_sigmoid = np.array([-1.0, -0.5, 0.5, 3.0]).astype(np.float32)

        config = tfe.LocalConfig(['server0', 'server1', 'crypto_producer'])

        # sigmoid pond
        with tfe.protocol.Pond(*config.get_players(
                'server0, server1, crypto_producer')) as prot:

            sigmoid_input = prot.define_private_variable(input_sigmoid)
            sigmoid_layer = Sigmoid(input_shape)

            sigmoid_out_pond = sigmoid_layer.forward(sigmoid_input)

            with tfe.Session() as sess:

                sess.run(tf.global_variables_initializer())
                # outputs
                out_pond = sess.run(sigmoid_out_pond.reveal())

            # reset graph
            tf.reset_default_graph()

            with tf.Session() as sess:
                x = tf.Variable(input_sigmoid, dtype=tf.float32)

                sigmoid_out_tf = tf.nn.sigmoid(x)

                sess.run(tf.global_variables_initializer())

                out_tensorflow = sess.run(sigmoid_out_tf)

        assert (np.isclose(out_pond, out_tensorflow, atol=0.6).all())
Exemple #3
0
    def _build_cluster(self, workers):
        if len(workers) != 3:
            raise ValueError("Expected three workers but {} were given".format(
                len(workers)))

        player_to_worker_mapping = OrderedDict()
        player_to_worker_mapping["server0"] = workers[0]
        player_to_worker_mapping["server1"] = workers[1]
        player_to_worker_mapping["server2"] = workers[2]

        use_local_config = all(worker.host is None for worker in workers)
        if use_local_config:
            config = tfe.LocalConfig(
                player_names=player_to_worker_mapping.keys(),
                auto_add_unknown_players=False)
            return config, player_to_worker_mapping

        # use tfe.RemoteConfig
        hostmap = OrderedDict([
            (player_name, worker.host)
            for player_name, worker in player_to_worker_mapping.items()
        ])
        config = tfe.RemoteConfig(hostmap)

        return config, player_to_worker_mapping
Exemple #4
0
    def test_masked_concat(self):
        config = tfe.LocalConfig([
            'server0',
            'server1',
            'crypto_producer'
        ])

        with tf.Session() as sess:
            t1 = [[1, 2, 3], [4, 5, 6]]
            t2 = [[7, 8, 9], [10, 11, 12]]
            out = tf.concat([t1, t2], 0)
            actual = sess.run(out)

        tf.reset_default_graph()

        with tfe.protocol.Pond(*config.get_players('server0, server1, crypto_producer')) as prot:
            x = prot.mask(prot.define_private_variable(np.array(t1)))
            y = prot.mask(prot.define_private_variable(np.array(t2)))

            out = prot.concat([x, y], 0)

            with tfe.Session() as sess:
                sess.run(tf.global_variables_initializer())
                final = sess.run(out.unmasked.reveal())

        np.testing.assert_array_equal(final, actual)
    def test_forward(self) -> None:

        batch_size, channels_in, img_height, img_width = (32, 3, 28, 28)

        input_shape = [batch_size, channels_in, img_height, img_width]
        input_batchnorm = np.random.normal(size=input_shape).astype(np.float32)

        # I reshaped the input because tf.nn.batch_normalization doesn't reshape it
        # automatically However tf encrypted will reshape automatically the input
        mean = np.array([2.0, 1.5, 20.8]).reshape(1, channels_in, 1,
                                                  1).astype(np.float32)
        variance = np.array([0.5, 0.3, 0.1]).reshape(1, channels_in, 1,
                                                     1).astype(np.float32)
        scale = np.array([0.3, 0.5, 0.8]).reshape(1, channels_in, 1,
                                                  1).astype(np.float32)
        offset = np.array([1.5, 1.2, 1.4]).reshape(1, channels_in, 1,
                                                   1).astype(np.float32)
        variance_epsilon = 1e-8

        config = tfe.LocalConfig(['server0', 'server1', 'crypto_producer'])

        with tfe.protocol.Pond(*config.get_players(
                'server0, server1, crypto_producer')) as prot:
            batchnorm_input = prot.define_private_variable(input_batchnorm)

            batchnorm_layer = Batchnorm(input_shape, mean, variance, scale,
                                        offset)
            batchnorm_layer.initialize()
            batchnorm_out_pond = batchnorm_layer.forward(batchnorm_input)

            with tfe.Session() as sess:
                sess.run(tf.global_variables_initializer())
                out_pond = sess.run(batchnorm_out_pond.reveal())

            # reset graph
            tf.reset_default_graph()

            with tf.Session() as sess:
                x = tf.Variable(input_batchnorm, dtype=tf.float32)

                batchnorm_out_tf = tf.nn.batch_normalization(
                    x, mean, variance, offset, scale, variance_epsilon)

                sess.run(tf.global_variables_initializer())

                out_tensorflow = sess.run(batchnorm_out_tf)

                np.testing.assert_array_almost_equal(out_pond,
                                                     out_tensorflow,
                                                     decimal=1)
    def _get_fixtures(self, even=True) -> fixtureType:
        if even:
            batch_size, channels_in = 2, 2
            img_height, img_width = 8, 8
        else:
            batch_size, channels_in = 1, 1
            img_height, img_width = 5, 11
        input_shape = (batch_size, channels_in, img_height, img_width)

        n_elements = batch_size * channels_in * img_height * img_width
        input_pool = np.ones(n_elements, dtype=np.float32).reshape(input_shape)

        config = tfe.LocalConfig(['server0', 'server1', 'crypto_producer'])

        return input_pool, input_shape, config
Exemple #7
0
    def test_reduce_sum_huge_vector(self):
        config = tfe.LocalConfig(['server0', 'server1', 'crypto_producer'])

        t = [1] * 2**13
        with tf.Session() as sess:
            out = tf.reduce_sum(t)
            actual = sess.run(out)

        with tfe.protocol.Pond(*config.get_players(
                'server0, server1, crypto_producer')) as prot:
            b = prot.define_private_variable(tf.constant(t))
            out = prot.reduce_sum(b)

            with tfe.Session() as sess:
                sess.run(tf.global_variables_initializer())
                final = sess.run(out.reveal())

        np.testing.assert_array_equal(final, actual)
    def test_forward(self):
        input_shape = [2, 3, 4, 5]
        output_shape = [2, -1]
        input_reshape = np.random.standard_normal(input_shape)

        config = tfe.LocalConfig([
            'server0',
            'server1',
            'crypto_producer'
        ])

        # reshape pond
        with tfe.protocol.Pond(*config.get_players('server0, server1, crypto_producer')) as prot:

            reshape_input = prot.define_private_variable(input_reshape)
            reshape_layer = Reshape(input_shape, output_shape)

            reshape_out_pond = reshape_layer.forward(reshape_input)

            with tfe.Session() as sess:

                sess.run(tf.global_variables_initializer())
                # outputs
                out_pond = sess.run(reshape_out_pond.reveal())

            # reset graph
            tf.reset_default_graph()

            with tf.Session() as sess:
                x = tf.Variable(input_reshape, dtype=tf.float32)

                reshape_out_tf = tf.reshape(x, output_shape)

                sess.run(tf.global_variables_initializer())

                out_tensorflow = sess.run(reshape_out_tf)

        assert(np.isclose(out_pond, out_tensorflow, atol=0.6).all())
Exemple #9
0
import sys

import tensorflow as tf
import tf_encrypted as tfe
from tf_encrypted.layers import Conv2D, Dense, Sigmoid, Reshape

config = tfe.LocalConfig([
    'server0',
    'server1',
    'crypto-producer',
    'weights-provider',
    'prediction-client'
])

# config = tfe.RemoteConfig([
#     ('server0', 'localhost:4440'),
#     ('server1', 'localhost:4441'),
#     ('crypto-producer', 'localhost:4442'),
#     ('weights-provider', 'localhost:4443'),
#     ('prediction-client', 'localhost:4444')
# ])


if len(sys.argv) > 1:
    if isinstance(config, tfe.LocalConfig):
        raise Exception("You can launch a configured server only with a remote configuration")
    #
    # assume we're running as a server
    #

    player_name = str(sys.argv[1])
import tf_encrypted as tfe

from examples.mnist.convert import get_data_from_tfrecord

# tfe.setMonitorStatsFlag(True)

if len(sys.argv) >= 2:
    # config file was specified
    config_file = sys.argv[1]
    config = tfe.config.load(config_file)
else:
    # default to using local config
    config = tfe.LocalConfig([
        'server0',
        'server1',
        'crypto-producer',
        'model-trainer',
        'prediction-client'
    ])
tfe.set_config(config)
tfe.set_protocol(tfe.protocol.SecureNN(*tfe.get_config().get_players(['server0', 'server1', 'crypto-producer'])))


def weight_variable(shape, gain):
    """weight_variable generates a weight variable of a given shape."""
    if len(shape) == 2:
        fan_in, fan_out = shape
    elif len(shape) == 4:
        h, w, c_in, c_out = shape
        fan_in = h * w * c_in
        fan_out = h * w * c_out
Exemple #11
0
    'server0', 'server1', 'crypto-producer', 'result-receiver'
]

if len(sys.argv) >= 2:
    # config file was specified
    config_file = sys.argv[1]
    config = tfe.config.load(config_file)
    player_names_inputter = [
        player.name for player in config.players
        if player.name not in player_names_fixed
    ]

else:
    # create a local config with all inputters and the usual suspects needed for the Pond protocol
    player_names_inputter = ['inputter-{}'.format(i) for i in range(5)]
    config = tfe.LocalConfig(player_names_fixed + player_names_inputter)
tfe.set_config(config)


def provide_input() -> tf.Tensor:
    # pick random tensor to be averaged
    return tf.random_normal(shape=(10, ))


def receive_output(*args: List[tf.Tensor]) -> tf.Operation:
    average, = args
    # simply print average
    return tf.Print([], [average], summarize=10, message="Average:")


# create players based on names from above
Exemple #12
0
import tensorflow as tf
import tensorflow.keras as keras

import tf_encrypted as tfe
from conv_convert import get_data_from_tfrecord

# tfe.set_tfe_events_flag(True)

if len(sys.argv) >= 2:
    # config file was specified
    config_file = sys.argv[1]
    config = tfe.config.load(config_file)
else:
    # default to using local config
    config = tfe.LocalConfig([
        "server0", "server1", "crypto-producer", "model-trainer",
        "prediction-client"
    ])
tfe.set_config(config)
tfe.set_protocol(
    tfe.protocol.SecureNN(*tfe.get_config().get_players(
        ["server0", "server1", "crypto-producer"])))

session_target = sys.argv[2] if len(sys.argv) > 2 else None


class ModelTrainer:
    """Contains code meant to be executed by a model training Player."""

    BATCH_SIZE = 256
    ITERATIONS = 60000 // BATCH_SIZE
    EPOCHS = 3
import sys

import tensorflow as tf

import tf_encrypted as tfe
from tf_encrypted.layers import Conv2D
from tf_encrypted.layers import Dense
from tf_encrypted.layers import Reshape
from tf_encrypted.layers import Sigmoid

config = tfe.LocalConfig([
    "server0", "server1", "crypto-producer", "weights-provider",
    "prediction-client"
])

# config = tfe.RemoteConfig([
#     ('server0', 'localhost:4440'),
#     ('server1', 'localhost:4441'),
#     ('crypto-producer', 'localhost:4442'),
#     ('weights-provider', 'localhost:4443'),
#     ('prediction-client', 'localhost:4444')
# ])

if len(sys.argv) > 1:
    if isinstance(config, tfe.LocalConfig):
        raise Exception(
            "You can launch a configured server only with a remote configuration"
        )
    #
    # assume we're running as a server
    #