Exemplo n.º 1
0
    def connect(self, TIMEOUT):
        if debug_level >= DEBUG_LEVEL.INFO:
            TERM.write_info(
                'Attempting to connect to {} (Timeout: {}s)'.format(
                    self.server, TIMEOUT))

        # Retry to connect to the server
        start = time.time()

        while (time.time() - start) < TIMEOUT:
            self.attempt_to_connect(TIMEOUT)

            if self.connected:
                break

        # Run the client (if connection succesful)
        if not self.connected:
            if debug_level >= DEBUG_LEVEL.INFO:
                TERM.write_failure(
                    'Time limit exceeded: {} not found'.format(SERVER))
        else:
            if debug_level >= DEBUG_LEVEL.INFO:
                TERM.write_success('Successfully connected to {} as {}'.format(
                    SERVER, self.sock.getsockname()))
            self.run()
Exemplo n.º 2
0
    def train(self):
        while len(self.connected_clients_by_addr) > 0:
            # select a subset of the clients and broadcast the model.
            if debug_level >= DEBUG_LEVEL.INFO:
                TERM.write_info("Selecting clients...")

            self.select_clients(self.subset_size)

            if debug_level >= DEBUG_LEVEL.INFO:
                TERM.write_info("Broadcasting model...")

            self.broadcast_model()

            if debug_level >= DEBUG_LEVEL.INFO:
                TERM.write_info("Waiting for updates...(Timeout: " +
                                str(self.TIMEOUT) + ")")

            start = time.time()

            # wait for each client's update and then aggregate.
            while (self.aggregated_update is
                   None) and time.time() - start < self.TIMEOUT:
                self.wait_for_updates()
                self.attempt_to_aggregate_updates()

            # if an aggregated update has been created...
            if self.aggregated_update is not None:
                # Stop communication (temporarily)
                self.stop()

                if debug_level >= DEBUG_LEVEL.INFO:
                    TERM.write_success("All updates received.")
                    TERM.write_info("Aggregating updates...")

                # Update the model using aggregated update.
                self.update_model(self.aggregated_update)
                self.aggregated_update = None

                # reset the selected client address list (to be re-selected)
                self.selected_clients_by_addr = {}
                self.selected_clients_updates = {}

                if debug_level >= DEBUG_LEVEL.INFO:
                    TERM.write_info("Sending aggregated update to clients...")

                # Restart communication.
                self.start()
            else:
                if debug_level >= DEBUG_LEVEL.INFO:
                    TERM.write_warning(
                        "Time-limit exceeded: Some updates were not received.")

        if debug_level >= DEBUG_LEVEL.INFO:
            TERM.write_failure("All clients disconected.")
Exemplo n.º 3
0
    def listen_for_clients(self):
        # TODO: make number bigger/variable
        self.listener_sock.listen(5)

        while self.listening:
            # Accept connection.
            client_sock, client_addr = self.listener_sock.accept()

            if debug_level >= DEBUG_LEVEL.INFO:
                TERM.write_success(
                    'Established connection to {}'.format(client_addr))

            # cache the client socket.
            self.connected_clients_by_addr[client_addr] = client_sock
            self.connected_clients_by_sock[client_sock] = client_addr
Exemplo n.º 4
0
    def run(self):
        while True:
            # Wait for weights from the FLServer
            if debug_level >= DEBUG_LEVEL.INFO:
                TERM.write_info('Waiting for model from server...(Timeout: ' +
                                str(self.TIMEOUT) + ')')

                start_time = time.time()
                weights = None
                while ((weights == None)
                       and (time.time() - start_time < self.TIMEOUT)):
                    # get the weights.
                    weights = Communication_Handler.recv_msg(self.sock)

                if weights == None:
                    if debug_level >= DEBUG_LEVEL.INFO:
                        TERM.write_warning(
                            "Time Limit Exceeded: Weights not received.")
                else:
                    if debug_level >= DEBUG_LEVEL.INFO:
                        TERM.write_success("Weights received.")
                        TERM.write_info("Training local model...")

                    # Load weights
                    self.trainer.load_weights(weights)

                    # Train model
                    self.trainer.train()

                    if debug_level >= DEBUG_LEVEL.INFO:
                        TERM.write_success("Training complete.")
                        TERM.write_info("Sending update to server...")

                    # Compute focused update
                    update = self.trainer.focused_update()

                    # Send update to the server
                    Communication_Handler.send_msg(self.sock, update)

                    if debug_level >= DEBUG_LEVEL.INFO:
                        TERM.write_success("Update sent.")