我正在 使用联邦学习进行手写数学的符号分类。我已经对图像进行了预处理,keras.preprocessing.image.ImageDataGenerator
并获得了每个图像的标签。
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
train_dataset = train_datagen.flow_from_directory(
'train_test_data/train/',
target_size=(45,45),
batch_size=32,
class_mode='categorical')
获取标签:
import os
# make label list '!/exp87530.jpg'
def make_labels(train_dataset):
labels = train_dataset.filenames
label = []
for l in labels:
l = l.split(os.path.sep)[0]
label.append(l)
return label
如何制作需要发送给客户的扁平图像和标签元组?从 tensorflow 教程Building Your Own Federated Learning Algorithm中可以看出
从教程:
import
emnist_train, emnist_test = tff.simulation.datasets.emnist.load_data()
NUM_CLIENTS = 10
BATCH_SIZE = 20
def preprocess(dataset):
def batch_format_fn(element):
"""Flatten a batch of EMNIST data and return a (features, label) tuple."""
return (tf.reshape(element['pixels'], [-1, 784]),
tf.reshape(element['label'], [-1, 1]))
return dataset.batch(BATCH_SIZE).map(batch_format_fn)