1.搭建虚拟环境
virtualenv -p python3 .env # Create virtual environment
source .env/bin/activate # Activate virtual environment
pip install -r requirements.txt # Install dependencies
echo $PWD > .env/lib/python3.5/site-packages/iep.pth # Add this package to virtual environment
# Work for a while ...
deactivate # Exit virtual environment
2.数据集的预处理
CLEVR数据集
(1)下载解压
(2)提取图像特征
提取ResNet-101特征 【train\val\test类似】
python scripts/extract_features.py \
--input_image_dir data/CLEVR_v1.0/images/train \
--output_h5_file data/train_features.h5
h5是什么格式的文件?
(3)预处理question【train\val\test类似】
python scripts/preprocess_questions.py \
--input_questions_json data/CLEVR_v1.0/questions/CLEVR_train_questions.json \
--output_h5_file data/train_questions.h5 \
--output_vocab_json data/vocab.json
vocab.json这个词汇表stores the mapping between tokens and indices for questions and programs。训练的时候生成vocab.json,可以重复利用在val\test那里用作输入
python scripts/preprocess_questions.py \
--input_questions_json data/CLEVR_v1.0/questions/CLEVR_val_questions.json \
--output_h5_file data/val_questions.h5 \
--input_vocab_json data/vocab.json
3.训练
训练细节和“ Program Generator + Execution Engine model”高度一致,虽然我们是单步训练,他们是三步训练。
三步训练:包括PG、EE、PG+EE
(1)训练Program Generator
python scripts/train_model.py \
--model_type PG \
--num_train_samples 18000 \ # a small number of ground-truth programs
--num_iterations 20000 \
--checkpoint_every 1000 \
--checkpoint_path data/program_generator.pt
(2)训练 Execution Engine
python scripts/train_model.py \
--model_type EE \
--program_generator_start_from data/program_generator.py \ #用的是PG预测生成的输出
--num_iterations 100000 \
--checkpoint_path data/execution_engine.pt
(3)联合训练PG+EE【无gt联合微调】
python scripts/train_model.py \
--model_type PG+EE \
--program_generator_start_from data/program_generator.pt \
--execution_engine_start_from data/execution_engine.pt \
--checkpoint_path data/joint_pg_ee.pt
FiLM的单步训练,直接运行下面的语句就开始训练了。
sh scripts/train/film.sh
生成film.pt文件(模型),film.log文件,film.pt.json文件
4.测试
python scripts/run_model.py \
--program_generator data/program_generator.pt \
--execution_engine data/execution_engine.pt \
--input_question_h5 data/val_questions.h5 \
--input_features_h5 data/val_features.h5
联合微调版
python scripts/run_model.py \
--program_generator data/joint_pg_ee.pt \
--execution_engine data/joint_pg_ee.pt \
--input_question_h5 data/val_questions.h5 \
--input_features_h5 data/val_features.h5
网友评论