ALBEF图文检索
作者:互联网
代码地址https://github.com/salesforce/ALBEF.git
目的使用少量的图片,训练模型。
准备文件(1)
/Users/xuehuiping/git/ALBEF/configs/Retrieval_flickr.yaml
train_file: ['/Users/xuehuiping/dataset/flickr_sample/flickr30k_train.json']
val_file: '/Users/xuehuiping/dataset/flickr_sample/flickr30k_val.json'
test_file: '/Users/xuehuiping/dataset/flickr_sample/flickr30k_test.json'
image_root: '/Users/xuehuiping/dataset/flickr_sample/'
bert_config: 'configs/config_bert.json'
image_res: 384
batch_size_train: 4 #32
batch_size_test: 8 #64
queue_size: 65536
momentum: 0.995
vision_width: 768
embed_dim: 256
temp: 0.07
k_test: 128
alpha: 0.4
distill: True
warm_up: True
optimizer: {opt: adamW, lr: 1e-5, weight_decay: 0.02}
schedular: {sched: cosine, lr: 1e-5, epochs: 10, min_lr: 1e-6, decay_rate: 1, warmup_lr: 1e-5, warmup_epochs: 1, cooldown_epochs: 0}
数据目录截图
图片路径写到flickr30k-images所在目录即可,不需要包括flickr30k-images。
test.json的格式是:一张图片对应5个caption
val.json也是这个格式。
{
"image": "flickr30k-images/183647966.jpg",
"caption": [
"A man waring a polo shirt fixes a ticket machine while persons line up at an adjoining machine.",
"A woman dressed in black with shopping bags is waiting on the sidewalk.",
"A male worker with his tool box is kneeling next to two women.",
"A man working on a ticket machine as two women stand near.",
"A man working on a ticket box."
]
},
train.json的格式是:一张图片对应1个caption
{
"image": "flickr30k-images/1000092795.jpg",
"caption": "Two young guys with shaggy hair look at their hands while hanging out in the yard.",
"image_id": 0
},
运行脚本
python Retrieval.py \
--config ./configs/Retrieval_flickr.yaml \
--output_dir output/Retrieval_flickr_sample \
Retrieval.py主要包含
(1)创建数据集create_dataset
(2)创建数据加载器create_loader
(3)创建模型model = ALBEF()
(4)创建优化器create_optimizer
(5)开始迭代:train、evaluation
部分日志
……
Train Epoch: [9] [ 0/362] eta: 0:03:35 lr: 0.000001 loss_itm: 0.4073 loss_ita: 7.9941 time: 0.5952 data: 0.2224 max mem: 8621
Train Epoch: [9] [ 50/362] eta: 0:01:44 lr: 0.000001 loss_itm: 0.4031 loss_ita: 8.4971 time: 0.3288 data: 0.0001 max mem: 8621
Train Epoch: [9] [100/362] eta: 0:01:26 lr: 0.000001 loss_itm: 0.6508 loss_ita: 8.5376 time: 0.3291 data: 0.0001 max mem: 8621
Train Epoch: [9] [150/362] eta: 0:01:10 lr: 0.000001 loss_itm: 0.3102 loss_ita: 7.9713 time: 0.3301 data: 0.0001 max mem: 8621
Train Epoch: [9] [200/362] eta: 0:00:53 lr: 0.000001 loss_itm: 0.5846 loss_ita: 8.0293 time: 0.3289 data: 0.0001 max mem: 8621
Train Epoch: [9] [250/362] eta: 0:00:37 lr: 0.000001 loss_itm: 0.2595 loss_ita: 7.7533 time: 0.3299 data: 0.0001 max mem: 8621
Train Epoch: [9] [300/362] eta: 0:00:20 lr: 0.000001 loss_itm: 0.4346 loss_ita: 8.0876 time: 0.3306 data: 0.0002 max mem: 8621
Train Epoch: [9] [350/362] eta: 0:00:03 lr: 0.000001 loss_itm: 0.5066 loss_ita: 8.3133 time: 0.3303 data: 0.0001 max mem: 8621
Train Epoch: [9] [361/362] eta: 0:00:00 lr: 0.000001 loss_itm: 0.3811 loss_ita: 8.0253 time: 0.3280 data: 0.0001 max mem: 8621
Train Epoch: [9] Total time: 0:01:59 (0.3305 s / it)
Averaged stats: lr: 0.0000 loss_itm: 0.4907 loss_ita: 8.1612
Computing features for evaluation...
Evaluation: [ 0/100] eta: 0:00:01 time: 0.0116 data: 0.0003 max mem: 8621
Evaluation: [ 50/100] eta: 0:00:00 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [ 99/100] eta: 0:00:00 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: Total time: 0:00:01 (0.0117 s / it)
Evaluation: [ 0/500] eta: 0:00:05 time: 0.0102 data: 0.0008 max mem: 8621
Evaluation: [ 50/500] eta: 0:00:05 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [100/500] eta: 0:00:04 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [150/500] eta: 0:00:04 time: 0.0117 data: 0.0000 max mem: 8621
Evaluation: [200/500] eta: 0:00:03 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [250/500] eta: 0:00:02 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [300/500] eta: 0:00:02 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [350/500] eta: 0:00:01 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [400/500] eta: 0:00:01 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [450/500] eta: 0:00:00 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [499/500] eta: 0:00:00 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: Total time: 0:00:05 (0.0116 s / it)
Evaluation time 0:00:08
Computing features for evaluation...
Evaluation: [ 0/100] eta: 0:00:01 time: 0.0119 data: 0.0004 max mem: 8621
Evaluation: [ 50/100] eta: 0:00:00 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [ 99/100] eta: 0:00:00 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: Total time: 0:00:01 (0.0117 s / it)
Evaluation: [ 0/500] eta: 0:00:05 time: 0.0106 data: 0.0009 max mem: 8621
Evaluation: [ 50/500] eta: 0:00:05 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [100/500] eta: 0:00:04 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [150/500] eta: 0:00:04 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [200/500] eta: 0:00:03 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [250/500] eta: 0:00:02 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [300/500] eta: 0:00:02 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [350/500] eta: 0:00:01 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [400/500] eta: 0:00:01 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: [450/500] eta: 0:00:00 time: 0.0117 data: 0.0000 max mem: 8621
Evaluation: [499/500] eta: 0:00:00 time: 0.0116 data: 0.0000 max mem: 8621
Evaluation: Total time: 0:00:05 (0.0116 s / it)
Evaluation time 0:00:08
{'txt_r1': 5.0, 'txt_r5': 19.0, 'txt_r10': 21.0, 'txt_r_mean': 15.0, 'img_r1': 4.2, 'img_r5': 15.6, 'img_r10': 23.6, 'img_r_mean': 14.466666666666669, 'r_mean': 14.733333333333334}
{'txt_r1': 4.0, 'txt_r5': 19.0, 'txt_r10': 24.0, 'txt_r_mean': 15.666666666666666, 'img_r1': 2.6, 'img_r5': 16.6, 'img_r10': 26.2, 'img_r_mean': 15.133333333333335, 'r_mean': 15.4}
Training time 0:24:54
Evaluation运行2次,是因为eval运行一次,test运行一次。
以上是用ALBEF代码对Image Retrieval任务的简单运行,数据量只有很少,最后的指标只是示意。
标签:检索,00,mem,max,8621,eta,time,ALBEF,图文 来源: https://www.cnblogs.com/xuehuiping/p/16128518.html