Hanjie's Blog

一只有理想的羊驼

测试DFANet语义分割网络,基于论文DFANet: Deep Feature Aggregation for Real-Time Semantic Segmentation,主要特点在于它的实时性:

测试使用cityscapes数据集,可以在这里下载。

服务器及数据准备

假设有已有一个远程docker服务器root@0.0.0.0 -p 9999。

Dependence

1
2
3
4
5
6
7
8
pytorch==1.0.0
python==3.6
numpy
torchvision
matplotlib
opencv-python
tensorflow
tensorboardX
1
2
3
4
apt install -y libsm6 libxext6
pip3 install opencv-python
pip3 install pyyaml
pip3 install tensorboardX

检查pytorch版本:

1
2
import torch
print(torch.__version__)
1
2
3
# Linux, pip, Python 3.6, CUDA 9
pip3 install --upgrade pip
pip3 install --upgrade torch torchvision
使用scp指令将本地程序和数据集上传到服务器:
1
scp -P 9999 local_file root@0.0.0.0:remote_directory
解压缩zip文件
1
2
3
apt-get update
apt-get install zip -y
unzip local_file
下载DFANet
1
2
git clone https://github.com/huaifeng1993/DFANet.git
cd DFANet
Pretrained model

打开utils/preprocess_data.py,修改dataset位置:

1
2
cityscapes_data_path = "/home/luohanjie/Documents/SLAM/data/cityscapes"
cityscapes_meta_path = "/home/luohanjie/Documents/SLAM/data/cityscapes/gtFine"

运行脚本,生成labels:

1
python3 utils/preprocess_data.py
main.py

打开main.py,修改dataset位置:

1
2
3
4
5
train_dataset = DatasetTrain(cityscapes_data_path="/home/luohanjie/Documents/SLAM/data/cityscapes",
cityscapes_meta_path="/home/luohanjie/Documents/SLAM/data/cityscapes/gtFine/")

val_dataset = DatasetVal(cityscapes_data_path="/home/luohanjie/Documents/SLAM/data/cityscapes",
cityscapes_meta_path="/home/luohanjie/Documents/SLAM/data/cityscapes/gtFine/")

2019.4.24 An function has been writed to load the pretrained model which was trained on imagenet-1k.The project of training the backbone can be Downloaded from here -https://github.com/huaifeng1993/ILSVRC2012. Limited to my computing resources(only have one RTX2080),I trained the backbone on ILSVRC2012 with only 22 epochs.But it have a great impact on the results.

由于我们没有ILSVRC2012的pretrained model,所以需要关掉标志位:

1
net = dfanet(pretrained=False, num_classes=20)
ERROR: TypeError: init() got an unexpected keyword argument 'log_dir'

打开train.py,修改为:

1
writer = SummaryWriter(logdir=self.log_dir)
ERROR: Unexpected bus error encountered in worker. This might be caused by insufficient shared memory (shm)

出现这个错误的情况是,在服务器上的docker中运行训练代码时,batch size设置得过大,shared memory不够(因为docker限制了shm).解决方法是,将Dataloader的num_workers设置为01

打开main.py,修改:

1
2
3
4
5
6
train_loader = DataLoader(dataset=train_dataset,
batch_size=10, shuffle=True,
num_workers=0)
val_loader = DataLoader(dataset=val_dataset,
batch_size=10, shuffle=False,
num_workers=0)

Train

1
python3 main.py

  1. https://blog.csdn.net/hyk_1996/article/details/80824747↩︎

下载opencv_contrib:

1
2
3
4
5
git clone https://github.com/opencv/opencv_contrib

cd opencv_contrib

git checkout 3.4.5

官网下载sources。

1
2
3
4
5
6
7
8
9
10
11
12
13
sudo apt-get install build-essential git make git yams libgtk2.0-dev pig-config libavcodec-dev libavformat-dev python-dev python-bumpy python-tk libtbb2 libtbb-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libdc1394-22-dev libswscale-dev libopenexr-dev libeigen2-dev libeigen3-dev libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev

mkdir build
cd build

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_EXAMPLES=OFF -D BUILD_DOCS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF -D WITH_GTK_2_X=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON -D WITH_CUDA=ON -D CMAKE_CXX_FLAGS="-std=c++11" -D CUDA_NVCC_FLAGS="-std=c++11 --expt-relaxed-constexpr" -D OPENCV_EXTRA_MODULES_PATH=/home/luohanjie/Documents/software/opencv-3.4.11/opencv_contrib/modules -D VTK_DIR=/home/luohanjie/Documents/software/VTK-7.1.1/build ..


make -j4
sudo make install

sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

