Exemplo n.º 1
0
def run_client(FLAGS, data):
    port = FLAGS.port
    if isinstance(port, list) or isinstance(port, tuple):
        print("WARNING: list ports were passed. Only one should be passed.")
        port = port[0]  # only one port should be passed
    if FLAGS.batch_size > 1:
        raise ValueError('batch size > 1 not currently supported.')
    inference_start = time.time()
    client = pyhe_client.HESealClient(
        FLAGS.hostname,
        port,
        FLAGS.batch_size,
        {
            "import/input"
            # FLAGS.tensor_name
            : (FLAGS.encrypt_data_str, data)},
    )
    print(f"data shape: {data.shape}")
    r_rstar = np.array(client.get_results())
    inference_end = time.time()
    print(f"Inference time: {inference_end - inference_start}s")
    with open(inference_times_name, 'a') as outfile:
        outfile.write(str(inference_end - inference_start))
        outfile.write('\n')
    print('r_rstar (r-r*): ', array_str(r_rstar))

    rstar = FLAGS.r_star
    if rstar is None:
        raise ValueError('r_star should be provided but was None.')

    r_rstar = round_array(x=r_rstar, exp=FLAGS.round_exp)
    print('rounded r_rstar (r-r*): ', array_str(r_rstar))
    print("Writing out logits file to txt.")
    with open(f'{out_client_name}{port}privacy.txt', 'w') as outfile:
        for val in r_rstar.flatten():
            outfile.write(f"{int(val)}\n")

    # do 2 party computation with each Answering Party
    msg = 'starting 2pc with Answering Party'
    print(msg)
    log_timing(stage='client:' + msg,
               log_file=FLAGS.log_timing_file)
    # completed = {port: False for port in flags.ports}
    max_t = time.time() + 100000
    while not os.path.exists(f"{out_final_name}{port}privacy.txt"):
        print(f'client starting 2pc with port: {port}')
        process = subprocess.Popen(
            ['./gc-emp-test/bin/argmax_1', '2', '12345',
             f'{out_client_name}{port}privacy.txt'])
        time.sleep(1)
        if time.time() > max_t:
            raise ValueError("Step 1' of protocol never finished. Issue.")
    log_timing(stage='client:finished 2PC',
               log_file=FLAGS.log_timing_file)
    return r_rstar, rstar
Exemplo n.º 2
0
 def test_round_array3(self):
     a = np.array([[0.38, 0.42], [0.28, 0.32]])
     a = round_array(x=a, exp=100)
     print('rounded a: ', array_str(a))
     b = [[481707228086727178198246752256, 532413252095856328925366976512],
          [354942168063904266196074102784, 405648192073033416923194327040]]
     np.testing.assert_array_equal(x=a, y=b)
Exemplo n.º 3
0
def check_rstar_stage1(rstar, r_rstar, labels, port):
    if len(labels.shape) > 1:
        # change from one-hot encoding to labels
        labels = labels.argmax(axis=1)

    correct, pred_r = is_correct_pred(r_rstar=r_rstar,
                                      rstar=rstar,
                                      labels=labels)
    if correct:
        print(f'expected label(s) for port {port}: ', labels)
        print('r_rstar label(s): ', pred_r)
        print('stage 1 correct')
    else:
        print('rstar: ', array_str(rstar))
        print('r_rstar: ', array_str(r_rstar))
        print(f'expected label(s) for port {port}: ', labels, ' type: ',
              type(labels))
        print('r_rstar label(s): ', pred_r, ' type: ', type(pred_r))
        raise Exception(f'Failed stage 1 for port: {port}.')
