目标
使用多GPU,在SSD_Mobilenet_v1模型上训练文1中的猫狗数据集
环境
Ubuntu 18.04.2 下使用docker镜像:
tensorflow/tensorflow 1.15.0-gpu-py3
CUDA Version 10.2
问题
直接使用model_main.py
文件默认使用第一块GPU, 需要将train_and_evaluate
方法中的的estimator
设置为支持多GPU,这里我们使用最简单的单机双卡,配置如下
python
1 | mirrored_strategy = tf.contrib.distribute.MirroredStrategy(num_gpus=2, cross_device_ops=tf.distribute.HierarchicalCopyAllReduce(num_packs=1)) |
直接运行会有各种各样的报错,报错与tf版本相关
解决
通过以下参考 issue#5421,进行调整
关于版本
tf 1.13.1 版本以前的,无法进行多GPU训练
报错处理
问题1
shell
1 | ValueError: Variable FeatureExtractor/MobilenetV2/Conv/weights/replica_1/ExponentialMovingAverage/ does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=tf.AUTO_REUSE in VarScope? |
具体内容可以参考 issue#27392
可以通过在训练中关闭use_moving_average
来暂时解决
shell
1 | train_config: { |
问题2
shell
1 | ... |
可以通过注释掉model_lib.py
里面的model_fn
方法下面scaffold相关代码
python
1 | # EVAL executes on CPU, so use regular non-TPU EstimatorSpec. |
测试
修改后的代码正常运行,使用2块1070ti显卡,10w步耗时8小时,相较文1中12+小时有一定提升。