Re3修改demo

这一篇记录

Re3-demo中单、多目标追踪代码的调试、修改

让其适用于MOT、VOT数据集

Re3: Real-Time Recurrent Regression Networks for Visual Tracking of Generic Objects

Re3论文:https://arxiv.org/pdf/1705.06368.pdf

Re3代码:https://gitlab.com/danielgordon10/re3-tensorflow

用MOT数据集测试:

MOT中的gt.txt文件中的坐标信息是[x, y, w, h],而Re3 tracker需要的坐标信息是[x1, y1, x2, y2],这里得修改一下。

用VOT数据集测试:

VOT中的gt.txt文件中的坐标信息是[x1, y1, x2, y2, x3, y3, x4, y4],同样,这里需要修改一下(保留[x1, y1, x3, y3])。

image_demo.py

这是追踪单个物体的例子,对于提供的data,以为这里只能追踪ball,因为这个文件中有一句是这样写的:

1
tracker.track('ball', image_paths[0], initial_bbox)

把这里面的ball随便改成其他字符串,也可以追踪自带data中的球球。所以这里面的ball只是起一个表示的作用。

对image_demo.py做一下优化,显示初始box位置,这样,用别的数据集进行测试能够更方便的看到初始box。

image_demo.py修改之后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import cv2
import glob
import numpy as np
import sys
import os.path

basedir = os.path.dirname(__file__)
sys.path.append(os.path.abspath(os.path.join(basedir, os.path.pardir)))
from tracker import re3_tracker

if not os.path.exists(os.path.join(basedir, 'data')):
import tarfile
tar = tarfile.open(os.path.join(basedir, 'data.tar.gz'))
tar.extractall(path=basedir)

cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Image', 640, 480)
tracker = re3_tracker.Re3Tracker()

example_index = 6 # 0 is origin demo.

if 0 == example_index:
data_type = 'origin_demo'
image_paths = sorted(glob.glob(os.path.join(os.path.dirname(__file__), 'data', '*.jpg'))) # origin demo path
initial_bbox = [175, 154, 251, 229] # origin data. format: x1, x2, y1, y2
elif 1 == example_index:
data_type = 'MOT'
your_data_path = '/home/ubuntu/Downloads/dataset/MOT/2DMOT2015/train/ETH-Pedcross2/img1'
image_paths = sorted(glob.glob(os.path.join(your_data_path, '*.jpg')))
initial_bbox = [16, 149, 76, 190] # MOT data. format: x, y, w, h
elif 2 == example_index:
data_type = 'MOT'
your_data_path = '/home/ubuntu/Downloads/dataset/MOT/MOT16/train/MOT16-04/img1'
image_paths = sorted(glob.glob(os.path.join(your_data_path, '*.jpg')))
initial_bbox = [371,410,80,239] # MOT data. format: x, y, w, h
elif 3 == example_index:
data_type = 'MOT'
your_data_path = '/home/ubuntu/Downloads/dataset/MOT/MOT16/train/MOT16-11/img1'
image_paths = sorted(glob.glob(os.path.join(your_data_path, '*.jpg')))
initial_bbox = [867,145,236,635] # MOT data. format: x, y, w, h
elif 4 == example_index:
data_type = 'MOT'
your_data_path = '/home/ubuntu/Downloads/dataset/MOT/2DMOT2015/train/ETH-Sunnyday/img1'
image_paths = sorted(glob.glob(os.path.join(your_data_path, '*.jpg')))
initial_bbox = [252,185,46,127] # MOT data. format: x, y, w, h
elif 5 == example_index:
data_type = 'MOT'
your_data_path = '/home/ubuntu/Downloads/dataset/MOT/2DMOT2015/train/ETH-Sunnyday/img1'
image_paths = sorted(glob.glob(os.path.join(your_data_path, '*.jpg')))
initial_bbox = [289,187,44,125] # MOT data. format: x, y, w, h
elif 6 == example_index:
data_type = 'VOT'
your_data_path = '/home/ubuntu/Downloads/dataset/VOT/vot2016/singer1'
image_paths = sorted(glob.glob(os.path.join(your_data_path, '*.jpg')))
initial_bbox = [62.24,55.967,138.02,55.977,137.99,334.93,62.205,334.92] # VOT data. format: x0, y0, x1, y1, x2, y2, x3, y3