Exemplo n.º 4
0
def run_server(FLAGS, query):
    # tf.import_graph_def(load_pb_file(FLAGS.model_file))
    tf.import_graph_def(
        load_pb_file("/home/dockuser/models/cryptonets-relu.pb"))
    # tf.import_graph_def(load_pb_file(f"/home/dockuser/models/{FLAGS.port}.pb"))
    print("loaded model")
    print_nodes()
    print(f"query: {query.shape}")

    # Get input / output tensors
    x_input = tf.compat.v1.get_default_graph().get_tensor_by_name(
        # FLAGS.input_node
        # "import/Placeholder:0"
        "import/input:0")
    y_output = tf.compat.v1.get_default_graph().get_tensor_by_name(
        "import/output/BiasAdd:0"
        # FLAGS.output_node
        # "import/dense/BiasAdd:0"
    )

    # weight_check = tf.compat.v1.get_default_graph().get_tensor_by_name("import/conv2d_1/kernel:0")
    # print(weight_check)

    # Load saved model
    #
    # model = models.Local_Model(FLAGS.num_classes, FLAGS.dataset_name)  # load the model object
    # input_x = tf.compat.v1.placeholder(  # define input
    #     shape=(
    #         None,
    #         3,
    #         32,
    #         32,
    #     ), name="input", dtype=tf.float32)
    # init = tf.compat.v1.global_variables_initializer()
    # model.build((None, 3, 32, 32))
    # model.compile('adam', tf.keras.losses.CategoricalCrossentropy())

    # output_y = model.get_out(input_x)  # input through layers
    # print("loaded model")
    # print_nodes()
    # print(input_x)
    # print(output_y)
    # Get input / output tensors
    # x_input = tf.compat.v1.get_default_graph().get_tensor_by_name(
    #     FLAGS.input_node)
    # y_output = tf.compat.v1.get_default_graph().get_tensor_by_name(
    #     FLAGS.output_node)
    # y_max = tf.nn.softmax(y_output)
    # y_max = y_output
    # y_max = approximate_softmax(x=y_output, nr_terms=2)
    # y_max = max_pool(y_output=y_output, FLAGS=FLAGS)
    print('r_star: ', FLAGS.r_star)
    r_star = np.array(FLAGS.r_star)
    # r - r* (subtract the random vector r* from logits)
    y_max = tf.subtract(y_output, tf.convert_to_tensor(r_star,
                                                       dtype=tf.float32))

    # Create configuration to encrypt input
    # config = server_config_from_flags(FLAGS, x_input.name)
    config = server_config_from_flags(FLAGS, x_input.name)
    with tf.compat.v1.Session(config=config) as sess:
        sess.run(tf.compat.v1.global_variables_initializer())
        # model.initialize_weights(FLAGS.model_file)
        start_time = time.time()
        print(f"query shape before processing: {query.shape}")
        inference_start = time.time()
        y_hat = sess.run(y_max, feed_dict={x_input: query})
        inference_end = time.time()
        print(f"Inference time: {inference_end - inference_start}s")
        with open(inference_no_network_times_name, 'a') as outfile:
            outfile.write(str(inference_end - inference_start))
            outfile.write('\n')
        elasped_time = time.time() - start_time
        print("total time(s)", np.round(elasped_time, 3))
        party_id = int(FLAGS.port)

        msg = "doing 2pc"
        print(msg)
        log_timing(stage='server_client:' + msg,
                   log_file=FLAGS.log_timing_file)
        print('r_star (r*): ', array_str(r_star))
        r_star = round_array(x=r_star, exp=FLAGS.round_exp)
        print('rounded r_star (r*): ', array_str(r_star))
        if FLAGS.backend == 'HE_SEAL':
            argmax_time_start = time.time()
            with open(f'{out_server_name}{FLAGS.port}.txt',
                      'w') as outfile:  # party id
                # assume batch size of 1 for now TODO: make this work for > 1 batch size
                for val in r_star.flatten():
                    outfile.write(f"{int(val)}" + '\n')
            process = subprocess.Popen([
                './gc-emp-test/bin/argmax_1', '1', '12345',
                f'{out_server_name}{FLAGS.port}.txt',
                f'{out_final_name}{FLAGS.port}.txt'
            ])
            # time.sleep(15)
            process.wait()
            argmax_time_end = time.time()
            with open(argmax_times_name, 'a') as outfile:
                outfile.write(str(argmax_time_end - argmax_time_start))
                outfile.write("\n")
        log_timing(stage='server_client:finished 2PC',
                   log_file=FLAGS.log_timing_file)
