bytetrack 内存泄露问题
背景
bytetrack 在生产运行过程中,发现有内存泄露问题,程序跑了几天,通过free -m观察,内存验证不足
走查完bytetrack源码,发现有内存泄露bug,目前bytetrack官方已经没有维护就不提pr了
c++问题代码
ByteTrack-main\deploy\TensorRT\cpp\src\BYTETracker.cpp
内存泄露在 removed_stracks
// Step 5: Update state //
for (int i = 0; i < this->lost_stracks.size(); i++) {
if (this->frame_id - this->lost_stracks[i].end_frame() >
this->max_time_lost) {
this->lost_stracks[i].mark_removed();
removed_stracks.push_back(this->lost_stracks[i]);
}
}
for (int i = 0; i < this->tracked_stracks.size(); i++) {
if (this->tracked_stracks[i].state == TrackState::Tracked) {
tracked_stracks_swap.push_back(this->tracked_stracks[i]);
}
}
this->tracked_stracks.clear();
this->tracked_stracks.assign(tracked_stracks_swap.begin(),
tracked_stracks_swap.end());
this->tracked_stracks =
joint_stracks(this->tracked_stracks, activated_stracks);
this->tracked_stracks = joint_stracks(this->tracked_stracks, refind_stracks);
this->lost_stracks = sub_stracks(this->lost_stracks, this->tracked_stracks);
for (int i = 0; i < lost_stracks.size(); i++) {
this->lost_stracks.push_back(lost_stracks[i]);
}
this->lost_stracks = sub_stracks(this->lost_stracks, this->removed_stracks);
for (int i = 0; i < removed_stracks.size(); i++) {
this->removed_stracks.push_back(removed_stracks[i]);
}
修复c++代码
只保留 max_time_lost *10 范围内的跟踪对象,超过就删除
// 更新 removed_stracks,只保留在 max_time_lost *10 范围内的跟踪对象
std::vector<STrack> filtered_removed_stracks;
for (auto& track : this->removed_stracks) {
if (this->frame_id - track.end_frame() < 10 * this->max_time_lost) {
filtered_removed_stracks.push_back(track);
}
}
this->removed_stracks = filtered_removed_stracks;
python问题代码
yolox/tracker/byte_tracker.py
""" Step 5: Update state"""
for track in self.lost_stracks:
if self.frame_id - track.end_frame > self.max_time_lost:
track.mark_removed()
removed_stracks.append(track)
# print('Ramained match {} s'.format(t4-t3))
self.tracked_stracks = [t for t in self.tracked_stracks if t.state == TrackState.Tracked]
self.tracked_stracks = joint_stracks(self.tracked_stracks, activated_starcks)
self.tracked_stracks = joint_stracks(self.tracked_stracks, refind_stracks)
self.lost_stracks = sub_stracks(self.lost_stracks, self.tracked_stracks)
self.lost_stracks.extend(lost_stracks)
self.lost_stracks = sub_stracks(self.lost_stracks, self.removed_stracks)
self.removed_stracks.extend(removed_stracks)
self.tracked_stracks, self.lost_stracks = remove_duplicate_stracks(self.tracked_stracks, self.lost_stracks)
# get scores of lost tracks
output_stracks = [track for track in self.tracked_stracks if track.is_activated]
return output_stracks
修复python代码
在removed_stracks后面添加类似c++的处理方式
self.removed_stracks.extend(removed_stracks)
self.removed_stracks = [track for track in self.removed_stracks if
self.frame_id - track.end_frame < 10 * self.max_time_lost]