我的理解是,在python中使用for循环会使我们的代码变慢。因此,应该尽可能地避免它们。我有一段代码,我想把它转换成PyTorch。有没有一种聪明的方法来编写代码,这样我们就不会有for循环在里面了?
def nms(dets, scores, thresh):
'''
dets is a numpy array : num_dets, 6
scores ia nump array : num_dets,
'''
x1 = dets[:, 0]
y1 = dets[:, 1]
z1 = dets[:, 2]
x2 = dets[:, 3]
y2 = dets[:, 4]
z2 = dets[:, 5]
volume = (x2 - x1 + 1) * (y2 - y1 + 1) * (z2 - z1 + 1)
order = scores.argsort()[::-1] # get boxes with more ious first
keep = []
while order.size > 0:
i = order[0] # pick maxmum iou box
keep.append(i)
xx1 = np.maximum(x1[i], x1[order[1:]])
yy1 = np.maximum(y1[i], y1[order[1:]])
zz1 = np.maximum(z1[i], z1[order[1:]])
xx2 = np.minimum(x2[i], x2[order[1:]])
yy2 = np.minimum(y2[i], y2[order[1:]])
zz2 = np.minimum(z2[i], z2[order[1:]])
w = np.maximum(0.0, xx2 - xx1 + 1) # maximum width
h = np.maximum(0.0, yy2 - yy1 + 1) # maxiumum height
l = np.maximum(0.0, zz2 - zz1 + 1) # maxiumum length
inter = w * h * l
ovr = inter / (volume[i] + volume[order[1:]] - inter)
inds = np.where(ovr <= thresh)[0]
order = order[inds + 1]
return keep如果我们可以使用索引来以某种方式替换for循环,我将非常感激。
发布于 2020-06-25 19:38:33
这个算法是连续的,因为下一次迭代都依赖于前一次迭代。所以,在这里你不能从向量并行化中获益。
https://stackoverflow.com/questions/62572624
复制相似问题