Exemplo n.º 5
0
def run_client(FLAGS, data=None, labels=None):
    if data is None:
        data = np.load(consts.input_data)
    if labels is None:
        labels = np.load(consts.input_labels)

    r_rstars = {}
    for i, port in enumerate(FLAGS.ports):

        client = pyhe_client.HESealClient(
            FLAGS.hostname,
            port,
            FLAGS.batch_size,
            {FLAGS.tensor_name: (FLAGS.encrypt_data_str, data)},
        )

        raw_results = np.array(client.get_results())
        print('raw results: ', array_str(raw_results))

        rstar = None
        if FLAGS.debug is True:
            if FLAGS.rstar is not None:
                raise Exception(
                    "Either debug or r_star or both flags have to be None.")
            raw_shape_0 = raw_results.shape[0]
            expected_shape_0 = 2 * FLAGS.batch_size
            if raw_shape_0 != expected_shape_0:
                raise Exception(
                    f'Expected r_star for each example in the batch'
                    f'and dim 0 size of the result: {expected_shape_0}'
                    f', but received result with dim 0 size:'
                    f' {raw_shape_0}')
            r_rstar = raw_results[:FLAGS.batch_size]
            rstar = raw_results[FLAGS.batch_size:]

        else:
            if FLAGS.rstar is None:
                rstar = None
            elif FLAGS.rstar == [-1.0]:
                raise Exception('We do not generate r_star in the client.'
                                'r_star provided is [-1.0].')
            if FLAGS.rstar is not None:
                rstar = np.array(FLAGS.rstar)
            r_rstar = raw_results

        print('r_rstar (r-r*): ', array_str(r_rstar))

        if FLAGS.round_exp:
            # r_rstar = (r_rstar * 2 ** FLAGS.round_exp).astype(np.int64)
            r_rstar = round_array(x=r_rstar, exp=FLAGS.round_exp)
            print('rounded r_rstar (r-r*): ', array_str(r_rstar))

        r_rstars[port] = r_rstar
        y_pred_reshape = np.array(r_rstar).reshape(FLAGS.batch_size, 10)

        y_labels = labels.argmax(axis=1)
        print("y_test: ", y_labels)

        y_pred = y_pred_reshape.argmax(axis=1)
        print("y_pred: ", y_pred)

        correct = np.sum(np.equal(y_pred, y_labels))
        acc = correct / float(FLAGS.batch_size)
        print("correct from original result: ", correct)
        print(
            "Accuracy original result (batch size", FLAGS.batch_size, ") =",
            acc * 100.0, "%")
        with open(f'{out_client_name}{port}privacy.txt', 'w') as outfile:
            for val in y_pred_reshape.flatten():
                outfile.write(f"{int(val)}\n")

        if rstar is not None:
            results_r = y_pred_reshape + rstar
            y_pred_r = results_r.argmax(axis=1)
            print('y_pred_r: ', y_pred_r)
            correct = np.sum(np.equal(y_pred_r, y_labels))
            acc = correct / float(FLAGS.batch_size)
            print("correct after adding r*: ", correct)
            print(
                "Accuracy after adding r* (batch size", FLAGS.batch_size, ") =",
                acc * 100.0, "%")

        print(port, "DONE----------------------")
        time.sleep(5)

    # do 2 party computation with each Answering Party
    print('starting 2pc')
    completed = {port: False for port in FLAGS.ports}
    n_parties = len(completed.keys())
    max_t = time.time() + 100000
    processes = []
    while sum(completed.values()) < n_parties and time.time() < max_t:
        for port in completed.keys():
            if not completed[port]:
                if not os.path.exists(f"{out_client_name}{port}privacy.txt"):
                    raise ValueError('something broke')
                out_server_file = f"{out_server_name}{port}privacy.txt"
                if os.path.exists(out_server_file):
                    if FLAGS.predict_labels_file is not None:
                        predict_labels_file = FLAGS.predict_labels_file + str(
                            port) + '.npy'
                        predict_labels = np.load(predict_labels_file)
                        check_rstar_file_stage1(
                            rstar_file=out_server_file,
                            r_rstar=r_rstars[port],
                            labels=predict_labels,
                            port=port,
                        )
                    print(f'client starting 2pc with port: {port}')
                    completed[port] = True
                    process = subprocess.Popen(
                        ['./gc-emp-test/bin/argmax_1', '2', '12345',
                         f'{out_client_name}{port}privacy.txt'])
                    process.wait()
                    processes.append(process)
                else:
                    print(
                        f'Expected output file {out_server_file} from the party {port} does not exist yet!')
    max_t = time.time() + 10000
    while any([p.poll() for p in
               processes]) is None:  # wait on all argmaxs to finish first
        time.sleep(1)
        if time.time() > max_t:
            raise ValueError(
                f'something broke while waiting on processes for 2pc with servers: {[p.poll() for p in processes]}')
    print("Prepping for 2pc with CSP")
    if not sum(completed.values()) == n_parties:
        raise ValueError('a 2pc with a server failed')

    r_rstars = []
    for port in FLAGS.ports:
        with open(f'output{port}privacy.txt', 'r') as infile:
            r_rstar = []
            for line in infile:
                r_rstar.append(int(line))
            r_rstars.append(r_rstar)
    r_rstars = np.array(r_rstars, np.int64)
    print(r_rstars)
    print('done')

    if FLAGS.final_call:
        fs = [f"output{port}privacy.txt" for port in FLAGS.ports]
        array_sum = csp.sum_files(fs)
        print(array_sum)
        with open("output.txt", 'w') as outfile:
            for v in array_sum.flatten():
                outfile.write(f'{v}\n')
        csp_filenames = [f'noise{port}privacy.txt' for port in FLAGS.ports]
        label = csp.get_histogram(
            client_filename='output.txt',
            csp_filenames=csp_filenames,
            csp_sum_filename='final.txt')
        print(label)