# change format to [x1, x2, y1, y2]
if data_type == 'MOT':
initial_bbox[2] += initial_bbox[0]
initial_bbox[3] += initial_bbox[1]
elif data_type == 'VOT':
initial_bbox = [initial_bbox[0], initial_bbox[1], initial_bbox[4], initial_bbox[5]]


tracker.track('ball', image_paths[0], initial_bbox)

is_first_image = True

for image_path in image_paths:
image = cv2.imread(image_path)

if is_first_image:
cv2.rectangle(image,
(int(initial_bbox[0]), int(initial_bbox[1])),
(int(initial_bbox[2]), int(initial_bbox[3])),
[255, 0, 0], 2)
cv2.imshow('Image', image)
cv2.waitKey(0)
is_first_image = False

# Tracker expects RGB, but opencv loads BGR.
imageRGB = image[:,:,::-1]
bbox = tracker.track('ball', imageRGB)
cv2.rectangle(image,
(int(bbox[0]), int(bbox[1])),
(int(bbox[2]), int(bbox[3])),
[0,0,255], 2)
cv2.imshow('Image', image)
# cv2.waitKey(1) # origin
cv2.waitKey(1)

batch_demo.py

batch_demo.py用来同时追踪多个物体。项目中自带的batch_demo.py文件从第101帧开始追踪第二个物体。如果我们想用MOT数据集测试,从第一帧开始追踪多个物体,可以修改进行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import cv2
import glob
import numpy as np
import sys
import os.path

basedir = os.path.dirname(__file__)
sys.path.append(os.path.abspath(os.path.join(basedir, os.path.pardir)))
from tracker import re3_tracker

if not os.path.exists(os.path.join(basedir, 'data')):
import tarfile
tar = tarfile.open(os.path.join(basedir, 'data.tar.gz'))
tar.extractall(path=basedir)

cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Image', 640, 480)

example_index = 1

if 1 == example_index:
data_type = 'MOT'
your_data_path = '/home/ubuntu/Downloads/dataset/MOT/MOT16/train/MOT16-04/img1'
image_paths = sorted(glob.glob(os.path.join(your_data_path, '*.jpg')))
initial_bboxes = []
initial_bboxes.append([1363,569,103,241])
initial_bboxes.append([103,549,83,251])
initial_bboxes.append([371, 410, 80, 239])
initial_bboxes.append([1734,457,76,213])
initial_bboxes.append([1098,980,78,208])

track_objects_name = [] # format: ['ball', 'object2', ...] name can be any str, just a index
track_objects_dict = {} # format: {'ball' : [1363,569,103,241], 'logo' : [103,549,83,251], ...}

if data_type == 'MOT':

for i, initial_bbox in enumerate(initial_bboxes):

# MOT data. format: [x, y, w, h]. what we need if [x1, x2, y1, y2]
initial_bbox[2] += initial_bbox[0]
initial_bbox[3] += initial_bbox[1]

object_name = "object"+str(i+1)
track_objects_name.append(object_name)
track_objects_dict[object_name] = initial_bbox

tracker = re3_tracker.Re3Tracker()

# Provide a unique id, an image/path, and a bounding box.
bboxes = tracker.multi_track(track_objects_name, image_paths[0], track_objects_dict)

is_first_image = True

for ii,image_path in enumerate(image_paths):
image = cv2.imread(image_path)

if is_first_image:
for initial_bbox in initial_bboxes:
cv2.rectangle(image,
(int(initial_bbox[0]), int(initial_bbox[1])),
(int(initial_bbox[2]), int(initial_bbox[3])),
[255, 0, 0], 2)
cv2.imshow('Image', image)
cv2.waitKey(0)
is_first_image = False

# Tracker expects RGB, but opencv loads BGR.

imageRGB = image[:,:,::-1]

# Both tracks are started, neither needs bounding boxes.
bboxes = tracker.multi_track(track_objects_name, imageRGB)

