为tensorflow分配GPU或CPU

0.环境

Win10
python3.5
tensorflow-gpu==1.12.0

持续监控GPU使用情况:

watch -n 10 nvidia-smi

 

1.只使用CPU

  在代码前加入一下代码: 

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

2.百分比使用GPU

  在代码前加入一下代码: 

import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5 # 占用GPU50%的显存
session = tf.Session(config=config)

3.最小量增长使用GPU

  在代码前加入一下代码: 

import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)

4.指定多GPU中一个或者几个GPU

  指定多GPU中的一个GPU时,在代码前加入一下代码(Pytorch也可以使用以下): 

import os
os.environ['CUDA_VISIBLE_DEVICES']='3'

  指定多GPU中的多个GPU时,在代码前加入一下代码: 

import os
os.environ['CUDA_VISIBLE_DEVICES']='2,3'#假设指定第二和第三块GPU

如果设置多GPU还是出现

2020-06-07 14:39:52.003640: I tensorflow/core/common_runtime/bfc_allocator.cc:641] 1 Chunks of size 524288 totalling 512.0KiB
2020-06-07 14:39:52.003650: I tensorflow/core/common_runtime/bfc_allocator.cc:641] 1 Chunks of size 524544 totalling 512.2KiB
2020-06-07 14:39:52.003661: I tensorflow/core/common_runtime/bfc_allocator.cc:641] 1 Chunks of size 690944 totalling 674.8KiB
2020-06-07 14:39:52.003671: I tensorflow/core/common_runtime/bfc_allocator.cc:645] Sum Total of in-use chunks: 10.37MiB
2020-06-07 14:39:52.003684: I tensorflow/core/common_runtime/bfc_allocator.cc:647] Stats:
Limit:                 11908877517
InUse:                    10878208
MaxInUse:               6485541376
NumAllocs:                 2717750
MaxAllocSize:           3227053056

2020-06-07 14:39:52.003740: W tensorflow/core/common_runtime/bfc_allocator.cc:271] *___________________________________________________________________________________________________
INFO:tensorflow:Error reported to Coordinator: <class 'tensorflow.python.framework.errors_impl.InternalError'>, Dst tensor is not initialized.
         [[{{node clone_1/prefetch_queue_Dequeue/_3899}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:1", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_500_clone_1/prefetch_queue_Dequeue", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:1"]()]]
I0607 14:39:58.117163 139928698971904 tf_logging.py:115] Error reported to Coordinator: <class 'tensorflow.python.framework.errors_impl.InternalError'>, Dst tensor is not initialized.
         [[{{node clone_1/prefetch_queue_Dequeue/_3899}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:1", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_500_clone_1/prefetch_queue_Dequeue", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:1"]()]]
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1334, in _do_call
    return fn(*args)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InternalError: Dst tensor is not initialized.
         [[{{node clone_1/prefetch_queue_Dequeue/_3899}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:1", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_500_clone_1/prefetch_queue_Dequeue", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:1"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./train.py", line 188, in <module>
    tf.app.run()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/deprecation.py", line 306, in new_func
    return func(*args, **kwargs)
  File "./train.py", line 184, in main
    graph_hook_fn=graph_rewriter_fn)
  File "/opt/SSD_Mobilenetv2/models-1.12.0/research/object_detection/legacy/trainer.py", line 415, in train
    saver=saver)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/slim/python/slim/learning.py", line 770, in train
    sess, train_op, global_step, train_step_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/slim/python/slim/learning.py", line 487, in train_step
    run_metadata=run_metadata)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1152, in _run
    feed_dict_tensor, options, run_metadata)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1328, in _do_run
    run_metadata)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1348, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InternalError: Dst tensor is not initialized.
         [[{{node clone_1/prefetch_queue_Dequeue/_3899}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:1", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_500_clone_1/prefetch_queue_Dequeue", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:1"]()]]

Dst tensor is not initialized,GPU显存问题,我们尝试可以添加

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8) #0.333
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True, gpu_options=gpu_options))

 

 参考

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wait a minutes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值