Exemplo n.º 6
0
def run_server(FLAGS):
    (x_train, y_train, x_test,
     y_test) = load_mnist_data(FLAGS.start_batch, FLAGS.batch_size)

    # Load saved model
    tf.import_graph_def(load_pb_file(FLAGS.model_file))

    print("loaded model")
    print_nodes()

    # Get input / output tensors
    x_input = tf.compat.v1.get_default_graph().get_tensor_by_name(
        FLAGS.input_node)
    y_output = tf.compat.v1.get_default_graph().get_tensor_by_name(
        FLAGS.output_node)
    # y_max = tf.nn.softmax(y_output)
    # y_max = y_output
    # y_max = approximate_softmax(x=y_output, nr_terms=2)
    # y_max = max_pool(y_output=y_output, FLAGS=FLAGS)
    print('r_star: ', FLAGS.rstar)
    print('is r_star [-1.0]', FLAGS.rstar == [-1.0])
    r_star = None
    if FLAGS.rstar is None:
        # for debugging: we don't want any r*
        y_max = y_output
    else:
        if FLAGS.rstar == [-1.0]:
            # Generate random r_star
            if y_test is not None:
                r_shape = y_test.shape
                batch_size = r_shape[0]
                num_classes = r_shape[1]
            else:
                batch_size, num_classes = FLAGS.batch_size, FLAGS.num_classes
            r_star = get_rstar_server(
                max_logit=FLAGS.max_logit,
                batch_size=batch_size,
                num_classes=num_classes,
                exp=FLAGS.rstar_exp,
            )
        else:
            r_star = np.array(FLAGS.rstar)
        # r - r* (subtract the random vector r* from logits)
        y_max = tf.subtract(y_output,
                            tf.convert_to_tensor(r_star, dtype=tf.float32))

        if FLAGS.debug is True:
            print('y_max shape: ', y_max.shape)
            print('r_star shape: ', r_star.shape)
            y_max = tf.concat([y_max, r_star], axis=0)

    # Create configuration to encrypt input
    config = server_config_from_flags(FLAGS, x_input.name)
    with tf.compat.v1.Session(config=config) as sess:
        sess.run(tf.compat.v1.global_variables_initializer())
        start_time = time.time()
        y_hat = sess.run(y_max, feed_dict={x_input: x_test})
        # y_hat = y_max.eval(feed_dict={x_input: x_test})
        print('y_hat: ', y_hat)
        if FLAGS.debug is True:
            r_star = y_hat[FLAGS.batch_size:]
            y_hat = y_hat[:FLAGS.batch_size]
        print("logits (y_hat): ", array_str(y_hat))
        print("logits (y_hat) type: ", type(y_hat))
        print("logits (y_hat) shape: ", y_hat.shape)
        # print("change y_hat to one_hot encoding")
        y_pred = y_hat.argmax(axis=1)
        print("y_pred: ", y_pred)

        elasped_time = time.time() - start_time
        print("total time(s)", np.round(elasped_time, 3))
        party_id = int(FLAGS.port)

        if r_star is not None:
            print("doing 2pc")
            print('r_star (r*): ', array_str(r_star))
            if FLAGS.round_exp is not None:
                # r_star = (r_star * 2 ** FLAGS.round_exp).astype(np.int64)
                r_star = round_array(x=r_star, exp=FLAGS.round_exp)
                print('rounded r_star (r*): ', array_str(r_star))
            with open(f'{out_server_name}{party_id}.txt',
                      'w') as outfile:  # party id
                # assume batch size of 1 for now TODO: make this work for > 1 batch size
                for val in r_star.flatten():
                    outfile.write(f"{int(val)}" + '\n')
            time.sleep(1)
            process = subprocess.Popen([
                './gc-emp-test/bin/argmax_1', '1', '12345',
                f'{out_server_name}{party_id}.txt',
                f'{out_final_name}{party_id}.txt'
            ])
            # time.sleep(15)
            process.wait()
        else:
            print('r_star is None in he_server.py!')

    if not FLAGS.enable_client:
        y_test_label = np.argmax(y_test, 1)

        if FLAGS.batch_size < 60:
            print("y_hat", np.round(y_hat, 2))

        y_pred = np.argmax(y_hat, 1)
        correct_prediction = np.equal(y_pred, y_test_label)
        error_count = np.size(correct_prediction) - np.sum(correct_prediction)
        test_accuracy = np.mean(correct_prediction)

        print("Error count", error_count, "of", FLAGS.batch_size, "elements.")
        print("Accuracy: %g " % test_accuracy)
Exemplo n.º 7
0
 def test_round_array2(self):
     a = np.array([[0.38, 0.42], [0.28, 0.32]])
     a = round_array(x=a, exp=4)
     print('rounded a: ', array_str(a))
     b = [[6, 6], [4, 5]]
     np.testing.assert_array_equal(x=a, y=b)
Exemplo n.º 8
0
 def test_round_array(self):
     a = np.array([0.28, 0.32])
     a = round_array(x=a, exp=4)
     print('rounded a: ', array_str(a))
     b = [4, 5]
     np.testing.assert_array_equal(x=a, y=b)