我有一个keras_yolo python实现,我正在尝试学习如何在多个GPU上工作,并且multi_gpu_mode选项听起来像是一个不错的起点。
但是,我的问题是,相同的代码在单个CPU / GPU设置中也能正常工作,但失败并出现NameError:作为multi_gpu_mode模型运行时未定义名称“ yolo_head”。完整的堆栈:
parallel_model = multi_gpu_model(model, cpu_relocation=True)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/utils/multi_gpu_utils.py", line 200, in multi_gpu_model
model = clone_model(model)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/models.py", line 251, in clone_model
return _clone_functional_model(model, input_tensors=input_tensors)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/models.py", line 152, in _clone_functional_model
layer(computed_tensors, **kwargs))
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/engine/base_layer.py", line 457, in __call__
output = self.call(inputs, **kwargs)
File "/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/layers/core.py", line 687, in call
return self.function(inputs, **arguments)
File "/mnt/data/DeepLeague/YAD2K/yad2k/models/keras_yolo.py", line 199, in yolo_loss
pred_xy, pred_wh, pred_confidence, pred_class_prob = yolo_head(
这里是yolo_head
的链接:https://github.com/farzaa/DeepLeague/blob/c87fcd89d9f9e81421609eb397bf95433270f0e2/YAD2K/yad2k/models/keras_yolo.py#L66
我还没有深入研究multi_gpu_model
代码,以了解复制是如何进行的,并且希望避免这样做。
答案 0 :(得分:0)
问题是因为Keras中使用的lambda中的自定义导入必须在引用它的函数中显式导入。
例如在这种情况下,yolo_head
必须在“ yolo_loss”功能级别被“重新导入”,如下所示:
def yolo_loss(args, anchors, num_classes, rescore_confidence=False, print_loss=False):
from yad2k.models.keras_yolo import yolo_head