def main(number, start_slice, end_slice): mnist_dataset = TrainDataset(transform=transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, ))]), number=number, start_slice=start_slice, end_slice=end_slice) _id = 'h%s' % number ip = '10.0.0.%s' % number hook = syft.TorchHook(torch) server = WebsocketServerWorker(id=_id, host=ip, port=8778, hook=hook, verbose=True) print("Worker:{}, Dataset contains {}".format(_id, str(len( mnist_dataset.data)))) dataset = syft.BaseDataset(data=mnist_dataset.data, targets=mnist_dataset.target, transform=mnist_dataset.transform) key = "targeted" server.add_dataset(dataset, key=key) server.start()
def activate(self): kwargs = { "hook": self.hook, "id": self.name, "host": "0.0.0.0", "port": self.syft_port } self.active = True self.local_worker = WebsocketServerWorker(**kwargs) self.local_worker.start()
def start_websocket_server_worker(id, host, port, hook, verbose, keep_labels=None, training=True): """Helper function for spinning up a websocket server and setting up the local datasets.""" server = WebsocketServerWorker(id=id, host=host, port=port, hook=hook, verbose=verbose) # Setup toy data (mnist example) mnist_dataset = datasets.MNIST( root="./data", train=training, download=True, transform=transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))] ), ) if training: indices = np.isin(mnist_dataset.targets, keep_labels).astype("uint8") logger.info("number of true indices: %s", indices.sum()) selected_data = ( torch.native_masked_select(mnist_dataset.data.transpose(0, 2), torch.tensor(indices)) .view(28, 28, -1) .transpose(2, 0) ) logger.info("after selection: %s", selected_data.shape) selected_targets = torch.native_masked_select(mnist_dataset.targets, torch.tensor(indices)) dataset = sy.BaseDataset( data=selected_data, targets=selected_targets, transform=mnist_dataset.transform ) key = "mnist" else: dataset = sy.BaseDataset( data=mnist_dataset.data, targets=mnist_dataset.targets, transform=mnist_dataset.transform, ) key = "mnist_testing" server.add_dataset(dataset, key=key) logger.info("datasets: %s", server.datasets) if training: logger.info("len(datasets[mnist]): %s", len(server.datasets["mnist"])) server.start() return server
def main(): parser = argparse.ArgumentParser( description="Run websocket server worker.") parser.add_argument( "--port", "-p", type=int, help="port number of the websocket server worker, e.g. --port 8777") parser.add_argument("--host", type=str, default="localhost", help="host for the connection") parser.add_argument( "--id", type=str, help="name (id) of the websocket server worker, e.g. --id alice") parser.add_argument( "--verbose", "-v", action="store_true", help="if set, websocket server worker will be started in verbose mode") args = parser.parse_args() hook = sy.TorchHook(torch) kwargs = { "id": str(args.id), "host": args.host, "port": args.port, "hook": hook, "verbose": args.verbose, } global local_worker local_worker = WebsocketServerWorker(**kwargs) local_worker.start() loop = asyncio.new_event_loop() update_thread = Thread(target=start_background_loop, args=(loop, ), daemon=True) update_thread.start()
def main(**kwargs): # pragma: no cover """Helper function for spinning up a websocket participant.""" # Create websocket worker worker = WebsocketServerWorker(**kwargs) # Setup toy data (xor example) data = th.tensor([[0.0, 1.0], [1.0, 0.0], [1.0, 1.0], [0.0, 0.0]], requires_grad=True) target = th.tensor([[1.0], [1.0], [0.0], [0.0]], requires_grad=False) # Create a dataset using the toy data dataset = sy.BaseDataset(data, target) # Tell the worker about the dataset worker.add_dataset(dataset, key="xor") # Start worker worker.start() return worker
def start_websocket_server_worker(id, host, port, hook, verbose): """Helper function for spinning up a websocket server and setting up the local datasets.""" server = WebsocketServerWorker(id=id, host=host, port=port, hook=hook, verbose=verbose) # # Setup toy data (mnist example) # mnist_dataset = datasets.MNIST( # root="./data", # train=True, # download=True, # transform=transforms.Compose( # [transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))] # ), # ) # # indices = np.isin(mnist_dataset.targets, keep_labels).astype("uint8") # logger.info("number of true indices: %s", indices.sum()) # selected_data = ( # torch.native_masked_select(mnist_dataset.data.transpose(0, 2), torch.tensor(indices)) # .view(28, 28, -1) # .transpose(2, 0) # ) # logger.info("after selection: %s", selected_data.shape) # selected_targets = torch.native_masked_select(mnist_dataset.targets, torch.tensor(indices)) # # dataset = sy.BaseDataset( # data=selected_data, targets=selected_targets, transform=mnist_dataset.transform # ) # key = "mnist" # # # Adding Dataset # server.add_dataset(dataset, key=key) # # logger.info("datasets: %s", server.datasets) server.start() return server
def start_websocket_server_worker(id, host, port, hook, verbose, dataset, training=True): """Helper function for spinning up a websocket server and setting up the local datasets.""" server = WebsocketServerWorker(id=id, host=host, port=port, hook=hook, verbose=verbose) dataset_key = dataset #if we are in the traning loop if training: with open("./data/split/%d" % int(id), "rb") as fp: # Unpickling data = pickle.load(fp) dataset_data, dataset_target = readnpy(data) print(type(dataset_data.long())) logger.info("Number of samples for client %s is %s : ", id, len(dataset_data)) dataset = sy.BaseDataset(data=dataset_data, targets=dataset_target) key = dataset_key nb_labels = len(torch.unique(dataset_target)) server.add_dataset(dataset, key=key) count = [0] * nb_labels logger.info("Dataset(train set) ,available numbers on %s: ", id) for i in range(nb_labels): count[i] = (dataset.targets == i).sum().item() logger.info(" %s: %s", i, count[i]) logger.info("datasets: %s", server.datasets) if training: logger.info("len(datasets): %s", len(server.datasets[key])) server.start() return server
parser.add_argument( "--id", type=str, help="name (id) of the websocket server worker, e.g. --id alice") parser.add_argument( "--verbose", "-v", action="store_true", help="if set, websocket server worker will be started in verbose mode", ) args = parser.parse_args() kwargs = { "id": args.id, "host": args.host, "port": args.port, "verbose": args.verbose, } kwargs = {"id": "worker", "host": "localhost", "port": "8777", "hook": hook} if os.name != "nt": print("not nt") server = start_proc(WebsocketServerWorker, kwargs) else: print("nt") server = WebsocketServerWorker(**kwargs) server.start()
def make_socket_server(host, hook, id, port, log_msgs=True, verbose=True): from syft.workers.websocket_server import WebsocketServerWorker server = WebsocketServerWorker(host=host, hook=hook, id=id, port=port, log_msgs=log_msgs, verbose=verbose) return server
"""if set, websocket server worker will be started in verbose mode""", ) parser.add_argument( "--notebook", type=str, default="normal", help= """can run websocket server for websockets examples of mnist/mnist-parallel or pen_testing/steal_data_over_sockets. Type 'mnist' for starting server for websockets-example-MNIST, `mnist-parallel` for websockets-example-MNIST-parallel and 'steal_data' for pen_tesing stealing data over sockets""", ) args = parser.parse_args() # Hook and start server hook = sy.TorchHook(torch) server = WebsocketServerWorker(id=args.id, host=args.host, port=args.port, hook=hook, verbose=args.verbose) server.start() # server = start_websocket_server_worker( # id=args.id, # host=args.host, # port=args.port, # hook=hook, # verbose=args.verbose # )
def start_websocket_server_worker(id, host, port, hook, verbose, keep_labels=None, training=True): # pragma: no cover """Helper function for spinning up a websocket server and setting up the local datasets.""" server = WebsocketServerWorker(id=id, host=host, port=port, hook=hook, verbose=verbose) # Setup toy data (mnist example) mnist_dataset = datasets.MNIST( root="./data", train=training, download=True, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ]), ) if training: indices = np.isin(mnist_dataset.targets, keep_labels).astype("uint8") logger.info("number of true indices: %s", indices.sum()) selected_data = (torch.native_masked_select( mnist_dataset.data.transpose(0, 2), torch.tensor(indices)).view(28, 28, -1).transpose(2, 0)) logger.info("after selection: %s", selected_data.shape) selected_targets = torch.native_masked_select(mnist_dataset.targets, torch.tensor(indices)) dataset = sy.BaseDataset(data=selected_data, targets=selected_targets, transform=mnist_dataset.transform) key = "mnist" else: dataset = sy.BaseDataset( data=mnist_dataset.data, targets=mnist_dataset.targets, transform=mnist_dataset.transform, ) key = "mnist_testing" server.add_dataset(dataset, key=key) # Setup toy data (vectors example) data_vectors = torch.tensor([[-1, 2.0], [0, 1.1], [-1, 2.1], [0, 1.2]], requires_grad=True) target_vectors = torch.tensor([[1], [0], [1], [0]]) server.add_dataset(sy.BaseDataset(data_vectors, target_vectors), key="vectors") # Setup toy data (xor example) data_xor = torch.tensor([[0.0, 1.0], [1.0, 0.0], [1.0, 1.0], [0.0, 0.0]], requires_grad=True) target_xor = torch.tensor([1.0, 1.0, 0.0, 0.0], requires_grad=False) server.add_dataset(sy.BaseDataset(data_xor, target_xor), key="xor") # Setup gaussian mixture dataset data, target = utils.create_gaussian_mixture_toy_data(nr_samples=100) server.add_dataset(sy.BaseDataset(data, target), key="gaussian_mixture") # Setup partial iris dataset data, target = utils.iris_data_partial() dataset = sy.BaseDataset(data, target) dataset_key = "iris" server.add_dataset(dataset, key=dataset_key) logger.info("datasets: %s", server.datasets) if training: logger.info("len(datasets[mnist]): %s", len(server.datasets["mnist"])) server.start() return server
import syft as sy from syft.workers import websocket_client as wsc from syft.workers.websocket_server import WebsocketServerWorker hook = sy.TorchHook(torch) server1_kwargs = { "id": 'mbp1', "host": 'localhost', "port": '8001', "hook": hook, "verbose": True, } server1 = WebsocketServerWorker(**server1_kwargs) server2_kwargs = { "id": 'mbp2', "host": 'localhost', "port": '8002', "hook": hook, "verbose": True, } server2 = WebsocketServerWorker(**server2_kwargs) server3_kwargs = { "id": 'mbp3', "host": 'localhost',
import torch import syft from syft.workers.websocket_server import WebsocketServerWorker # Hook and start server hook = syft.TorchHook(torch) server_worker = WebsocketServerWorker(id="bad", host="localhost", port=8777, hook=hook) test_data = torch.tensor([1, 2, 3]).tag("test") server_worker.set_obj(test_data) print("Bad server started.") server_worker.start()
def start_websocket_server_worker(id, host, port, hook, verbose, keep_labels=None, training=True, pytest_testing=False): """Helper function for spinning up a websocket server and setting up the local datasets.""" server = WebsocketServerWorker(id=id, host=host, port=port, hook=hook, verbose=verbose) X, Y, max_features, max_len = init_data(id, keep_labels) X, x_test, Y, y_test = train_test_split(X, Y, test_size=0.0001, shuffle=True) if not training: selected_data = torch.LongTensor(X) selected_targets = torch.LongTensor(Y).squeeze(1) else: if id == 'alice': selected_data = torch.LongTensor(X) selected_targets = torch.LongTensor(Y).squeeze(1) elif id == 'bob': selected_data = torch.LongTensor(X) selected_targets = torch.LongTensor(Y).squeeze(1) elif id == 'charlie': selected_data = torch.LongTensor(X) selected_targets = torch.LongTensor(Y).squeeze(1) if training: dataset = sy.BaseDataset(data=selected_data, targets=selected_targets) key = "dga" else: dataset = sy.BaseDataset( data=selected_data, targets=selected_targets, ) key = "dga_testing" # Adding Dataset server.add_dataset(dataset, key=key) count = [0] * 2 for i in range(2): count[i] = (dataset.targets == i).sum().item() logger.info(" %s: %s", i, count[i]) logger.info("datasets: %s", server.datasets) if training: logger.info("Examples in local dataset: %s", len(server.datasets[key])) server.start() return server
try: host = sys.argv[1] id = sys.argv[2] port = sys.argv[3] print(host, id, port) except Exception as e: host, id, port = None, None, None print(str(e)) print('run the server by: "python server.py host id port"') print('for example: "python server.py localhost server1 8182"') exit(-1) hook = sy.TorchHook(torch) server_worker = WebsocketServerWorker( host=host, # host="192.168.2.101", # the host of server machine hook=hook, id=id, port=port) # hook = sy.TorchHook(torch, local_worker=server_worker) # data in server x = torch.tensor([[0, 0], [0, 1], [1, 0], [1, 1.]], requires_grad=True).tag("toy", "data") y = torch.tensor([[0], [0], [1], [1.]], requires_grad=True).tag("toy", "target") # x.private, x.private = True, True x_ptr = x.send(server_worker) y_ptr = y.send(server_worker) print(x_ptr, y_ptr)