Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
0
    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)
Beispiel #4
0
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)
    prediction = y.eval(sess, feed_dict=feed_dict, tag='reveal')

    print('tf pred: ', prediction)
    print('numpy pred: ', sigmoid(np_w.dot(np_x) + np_b))
Beispiel #5
0
# c = a * b

a = prot.define_private_variable(np.array([1., 2., 3., 4.]).reshape(2, 2))
b = prot.define_private_variable(np.array([1., 2., 3., 4.]).reshape(2, 2))
c = prot.define_private_variable(np.array([1., 2., 3., 4.]).reshape(2, 2))

x = (a * b)
y = (a * c)
z = x + y

w = prot.define_private_variable(np.zeros((2, 2)))

with config.session() as sess:
    # print(c.eval(sess, tag='c'))

    tfe.run(sess, prot.initializer, tag='init')
    tfe.run(sess, prot.assign(w, z), tag='assign')
    tfe.run(sess, prot.assign(w, z), tag='assign')

    print(w.reveal().eval(sess, tag='reveal'))

    # g.eval(sess, tag='g')

    # sess.run(prot.assign(d, f))
    # sess.run(prot.assign(e, e))

    # print(f.reveal().eval(sess))

    # g = prot.sigmoid(d)
    # print(g.reveal().eval(sess))
Beispiel #6
0
    prediction_output = PredictionOutputReceiver(config.get_player('prediction_client'))

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

        # # treat weights as private
        # initial_w = prot.define_private_input(weights_input)
        # w = prot.define_private_variable(initial_w)

        # treat weights as private, but initial value as public
        # initial_w = prot.define_public_input(weights_input)
        # w = prot.define_private_variable(initial_w)

        # treat weights as public
        initial_w = prot.define_public_input(weights_input)
        w = prot.define_public_variable(initial_w)

        # load input for prediction
        x = prot.define_private_input(prediction_input)

        # compute prediction
        y = x.dot(w)

        # send output
        prediction_op = prot.define_output(y, prediction_output)

        with config.session() as sess:
            tfe.run(sess, tf.global_variables_initializer(), tag='init')

            for _ in range(5):
                tfe.run(sess, prediction_op, tag='prediction')
Beispiel #7
0

export_cnn()

tf.reset_default_graph()

model_filename = 'cnn.pb'
with gfile.FastGFile(model_filename, 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

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

with tfe.protocol.Pond(
        *config.get_players('server0, server1, crypto_producer')) as prot:
    input = PredictionInputProvider(config.get_player('prediction_client'))
    output = PredictionOutputReceiver(config.get_player('prediction_client'))

    c = convert.Converter(config, prot, config.get_player('weights_provider'))
    x = c.convert(graph_def, input, register())

    prediction_op = prot.define_output(x, output)

    with config.session() as sess:
        tfe.run(sess, prot.initializer, tag='init')

        tfe.run(sess, prediction_op, tag='prediction')
Beispiel #8
0
    w0, b0, w1, b1 = prot.define_private_input(model_trainer, masked=True)  # pylint: disable=E0632

    # we'll use the same parameters for each prediction so we cache them to avoid re-training each time
    w0, b0, w1, b1 = prot.cache([w0, b0, w1, b1])

    # get prediction input from client
    x = prot.define_private_input(prediction_client, masked=True)  # pylint: disable=E0632

    # compute prediction
    layer0 = prot.dot(x, w0) + b0
    layer1 = prot.sigmoid(layer0 * 0.1)  # input normalized to avoid large values
    layer2 = prot.dot(layer1, w1) + b1
    prediction = layer2

    # send prediction output back to client
    prediction_op = prot.define_output(prediction, prediction_client)


target = sys.argv[2] if len(sys.argv) > 2 else None
with config.session(target) as sess:

    print("Init")
    tfe.run(sess, tf.global_variables_initializer(), tag='init')

    print("Training")
    tfe.run(sess, tfe.global_caches_updator(), tag='training')

    for _ in range(5):
        print("Predicting")
        tfe.run(sess, prediction_op, tag='prediction')
Beispiel #9
0
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:")


# create players based on names from above
inputters = [Inputter(config.get_player(name)) for name in player_names_inputter]
result_receiver = ResultReceiver(config.get_player('result_receiver'))
server0 = config.get_player('server0')
server1 = config.get_player('server1')
crypto_producer = config.get_player('crypto_producer')

with tfe.protocol.Pond(server0, server1, crypto_producer) as prot:

    # get input from inputters as private values
    inputs = [prot.define_private_input(inputter)[0] for inputter in inputters]

    # sum all inputs and divide by count
    result = reduce(lambda x, y: x + y, inputs) * (1 / len(inputs))

    # send result to receiver
    result_op = prot.define_output([result], result_receiver)

    with config.session() as sess:
        tfe.run(sess, tf.global_variables_initializer())
        for _ in range(3):
            tfe.run(sess, result_op, tag='average')