Giter VIP home page Giter VIP logo

mkg_analogy's Introduction

MKG_Analogy

Code and datasets for the ICLR2023 paper "Multimodal Analogical Reasoning over Knowledge Graphs"

Quick links

Overview

In this work, we propose a new task of multimodal analogical reasoning over knowledge graph. A overview of the Multimodal Analogical Reasoning task can be seen as follows:

We provide a knowledge graph to support and further divide the task into single and blended patterns. Note that the relation marked by dashed arrows ($\dashrightarrow$) and the text around parentheses under images are only for annotation and not provided in the input.

Requirements

pip install -r requirements.txt

Data Collection and Preprocessing

To support the multimodal analogical reasoning task, we collect a multimodal knowledge graph dataset MarKG and a Multimodal Analogical ReaSoning dataset MARS. A visual outline of the data collection as shown in following figure:

We collect the datasets follow below steps:

  1. Collect Analogy Entities and Relations
  2. Link to Wikidata and Retrieve Neighbors
  3. Acquire and Validate Images
  4. Sample Analogical Reasoning Data

The statistics of the two datasets are shown in following figures:

We put the text data under MarT/dataset/, and the image data can be downloaded through the Google Drive or the Baidu Pan(TeraBox) (code:7hoc) and placed on MarT/dataset/MARS/images. Please refer to MarT for details.

The expected structure of files is:

MKG_Analogy
 |-- M-KGE	# multimodal knowledge representation methods
 |    |-- IKRL_TransAE   
 |    |-- RSME
 |-- MarT
 |    |-- data          # data process functions
 |    |-- dataset
 |    |    |-- MarKG    # knowledge graph data
 |    |    |-- MARS     # analogical reasoning data
 |    |-- lit_models    # pytorch_lightning models
 |    |-- models        # source code of models
 |    |-- scripts       # running scripts
 |    |-- tools         # tool function
 |    |-- main.py       # main function
 |-- resources   # image resources
 |-- requirements.txt
 |-- README.md

Evaluate on Benchmark Mehods

We select some baseline methods to establish the initial benchmark results on MARS, including multimodal knowledge representation methods (IKRL, TransAE, RSME), pre-trained vision-language models (VisualBERT, ViLBERT, ViLT, FLAVA) and a multimodal knowledge graph completion method (MKGformer).

In addition, we follow the structure-mapping theory to regard the Abudction-Mapping-Induction as explicit pipline steps for multimodal knowledge representation methods. As for transformer-based methods, we further propose MarT, a novel framework that implicitly combines these three steps to accomplish the multimodal analogical reasoning task end-to-end, which can avoid error propagation during analogical reasoning. The overview of the baseline methods can be seen in above figure.

Multimodal Knowledge Representation Methods

1. IKRL

We reproduce the IKRL models via TransAE framework, to evaluate on IKRL, running following code:

cd M-KGE/IKRL_TransAE
python IKRL.py

You can choose pre-train/fine-tune and TransE/ANALOGY by modifing finetune and analogy parameters in IKRL.py, respectively.

To evaluate on IKRL, running following code:

cd M-KGE/IKRL_TransAE
python TransAE.py

You can choose pre-train/fine-tune and TransE/ANALOGY by modifing finetune and analogy parameters in TransAE.py, respectively.

3. RSME

We only provide part of the data for RSME. To evaluate on RSME, you need to generate the full data by following scripts:

cd M-KGE/RSME
python image_encoder.py  # -> analogy_vit_best_img_vec.pickle
python utils.py          # -> img_vec_id_analogy_vit.pickle

Firstly, pre-train the models over MarKG:

bash run.sh

Then modify the --checkpoint parameter and fine-tune the models on MARS:

bash run_finetune.sh

More training details about the above models can refer to their offical repositories.

Transformer-based Methods

We leverage the MarT framework for transformer-based models. MarT contains two steps: pre-train and fine-tune.

To train the models fast, we encode the image data in advance with this script (Note that the size of the encoded data is about 7GB):

cd MarT
python tools/encode_images_data.py

Taking MKGformer as an example, first pre-train the model via following script:

bash scripts/run_pretrain_mkgformer.sh

After pre-training, fine-tune the model via following script:

bash scripts/run_finetune_mkgformer.sh

🍓 We provide the best checkpoints of transformer-based models during the fine-tuning and pre-training phrases at this Google Drive. Download them and add --only_test in scripts/run_finetune_xxx.sh for testing experiments.

