# MatCha

## 概要

MatChaは、Fangyu Liu、Francesco Piccinno、Syrine Krichene、Chenxi Pang、Kenton Lee、Mandar Joshi、Yasemin Altun、Nigel Collier、Julian Martin Eisenschlos による論文 [MatCha: Enhancing Visual Language Pretraining with Math Reasoning and Chart Derendering](https://huggingface.co/papers/2212.09662) で提案されました。

この論文の要旨には、次のように記載されています。

*プロット、チャート、インフォグラフィックなどの視覚言語データは、人間の社会に広く存在しています。しかし、最先端の視覚言語モデルは、これらのデータに対して高い性能を発揮していません。私たちは、視覚言語モデルがチャート/プロットと言語データを同時にモデル링する能力を高めるために、MatCha（Math reasoning and Chart derendering pretraining）を提案します。具体的には、視覚言語モデリングにおける重要な能力であるプロットの分解と数値推論をカバーするいくつかの事前学習タスクを提案します。MatChaの事前学習は、最近提案された画像からテキストへの視覚言語モデルであるPix2Structから開始します。PlotQAやChartQAなどの標準ベンチマークにおいて、MatChaモデルは最先端の手法を最大で約20%上回る性能を示しました。さらに、スクリーンショット、教科書の図、文書の図などのドメインへのMatCha事前学習の転移を調査し、全体的な性能向上を観察しました。これにより、より幅広い視覚言語タスクにおけるMatCha事前学習の有効性が検証されました。*

## モデルの説明

MatCha は `Pix2Struct` アーキテクチャを使用して学習されたモデルです。`Pix2Struct` に関する詳細は、[Pix2Struct のドキュメント](./pix2struct)を参照してください。MatCha は `Pix2Struct` アーキテクチャの Visual Question Answering サブセットです。入力された質問を画像上にレンダリングし、答えを予測します。

## 使用方法

現在、MatChaには6つのチェックポイントが利用可能です:

- `google/matcha`: MatChaのベースモデル。MatChaをダウンストリームタスクにファインチューニングするために使用します
- `google/matcha-chartqa`: ChartQAデータセットでファインチューニングされたMatChaモデルです。チャートに関する質問に回答するために使用できます。
- `google/matcha-plotqa-v1`: PlotQAデータセットでファインチューニングされたMatChaモデルです。プロットに関する質問に回答するために使用できます。
- `google/matcha-plotqa-v2`: PlotQAデータセットでファインチューニングされたMatChaモデルです。プロットに関する質問に回答するために使用できます。
- `google/matcha-chart2text-statista`: Statista データセットでファインチューニングされた MatCha モデル。
- `google/matcha-chart2text-pew`: PewデータセットでファインチューニングされたMatChaモデル。

`chart2text-pew` と `chart2text-statista` でファインチューニングされたモデルは要約に適しており、`plotqa` と `chartqa` でファインチューニングされたモデルは質問応答に適しています。

これらのモデルは、次のように使用できます（ChartQAデータセットの例）：

```python
import requests
from PIL import Image

from transformers import AutoProcessor, Pix2StructForConditionalGeneration

model = Pix2StructForConditionalGeneration.from_pretrained("google/matcha-chartqa").to(0, device_map="auto")
processor = AutoProcessor.from_pretrained("google/matcha-chartqa")
url = "https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/val/png/20294671002019.png"
image = Image.open(requests.get(url, stream=True).raw)

inputs = processor(images=image, text="Is the sum of all 4 places greater than Laos?", return_tensors="pt").to(0)
predictions = model.generate(**inputs, max_new_tokens=512)
print(processor.decode(predictions[0], skip_special_tokens=True))
```

## ファインチューニング

MatChaのファインチューニングについては、pix2structの[ファインチューニングノートブック](https://github.com/huggingface/notebooks/blob/main/examples/image_captioning_pix2struct.ipynb)を参照してください。`Pix2Struct`モデルについては、Adafactorとコサイン学習率スケジューラーを用いてモデルをファインチューニングすると、収束が速まることが確認されています:

```python
from transformers.optimization import Adafactor, get_cosine_schedule_with_warmup

optimizer = Adafactor(self.parameters(), scale_parameter=False, relative_step=False, lr=0.01, weight_decay=1e-05)
scheduler = get_cosine_schedule_with_warmup(optimizer, num_warmup_steps=1000, num_training_steps=40000)
```

MatCha は、`Pix2Struct` アーキテクチャを使用して学習されたモデルです。`Pix2Struct` に関する詳細は、[Pix2Struct のドキュメント](./pix2struct)を参照してください。