for bb,bbox in enumerate(bboxes):
color = cv2.cvtColor(np.uint8([[[bb * 255 / len(bboxes), 128, 200]]]),
cv2.COLOR_HSV2RGB).squeeze().tolist()
cv2.rectangle(image,
(int(bbox[0]), int(bbox[1])),
(int(bbox[2]), int(bbox[3])),
color, 2)

cv2.imshow('Image', image)
cv2.waitKey(1)

报错

修改程序

1
2
3
4
5
6
7
8
9
10
Traceback (most recent call last):
File "/home/ubuntu/anaconda2/envs/tf15/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1350, in _do_call
return fn(*args)
File "/home/ubuntu/anaconda2/envs/tf15/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1329, in _run_fn
status, run_metadata)
File "/home/ubuntu/anaconda2/envs/tf15/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value re3/fc6/W_fc
[[Node: re3/fc6/W_fc/_40 = _Send[T=DT_FLOAT, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_4_re3/fc6/W_fc", _device="/job:localhost/replica:0/task:0/device:GPU:0"](re3/fc6/W_fc)]]
[[Node: re3/fc6/W_fc/_41 = _Recv[_start_time=0, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_4_re3/fc6/W_fc", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

参考该仓库下的issue:https://gitlab.com/danielgordon10/re3-tensorflow/issues/6:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#constants.py 

CROP_SIZE = 227
CROP_PAD = 2
MAX_TRACK_LENGTH = 32
- LSTM_SIZE = 1024
+ LSTM_SIZE = 512

import os.path
LOG_DIR = os.path.join(os.path.dirname(__file__), 'logs')

#tracker/network.py

# Embed Fully Connected Layer
with tf.variable_scope('fc6'):
- fc6_out = tf_util.fc_layer(conv_layers, 2048)
+ fc6_out = tf_util.fc_layer(conv_layers, 1024)

# (BxT)xC
fc6_reshape = tf.reshape(fc6_out, tf.stack([batch_size, num_unrolls, fc6_out.get_shape().as_list()[-1]]))
# LSTM stuff
swap_memory = num_unrolls > 1
with tf.variable_scope('lstm1'):
- lstm1 = CaffeLSTMCell(LSTM_SIZE, initializer=msra_initializer)
+ #lstm1 = CaffeLSTMCell(LSTM_SIZE, initializer=msra_initializer)
+ lstm1 = tf.contrib.rnn.LSTMCell(LSTM_SIZE, use_peepholes=True, initializer=msra_initializer, reuse=reuse)
if prevLstmState is not None:
state1 = tf.contrib.rnn.LSTMStateTuple(prevLstmState[0], prevLstmState[1])
else:
tf_util.variable_summaries(var, var.name[:-2])

with tf.variable_scope('lstm2'):
- lstm2 = CaffeLSTMCell(LSTM_SIZE, initializer=msra_initializer)
+ #lstm2 = CaffeLSTMCell(LSTM_SIZE, initializer=msra_initializer)
+ lstm2 = tf.contrib.rnn.LSTMCell(LSTM_SIZE, use_peepholes=True, initializer=msra_initializer, reuse=reuse)
state2 = lstm2.zero_state(batch_size, dtype=tf.float32)
if prevLstmState is not None:
state2 = tf.contrib.rnn.LSTMStateTuple(prevLstmState[2], prevLstmState[3])

现在这个问题应该已经被作者修改好了。

pycharm添加环境变量

终端可以运行,Pycharm中找不到cuda9.0相关文件,需要到Run->Edit Configurations->Environment Variables中添加一组变量:Name设置为LD_LIBRARY_PATH,Value设置为/usr/local/cuda/lib64(cuda是个软链接,连接到对应版本的CUDA)。

opencv不能打开摄像头

错误原因,安装opencv-python不合适,从menpo库里安装:conda install -c menpo opencv3

menpo库已经停止支持,用如下方式安装:

1
conda install -c conda-forge opencv

即可打开摄像头。

1
cap = cv2.VideoCapture(0)

无法打开的话,可以试着改成:

1
cap = cv2.VideoCapture(1)

可能是系统的原因,导致0被占用。


----------over----------


文章标题:Re3修改demo

文章作者:Ge垚

发布时间:2018年07月04日 - 16:07

最后更新:2018年07月25日 - 11:07

原始链接:http://geyao1995.com/Re3_run_demo/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。