如何让tensorflow同时使用cpu和gpu



我目前正在进行一个项目,其中我需要使用Keras pickle来检测文本,然后我需要运行yolov3对象检测。我根据本教程使用Keras。我正在尝试根据以下教程使用TensorFlow应用Yolov3。

代码的工作流程是:首先使用Keras,然后使用yolo,两者都是分离的函数。因此,如果我试图在GPU上运行我的代码,在运行Keras函数后,我似乎无法运行yolo函数,我会不断收到这个错误:

Physical devices cannot be modified after being initialized")
RuntimeError: Physical devices cannot be modified after being initialized

我在互联网上搜索了如何强制Keras使用Cpu,我发现你需要放这行代码

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

在导入Keras之前,但此方法使整个代码使用CPU。我还试着把

del os.environ['CUDA_VISIBLE_DEVICES']

在调用yolo函数使yolo使用GPU之前,它没有工作。整个代码仍在使用CPU所以我的问题是:有没有什么方法可以让Keras使用CPU,然后让yolo使用GPU或者如果我可以让Keras和yolo都使用GPU而不会得到以前的错误。我希望我能用正确的方式解释这个问题。

编辑:

所以首先我调用Keras函数

def Keras():
pickle_in = open("model_trained.p", "rb")
model = pickle.load(pickle_in)
"""some codes detect text from some image
and save it in  variable
"""
yolo()

然后我调用yolo函数

def yolo():
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
yolo = YoloV3(classes=6)
yolo.load_weights('./weights/yolov3.tf')
logging.info('weights loaded')
class_names = [c.strip() for c in open('./data/labels/coco.names').readlines()]
logging.info('classes loaded')
times = []
try:
vid = cv2.VideoCapture(0)
except:
vid = cv2.VideoCapture(0)
out = None
fps = 0.0
count = 0
while True:
_, img = vid.read()
if img is None:
logging.warning("Empty Frame")
time.sleep(0.1)
count += 1
if count < 3:
continue
else:
break
img_in = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_in = tf.expand_dims(img_in, 0)
img_in = transform_images(img_in, 416)
t1 = time.time()
boxes, scores, classes, nums = yolo.predict(img_in)
fps = (fps + (1. / (time.time() - t1))) / 2
img = draw_outputs(img, (boxes, scores, classes, nums), class_names)
img = cv2.putText(img, "FPS: {:.2f}".format(fps), (0, 30),
cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2)
cv2.imshow('output', img)
if cv2.waitKey(1) == ord('q'):
break
cv2.destroyAllWindows()

错误的完整回溯

File "DesktopAppyolov3.py", 
line 94, in start yolo = YoloV3(classes=number_of_c)
File "Appyolov3_tf2models.py", line 211, in YoloV3     
tf.config.experimental.set_memory_growth(physical_devices[0], True)
File"anaconda3envsyolov3gpulibsitepackagestensorflow_corepythonframeworkconfig.py", line 494,
in set_memory_growth     context.context().set_memory_growth(device, enable)
File "anaconda3envsyolov3-gpulibsite-packagestensorflow_corepythoneagercontext.py", line 1241, in set_memory_growth

mirrored_strategy:

import os
import tensorflow as tf
os.environ["CUDA_VISIBLE_DEVICES"]="0,1" 
def Keras():
mirrored_strategy = tf.distribute.MirroredStrategy(devices["/gpu:0","/gpu:1"])
pickle_in = open("model_trained.p", "rb")
with mirrored_strategy.scope():
model = pickle.load(pickle_in)
"""some codes detect text from some image
and save it in  variable
"""

yolo((

可以在CPU上运行整个脚本。

您需要以下代码:

import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"

如果你有一个以上的GPU,你可以使用镜像策略:

import os
import tensorflow as tf
os.environ["CUDA_VISIBLE_DEVICES"]="0,1,2,..." #Num of your GPUs
mirrored_strategy = tf.distribute.MirroredStrategy(devices=["/gpu:0", "/gpu:1", ...])

然后把你的模型放在范围内:

with mirrored_strategy.scope():
model = Sequential()
model.add(...)

最新更新