File size: 14,458 Bytes
3a3be3b c18c83f 3a3be3b 6c6d5b4 3a3be3b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
import gradio as gr
from flwr_datasets import FederatedDataset
from flwr_datasets.partitioner import (
DirichletPartitioner,
IidPartitioner,
PathologicalPartitioner,
ShardPartitioner,
LinearPartitioner,
SquarePartitioner,
ExponentialPartitioner,
NaturalIdPartitioner
)
from flwr_datasets.visualization import plot_label_distributions
import matplotlib.pyplot as plt
partitioner_types = {
"DirichletPartitioner": DirichletPartitioner,
"IidPartitioner": IidPartitioner,
"PathologicalPartitioner": PathologicalPartitioner,
"ShardPartitioner": ShardPartitioner,
"LinearPartitioner": LinearPartitioner,
"SquarePartitioner": SquarePartitioner,
"ExponentialPartitioner": ExponentialPartitioner,
"NaturalIdPartitioner": NaturalIdPartitioner,
}
partitioner_parameters = {
"DirichletPartitioner": ["num_partitions", "alpha", "partition_by", "min_partition_size", "self_balancing"],
"IidPartitioner": ["num_partitions"],
"PathologicalPartitioner": ["num_partitions", "partition_by", "num_classes_per_partition", "class_assignment_mode"],
"ShardPartitioner": ["num_partitions", "partition_by", "num_shards_per_partition", "shard_size", "keep_incomplete_shard"],
"NaturalIdPartitioner": ["partition_by"],
"LinearPartitioner": ["num_partitions"],
"SquarePartitioner": ["num_partitions"],
"ExponentialPartitioner": ["num_partitions"],
}
def update_parameter_visibility(partitioner_type):
required_params = partitioner_parameters.get(partitioner_type, [])
updates = []
# For num_partitions_input
if "num_partitions" in required_params:
updates.append(gr.update(visible=True))
else:
updates.append(gr.update(visible=False))
# For alpha_input
if "alpha" in required_params:
updates.append(gr.update(visible=True))
else:
updates.append(gr.update(visible=False))
# For partition_by_input
if "partition_by" in required_params:
updates.append(gr.update(visible=True))
else:
updates.append(gr.update(visible=False))
# For min_partition_size_input
if "min_partition_size" in required_params:
updates.append(gr.update(visible=True))
else:
updates.append(gr.update(visible=False))
# For self_balancing_input
if "self_balancing" in required_params:
updates.append(gr.update(visible=True))
else:
updates.append(gr.update(visible=False))
# For num_classes_per_partition_input
if "num_classes_per_partition" in required_params:
updates.append(gr.update(visible=True))
else:
updates.append(gr.update(visible=False))
# For class_assignment_mode_input
if "class_assignment_mode" in required_params:
updates.append(gr.update(visible=True))
else:
updates.append(gr.update(visible=False))
# For num_shards_per_partition_input
if "num_shards_per_partition" in required_params:
updates.append(gr.update(visible=True))
else:
updates.append(gr.update(visible=False))
# For shard_size_input
if "shard_size" in required_params:
updates.append(gr.update(visible=True))
else:
updates.append(gr.update(visible=False))
# For keep_incomplete_shard_input
if "keep_incomplete_shard" in required_params:
updates.append(gr.update(visible=True))
else:
updates.append(gr.update(visible=False))
return updates
def partition_and_plot(
dataset,
partitioner_type,
num_partitions,
alpha,
partition_by,
min_partition_size,
self_balancing,
num_classes_per_partition,
class_assignment_mode,
num_shards_per_partition,
shard_size,
keep_incomplete_shard,
label_name,
title,
legend,
verbose_labels,
size_unit,
partition_id_axis,
):
partitioner_params = {}
try:
if partitioner_type == "DirichletPartitioner":
partitioner_params = {
"num_partitions": int(num_partitions),
"partition_by": partition_by,
"alpha": float(alpha),
"min_partition_size": int(min_partition_size),
"self_balancing": self_balancing,
}
elif partitioner_type == "IidPartitioner":
partitioner_params = {
"num_partitions": int(num_partitions),
}
elif partitioner_type == "PathologicalPartitioner":
partitioner_params = {
"num_partitions": int(num_partitions),
"partition_by": partition_by,
"num_classes_per_partition": int(num_classes_per_partition),
"class_assignment_mode": class_assignment_mode,
}
elif partitioner_type == "ShardPartitioner":
partitioner_params = {
"num_partitions": int(num_partitions),
"partition_by": partition_by,
"num_shards_per_partition": int(num_shards_per_partition),
"shard_size": int(shard_size),
"keep_incomplete_shard": keep_incomplete_shard == "True",
}
elif partitioner_type == "NaturalIdPartitioner":
partitioner_params = {
"partition_by": partition_by,
}
elif partitioner_type in ["LinearPartitioner", "SquarePartitioner", "ExponentialPartitioner"]:
partitioner_params = {
"num_partitions": int(num_partitions),
}
partitioner_class = partitioner_types[partitioner_type]
partitioner = partitioner_class(**partitioner_params)
fds = FederatedDataset(
dataset=dataset,
partitioners={
"train": partitioner,
},
trust_remote_code=True,
)
partitioner = fds.partitioners["train"]
figure, axis, dataframe = plot_label_distributions(
partitioner=partitioner,
label_name=label_name,
title=title,
legend=legend,
verbose_labels=verbose_labels,
size_unit=size_unit,
partition_id_axis=partition_id_axis,
)
# Save plot to a file
plot_filename = "label_distribution.png"
figure.savefig(plot_filename, bbox_inches='tight')
# Generate the code
partitioner_params_str = "\n"
n_params = len(partitioner_params)
i = 0
for k, v in partitioner_params.items():
if isinstance(v, str):
v = f'"{v}"'
if i != (n_params - 1):
partitioner_params_str = partitioner_params_str + f"\t{k} = {v},\n"
else:
partitioner_params_str = partitioner_params_str + f"\t{k} = {v}\n"
i +=1
code = f"""
from flwr_datasets import FederatedDataset
from flwr_datasets.partitioner import {partitioner_type}
from flwr_datasets.visualization import plot_label_distributions
partitioner = {partitioner_type}({partitioner_params_str})
fds = FederatedDataset(
dataset="{dataset}",
partitioners={{
"train": partitioner,
}},
trust_remote_code=True,
)
partitioner = fds.partitioners["train"]
figure, axis, dataframe = plot_label_distributions(
partitioner=partitioner,
label_name="label",
title="{title}",
legend={legend},
verbose_labels={verbose_labels},
size_unit="{size_unit}",
partition_id_axis="{partition_id_axis}",
)
"""
return plot_filename, code#, plot_filename # with df: plot_filename, code, dataframe, plot_filename
except Exception as e:
# Return error messages
error_message = str(e)
return None, f"Error: {error_message}", None, None
with gr.Blocks() as demo:
gr.Markdown("# Federated Dataset: Partitioning Visualization")
gr.Markdown("See partitioned datasets for Federated Learning experiments. The partitioning and visualization were created using `flwr-datasets`. To open in a new tab, click the [link](https://huggingface.co/spaces/flwrlabs/federated-learning-datasets-by-flwr-datasets).")
with gr.Row():
with gr.Column(scale=1):
# gr.Markdown("## Federated Dataset Parameters")
with gr.Accordion("Federated Dataset Parameters", open=True):
dataset_input = gr.Textbox(label="Dataset", value="cifar10")
partitioner_type_input = gr.Dropdown(label="Partitioner", choices=list(partitioner_types.keys()), value="DirichletPartitioner")
num_partitions_input = gr.Number(label="num_partitions", value=10, visible=True)
alpha_input = gr.Number(label="alpha", value=0.3, visible=True)
partition_by_input = gr.Textbox(label="partition_by", value="label", visible=True)
min_partition_size_input = gr.Number(label="min_partition_size", value=0, visible=True)
self_balancing_input = gr.Radio(label="self_balancing", choices=[True, False], value=False, visible=True)
num_classes_per_partition_input = gr.Number(label="num_classes_per_partition", value=2, visible=False)
class_assignment_mode_input = gr.Dropdown(label="class_assignment_mode", choices=["random", "first-deterministic", "deterministic"], value="first-deterministic", visible=False)
num_shards_per_partition_input = gr.Number(label="num_shards_per_partition", value=2, visible=False)
shard_size_input = gr.Number(label="shard_size", value=0, visible=False)
keep_incomplete_shard_input = gr.Radio(label="keep_incomplete_shard", choices=["True", "False"], value="True", visible=False)
with gr.Accordion("Plot Parameters", open=False):
label_name = gr.Textbox(label="label_name", value="label")
title = gr.Textbox(label="title", value="Per Partition Label Distribution")
# legend_title = gr.Textbox(label="legend_title", value=None)
legend = gr.Radio(label="legend", choices=[True, False], value=True)
verbose_labels = gr.Radio(label="verbose_labels", choices=[True, False], value=True)
size_unit = gr.Radio(label="size_unit", choices=["absolute", "percent"], value="absolute")
partition_id_axis = gr.Radio(label="partition_id_axis", choices=["x", "y"], value="x")
# Update parameter visibility when partitioner_type_input changes
partitioner_type_input.change(
fn=update_parameter_visibility,
inputs=[partitioner_type_input],
outputs=[
num_partitions_input,
alpha_input,
partition_by_input,
min_partition_size_input,
self_balancing_input,
num_classes_per_partition_input,
class_assignment_mode_input,
num_shards_per_partition_input,
shard_size_input,
keep_incomplete_shard_input
]
)
with gr.Column(scale=3, min_width=480):
gr.Markdown("## Label Distribution Plot")
plot_output = gr.Image(label="Label Distribution Plot")
submit_button = gr.Button("Partition and Plot", variant="primary")
# download_button = gr.DownloadButton(label="Download Plot", value="label_distribution.png")
gr.Markdown("## Code")
code_output = gr.Code(label="Code", language="python")
# Uncomment to show dataframe (note that it only works with header that is of type "string")
# gr.Markdown("## Partitioning DataFrame")
# dataframe_output = gr.Dataframe(label="Partitioning DataFrame")
size_skew_examples = gr.Examples(
examples=[
["cifar10", "IidPartitioner", 10],
["cifar10", "LinearPartitioner", 10],
["cifar10", "SquarePartitioner", 10],
["cifar10", "ExponentialPartitioner", 10],
],
inputs=[
dataset_input,
partitioner_type_input,
num_partitions_input,
],
label="Size Skew Examples",
)
dirichlet_examples = gr.Examples(
examples=[
["cifar10", "DirichletPartitioner", 10, 0.1, "label", 0, False, "absolute"],
["cifar10", "DirichletPartitioner", 10, 0.1, "label", 0, False, "percent"],
],
inputs=[
dataset_input,
partitioner_type_input,
num_partitions_input,
alpha_input,
partition_by_input,
min_partition_size_input,
self_balancing_input,
size_unit,
],
label="Dirichlet Examples",
)
pathological_examples = gr.Examples(
examples=[
["cifar10", "PathologicalPartitioner", 10, 2, "first-deterministic", "label"],
["cifar10", "PathologicalPartitioner", 10, 3, "deterministic", "label"],
],
inputs=[
dataset_input,
partitioner_type_input,
num_partitions_input,
num_classes_per_partition_input,
class_assignment_mode_input,
partition_by_input,
],
label="Pathological Examples",
)
markdown = gr.Markdown("See more tutorial, examples and documentation on [https://flower.ai/docs/datasets/index.html](https://flower.ai/docs/datasets/index.html).")
# Set up the event handler for the submit_button
submit_button.click(
fn=partition_and_plot,
inputs=[
dataset_input,
partitioner_type_input,
num_partitions_input,
alpha_input,
partition_by_input,
min_partition_size_input,
self_balancing_input,
num_classes_per_partition_input,
class_assignment_mode_input,
num_shards_per_partition_input,
shard_size_input,
keep_incomplete_shard_input,
label_name,
title,
legend,
verbose_labels,
size_unit,
partition_id_axis,
],
outputs=[
plot_output,
code_output,
# dataframe_output,
# download_button
]
)
if __name__ == "__main__":
demo.launch() |