【发布时间】:2020-04-30 06:00:15
【问题描述】:
我正在使用 Keras 和 Tensorflow 执行使用 Yolov3 标准以及 Yolov3-Tiny 的对象检测(大约快 10 倍)。一切正常,但性能相当差,我在 GPU 上每 2 秒获得一帧,在 CPU 上每 4 秒左右获得一帧。在分析代码时,发现decode_netout 方法花费了很多时间。我一般以this tutorial 为例。
- 有人可以帮我看看它在做什么吗?
- Tensorflow(或其他库)中是否有其他方法可以进行这些计算?例如,我将一些自定义 Python 换成了
tf.image.non_max_suppression,它在性能方面有很大帮助。
# 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
【问题讨论】:
标签: python-3.x keras object-detection tensorflow2.0 yolo