We present PICa, a simple yet effective method to use GPT-3 for knowledge-based VQA, demonstrating the first use of GPT-3 for multimodal tasks.
reference
[1] Zhengyuan Yang, Zhe Gan, Jianfeng Wang, Xiaowei Hu, Yumao Lu, Zicheng Liu, and Lijuan Wang. An empirical study of gpt-3 for few-shot knowledge-based vqa. In AAAI, pages 3081–3089, 2022
虽然是用于VQA领域的问题,但是也不妨细看一下。
进一步研究PICa发现,对于一次VQA预测,它使用如下两种策略:
In-context example selection(上下文示例选择)
Multi-query ensemble(多查询集成)
其中,上下文示例选择就是在问题之外给模型提供n个样例,这样模型的输出就和样例一致。多查询集成本质上就是多问几次,然后根据问的结果通过某种指标进行排序,得到排序最靠前的一个作为答案。
不难发现,上下文示例选择这个方法对于我的任务很有帮助。
我们定义一个函数 `simple_multimodal_conversation_call` 来与 Dashscope API 进行交互,并获取模型的响应。
```python
def simple_multimodal_conversation_call(img, question):
messages = [
{
"role": "user",
"content": [
{"image": f"{img}"},
{"text": f"{question}"}
]
}
]
response = dashscope.MultiModalConversation.call(model='qwen-vl-plus', messages=messages)
if response.status_code == HTTPStatus.OK:
return response.output.choices[0]['message']['content'][0]['text']
else:
print(response.code)
print(response.message)
return None
```
为了帮助模型更好地理解我们的需求,我们提供了一段背景描述和两个示例。
```python
background = """
略
"""example1 = """
样例1:
================
输入:1.png: 产品图 2.png: 图标 3.png: 另一个图标
输出:<positions><position><file>1.png</file><coords>(400,400)</coords><size>(500,500)</size></position><position><file>2.png</file><coords>(50,50)</coords><size>(150,150)</size></position></positions><texts><text><font_name>msyh.ttc</font_name><color>ffffff</color><size>36</size><coords>(150,150)</coords><content>清新透明</content></text><text><font_name>msyh.ttc</font_name><color>ffffff</color><size>24</size><coords>(150,200)</coords><content>纯净之选</content></text><text><font_name>msyh.ttc</font_name><color>ffffff</color><size>20</size><coords>(150,250)</coords><content>立即购买享受专属优惠</content></text></texts>
================
"""example2 = """
样例2:
================
输入:1.png: 产品图 2.png: 图标 3.png: 另一个图标
输出:<positions><position><file>1.png</file><coords>(300,300)</coords><size>(400,400)</size></position><position><file>3.png</file><coords>(100,100)</coords><size>(200,200)</size></position></positions><texts><text><font_name>arial.ttf</font_name><color>000000</color><size>48</size><coords>(200,200)</coords><content>精美设计</content></text><text><font_name>arial.ttf</font_name><color>000000</color><size>30</size><coords>(200,250)</coords><content>高端品质</content></text><text><font_name>arial.ttf</font_name><color>000000</color><size>24</size><coords>(200,300)</coords><content>立即购买</content></text></texts>
================
"""
```
我们定义一个问题模板,并将背景和示例组合成一个基础提示。
```python
question_template = """
问题1:
================
输入:1.png: 产品图 2.png: 图标 3.png: 另一个图标
输出:
================
"""prompt_base = background + example1 + example2
img = 'http://mms1.baidu.com/it/u=638848281,2512481837&fm=253&app=138&f=JPEG?w=500&h=556'
```
实现海报布局生成函数
实现一个函数 `generate_poster_layout`,它会调用模型生成海报布局,并分别截取 `<positions>` 和 `<texts>` 部分的内容。
```python
def generate_poster_layout(img, question):
prompt = prompt_base + question_template.format(question)
response = simple_multimodal_conversation_call(img, prompt)
if response:
positions_start = response.find("<positions>")
positions_end = response.find("</positions>") + len("</positions>")
texts_start = response.find("<texts>")
texts_end = response.find("</texts>") + len("</texts>")positions_content = response[positions_start:positions_end] if positions_start != -1 and positions_end != -1 else None
texts_content = response[texts_start:texts_end] if texts_start != -1 and texts_end != -1 else Nonereturn positions_content, texts_content
return None, None
```
6. 生成海报布局并输出结果
最后,我们调用 `generate_poster_layout` 函数生成海报布局,并分别输出 `<positions>` 和 `<texts>` 部分的内容。