发现在复原《neural_motifs》实验环境的时候,碰到了一些小问题。在此做一些小记录。
参考了这篇博文
配置conda虚拟环境
参考 博文1
2
3
4
5
6
7
8
9
10
11conda create -n motif python=3.5
# 创建虚拟环境
source activate motif
# 进入虚拟环境
conda install pytorch=0.3.0 torchvision=0.2.0 cuda80 -c pytorch
# cuda80: 实验室机器的cuda版本是 cuda8.0
# conda deactivate
# 退出虚拟环境在国内用默认源下载库的速度会很慢,因此使用清华镜像,所以 conda install 的命令改为:
1
2conda install pytorch=0.3.0 torchvision=0.2.0 cuda80
# "-c pytorch" 表示使用默认源下载可以参考[Anaconda 镜像使用帮助(https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/)
也可以在清华镜像上下载压缩包添加路径
1
export PYTHONPATH=/home/lab/zmr/neural_motif
记得添加路径,不然可能会报错 no module found
此外,还要修改 config.py中第20行:
1
2
3
4
5VG_IMAGES = '/home/lab/liangjinrui/neural-motifs/dataset/VG_100K'
# 改为VG图片数据集的地址,不然会报错
# File “/home/lab/zmr/neural_motif/dataloaders/visual_genome.py”, line 260, in load_image_filenames
# assert len(fns) == 108073
# AssertionError运行make
这时会提醒权限不够,运行指令
1
chmod 777 ./lib/lstm/highway_lstm_cuda/make.sh
首先训练 object detector。修改 ./scripts/pretrain_detector.sh
1
2python models/train_detector.py -b 6 -lr 1e-3 -save_dir checkpoints/vgdet -nepoch 50 -ngpu 3 -nwork 3 -p 100 -clip 5
论文用到了3颗GPU,但实验室机器上是单核GPU,因此记得修改为 “-ngpu 1”执行指令
1
./scripts/pretrain_detector.sh
如果再次提醒权限不够,再次执行指令
1
chmod 777 ./scripts/pretrain_detector.sh
pretrain_detector 运行结果
实验总共有 50 epoch,单核GPU上,每个 epoch 的运行时间大约为 2h.
因为时间有限,运行到 24 个epoch就退出了。
接着训练 sgcls。还是要记得修改 ./scripts/train_models_sgcls.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20export CUDA_VISIBLE_DEVICES=$1
if [ $1 == "2" ]; then
echo "TRAINING THE BASELINE"
python models/train_rels.py -m sgcls -model motifnet -nl_obj 0 -nl_edge 0 -b 6 \
-clip 5 -p 100 -pooling_dim 4096 -lr 1e-3 -ngpu 1 -ckpt checkpoints/vgdet/vg-24.tar -save_dir checkpoints/baseline2 \
-nepoch 50 -use_bias
elif [ $1 == "1" ]; then
echo "TRAINING MESSAGE PASSING"
python models/train_rels.py -m sgcls -model stanford -b 6 -p 100 -lr 1e-3 -ngpu 1 -clip 5 \
-ckpt checkpoints/vgdet/vg-24.tar -save_dir checkpoints/stanford2
elif [ $1 == "0" ]; then
echo "TRAINING MOTIFNET"
python models/train_rels.py -m sgcls -model motifnet -order leftright -nl_obj 2 -nl_edge 4 -b 6 -clip 5 \
-p 100 -hidden_dim 512 -pooling_dim 4096 -lr 1e-3 -ngpu 1 -ckpt checkpoints/vgdet/vg-24.tar \
-save_dir checkpoints/motifnet-sgcls -nepoch 50 -use_bias
fi
因为是在单核GPU上运行,要修改GPU的序号。checkpoint和save路径按照项目文件修改。
执行指令
1
./scripts/train_models_sgcls.sh 0
如果再次提醒权限不够,如上。
sgcls运行结果
实验总共有 50 epoch,在单核GPU上,每个epoch的运行时间大约为 1h
因为时间有限,运行到11个 epoch 就退出了。
最后,refine SG dectection,依旧要修改 ./scripts/refine_for_detection.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17export CUDA_VISIBLE_DEVICES=$1
if [ $1 == "2" ]; then
echo "TRAINING THE BASELINE"
python models/train_rels.py -m sgdet -model motifnet -nl_obj 0 -nl_edge 0 -b 6 \
-clip 5 -p 100 -pooling_dim 4096 -lr 1e-4 -ngpu 1 -ckpt checkpoints/baseline-sgcls/vgrel-11.tar -save_dir checkpoints/baseline-sgdet \
-nepoch 50 -use_bias
elif [ $1 == "1" ]; then
echo "TRAINING STANFORD"
python models/train_rels.py -m sgdet -model stanford -b 6 -p 100 -lr 1e-4 -ngpu 1 -clip 5 \
-ckpt checkpoints/stanford-sgcls/vgrel-11.tar -save_dir checkpoints/stanford-sgdet
elif [ $1 == "0" ]; then
echo "Refining Motifnet for detection!"
python models/train_rels.py -m sgdet -model motifnet -order leftright -nl_obj 2 -nl_edge 4 -b 6 -clip 5 \
-p 100 -hidden_dim 512 -pooling_dim 4096 -lr 1e-4 -ngpu 1 -ckpt checkpoints/motifnet-sgcls/vgrel-7.tar \
-save_dir checkpoints/motifnet-sgdet -nepoch 10 -use_bias
fi执行指令
1
./scripts/refine_for_detection.sh 0
SG dectection运行结果
实验总共有 10 epoch,在单核GPU上,每个 epoch 的运行时间大约为 2.5h
Evaluation
实验总共分为三个任务:
- Predicate Classification: given a ground truth set of boxes and labels, predict edge labels
- Scene Graph Classification: given ground truth boxes, predict box labels and edge label
- Scene Graph Detection: predict boxes, box labels, and edge labels
eval_models_sgcls.sh计算 precls 和 sgcls 的召回率,eval_models_sgdet.sh计算 sgdet 的召回率结果如下:
Scene Graph Detection Scene Graph Classification Predicate Classification Model R@20 R@50 R@100 R@20 R@50 R@100 R@20 R@50 R@100 MOTIFNET-LR 0.201 0.255 0.286 0.322 0.351 0.356 0.583 0.650 0.669 论文的实验结果如下:
Scene Graph Detection Scene Graph Classification Predicate Classification Model R@20 R@50 R@100 R@20 R@50 R@100 R@20 R@50 R@100 MOTIFNET-LR 0.214 0.272 0.303 0.329 0.358 0.365 0.585 0.652 0.671