Citation

If you use or extend our work, please cite the paper as follows:

@inproceedings{
zhang2023multimodal,
title={Multimodal Analogical Reasoning over Knowledge Graphs},
author={Ningyu Zhang and Lei Li and Xiang Chen and Xiaozhuan Liang and Shumin Deng and Huajun Chen},
booktitle={The Eleventh International Conference on Learning Representations },
year={2023},
url={https://openreview.net/forum?id=NRHajbzg8y0P}
}

mkg_analogy's People

Contributors

flow3rdown avatar zxlzr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mkg_analogy's Issues

Unable to download the data from Google Drive

Hi,

Thanks for this work! I was trying to download the data from Google Drive using the link provided,
and It seems to be downloading it fine till the end, but after it has finished it says "Failed - Forbidden".
Any idea why is it?

environment problem

老师您好 就是我在用您这个代码的时候,显示OSError: [WinError 193] %1 不是有效的 Win32 应用程序。错误,在网上查询后发现是虚拟环境要是32位,但是创建32位的虚拟环境后无法安装torch包。想请问您一下这个代码的环境应该是什么样的?

error

when I run the "main.py",line16, An error was prompted:AttributeError: module 'models' has no attribute 'RobertaUseLabelWord',what should I do?

运行TransAE.py出现的问题

尊敬的作者,您好!
当我在运行TransAE.py文件时,显示FileNotFoundError: [Errno 2] No such file or directory: 'embedding_weights/textembed_11292_100_2_40.pkl'. 我没有找到生成文本嵌入pkl格式的文件,请问您提供的代码中是否有生成文本嵌入的文件?如果您能指导,我将不胜感激。
此致
敬礼
bjut-zhangdabao
2024.3.25
附加问题截图:
problem

预训练和微调

尊敬的作者您好:
请问这样是不是复现成功,这个预训练的testing是541,微调的testing是11,想知道这个的含义
0d365b47a136099350c84e4831059db
6cbc818881f78123ace0c730f75d5cb

Lack of pickle files

Thank you for your amazing work!
When I was running the image_encoder.py, I encountered a problem like this:

image

If it is convenient for you could you please give me some hint about this problem?

Thank you in advance!

reproducibility problem

Hi,thanks for this work!
I‘m trying to reproduce the results on my PC(ubuntu18,torch2.0.1),but it seems to be strange.Taking hits@10 on mkgformer for example:

  • pretrain and finetune: 0.3289
  • finetune based on yout checkpoint "pretrain_mkgformer.ckpt": 0.4001
  • only test your checkpoint "ft_mart_mkgformer.ckpt": 0.3789
  • in your paper: 0.408
    all Hyperparameters are the same as your codes except the patience for earlyStopping increase from 5 to 10.
    Any idea why is it?

Enviromental problem

您好,麻烦问一下跑这个project你们用的windows还是Linux,我mac运行的时候出现了一些问题,windows也是。

在IKRL.py中的代码问题

尊敬的作者您好,我在运行MKG_Analogy-main\M-KGE\IKRL_TransAE\IKRL.py文件时,该文件的第420行代码:
with open("data/analogy-img/" + entity + "/avg_embedding.pkl", "rb") as visef:
文件中并没有avg_embedding.pkl,请问:avg_embedding.pkl您能否提供一下,我的邮箱是([email protected]),如果可以我将不胜感激。祝您科研顺利!!!

fine-tune error

您好,在我下载GoogleDrive提供的ckpt后,复现微调任务时出现报错,好像是ckpt的维度与模型不匹配。

image

Image path problem

Sorry again for bothering you. When I was running image_encoder.py, there is a weird problem:

image

I checked the path in the program but I can't find some information...
If it's convenient for you could you give me some hint for it?

Thanks in advance!

KeyError:'label'

作者您好,抱歉打扰,我在运行 bash scripts/run_pretrain_mkgformer.sh 时,遇到这样的问题:
File "/home/zzz/zzz/miniconda3/envs/base_G/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 652, in next
data = self._next_data()
File "/home/zzz/zzz/miniconda3/envs/base_G/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 692, in _next_data
data = self._dataset_fetcher.fetch(index) # may raise StopIteration
File "/home/zzz/zzz/miniconda3/envs/base_G/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 52, in fetch
return self.collate_fn(data)
File "/mnt/dataX/zzz/www/MKG_Analogy-main/MarT/data/data_module.py", line 98, in call
label = [feature.pop("label") for feature in features]
File "/mnt/dataX/zzz/www/MKG_Analogy-main/MarT/data/data_module.py", line 98, in
label = [feature.pop("label") for feature in features]
KeyError: 'label'
Epoch 0: 67%|██████▋ | 1042/1562 [11:29<05:43, 1.51it/s, loss=10.5, v_num=12]

把data_module.py,line98行改成这样:
label = [feature.pop("label") for feature in features] if "label" in features[0].keys() else None
它仍然报错:
File "/home/zzz/zzz/miniconda3/envs/base_G/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 652, in next
data = self._next_data()
File "/home/zzz/zzz/miniconda3/envs/base_G/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 692, in _next_data
data = self._dataset_fetcher.fetch(index) # may raise StopIteration
File "/home/zzz/zzz/miniconda3/envs/base_G/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 52, in fetch
return self.collate_fn(data)
File "/mnt/dataX/zzz/www/MKG_Analogy-main/MarT/data/data_module.py", line 130, in call
for head, tail in zip(head_ent, tail_ent):
TypeError: 'NoneType' object is not iterable
Epoch 0: 67%|██████▋ | 1042/1562 [11:31<05:44, 1.51it/s, loss=10.5, v_num=13]

找不到原因
您方便的时候能解答一下吗?

Reproduce paper accuracy

参照之前问题,我已在原论文更改如下代码。这里也有个问题,为何更改这个代码后,提升精度那么大?

#features['sep_idx'] = torch.tensor(sep_idx)
features['attention_mask'] = features['attention_mask'].unsqueeze(1).expand([features['input_ids'].size(0), features['input_ids'].size(1), features['input_ids'].size(1)]).clone()
for i, idx in enumerate(sep_idx):
features['attention_mask'][i, :idx[2], idx[2]:] = 0

按照说明运行mkgformer.sh文件,这里以hits@10为指标评价,展示以下结果:

1.自己预训练MarKG,结果为0.71,论文附录的表9为0.779;基于这个预训练再去微调MARS,结果为0.331,论文表2为0.408.
2.运行checkpoint "pretrain_mkgformer.ckpt",结果为0.779,与论文相同;基于"pretrain_mkgformer.ckpt"微调MARS,结果为0.402.
3.直接测试checkpoint "ft_mart_mkgformer.ckpt",结果为0.408,与论文相同。

所以有一些疑问,请您麻烦解答一下,非常感谢。
对于结果1,自己按照给定的sh文件预训练MarKG,为何仅为0.71.
对于结果2,在checkpoint "pretrain_mkgformer.ckpt"微调MARS,结果0.402,随机种子设置是一样的,但还是和论文的0.408差一点。

environment problem

Hello, I am a newbee, and trying to build your project in local, but confronted with the problem below:

I follow the README and run python IKRL.py, but it prompt these:
图片

I target the break point at this function call, probably, which is in TrainDataLoader.py
图片
图片

I guess it may have some problem to do with Base.so. Do you have any hint how to resolve this?

Lower results

Nice work! But I reproduce the program's results based on pretrained checkpoints, which are lower than those mentioned in the paper. I would like to know which part of the reason is causing this? And when I saw the image encoding features, the code used a random sampling process. Will it affect the results? Thanks!
image

Requirements issue

您好,打扰了。我在pip requirements的时候,出现了could not find torchvision==0.8.0, torch=1.7.0等 (只有一些其他版本)问题,麻烦问一下这种情况我应该怎么解决,安装其他版本的是否可行。希望能得到您的回复与帮助,谢谢!

error

There was an error when I ran cd MarT/tools/encode_images_data.py:
Can't load image processor for '/zjunlp/lilei/PLMs/clip-vit-base-patch32'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure '/zjunlp/lilei/PLMs/clip-vit-base-patch32' is the correct path to a directory containing a preprocessor_config.json file
I also couldn't find '/zjunlp/lilei/PLMs/clip-vit-base-patch32' on huggingface.
What shuold I do?

运行IKRL.py出现问题

尊敬的作者您好,我在运行完visual_embed.py后运行IKRL.py时出现如图所示的问题
ZSX3I1%D7EYBMB5)PRI_F)V

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.