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 config.session() as sess: sess.run(tf.global_variables_initializer()) final = out.reveal().eval(sess) np.testing.assert_array_equal(final, actual)
def test_stack(self): config = tfe.LocalConfig(['server0', 'server1', 'crypto_producer']) with tf.Session() as sess: x = tf.constant([1, 4]) y = tf.constant([2, 5]) z = tf.constant([3, 6]) out = tf.stack([x, y, z]) actual = sess.run(out) tf.reset_default_graph() with tfe.protocol.Pond(*config.get_players( 'server0, server1, crypto_producer')) as prot: x = prot.define_private_variable(np.array([1, 4])) y = prot.define_private_variable(np.array([2, 5])) z = prot.define_private_variable(np.array([3, 6])) out = prot.stack((x, y, z)) with config.session() as sess: sess.run(tf.global_variables_initializer()) final = out.reveal().eval(sess) np.testing.assert_array_equal(final, actual)
def test_forward(self): input_relu = np.array([-1.0, -0.5, 0.5, 3.0]).astype(np.float32) config = tfe.LocalConfig(['server0', 'server1', 'crypto_producer']) # relu pond with tfe.protocol.Pond(*config.get_players( 'server0, server1, crypto_producer')) as prot: relu_input = prot.define_private_variable(input_relu) relu_layer = Relu() relu_out_pond = relu_layer.forward(relu_input) with config.session() as sess: sess.run(tf.global_variables_initializer()) # outputs out_pond = relu_out_pond.reveal().eval(sess) # reset graph tf.reset_default_graph() with tf.Session() as sess: x = tf.Variable(input_relu, dtype=tf.float32) relu_out_tf = tf.nn.relu(x) sess.run(tf.global_variables_initializer()) out_tensorflow = sess.run(relu_out_tf) assert (np.isclose(out_pond, out_tensorflow, atol=0.6).all())
def test_stack_convert(self): tf.reset_default_graph() global global_filename global_filename = "stack.pb" input1 = np.array([1, 4]) input2 = np.array([2, 5]) input3 = np.array([3, 6]) path = export_stack(global_filename, input1.shape) tf.reset_default_graph() graph_def = read_graph(path) tf.reset_default_graph() actual = run_stack(input1, input2, input3) tf.reset_default_graph() config = tfe.LocalConfig([ 'server0', 'server1', 'crypto_producer', 'prediction_client', 'weights_provider', ]) with tfe.protocol.Pond(*config.get_players( 'server0, server1, crypto_producer')) as prot: prot.clear_initializers() class PredictionClient(tfe.io.InputProvider): self.input = None def provide_input(self): return tf.constant(self.input) i1 = PredictionClient(config.get_player('prediction_client')) i1.input = input1 i2 = PredictionClient(config.get_player('prediction_client')) i2.input = input2 i3 = PredictionClient(config.get_player('prediction_client')) i3.input = input3 input = [i1, i2, i3] converter = Converter(config, prot, config.get_player('weights_provider')) x = converter.convert(graph_def, input, register()) with config.session() as sess: tfe.run(sess, prot.initializer, tag='init') output = x.reveal().eval(sess, tag='reveal') np.testing.assert_array_almost_equal(output, actual, decimal=3)
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 config.session() as sess: sess.run(tf.global_variables_initializer()) # outputs out_pond = reshape_out_pond.reveal().eval(sess) # 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())
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(mean, variance, scale, offset) batchnorm_layer.initialize(input_shape=input_shape) batchnorm_out_pond = batchnorm_layer.forward(batchnorm_input) with config.session() as sess: sess.run(tf.global_variables_initializer()) out_pond = batchnorm_out_pond.reveal().eval(sess) # 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=3)
def test_strided_slice_convert(self): tf.reset_default_graph() global global_filename global_filename = "strided_slice.pb" path = export_strided_slice(global_filename) tf.reset_default_graph() graph_def = read_graph(path) tf.reset_default_graph() input = [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]] actual = run_strided_slice(input) tf.reset_default_graph() config = tfe.LocalConfig([ 'server0', 'server1', 'crypto_producer', 'prediction_client', 'weights_provider', ]) with tfe.protocol.Pond(*config.get_players( 'server0, server1, crypto_producer')) as prot: prot.clear_initializers() class PredictionClient(tfe.io.InputProvider): def provide_input(self): return tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]]) input = PredictionClient(config.get_player('prediction_client')) converter = Converter(config, prot, config.get_player('weights_provider')) x = converter.convert(graph_def, input, register()) with config.session() as sess: tfe.run(sess, prot.initializer, tag='init') output = x.reveal().eval(sess, tag='reveal') np.testing.assert_array_almost_equal(output, actual, decimal=3)
def test_forward(self): # input batch_size, channels_in, channels_out = 32, 3, 64 img_height, img_width = 28, 28 input_shape = (batch_size, channels_in, img_height, img_width) input_conv = np.random.normal(size=input_shape).astype(np.float32) # filters h_filter, w_filter, strides, padding = 2, 2, 2, 0 filter_shape = (h_filter, w_filter, channels_in, channels_out) filter_values = np.random.normal(size=filter_shape) config = tfe.LocalConfig(['server0', 'server1', 'crypto_producer']) # convolution pond with tfe.protocol.Pond(*config.get_players( 'server0, server1, crypto_producer')) as prot: conv_input = prot.define_private_variable(input_conv) conv_layer = tfe.layer.Conv2D(filter_shape, strides=2) conv_layer.initialize(input_shape, initial_weights=filter_values) conv_out_pond = conv_layer.forward(conv_input) with config.session() as sess: sess.run(tf.global_variables_initializer()) # outputs out_pond = conv_out_pond.reveal().eval(sess) # reset graph tf.reset_default_graph() # convolution tensorflow with tf.Session() as sess: # conv input x = tf.Variable(input_conv, dtype=tf.float32) x_NHWC = tf.transpose(x, (0, 2, 3, 1)) # convolution Tensorflow filters_tf = tf.Variable(filter_values, dtype=tf.float32) conv_out_tf = tf.nn.conv2d(x_NHWC, filters_tf, strides=[1, strides, strides, 1], padding="SAME") sess.run(tf.global_variables_initializer()) out_tensorflow = sess.run(conv_out_tf).transpose(0, 3, 1, 2) np.testing.assert_array_almost_equal(out_pond, out_tensorflow, decimal=3)
def test_avgpooling_convert(self): tf.reset_default_graph() global global_filename global_filename = "avgpool.pb" input_shape = [1, 28, 28, 1] path = export_avgpool(global_filename, input_shape) tf.reset_default_graph() graph_def = read_graph(path) tf.reset_default_graph() actual = run_avgpool(input_shape) tf.reset_default_graph() config = tfe.LocalConfig([ 'server0', 'server1', 'crypto_producer', 'prediction_client', 'weights_provider', ]) with tfe.protocol.Pond(*config.get_players( 'server0, server1, crypto_producer')) as prot: prot.clear_initializers() class PredictionClient(tfe.io.InputProvider): def provide_input(self): return tf.constant(np.ones(input_shape)) input = PredictionClient(config.get_player('prediction_client')) converter = Converter(config, prot, config.get_player('weights_provider')) x = converter.convert(graph_def, input, register()) with config.session() as sess: tfe.run(sess, prot.initializer, tag='init') output = x.reveal().eval(sess, tag='reveal') np.testing.assert_array_almost_equal(output, actual, decimal=3)
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
def test_noninteractive_truncate(self): config = tfe.LocalConfig(['server0', 'server1', 'crypto_producer']) server0, server1, cp = config.get_players( 'server0, server1, crypto_producer') prot = tfe.protocol.Pond(server0, server1, cp, use_noninteractive_truncation=True) with config.session() as sess: expected = np.array([12345.6789]) w = prot.define_private_variable(expected * (2**16)) # double precision v = prot.truncate(w) # single precision sess.run(tf.global_variables_initializer()) actual = v.reveal().eval(sess, tag='foo') assert np.isclose(actual, expected).all(), actual
import numpy as np import tensorflow as tf import tensorflow_encrypted as tfe from tensorflow_encrypted.protocol import Pond from tensorflow_encrypted.layer import Conv2D config = tfe.LocalConfig(['server0', 'server1', 'crypto_producer']) with tfe.protocol.Pond( *config.get_players('server0, server1, crypto_producer')) as prot: a = prot.define_constant(np.array([4, 3, 2, 1]).reshape(2, 2)) b = prot.define_constant(np.array([4, 3, 2, 1]).reshape(2, 2)) c = a * b d = prot.define_private_variable(np.array([1., 2., 3., 4.]).reshape(2, 2)) e = prot.define_private_variable(np.array([1., 2., 3., 4.]).reshape(2, 2)) # f = (d * .5 + e * .5) f = d * e # convolutions conv_input_shape = (32, 1, 28, 28) # NCHW conv_input = prot.define_private_variable( np.random.normal(size=conv_input_shape)) conv_layer = Conv2D((4, 4, 1, 20), strides=2) conv_layer.initialize(conv_input_shape) conv_out = conv_layer.forward(conv_input) with config.session() as sess: sess.run(tf.global_variables_initializer())
import numpy as np import tensorflow_encrypted as tfe from tensorflow_encrypted.protocol import Pond config = tfe.LocalConfig(3) prot = Pond(*config.players) # parameters np_w = np.array([.1, .2, .3, .4]).reshape(2, 2) np_b = np.array([.1, .2, .3, .4]).reshape(2, 2) w = prot.define_private_variable(np_w) b = prot.define_private_variable(np_b) # input x = prot.define_private_placeholder((2, 2)) # prediction y = prot.sigmoid(w.dot(x) + b).reveal() def sigmoid(x): return 1 / (1 + np.exp(-x)) with config.session() as sess: tfe.run(sess, prot.initializer, tag='init') np_x = np.array([.1, -.1, .2, -.2]).reshape(2, 2) feed_dict = x.feed_from_native(np_x)
import sys import numpy as np import tensorflow as tf import tensorflow_encrypted as tfe 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: # # assume we're running as a server # player_name = str(sys.argv[1]) server = config.server(player_name)
import tensorflow as tf import tensorflow_encrypted as tfe from convert import decode if len(sys.argv) > 1: # 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' ]) class ModelTrainer(tfe.io.InputProvider): BATCH_SIZE = 30 ITERATIONS = 60000 // BATCH_SIZE EPOCHS = 1 def build_data_pipeline(self): def normalize(image, label): image = tf.cast(image, tf.float32) / 255.0
import tensorflow as tf import tensorflow_encrypted as tfe player_names_fixed = ['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) class Inputter(tfe.io.InputProvider): def provide_input(self) -> List[tf.Tensor]: # pick random tensor to be averaged return [tf.random_normal(shape=(10,))] class ResultReceiver(tfe.io.OutputReceiver): def receive_output(self, tensors: List[tf.Tensor]) -> tf.Operation: average, = tensors # simply print average return tf.Print([], [average], summarize=10, message="Average:")