import collections
import numpy as np
import tensorflow as tf
import tensorflow_federated as tff
# 测试tff是否安装成功
# print(tff.federated_computation(lambda: 'Hello World')())
# 加载数据集
emnist_train, emnist_test = tff.simulation.datasets.emnist.load_data(
cache_dir='/home/cqx/PycharmProjects/cache/fed_emnist_digitsonly')
# 查看数据集长度和结构
print(len(emnist_train.client_ids))
print(emnist_train.element_type_structure)
# 给指定客户端创造数据集 返回值tf.data.Dataset` object.
example_dataset = emnist_train.create_tf_dataset_for_client(
emnist_train.client_ids[0])
# iter迭代,Iterator对象可以被next()函数调用并不断返回下一个数据,直到没有数据时抛出StopIteration错误。
example_element = next(iter(example_dataset))
print(example_element['label'].numpy())
from matplotlib import pyplot as plt
# 获取一个客户端数据的样本,以了解一个模拟设备上的示例
plt.imshow(example_element['pixels'].numpy(), cmap='gray', aspect='equal')
plt.grid(False)
_ = plt.show()
# 取40个样本并展示
figure = plt.figure(figsize=(20, 4))
j = 0
for example in example_dataset.take(40):
plt.subplot(4, 10, j + 1)
plt.imshow(example['pixels'].numpy(), cmap='gray', aspect='equal')
plt.axis('off')
j += 1
plt.show()
# 可视化每个客户端上每个 MNIST 数字标签的示例数。
f = plt.figure(figsize=(12, 7))
f.suptitle('Label Counts for a Sample of Clients')
for i in range(6):
client_dataset = emnist_train.create_tf_dataset_for_client(
emnist_train.client_ids[i])
plot_data = collections.defaultdict(list)
for example in client_dataset:
# Append counts individually per label to make plots
# more colorful instead of one color per plot.
label = example['label'].numpy()
plot_data[label].append(label)
plt.subplot(2, 3, i + 1)
plt.title('Client {}'.format(i))
# 直方图 plt.hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, *, data=None, **kwargs)
# x一维数组,bin组数,density=False表示频数,true表示频率
for j in range(10):
plt.hist(
plot_data[j],
density=False,
bins=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
plt.show()
# 查看每一个客户端的数据集每个标签的平均图像。
for i in range(5):
client_dataset = emnist_train.create_tf_dataset_for_client(
emnist_train.client_ids[i])
plot_data = collections.defaultdict(list)
for example in client_dataset:
plot_data[example['label'].numpy()].append(example['pixels'].numpy())
f = plt.figure(i, figsize=(12, 5))
f.suptitle("Client #{}'s Mean Image Per Label".format(i))
for j in range(10):
mean_img = np.mean(plot_data[j], 0)
plt.subplot(2, 5, j + 1)
plt.imshow(mean_img.reshape((28, 28)))
plt.axis('off')
plt.show()
实验结果
3383
OrderedDict([('label', TensorSpec(shape=(), dtype=tf.int32, name=None)), ('pixels', TensorSpec(shape=(28, 28), dtype=tf.float32, name=None))])
1
图像:
![]() | ![]() |
![]() | ![]() |
![]() |