如何利用我们拥有的共享资源来高效快速地训练深度学习模型

与大多数 ai 研究部门一样,zalando research 也意识到了对创意进行尝试和快速原型设计的重要性。随着数据集变得越来越庞大,了解如何利用我们拥有的共享资源来高效快速地训练深度学习模型变得大有用处。
tensorflow 的估算器api 对于在分布式环境中使用多个 gpu 来训练模型非常有用。本文将主要介绍这一工作流程。我们先使用 fashion-mnist 小数据集训练一个用tf.keras编写的自定义估算器,然后在文末介绍一个较实际的用例。
请注意:tensorflow 团队一直在开发另一项很酷的新功能(在我写这篇文章时,该功能仍处于 master 阶段),使用这项新功能,您只需多输入几行代码即可训练tf.keras模型,而无需先将该模型转化为估算器!其工作流程也很赞。下面我着重讲讲估算器 api。选择哪一个由您自己决定!
注:功能链接
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/distribute/python/examples/keras_mnist.py#l106-l114
tl; dr:基本上,我们需要记住,对于 tf.keras. 模型,我们只要通过 tf.keras.estimator.model_to_estimator 方法将其转化为 tf.estimator.estimator 对象,即可使用 tf.estimator api 来进行训练。转化完成后,我们可以使用估算器提供的机制用不同的硬件配置训练模型。
您可以从此笔记本下载本文中的代码并亲自运行。
注:笔记本链接
https://github.com/kashif/tf-keras-tutorial/blob/master/7-estimators-multi-gpus.ipynb
import osimport time
#!pip install -q -u tensorflow-gpuimport tensorflow as tf
import numpy as np
导入 fashion-mnist 数据集
我们用fashion-mnist数据集随手替换一下 mnist,这里面包含几千张zalando时尚文章的灰度图像。获取训练和测试数据非常简单,如下所示:
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.fashion_mnist.load_data()
我们想把这些图像的像素值从 0 到 255 之间的一个数字转换为 0 到 1 之间的一个数字,并将该数据集转换为 [b, h, w ,c] 格式,其中 b 代表批处理的图像数,h 和 w 分别是高度和宽度,c 是我们数据集的通道数(灰度为 1):
training_size = len(train_images)test_size = len(test_images)
train_images = np.asarray(train_images, dtype=np.float32) / 255# convert the train images and add channelstrain_images = train_images.reshape((training_size, 28, 28, 1))
test_images = np.asarray(test_images, dtype=np.float32) / 255# convert the test images and add channelstest_images = test_images.reshape((test_size, 28, 28, 1))
接下来,我们想将标签从整数编号(例如,2 或套衫)转换为独热编码(例如,0,0,1,0,0,0,0,0,0,0)。为此,我们要使用 tf.keras.utils.to_categorical函数:
# how many categories we are predicting from (0-9)label_dimensions = 10
train_labels = tf.keras.utils.to_categorical(train_labels, label_dimensions)
test_labels = tf.keras.utils.to_categorical(test_labels, label_dimensions)
# cast the labels to floats, needed latertrain_labels = train_labels.astype(np.float32)test_labels = test_labels.astype(np.float32)
构建 tf.keras 模型
我们会使用keras 功能 api来创建神经网络。keras 是一个高级 api,可用于构建和训练深度学习模型,其采用模块化设计,使用方便,易于扩展。tf.keras 是 tensorflow 对这个 api 的实现,其支持eager execution、tf.data 管道和估算器等。
在架构方面,我们会使用 convnet。一个非常笼统的说法是,convnet 是卷积层 (conv2d) 和池化层 (maxpooling2d) 的堆栈。但最重要的是,convnet 将每个训练示例当作一个 3d 形状张量(高度、宽度、通道),对于灰度图像,张量从通道 = 1 开始,然后返回一个 3d 张量。
因此,在 convnet 部分之后,我们需要将张量平面化,并添加密集层,其中最后一个返回 label_dimensions 大小的向量,并附带 tf.nn.softmax 激活:
inputs = tf.keras.input(shape=(28,28,1)) # returns a placeholder
x = tf.keras.layers.conv2d(filters=32, kernel_size=(3, 3), activation=tf.nn.relu)(inputs)
x = tf.keras.layers.maxpooling2d(pool_size=(2, 2), strides=2)(x)
x = tf.keras.layers.conv2d(filters=64, kernel_size=(3, 3), activation=tf.nn.relu)(x)
x = tf.keras.layers.maxpooling2d(pool_size=(2, 2), strides=2)(x)
x = tf.keras.layers.conv2d(filters=64, kernel_size=(3, 3), activation=tf.nn.relu)(x)
x = tf.keras.layers.flatten()(x)
x = tf.keras.layers.dense(64, activation=tf.nn.relu)(x)predictions = tf.keras.layers.dense(label_dimensions, activation=tf.nn.softmax)(x)
现在,我们可以定义学习模型,请选择优化器(我们从 tensorflow 中选择一个,而不使用来自 tf.keras. optimizers 的优化器)并进行编译:
model = tf.keras.model(inputs=inputs, outputs=predictions)
optimizer = tf.train.adamoptimizer(learning_rate=0.001)
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
创建估算器
使用已编译的 keras 模型创建估算器,也就是我们所说的 model_to_estimator 方法。请注意,keras 模型的初始模型状态保存在创建的估算器中。
那估算器有哪些优点呢?首先要提以下几点:
您可以在本地主机或分布式多 gpu 环境中运行基于估算器的模型,而无需更改您的模型;
估算器能够简化模型开发者之间的共享实现;
估算器能够为您构建图形,所以有点像 eager execution,没有明确的会话。
那么我们要如何训练简单的 tf.keras 模型来使用多 gpu?我们可以使用 tf.contrib.distribute.mirroredstrategy 范式,通过同步训练进行图形内复制。如需了解更多关于此策略的信息,请观看分布式 tensorflow 训练讲座。
注:分布式 tensorflow 链接
https://www.youtube.com/watch?v=brmgopqsn20
基本上,每个工作器 gpu 都有一个网络拷贝,并会获取一个数据子集,据以计算本地梯度,然后等待所有工作器以同步方式结束。然后,工作器通过 ring all-reduce 运算互相传递其本地梯度,这通常要进行优化,以减少网络带宽并增加吞吐量。在所有梯度到达后,每个工作器会计算其平均值并更新参数,然后开始下一步。理想情况下,您在单个节点上有多个高速互联的 gpu。
要使用此策略,我们首先要用已编译的 tf.keras 模型创建一个估算器,然后通过 runconfig config 赋予其 mirroredstrategy 配置。默认情况下,该配置会使用全部 gpu,但您也可以赋予其一个 num_gpus 选项,以使用特定数量的 gpu:
num_gpus = 2
strategy = tf.contrib.distribute.mirroredstrategy(num_gpus=num_gpus)config = tf.estimator.runconfig(train_distribute=strategy)
estimator = tf.keras.estimator.model_to_estimator(model, config=config)
创建估算器输入函数
要通过管道将数据传递到估算器,我们需要定义一个数据导入函数,该函数返回批量数据的 tf.data 数据集(图像、标签)。下面的函数接收 numpy 数组,并通过etl过程返回数据集。
请注意,最后我们还调用了预读取方法,该方法会在训练时将数据缓冲到 gpu,以便下一批数据准备就绪并等待 gpu,而不是在每次迭代时让 gpu 等待数据。gpu 可能仍然没有得到充分利用,要改善这一点,我们可以使用融合版转换运算(如 shuffle_and_repeat),而不是两个单独的运算。不过,我在这里选用的是简单用例。
def input_fn(images, labels, epochs, batch_size):
# convert the inputs to a dataset. (e)ds = tf.data.dataset.from_tensor_slices((images, labels))
# shuffle, repeat, and batch the examples. (t)shuffle_size = 5000ds = ds.shuffle(shuffle_size).repeat(epochs).batch(batch_size)ds = ds.prefetch(2)
# return the dataset. (l)return ds
训练估算器
首先,我们定义一个 sessionrunhook 类,用于记录随机梯度下降法每次迭代的次数:
class timehistory(tf.train.sessionrunhook):def begin(self): self.times = []
def before_run(self, run_context): self.iter_time_start = time.time()
def after_run(self, run_context, run_values): self.times.append(time.time() - self.iter_time_start)
亮点在这里!我们可以对估算器调用 train 函数,并通过 hooks 参数,向其赋予我们定义的 input_fn (包含批次大小和我们希望的训练回合次数)和 timehistory 实例:
time_hist = timehistory()
batch_size = 512epochs = 5
estimator.train(lambda:input_fn(train_images, train_labels, epochs=epochs, batch_size=batch_size), hooks=[time_hist])
性能
现在,我们可以使用时间钩子来计算训练的总时间和平均每秒训练的图像数量(平均吞吐量):
total_time = sum(time_hist.times)print(ftotal time with {num_gpus} gpu(s): {total_time} seconds)
avg_time_per_batch = np.mean(time_hist.times)print(f{batch_size*num_gpus/avg_time_per_batch} images/second with {num_gpus} gpu(s))
使用两块 k80 gpu 进行训练时的 fashion-mnist 训练吞吐量和总时间,采用不同 num_gpus,显示缩放不良
评估估算器
为了检验模型的性能,我们要对估算器调用评估方法:
estimator.evaluate(lambda:input_fn(test_images, test_labels, epochs=1, batch_size=batch_size))
视网膜 oct (光学相干断层成像术)图像示例
为了测试模型在处理较大数据集时的扩展性能,我们使用视网膜 oct 图像数据集,这是kaggle众多大型数据集中的一个。该数据集由活人视网膜的横截面 x 光图像组成,分为四个类别:normal、cnv、dme和drusen:
光学相干断层成像术的代表图像,选自 kermany 等人所著的《通过基于图像的深度学习技术确定医学诊断和可治疗疾病》(identifying medical diagnoses and treatable diseases by image-based deep learning)
该数据集共有 84,495 张 jpeg 格式的 x 光图像,尺寸多为 512x496,可以通过 kagglecli下载:
注:cli 链接
https://github.com/kaggle/kaggle-api
#!pip install kaggle#!kaggle datasets download -d paultimothymooney/kermany2018
下载完成后,训练集和测试集图像类位于各自的文件夹内,因此我们可以将模式定义为:
labels = ['cnv', 'dme', 'drusen', 'normal']
train_folder = os.path.join('oct2017', 'train', '**', '*.jpeg')test_folder = os.path.join('oct2017', 'test', '**', '*.jpeg')
接下来,我们要编写估算器的输入函数,该函数可以提取任何文件模式,并返回已缩放图像和独热编码标签作为 tf.data.dataset。这次,我们遵循输入管道性能指南中的最佳实践。请特别注意,如果 prefetch 的 buffer_size 为 none,则 tensorflow 会自动使用最优的预读取缓冲区大小:
注:输入管道性能指南链接
https://www.tensorflow.org/performance/datasets_performance
1def input_fn(file_pattern, labels,
2image_size=(224,224),
3shuffle=false,
4batch_size=64,
5num_epochs=none,
6buffer_size=4096,
7prefetch_buffer_size=none):
8
9table = tf.contrib.lookup.index_table_from_tensor(mapping=tf.constant(labels))
10num_classes = len(labels)
11
12def _map_func(filename):
13label = tf.string_split([filename], delimiter=os.sep).values[-2]
14image = tf.image.decode_jpeg(tf.read_file(filename), channels=3)
15image = tf.image.convert_image_dtype(image, dtype=tf.float32)
16image = tf.image.resize_images(image, size=image_size)
17return (image, tf.one_hot(table.lookup(label), num_classes))
18
19dataset = tf.data.dataset.list_files(file_pattern, shuffle=shuffle)
20
21if num_epochs is not none and shuffle:
22dataset = dataset.apply(
23tf.contrib.data.shuffle_and_repeat(buffer_size, num_epochs))
24elif shuffle:
25dataset = dataset.shuffle(buffer_size)
26elif num_epochs is not none:
27dataset = dataset.repeat(num_epochs)
28
29dataset = dataset.apply(
30tf.contrib.data.map_and_batch(map_func=_map_func,
31 batch_size=batch_size,
32num_parallel_calls=os.cpu_count()))
33dataset = dataset.prefetch(buffer_size=prefetch_buffer_size)
34
35return dataset
这次训练该模型时,我们将使用一个经过预训练的 vgg16,并且只重新训练其最后 5 层:
keras_vgg16 = tf.keras.applications.vgg16(input_shape=(224,224,3), include_top=false)
output = keras_vgg16.outputoutput = tf.keras.layers.flatten()(output)prediction = tf.keras.layers.dense(len(labels), activation=tf.nn.softmax)(output)
model = tf.keras.model(inputs=keras_vgg16.input, outputs=prediction)
for layer in keras_vgg16.layers[:-4]: layer.trainable = false
现在,我们万事皆备,可以按照上述步骤进行,并使用 num_gpus gpu 在几分钟内训练我们的模型:
model.compile(loss='categorical_crossentropy', optimizer=tf.train.adamoptimizer(), metrics=['accuracy'])
num_gpus = 2
strategy = tf.contrib.distribute.mirroredstrategy(num_gpus=num_gpus)
config = tf.estimator.runconfig(train_distribute=strategy)
estimator = tf.keras.estimator.model_to_estimator(model, config=config)
batch_size = 64
epochs = 1
estimator.train(input_fn=lambda:input_fn(train_folder, labels, shuffle=true, batch_size=batch_size, buffer_size=2048, num_epochs=epochs, prefetch_buffer_size=4), hooks=[time_hist])
训练结束后,我们可以评估测试集的准确度,应该在 95% 左右(对初始基线来说还不错):
estimator.evaluate(input_fn=lambda:input_fn(test_folder,
labels, shuffle=false, batch_size=batch_size, buffer_size=1024, num_epochs=1))
使用两块 k80 gpu 进行训练时的 fashion-mnist 训练吞吐量和总时间,采用不同 num_gpus,显示线性缩放
总结
我们在上文中介绍了如何使用估算器 api 在多个 gpu 上轻松训练 keras 深度学习模型,如何编写符合最佳实践的输入管道,以充分利用我们的资源(线性缩放),以及如何通过钩子为我们的训练吞吐量计时。
请务必注意,最后我们主要关注的是测试集错误。您可能会注意到,测试集的准确度会随着 num_gpus 值的增加而下降。其中一个原因可能是,使用 batch_size*num_gpus 的批量大小时,mirroredstrategy 能够有效地训练模型,而当我们增加 gpu 数量时,可能需要调整 batch_size 或学习率。为便于制图,文中除 num_gpus 之外的所有其他超参数均保持不变,但实际上我们需要调整这些超参数。
数据集和模型的大小也会影响这些方案的缩放效果。在读取或写入小数据时,gpu 的带宽较差,如果是较为老旧的 gpu(如 k80),则情形尤其如此,而且可能会造成上面 fashion-mnist 图中所示情况。

基于VC++的发动机ECU测试系统的研究与设计
PCB企业应如何防范火灾
你最看好哪个深度学习框架呢?
全球主要国家的氢能利用的不同,我国氢燃料电池汽车市场发展如何?
红米4高配版和红米4A评测:低价高配的千元机你还在等什么?
如何利用我们拥有的共享资源来高效快速地训练深度学习模型
采用Easy Drive技术的增量累加模数转换器降低设计复杂度与时间
基于LT3754的16通道LED驱动器电路
如何为应用选择最佳类型的温度传感器
PRU处理器架构介绍 (开发,调试方法)
第四次工业革命速度展开 深入实施工业互联网创新发展战略
聚飞光电和远方信息两大LED照明公司发布业绩预喜公告
四川北川发生4.7级地震,10秒后小米手机发出地震预警
【计量】浅谈交流采样校验装置2大主要功能
采用高速单片机作为引导加载-Using the High-S
星宸科技发布轩辕、越影、降龙三大系列芯片,引领 AI 赋能各行各业
简单介绍BOOST拓补电路PCB布线注意事项
非常实用的LED显示屏小知识
全面支持PCIe 4.0,SD卡再度提速
快仓智能和凯傲集团首批合作AMR下线