【问题标题】:Keras multi_gpu_model returns error 'tensorflow_core._api.v2.config' has no attribute 'experimental_list_devices'Keras multi_gpu_model 返回错误“tensorflow_core._api.v2.config”没有属性“experimental_list_devices”
【发布时间】:2020-07-04 20:54:57
【问题描述】:

我在两个 GPU 上训练我的 unet 模型时遇到问题,

该模型是一个我知道可行的简单 U-net 实现,因为它的 testet 不是 multi_gpu_model

train_generator = zip(image_generator, mask_generator)

with tf.device("/cpu:0"):
    # initialize the model
    model = unet((512,512,3))

# make the model parallel
model = multi_gpu_model(model, gpus=2)
model.compile(optimizer='adam', loss="mean_squared_error")

model.fit_generator(train_generator, steps_per_epoch=250, epochs=10)

:output
File "C:/Users/PycharmProjects/U-net/U-net.py", line 29, in <module>
    model = multi_gpu_model(model, gpus=2)
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\utils\multi_gpu_utils.py", line 150, in multi_gpu_model
    available_devices = _get_available_devices()
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\utils\multi_gpu_utils.py", line 16, in _get_available_devices
    return K.tensorflow_backend._get_available_gpus() + ['/cpu:0']
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\backend\tensorflow_backend.py", line 506, in _get_available_gpus
    _LOCAL_DEVICES = tf.config.experimental_list_devices()
AttributeError: module 'tensorflow_core._api.v2.config' has no attribute 'experimental_list_devices'

我也尝试过 tf.distributed.mirroredStrategy() 但也没有运气

任何帮助将不胜感激

【问题讨论】:

    标签: python tensorflow keras multi-gpu


    【解决方案1】:

    experimental_list_devices 在 tf 2.1 中已弃用,请使用 tf.config.list_logical_devices 替换。

    def _get_available_gpus():
    """Get a list of available gpu devices (formatted as strings).
    
    # Returns
        A list of available GPU devices.
    """
    global _LOCAL_DEVICES
    if _LOCAL_DEVICES is None:
        if _is_tf_1():
            devices = get_session().list_devices()
            _LOCAL_DEVICES = [x.name for x in devices]
        else:
            devices = tf.config.list_logical_devices()
            _LOCAL_DEVICES = [x.name for x in devices]
    return [x for x in _LOCAL_DEVICES if 'device:gpu' in x.lower()]
    

    该链接有助于解决您的问题LINK

    【讨论】:

    • 谢谢,它修复了错误,但仍然只使用 1 个 gpu 来训练。
    • 它将副本添加到每个 gpu,但是在分配内存/训练时它只使用 GPU:0
    猜你喜欢
    • 2020-04-03
    • 1970-01-01
    • 2020-06-20
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2022-01-12
    相关资源
    最近更新 更多