给定图片\(I\)上的一个特征点\(\mathbf{x}\)和对应的搜索向量\(\mathbf{n}\),求在另一张图\(J\)中找到匹配的点\(\mathbf{x'}\)。假定在图\(J\)上的搜索起始位置为\(\mathbf{x} _{init}'\),搜索窗口为\(W\),迭代次数\(K\)(在实际使用中,我们往往会将\(\mathbf{x} _{init}'=\mathbf{x}\)123

计算步骤
  1. 分别对图像\(I\)和图像\(J\)建立金字塔\(\lbrace I^L \rbrace _{L=0,…,L _m}\)\(\lbrace J^L \rbrace _{L=0,…,L _m}\)\(L _m\)为给定的金字塔层数,一般为3(图像金字塔化一般包括两个步骤:首先对图像进行一次低通滤波进行平滑,然后对图像的横纵两个方向1/2抽样,从而得到一系列尺度缩小的图像。当L=0时,为原图,当向金字塔的上层移动时,尺寸和分辨率降低,伴随的细节就越少。我们从顶层开始对目标点进行跟踪,先获得一个粗糙的结果,然后将结果作为下一层的初始点再进行跟踪,不断迭代直到到达第0层。这是一种由粗到细分析策略)。
jinzita
  1. 初始化顶层金字塔的搜索偏移位置:\(\mathbf{g}^{L _m}=[g _{x}^{L _m}\ g _{y}^{L _m }]^T =[0\ 0]^T\)

  2. 从第\(L=L _m\)层(顶层)金字塔图像开始不断往下,对每一层图像作作以下操作:

(3.1) 计算特征点\(\mathbf{x}\)在金字塔第\(L\)层图\(I^L\)上的位置:\(\mathbf{x}^L=[p _x\ p _y]^T=\mathbf{x}/2^L\)

(3.2) 计算搜索起始位置\(\mathbf{x} _{init}'\)在金字塔第\(L\)层图\(J^L\)上的位置:\(\mathbf{x}'^L=[p' _x\ p' _y]^T=\mathbf{x} _{init}'/2^L\)

(3.3) 计算最速下降矩阵\(\mathbf{S}(u,v)=[I _x (p _x+u,p _y+v)\ I _y (p _x+u,p _y+v)]\mathbf{n}\)\((u,v)⊆W\)\(\mathbf{S}\)矩阵跟窗口\(W\)大小一致。其中,\(I _x (x,y)\)\(I _y (x,y)\)为图\(I^L\)\((x,y)\)位置\(X\)\(Y\)两个方向的梯度。

(3.4) 计算在特征点\(\mathbf{x}\)在第\(L\)层的空间梯度值\(\textstyle m^L = \sum _{(u,v)⊆W} \mathbf{S}(u,v)^2\)\(m^L\)体现的是,图\(I^L\)中,位于\(\mathbf{x}^L\)的窗口\(W\)内,图像在\(\mathbf{n}\)方向的梯度变化。

(3.5) 初始化位置迭代参数\(\mathbf{\Gamma}^0 = [{\Gamma} _{x}^0 \ {\Gamma} _{y}^0]^T = [0\ 0]^T\),参数记录了偏移位置,用于寻找偏移了的特征点。

(3.6) 变量\(k\)从1到\(K\)\(K\)为控制变量,用于控制(3.6.1)至(3.6.3)的迭代次数),迭代以下操作:

(3.6.1) 此时,特征点在图\(J^L\)的跟踪位置在\((p' _x+g _x^L+{\Gamma} _x^{k-1},p' _y+g _y^L+{\Gamma} _y^{k-1} )\),计算图像偏差值\(b^k = \sum _{(u,v)⊆W}[\mathbf{S}(u,v)[I^L (p _x+u,p _y+v)-J^L (p' _x+g _x^L+{\Gamma} _x^{k-1},p' _y+g _y^L+{\Gamma} _y^{k-1} )]\)

(3.6.2) 更新位置迭代参数\(\mathbf{\Gamma}^k=\mathbf{\Gamma}^{k-1}-(b^k/m^L)\mathbf{n}\)

(3.6.3)\(k=k+1\),回到(3.6.1)继续迭代。

(3.7) 在\(L\)层金字塔最终跟踪偏移:\(\mathbf{d}^L=\mathbf{\Gamma}^k\)

(3.8) 初始化下一层金字塔的跟踪偏移位置:\(g^{L-1}=2(g^L+d^L)\)

(3.9)\(L=L-1\),回到(3.1)继续迭代。

  1. 特征点\(x\)在图\(J\)中的匹配的点位置\(\mathbf{x}'=\mathbf{x} _{init}'+g^0+d^0\)
1d_klt

  1. Baker, Simon, and Iain Matthews. "Lucas-kanade 20 years on: A unifying framework." International journal of computer vision 56.3 (2004): 221-255.↩︎

  2. Bouguet, Jean-Yves. "Pyramidal implementation of the affine lucas kanade feature tracker description of the algorithm." Intel Corporation 5.1-10 (2001): 4.↩︎

  3. Bouguet, Jean-Yves. "Pyramidal implementation of the affine lucas kanade feature tracker description of the algorithm." Intel Corporation 5.1-10 (2001): 4.↩︎

0%