Exemplo n.º 1
0
def plot_figures_from_dict(result_csv_file: str, result_dir: str):
    """Plot figures with dictionary of results."""
    result_dict = read_csv_to_dict(result_csv_file)

    plot_pairs = Config().results.plot
    plot_pairs = [x.strip() for x in plot_pairs.split(',')]

    for pairs in plot_pairs:
        figure_file_name = result_dir + pairs + '.pdf'
        pair = [x.strip() for x in pairs.split('-')]
        x_y_labels: List = []
        x_y_values: Dict[str, List] = {}
        for item in pair:
            label = {
                'round': 'Round',
                'accuracy': 'Accuracy (%)',
                'elapsed_time': 'Wall clock time elapsed (s)',
                'round_time': 'Training time in each round (s)',
                'global_round': 'Global training round',
                'local_epoch_num': 'Local epochs',
                'edge_agg_num': 'Aggregation rounds on edge servers'
            }[item]
            x_y_labels.append(label)
            x_y_values[label] = result_dict[item]

        x_label = x_y_labels[0]
        y_label = x_y_labels[1]
        x_value = x_y_values[x_label]
        y_value = x_y_values[y_label]
        plot(x_label, x_value, y_label, y_value, figure_file_name)
Exemplo n.º 2
0
def read_csv_to_dict(result_csv_file: str) -> Dict[str, List]:
    """Read a CSV file and write the values that need to be plotted
    into a dictionary."""
    result_dict: Dict[str, List] = {}

    plot_pairs = Config().results.plot
    plot_pairs = [x.strip() for x in plot_pairs.split(',')]

    for pairs in plot_pairs:
        pair = [x.strip() for x in pairs.split('-')]
        for item in pair:
            if item not in result_dict:
                result_dict[item] = []

    with open(result_csv_file, 'r') as f:
        reader = csv.DictReader(f)
        for row in reader:
            for item in result_dict:
                if item in (
                        'round',
                        'global_round',
                        'local_epochs',
                ):
                    result_dict[item].append(int(row[item]))
                else:
                    result_dict[item].append(float(row[item]))

    return result_dict
Exemplo n.º 3
0
    def __init__(self, datasource, client_id, testing):
        super().__init__(datasource, client_id, testing)

        assert hasattr(Config().data, 'non_iid_clients')
        non_iid_clients = Config().data.non_iid_clients

        if isinstance(non_iid_clients, int):
            # If only one client's dataset is non-iid
            self.non_iid_clients_list = [int(non_iid_clients)]
        else:
            self.non_iid_clients_list = [
                int(x.strip()) for x in non_iid_clients.split(',')
            ]

        if int(client_id) not in self.non_iid_clients_list:
            if testing:
                target_list = datasource.get_test_set().targets
            else:
                target_list = datasource.targets()
            class_list = datasource.classes()
            self.sample_weights = np.array([
                1 / len(class_list) for _ in range(len(class_list))
            ])[target_list]

            # Different iid clients should have a different random seed for Generator
            self.random_seed = self.random_seed * int(client_id)
Exemplo n.º 4
0
def get():
    """Get the model with the provided name."""
    model_name = Config().trainer.model_name
    model_type = model_name.split('_')[0]
    model = None

    if model_name == 'yolov5':
        from plato.models import yolo
        return yolo.Model.get_model()

    if model_name == 'HuggingFace_CausalLM':
        from transformers import AutoModelForCausalLM, AutoConfig

        model_checkpoint = Config().trainer.model_checkpoint
        config_kwargs = {
            "cache_dir": None,
            "revision": 'main',
            "use_auth_token": None,
        }
        config = AutoConfig.from_pretrained(model_checkpoint, **config_kwargs)
        return AutoModelForCausalLM.from_pretrained(
            model_checkpoint, config=config, cache_dir='./models/huggingface')

    else:
        for name, registered_model in registered_models.items():
            if name.startswith(model_type):
                model = registered_model.get_model(model_name)

    if model is None:
        raise ValueError('No such model: {}'.format(model_name))

    return model
Exemplo n.º 5
0
    def __init__(self, model=None, algorithm=None, trainer=None):
        super().__init__()

        if hasattr(Config().trainer, 'use_wandb'):
            import wandb

            wandb.init(project="plato", reinit=True)

        self.model = model
        self.algorithm = algorithm
        self.trainer = trainer

        self.testset = None
        self.total_samples = 0

        self.total_clients = Config().clients.total_clients
        self.clients_per_round = Config().clients.per_round

        logging.info(
            "[Server #%d] Started training on %s clients with %s per round.",
            os.getpid(), self.total_clients, self.clients_per_round)

        # starting time of a global training round
        self.round_start_time = 0

        if hasattr(Config(), 'results'):
            recorded_items = Config().results.types
            self.recorded_items = ['round'] + [
                x.strip() for x in recorded_items.split(',')
            ]

        random.seed()
Exemplo n.º 6
0
    def __init__(self, datasource, client_id, testing):
        super().__init__()

        # Different clients should have a different bias across the labels
        np.random.seed(self.random_seed * int(client_id))

        self.partition_size = Config().data.partition_size

        if testing:
            target_list = datasource.get_test_set().targets
        else:
            # The list of labels (targets) for all the examples
            target_list = datasource.targets()
        class_list = datasource.classes()

        if hasattr(Config().clients,
                   'simulation') and Config().clients.simulation:
            max_client_id = int(Config().clients.per_round)
        else:
            max_client_id = int(Config().clients.total_clients)

        if client_id > max_client_id:
            # This client is an edge server
            institution_id = client_id - 1 - max_client_id
        else:
            institution_id = (client_id - 1) % int(
                Config().algorithm.total_silos)

        if hasattr(Config().data, 'institution_class_ids'):
            institution_class_ids = Config().data.institution_class_ids
            class_ids = [x.strip() for x in institution_class_ids.split(';')
                         ][institution_id]
            class_id_list = [int(x.strip()) for x in class_ids.split(',')]
        else:
            class_ids = np.array_split(
                [i for i in range(len(class_list))],
                Config().algorithm.total_silos)[institution_id]
            class_id_list = class_ids.tolist()

        if hasattr(Config().data, 'label_distribution') and Config(
        ).data.label_distribution == 'noniid':
            # Concentration parameter to be used in the Dirichlet distribution
            concentration = Config().data.concentration if hasattr(
                Config().data, 'concentration') else 1.0

            class_proportions = np.random.dirichlet(
                np.repeat(concentration, len(class_id_list)))

        else:
            class_proportions = [
                1.0 / len(class_id_list) for i in range(len(class_id_list))
            ]

        target_proportions = [0 for i in range(len(class_list))]
        for index, class_id in enumerate(class_id_list):
            target_proportions[class_id] = class_proportions[index]
        target_proportions = np.asarray(target_proportions)

        self.sample_weights = target_proportions[target_list]
Exemplo n.º 7
0
def get():
    """Get the model with the provided name."""
    model_name = Config().trainer.model_name
    model_type = model_name.split('_')[0]
    model = None

    if model_name == 'yolov5':
        from plato.models import yolo
        return yolo.Model.get_model()

    if model_name == 'HuggingFace_CausalLM':
        from transformers import AutoModelForCausalLM
        model_checkpoint = Config.trainer.model_checkpoint
        return AutoModelForCausalLM.from_pretrained(model_checkpoint)

    else:
        for name, registered_model in registered_models.items():
            if name.startswith(model_type):
                model = registered_model.get_model(model_name)

    if model is None:
        raise ValueError('No such model: {}'.format(model_name))

    return model