加速并理解Python Keras预测方法的结果分析



我使用Keras和Tensorflow,使用Yolov3标准和Yolov3 Tiny(大约快10倍(执行对象检测。一切正常,但性能相当差,我在GPU上大约每2秒得到一帧,在CPU上大约每4秒得到一个帧。在分析代码时,发现decode_netout方法花费了大量时间。我通常以本教程为例。

  1. 有人能帮我了解一下它在做什么吗
  2. Tensorflow(或其他库(中是否有其他方法可以进行这些计算?例如,我用tf.image.non_max_suppression替换了一些自定义Python,它在性能方面有很大帮助
# https://keras.io/models/model/
yhat = model.predict(image, verbose=0, use_multiprocessing=True)
# define the probability threshold for detected objects
class_threshold = 0.6
boxes = list()
for i in range(len(yhat)):
# decode the output of the network
boxes += detect.decode_netout(yhat[i][0], anchors[i], class_threshold, input_h, input_w)
def decode_netout(netout, anchors, obj_thresh, net_h, net_w):
grid_h, grid_w = netout.shape[:2]
nb_box = 3
netout = netout.reshape((grid_h, grid_w, nb_box, -1))
boxes = []
netout[..., :2]  = _sigmoid(netout[..., :2])
netout[..., 4:]  = _sigmoid(netout[..., 4:])
netout[..., 5:]  = netout[..., 4][..., np.newaxis] * netout[..., 5:]
netout[..., 5:] *= netout[..., 5:] > obj_thresh
for i in range(grid_h*grid_w):
row = i / grid_w
col = i % grid_w
for b in range(nb_box):
# 4th element is objectness score
objectness = netout[int(row)][int(col)][b][4]
if(objectness.all() <= obj_thresh): continue
# first 4 elements are x, y, w, and h
x, y, w, h = netout[int(row)][int(col)][b][:4]
x = (col + x) / grid_w # center position, unit: image width
y = (row + y) / grid_h # center position, unit: image height
w = anchors[2 * b + 0] * np.exp(w) / net_w # unit: image width
h = anchors[2 * b + 1] * np.exp(h) / net_h # unit: image height
# last elements are class probabilities
classes = netout[int(row)][col][b][5:]
box = BoundBox(x-w/2, y-h/2, x+w/2, y+h/2, objectness, classes)
boxes.append(box)
return boxes

我有一个类似的GPU设置,并且一直面临同样的问题。我一直在做YoloV3 Keras项目,在过去的两周里一直在寻找确切的问题。在最后对我的所有函数进行时间装箱后,我发现问题缩小到了"def do_nms",然后我找到了你在"def decode_netout"上面发布的函数。问题是"非最大抑制"速度较慢。

我发现的解决方案是调整这条线的

if(objectness.all() <= obj_thresh): continue

if (objectness <= obj_thresh).all(): continue

白天和晚上的表现不同。我的帧速率接近30,一切都好得多。

这归功于Git的问题/解决方案:

https://github.com/experiencor/keras-yolo3/issues/177

我花了一段时间才弄清楚,所以我希望这能帮助其他人。

最新更新