【问题标题】:How to force tensorflow to use all available GPUs?如何强制张量流使用所有可用的 GPU?
【发布时间】:2018-04-25 23:30:11
【问题描述】:

我有一个 8 个 GPU 集群,当我运行 piece of Tensorflow code from Kaggle(粘贴在下面)时,它只使用一个 GPU 而不是全部 8 个。我使用 nvidia-smi 确认了这一点。

# Set some parameters
IMG_WIDTH = 256
IMG_HEIGHT = 256
IMG_CHANNELS = 3
TRAIN_IM = './train_im/'
TRAIN_MASK = './train_mask/'
TEST_PATH = './test/'

warnings.filterwarnings('ignore', category=UserWarning, module='skimage')
num_training = len(os.listdir(TRAIN_IM))
num_test = len(os.listdir(TEST_PATH))
# Get and resize train images
X_train = np.zeros((num_training, IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)
Y_train = np.zeros((num_training, IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)
print('Getting and resizing train images and masks ... ')
sys.stdout.flush()

#load training images
for count, filename in tqdm(enumerate(os.listdir(TRAIN_IM)), total=num_training):
    img = imread(os.path.join(TRAIN_IM, filename))[:,:,:IMG_CHANNELS]
    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
    X_train[count] = img
    name, ext = os.path.splitext(filename)
    mask_name = name + '_mask' + ext
    mask = cv2.imread(os.path.join(TRAIN_MASK, mask_name))[:,:,:1]
    mask = resize(mask, (IMG_HEIGHT, IMG_WIDTH))
    Y_train[count] = mask

# Check if training data looks all right
ix = random.randint(0, num_training-1)
print(ix)
imshow(X_train[ix])
plt.show()
imshow(np.squeeze(Y_train[ix]))
plt.show()
# Define IoU metric
def mean_iou(y_true, y_pred):
    prec = []
    for t in np.arange(0.5, 1.0, 0.05):
        y_pred_ = tf.to_int32(y_pred > t)
        score, up_opt = tf.metrics.mean_iou(y_true, y_pred_, 2)
        K.get_session().run(tf.local_variables_initializer())
        with tf.control_dependencies([up_opt]):
            score = tf.identity(score)
        prec.append(score)
    return K.mean(K.stack(prec), axis=0)

# Build U-Net model
inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
s = Lambda(lambda x: x / 255) (inputs)
width = 64
c1 = Conv2D(width, (3, 3), activation='relu', padding='same') (s)
c1 = Conv2D(width, (3, 3), activation='relu', padding='same') (c1)
p1 = MaxPooling2D((2, 2)) (c1)

c2 = Conv2D(width*2, (3, 3), activation='relu', padding='same') (p1)
c2 = Conv2D(width*2, (3, 3), activation='relu', padding='same') (c2)
p2 = MaxPooling2D((2, 2)) (c2)

c3 = Conv2D(width*4, (3, 3), activation='relu', padding='same') (p2)
c3 = Conv2D(width*4, (3, 3), activation='relu', padding='same') (c3)
p3 = MaxPooling2D((2, 2)) (c3)

c4 = Conv2D(width*8, (3, 3), activation='relu', padding='same') (p3)
c4 = Conv2D(width*8, (3, 3), activation='relu', padding='same') (c4)
p4 = MaxPooling2D(pool_size=(2, 2)) (c4)

c5 = Conv2D(width*16, (3, 3), activation='relu', padding='same') (p4)
c5 = Conv2D(width*16, (3, 3), activation='relu', padding='same') (c5)

u6 = Conv2DTranspose(width*8, (2, 2), strides=(2, 2), padding='same') (c5)
u6 = concatenate([u6, c4])
c6 = Conv2D(width*8, (3, 3), activation='relu', padding='same') (u6)
c6 = Conv2D(width*8, (3, 3), activation='relu', padding='same') (c6)

u7 = Conv2DTranspose(width*4, (2, 2), strides=(2, 2), padding='same') (c6)
u7 = concatenate([u7, c3])
c7 = Conv2D(width*4, (3, 3), activation='relu', padding='same') (u7)
c7 = Conv2D(width*4, (3, 3), activation='relu', padding='same') (c7)

u8 = Conv2DTranspose(width*2, (2, 2), strides=(2, 2), padding='same') (c7)
u8 = concatenate([u8, c2])
c8 = Conv2D(width*2, (3, 3), activation='relu', padding='same') (u8)
c8 = Conv2D(width*2, (3, 3), activation='relu', padding='same') (c8)

u9 = Conv2DTranspose(width, (2, 2), strides=(2, 2), padding='same') (c8)
u9 = concatenate([u9, c1], axis=3)
c9 = Conv2D(width, (3, 3), activation='relu', padding='same') (u9)
c9 = Conv2D(width, (3, 3), activation='relu', padding='same') (c9)

outputs = Conv2D(1, (1, 1), activation='sigmoid') (c9)

model = Model(inputs=[inputs], outputs=[outputs])

sgd = optimizers.SGD(lr=0.03, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=[mean_iou])
model.summary()
    
# Fit model
earlystopper = EarlyStopping(patience=20, verbose=1)
checkpointer = ModelCheckpoint('nuclei_only.h5', verbose=1, save_best_only=True)
results = model.fit(X_train, Y_train, validation_split=0.05, batch_size = 32, verbose=1, epochs=100, 
                callbacks=[earlystopper, checkpointer])

我想使用 mxnet 或其他方法在所有可用的 GPU 上运行此代码。但是,我不确定如何执行此操作。所有资源仅显示如何在 mnist 数据集上执行此操作。我有自己的数据集,我正在以不同的方式阅读。因此,不太清楚如何修改代码。

【问题讨论】:

    标签: tensorflow gpu


    【解决方案1】:

    TL;DR:使用tf.distribute.MirroredStrategy() 作为范围,例如

    strategy = tf.distribute.MirroredStrategy()
    with strategy.scope():
        [...create model as you would otherwise...]
    

    如果您不指定任何参数,tf.distribute.MirroredStrategy() 将使用所有可用的 GPU。如果需要,您还可以指定要使用的那些,例如:mirrored_strategy = tf.distribute.MirroredStrategy(devices=["/gpu:0", "/gpu:1"])

    有关实施细节和其他策略,请参阅此Distributed training with TensorFlow 指南。

    较早的答案(现已过时:deprecated, removed as of April 1, 2020。): 使用来自 Keras 的multi_gpu_model()。 ()


    TS;WM

    TensorFlow 2.0 现在具有 tf.distribute 模块,“用于跨多个设备运行计算的库”。它建立在“分销策略”的概念之上。您可以指定分发策略,然后将其用作范围。 TensorFlow 将基本透明地拆分输入、并行计算并加入输出。反向传播也受制于此。由于所有处理现在都在幕后完成,您可能需要熟悉可用的策略及其参数,因为它们可能会极大地影响您的训练速度。例如,您希望变量驻留在 CPU 上吗?然后使用tf.distribute.experimental.CentralStorageStrategy()。有关详细信息,请参阅Distributed training with TensorFlow 指南。

    较早的答案(现已过时,留在这里供参考):

    来自Tensorflow Guide

    如果您的系统中有多个 GPU,则默认选择 ID 最低的 GPU。

    如果您想使用多个 GPU,不幸的是您必须手动指定要在每个 GPU 上放置的张量,例如

    with tf.device('/device:GPU:2'):
    

    Tensorflow Guide Using Multiple GPUs 中的更多信息。

    就如何在多个 GPU 上分布网络而言,主要有两种方法。

    1. 您将网络分层分布在 GPU 上。这更容易实现,但不会产生很多性能优势,因为 GPU 会互相等待完成操作。

    2. 您创建网络的单独副本,在每个 GPU 上称为“塔”。当您输入八元组网络时,您将输入批次分成 8 个部分,然后分发它们。让网络前向传播,然后对梯度求和,然后进行反向传播。这将导致带有 GPU 数量的almost-linear speedup。但是,实现起来要困难得多,因为您还必须处理与批量标准化相关的复杂性,并且非常建议您确保正确随机化批量。有a nice tutorial here。您还应该查看那里引用的Inception V3 code,以了解如何构建这样的东西。尤其是_tower_loss()_average_gradients()train()中以for i in range(FLAGS.num_gpus):开头的部分。

    如果您想尝试一下 Keras,它现在使用 multi_gpu_model() 显着简化了多 GPU 训练。它可以为您完成所有繁重的工作。

    【讨论】:

    • 对,我看到了,这是否意味着,我只是将不同的层分配给不同的 GPU?还是输出?还是批次?
    • 在我的回答中添加了更多信息。我希望这会有所帮助!
    • CentralStorageStrategy有什么用?我可以从中获得哪些优势?
    猜你喜欢
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 2020-10-14
    • 2021-08-24
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多