说明:该系列博客源码链接为:https://github.com/bilylee/SiamFC-TensorFlow,是实验室同小组的师兄用TensorFlow实现SiameseFC算法的最终公开版本,经过了长时间的打磨,各个模块功能明确,整体可读性和可移植性极好,我相信这对做Tracking的小伙伴来说,是个入门SiameseFC Tracker的特别好的选择。哈哈,觉得代码很棒的小伙伴们可以点个Star哦,也欢迎交流学习和指教。
这篇博客主要的目的就是简单地跑一下实验,让下载的代码能用预训练的模型去测试单个视频,并对结果可视化,从视觉上感受一下这个跟踪算法的效果,至于如果要自己训练自己的模型该如何准备训练数据,如何设计自己的模型,如何训练自己的模型,以及如何评估自己的模型等,这些问题都将在后面的系列博客中慢慢道来。
1: SiameseFC-TensorFlow环境配置
可参考源码中的说明,这里将截图放在这里,大家自行准备可运行的环境。
2:预训练模型下载转换和测试
可参考源代码中的说明,这里也给个截图,然后主要对一些文件做一些详细一点的注解。内容主要有预训练模型和测试视频的下载,模型转换以及转换前后的模型对比检验,视频的测试和结果的可视化。
2.1 预训练模型和测试视频下载
核心文件:scripts/download_assets.py
核心功能:下载SiameseFC的matlab版本预训练模型,同时下载用于测试的单个视频。
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2017 bily Huazhong University of Science and Technology
#
# Distributed under terms of the MIT license.
import os.path as osp
import sys
import zipfile
import six.moves.urllib as urllib # 数据下载相关库 , urllib
CURRENT_DIR = osp.dirname(__file__) # 返回当前.py脚本文件的路径
ROOT_DIR = osp.join(CURRENT_DIR, '..')
sys.path.append(ROOT_DIR) # 增加模块的搜索路径
from utils.misc_utils import mkdir_p # 自己编写的makir_p函数,生成路径
def download_or_skip(download_url, save_path): # 数据下载函数
if not osp.exists(save_path): # 判断数据是否已经下载,避免重复下载
print('Downloading: {}'.format(download_url))
opener = urllib.request.URLopener()
opener.retrieve(download_url, save_path)
else:
print('File {} exists, skip downloading.'.format(save_path))
if __name__ == '__main__': # 数据下载准备 主函数
assets_dir = osp.join(ROOT_DIR, 'assets') # 添加数据资源保存的路径assets_dir
# Make assets directory
mkdir_p(assets_dir) # 生成路径,存储数据资源
# Download the pretrained color model # 下载SiameseFC-color pretrained 模型
download_base = 'https://www.robots.ox.ac.uk/~luca/stuff/siam-fc_nets/'
model_name = '2016-08-17.net.mat'
download_or_skip(download_base + model_name, osp.join(assets_dir, model_name))
# Download the pretrained gray model # 下载SiameseFC-color-gray pretrained 模型
download_base = 'https://www.robots.ox.ac.uk/~luca/stuff/siam-fc_nets/'
model_name = '2016-08-17_gray025.net.mat'
download_or_skip(download_base + model_name, osp.join(assets_dir, model_name))
# Download one test sequence # 下载一个测试视频,供测试和显示
download_base = "http://cvlab.hanyang.ac.kr/tracker_benchmark/seq_new/"
seq_name = 'KiteSurf.zip'
download_or_skip(download_base + seq_name, osp.join(assets_dir, seq_name))
# Unzip the test sequence # 将下载的视频.zip文件解压缩
with zipfile.ZipFile(osp.join(assets_dir, seq_name), 'r') as zip_ref:
zip_ref.extractall(assets_dir)
2.2 模型转换
核心文件:experiments/SiamFC-3s-color-pretrained.py、SiamFC-3s-gray-pretrained.py
相关文件:scripts/convert_pretrained_model.py,utils/train_utils.py
核心功能:将siameseFC的color和gray模型由matlab格式转换成tensorflow方便读取的格式。
就从最外边的实验封装文件慢慢往里看吧:experiments/SiamFC-3s-color-pretrained.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2017 bily Huazhong University of Science and Technology
#
# Distributed under terms of the MIT license.
"""Load pretrained color model in the SiamFC paper and save it in the TensorFlow format"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os.path as osp
import sys
CURRENT_DIR = osp.dirname(__file__)
sys.path.append(osp.join(CURRENT_DIR, '..'))
print( osp.join(CURRENT_DIR, '..')) # 添加搜索路径
from configuration import LOG_DIR # 从配置文件中导入log存储的路径
from scripts.convert_pretrained_model import ex # 导入对应的experiment
# 这里只是实验的表皮,具体实验还得看scripts.convert_pretrained_model详细内容
if __name__ == '__main__':
RUN_NAME = 'SiamFC-3s-color-pretrained'
ex.run(config_updates={'model_config': {'embed_config': {'embedding_checkpoint_file': '/workspace/czx/Projects/SiamFC-TensorFlow/assets/2016-08-17.net.mat',
'train_embedding': False, },
},
'train_config': {'train_dir': osp.join(LOG_DIR, 'track_model_checkpoints', RUN_NAME), },
'track_config': {'log_dir': osp.join(LOG_DIR, 'track_model_inference', RUN_NAME), }
}, # 实验运行管理,这里的话就是根据需要更新一些配置文件中的参数
options={'--name': RUN_NAME,
'--force': True,
'--enforce_clean': False,
})
模型转换实验文件:scripts/convert_pretrained_model.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2017 bily Huazhong University of Science and Technology
#
# Distributed under terms of the MIT license.
"""Convert the matlab-pretrained model into TensorFlow format"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import logging
import os
import os.path as osp
import sys
import numpy as np
import tensorflow as tf
CURRENT_DIR = osp.dirname(__file__)
sys.path.append(osp.join(CURRENT_DIR, '..')) # 添加搜索路径
import configuration
import siamese_model
from utils.misc_utils import auto_select_gpu, save_cfgs
# Set GPU
os.environ['CU