Update README.md
Browse files
README.md
CHANGED
|
@@ -1,156 +1,156 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
---
|
| 4 |
-
# Metom (
|
| 5 |
-
|
| 6 |
-
The **Metom** is a Vision Transformer (ViT) based **Kuzushiji** classifier.
|
| 7 |
-
The model takes an image with one character and returns what the character is.
|
| 8 |
-
**This model is not an official SakanaAI product and is for research / educational purposes only.**
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
モデルは1文字が写った画像を受け取り、その文字がどの文字であるかを返します。
|
| 12 |
-
**本モデルはSakanaAIの公式製品ではありません。研究・教育目的のみに利用してください。**
|
| 13 |
-
|
| 14 |
-
*Japanese section follows English section (日本語セクションは英語セクションの後に続きます。)*
|
| 15 |
-
|
| 16 |
-
--------------------------------------------------------------------------------
|
| 17 |
-
|
| 18 |
-
This model was trained by using [日本古典籍くずし字データセット](http://codh.rois.ac.jp/char-shape/book/).
|
| 19 |
-
This dataset contains 1,086,326 characters in 4,328 types of Kuzushiji.
|
| 20 |
-
However, we used only 2,703 types of characters that appeared at least 5 times in the dataset.
|
| 21 |
-
|
| 22 |
-
The dataset was split into train, validation, and test subsets in a ratio of 3:1:1.
|
| 23 |
-
As a result, the train subset contained 649,932 characters, the validation subset contained 216,644 characters, and the test subset contained 216,645 characters.
|
| 24 |
-
|
| 25 |
-
The model was trained on the train subset, and hyperparameters were tuned based on the performance on the validation subset.
|
| 26 |
-
The final evaluation on the test subset yielded a micro accuracy of 0.9722 and a macro accuracy of 0.8354.
|
| 27 |
-
|
| 28 |
-
## Usage
|
| 29 |
-
Please see also [Google Colab Notebook](https://colab.research.google.com/drive/1jFMZENoTjjum3qlBxV0Q5dTxmpCvqlpf?usp=sharing).
|
| 30 |
-
1. Install dependencies (Not required on Google Colab)
|
| 31 |
-
```sh
|
| 32 |
-
python -m pip install einops torch torchvision transformers
|
| 33 |
-
|
| 34 |
-
# Optional (This is also required on Google Colab if you want to use FlashAttention-2)
|
| 35 |
-
pip install flash-attn --no-build-isolation
|
| 36 |
-
```
|
| 37 |
-
|
| 38 |
-
2. Run the following code
|
| 39 |
-
```python
|
| 40 |
-
from io import BytesIO
|
| 41 |
-
|
| 42 |
-
from PIL import Image
|
| 43 |
-
import requests
|
| 44 |
-
import torch
|
| 45 |
-
from transformers import AutoModel, AutoProcessor
|
| 46 |
-
|
| 47 |
-
repo_name = "SakanaAI/Metom"
|
| 48 |
-
device = "cuda"
|
| 49 |
-
torch_dtype = torch.float32 # This can also set `torch.float16` or `torch.bfloat16`
|
| 50 |
-
|
| 51 |
-
def get_image(image_url: str) -> Image.Image:
|
| 52 |
-
return Image.open(BytesIO(requests.get(image_url).content)).convert("RGB")
|
| 53 |
-
|
| 54 |
-
processor = AutoProcessor.from_pretrained(repo_name, trust_remote_code=True)
|
| 55 |
-
model = AutoModel.from_pretrained(
|
| 56 |
-
repo_name,
|
| 57 |
-
torch_dtype=torch_dtype,
|
| 58 |
-
_attn_implementation="eager", # This can also set `"sdpa"` or `"flash_attention_2"`
|
| 59 |
-
trust_remote_code=True
|
| 60 |
-
).to(device=device)
|
| 61 |
-
|
| 62 |
-
image1 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example1_4E00.jpg") # An example image
|
| 63 |
-
image_array1 = processor(images=image1, return_tensors="pt")["pixel_values"].to(device=device, dtype=torch_dtype)
|
| 64 |
-
with torch.inference_mode():
|
| 65 |
-
print(model.get_predictions(image_array1)) # Returns the prediction label
|
| 66 |
-
# ['一']
|
| 67 |
-
|
| 68 |
-
image2 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example2_5B9A.jpg") # An example image
|
| 69 |
-
image3 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example3_5009.jpg") # An example image
|
| 70 |
-
image_array2 = processor(images=[image2, image3], return_tensors="pt")["pixel_values"].to(device=device, dtype=torch_dtype)
|
| 71 |
-
with torch.inference_mode():
|
| 72 |
-
print(model.get_topk_labels(image_array2)) # Returns top-k prediction labels (label only)
|
| 73 |
-
# [['定', '芝', '乏', '淀', '実'], ['倉', '衾', '斜', '会', '急']]
|
| 74 |
-
print(model.get_topk_labels(image_array2, k=3, return_probs=True)) # Returns prediction top-k labels (label with probability)
|
| 75 |
-
# [[('定', 0.9979104399681091), ('芝', 0.0002953427319880575), ('乏', 0.00012814522779081017)], [('倉', 0.9862521290779114), ('衾', 0.0005956474924460053), ('斜', 0.00039981433656066656)]]
|
| 76 |
-
```
|
| 77 |
-
|
| 78 |
-
## Citation
|
| 79 |
-
```bibtex
|
| 80 |
-
@misc{Metom,
|
| 81 |
-
url = {[https://huggingface.co/SakanaAI/Metom](https://huggingface.co/SakanaAI/Metom)},
|
| 82 |
-
title = {Metom},
|
| 83 |
-
author = {Imajuku, Yuki and Clanuwat, Tarin}
|
| 84 |
-
}
|
| 85 |
-
```
|
| 86 |
-
|
| 87 |
-
--------------------------------------------------------------------------------
|
| 88 |
-
|
| 89 |
-
本モデルは[日本古典籍くずし字データセット](http://codh.rois.ac.jp/char-shape/book/)を用いて訓練されました。
|
| 90 |
-
このデータセットは4,328種1,086,326枚のくずし字画像が含まれています。
|
| 91 |
-
ですが、データセット中に最低5回以上出現する2,703種類の文字のみを利用しました。
|
| 92 |
-
|
| 93 |
-
データセットは訓練、検証、テストの3つのセットに、比率が3:1:1となるように分割されました。
|
| 94 |
-
その結果、訓練セットは649,932枚、検証セットは216,644枚、テストセットは216,645枚、画像が含まれました。
|
| 95 |
-
|
| 96 |
-
本モデルは訓練セットのみを用いて学習され、検証セットにおける性能を見ながらハイパーパラメータを調整しました。
|
| 97 |
-
最終的にテストセットにおける評価の結果、216,645枚全体の正解率は0.9722となり、2,703種類のクラス別正解率の平均は0.8354となりました。
|
| 98 |
-
|
| 99 |
-
## 使用方法
|
| 100 |
-
[Google Colab Notebook](https://colab.research.google.com/drive/1jFMZENoTjjum3qlBxV0Q5dTxmpCvqlpf?usp=sharing)もご確認ください。
|
| 101 |
-
1. 依存ライブラリをインストールする (Google Colabを使う場合は不要)
|
| 102 |
-
```sh
|
| 103 |
-
python -m pip install einops torch torchvision transformers
|
| 104 |
-
|
| 105 |
-
# 任意 (FlashAttention-2を使いたい場合はGoogle Colabを使う時でも必要)
|
| 106 |
-
pip install flash-attn --no-build-isolation
|
| 107 |
-
```
|
| 108 |
-
|
| 109 |
-
2. 以下のコードを実行する
|
| 110 |
-
```python
|
| 111 |
-
from io import BytesIO
|
| 112 |
-
|
| 113 |
-
from PIL import Image
|
| 114 |
-
import requests
|
| 115 |
-
import torch
|
| 116 |
-
from transformers import AutoModel, AutoProcessor
|
| 117 |
-
|
| 118 |
-
repo_name = "SakanaAI/Metom"
|
| 119 |
-
device = "cuda"
|
| 120 |
-
torch_dtype = torch.float32 # `torch.float16` や `torch.bfloat16` も指定可能
|
| 121 |
-
|
| 122 |
-
def get_image(image_url: str) -> Image.Image:
|
| 123 |
-
return Image.open(BytesIO(requests.get(image_url).content)).convert("RGB")
|
| 124 |
-
|
| 125 |
-
processor = AutoProcessor.from_pretrained(repo_name, trust_remote_code=True)
|
| 126 |
-
model = AutoModel.from_pretrained(
|
| 127 |
-
repo_name,
|
| 128 |
-
torch_dtype=torch_dtype,
|
| 129 |
-
_attn_implementation="eager", # `"sdpa"` や `"flash_attention_2"` も指定可能
|
| 130 |
-
trust_remote_code=True
|
| 131 |
-
).to(device=device)
|
| 132 |
-
|
| 133 |
-
image1 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example1_4E00.jpg") # 画像例
|
| 134 |
-
image_array1 = processor(images=image1, return_tensors="pt")["pixel_values"].to(device=device, dtype=torch_dtype)
|
| 135 |
-
with torch.inference_mode():
|
| 136 |
-
print(model.get_predictions(image_array1)) # 予測ラベルを返す
|
| 137 |
-
# ['一']
|
| 138 |
-
|
| 139 |
-
image2 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example2_5B9A.jpg") # 画像例
|
| 140 |
-
image3 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example3_5009.jpg") # 画像例
|
| 141 |
-
image_array2 = processor(images=[image2, image3], return_tensors="pt")["pixel_values"].to(device=device, dtype=torch_dtype)
|
| 142 |
-
with torch.inference_mode():
|
| 143 |
-
print(model.get_topk_labels(image_array2)) # 上位k件の予測ラベルを返す (ラベルのみ)
|
| 144 |
-
# [['定', '芝', '乏', '淀', '実'], ['倉', '衾', '斜', '会', '急']]
|
| 145 |
-
print(model.get_topk_labels(image_array2, k=3, return_probs=True)) # 上位k件の予測ラベルを返す (ラベルと確率)
|
| 146 |
-
# [[('定', 0.9979104399681091), ('芝', 0.0002953427319880575), ('乏', 0.00012814522779081017)], [('倉', 0.9862521290779114), ('衾', 0.0005956474924460053), ('斜', 0.00039981433656066656)]]
|
| 147 |
-
```
|
| 148 |
-
|
| 149 |
-
## 引用
|
| 150 |
-
```bibtex
|
| 151 |
-
@misc{Metom,
|
| 152 |
-
url = {[https://huggingface.co/SakanaAI/Metom](https://huggingface.co/SakanaAI/Metom)},
|
| 153 |
-
title = {Metom},
|
| 154 |
-
author = {Imajuku, Yuki and Clanuwat, Tarin}
|
| 155 |
-
}
|
| 156 |
-
```
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
---
|
| 4 |
+
# Metom (めとむ)
|
| 5 |
+
|
| 6 |
+
The **Metom** is a Vision Transformer (ViT) based **Kuzushiji** classifier.
|
| 7 |
+
The model takes an image with one character and returns what the character is.
|
| 8 |
+
**This model is not an official SakanaAI product and is for research / educational purposes only.**
|
| 9 |
+
|
| 10 |
+
**めとむ**は Vision Transformer (ViT) ベースの**くずし字**分類器です。
|
| 11 |
+
モデルは1文字が写った画像を受け取り、その文字がどの文字であるかを返します。
|
| 12 |
+
**本モデルはSakanaAIの公式製品ではありません。研究・教育目的のみに利用してください。**
|
| 13 |
+
|
| 14 |
+
*Japanese section follows English section (日本語セクションは英語セクションの後に続きます。)*
|
| 15 |
+
|
| 16 |
+
--------------------------------------------------------------------------------
|
| 17 |
+
|
| 18 |
+
This model was trained by using [日本古典籍くずし字データセット](http://codh.rois.ac.jp/char-shape/book/).
|
| 19 |
+
This dataset contains 1,086,326 characters in 4,328 types of Kuzushiji.
|
| 20 |
+
However, we used only 2,703 types of characters that appeared at least 5 times in the dataset.
|
| 21 |
+
|
| 22 |
+
The dataset was split into train, validation, and test subsets in a ratio of 3:1:1.
|
| 23 |
+
As a result, the train subset contained 649,932 characters, the validation subset contained 216,644 characters, and the test subset contained 216,645 characters.
|
| 24 |
+
|
| 25 |
+
The model was trained on the train subset, and hyperparameters were tuned based on the performance on the validation subset.
|
| 26 |
+
The final evaluation on the test subset yielded a micro accuracy of 0.9722 and a macro accuracy of 0.8354.
|
| 27 |
+
|
| 28 |
+
## Usage
|
| 29 |
+
Please see also [Google Colab Notebook](https://colab.research.google.com/drive/1jFMZENoTjjum3qlBxV0Q5dTxmpCvqlpf?usp=sharing).
|
| 30 |
+
1. Install dependencies (Not required on Google Colab)
|
| 31 |
+
```sh
|
| 32 |
+
python -m pip install einops torch torchvision transformers
|
| 33 |
+
|
| 34 |
+
# Optional (This is also required on Google Colab if you want to use FlashAttention-2)
|
| 35 |
+
pip install flash-attn --no-build-isolation
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
2. Run the following code
|
| 39 |
+
```python
|
| 40 |
+
from io import BytesIO
|
| 41 |
+
|
| 42 |
+
from PIL import Image
|
| 43 |
+
import requests
|
| 44 |
+
import torch
|
| 45 |
+
from transformers import AutoModel, AutoProcessor
|
| 46 |
+
|
| 47 |
+
repo_name = "SakanaAI/Metom"
|
| 48 |
+
device = "cuda"
|
| 49 |
+
torch_dtype = torch.float32 # This can also set `torch.float16` or `torch.bfloat16`
|
| 50 |
+
|
| 51 |
+
def get_image(image_url: str) -> Image.Image:
|
| 52 |
+
return Image.open(BytesIO(requests.get(image_url).content)).convert("RGB")
|
| 53 |
+
|
| 54 |
+
processor = AutoProcessor.from_pretrained(repo_name, trust_remote_code=True)
|
| 55 |
+
model = AutoModel.from_pretrained(
|
| 56 |
+
repo_name,
|
| 57 |
+
torch_dtype=torch_dtype,
|
| 58 |
+
_attn_implementation="eager", # This can also set `"sdpa"` or `"flash_attention_2"`
|
| 59 |
+
trust_remote_code=True
|
| 60 |
+
).to(device=device)
|
| 61 |
+
|
| 62 |
+
image1 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example1_4E00.jpg") # An example image
|
| 63 |
+
image_array1 = processor(images=image1, return_tensors="pt")["pixel_values"].to(device=device, dtype=torch_dtype)
|
| 64 |
+
with torch.inference_mode():
|
| 65 |
+
print(model.get_predictions(image_array1)) # Returns the prediction label
|
| 66 |
+
# ['一']
|
| 67 |
+
|
| 68 |
+
image2 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example2_5B9A.jpg") # An example image
|
| 69 |
+
image3 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example3_5009.jpg") # An example image
|
| 70 |
+
image_array2 = processor(images=[image2, image3], return_tensors="pt")["pixel_values"].to(device=device, dtype=torch_dtype)
|
| 71 |
+
with torch.inference_mode():
|
| 72 |
+
print(model.get_topk_labels(image_array2)) # Returns top-k prediction labels (label only)
|
| 73 |
+
# [['定', '芝', '乏', '淀', '実'], ['倉', '衾', '斜', '会', '急']]
|
| 74 |
+
print(model.get_topk_labels(image_array2, k=3, return_probs=True)) # Returns prediction top-k labels (label with probability)
|
| 75 |
+
# [[('定', 0.9979104399681091), ('芝', 0.0002953427319880575), ('乏', 0.00012814522779081017)], [('倉', 0.9862521290779114), ('衾', 0.0005956474924460053), ('斜', 0.00039981433656066656)]]
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
## Citation
|
| 79 |
+
```bibtex
|
| 80 |
+
@misc{Metom,
|
| 81 |
+
url = {[https://huggingface.co/SakanaAI/Metom](https://huggingface.co/SakanaAI/Metom)},
|
| 82 |
+
title = {Metom},
|
| 83 |
+
author = {Imajuku, Yuki and Clanuwat, Tarin}
|
| 84 |
+
}
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
--------------------------------------------------------------------------------
|
| 88 |
+
|
| 89 |
+
本モデルは[日本古典籍くずし字データセット](http://codh.rois.ac.jp/char-shape/book/)を用いて訓練されました。
|
| 90 |
+
このデータセットは4,328種1,086,326枚のくずし字画像が含まれています。
|
| 91 |
+
ですが、データセット中に最低5回以上出現する2,703種類の文字のみを利用しました。
|
| 92 |
+
|
| 93 |
+
データセットは訓練、検証、テストの3つのセットに、比率が3:1:1となるように分割されました。
|
| 94 |
+
その結果、訓練セットは649,932枚、検証セットは216,644枚、テストセットは216,645枚、画像が含まれました。
|
| 95 |
+
|
| 96 |
+
本モデルは訓練セットのみを用いて学習され、検証セットにおける性能を見ながらハイパーパラメータを調整しました。
|
| 97 |
+
最終的にテストセットにおける評価の結果、216,645枚全体の正解率は0.9722となり、2,703種類のクラス別正解率の平均は0.8354となりました。
|
| 98 |
+
|
| 99 |
+
## 使用方法
|
| 100 |
+
[Google Colab Notebook](https://colab.research.google.com/drive/1jFMZENoTjjum3qlBxV0Q5dTxmpCvqlpf?usp=sharing)もご確認ください。
|
| 101 |
+
1. 依存ライブラリをインストールする (Google Colabを使う場合は不要)
|
| 102 |
+
```sh
|
| 103 |
+
python -m pip install einops torch torchvision transformers
|
| 104 |
+
|
| 105 |
+
# 任意 (FlashAttention-2を使いたい場合はGoogle Colabを使う時でも必要)
|
| 106 |
+
pip install flash-attn --no-build-isolation
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
2. 以下のコードを実行する
|
| 110 |
+
```python
|
| 111 |
+
from io import BytesIO
|
| 112 |
+
|
| 113 |
+
from PIL import Image
|
| 114 |
+
import requests
|
| 115 |
+
import torch
|
| 116 |
+
from transformers import AutoModel, AutoProcessor
|
| 117 |
+
|
| 118 |
+
repo_name = "SakanaAI/Metom"
|
| 119 |
+
device = "cuda"
|
| 120 |
+
torch_dtype = torch.float32 # `torch.float16` や `torch.bfloat16` も指定可能
|
| 121 |
+
|
| 122 |
+
def get_image(image_url: str) -> Image.Image:
|
| 123 |
+
return Image.open(BytesIO(requests.get(image_url).content)).convert("RGB")
|
| 124 |
+
|
| 125 |
+
processor = AutoProcessor.from_pretrained(repo_name, trust_remote_code=True)
|
| 126 |
+
model = AutoModel.from_pretrained(
|
| 127 |
+
repo_name,
|
| 128 |
+
torch_dtype=torch_dtype,
|
| 129 |
+
_attn_implementation="eager", # `"sdpa"` や `"flash_attention_2"` も指定可能
|
| 130 |
+
trust_remote_code=True
|
| 131 |
+
).to(device=device)
|
| 132 |
+
|
| 133 |
+
image1 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example1_4E00.jpg") # 画像例
|
| 134 |
+
image_array1 = processor(images=image1, return_tensors="pt")["pixel_values"].to(device=device, dtype=torch_dtype)
|
| 135 |
+
with torch.inference_mode():
|
| 136 |
+
print(model.get_predictions(image_array1)) # 予測ラベルを返す
|
| 137 |
+
# ['一']
|
| 138 |
+
|
| 139 |
+
image2 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example2_5B9A.jpg") # 画像例
|
| 140 |
+
image3 = get_image("https://huggingface.co/SakanaAI/Metom/resolve/main/examples/example3_5009.jpg") # 画像例
|
| 141 |
+
image_array2 = processor(images=[image2, image3], return_tensors="pt")["pixel_values"].to(device=device, dtype=torch_dtype)
|
| 142 |
+
with torch.inference_mode():
|
| 143 |
+
print(model.get_topk_labels(image_array2)) # 上位k件の予測ラベルを返す (ラベルのみ)
|
| 144 |
+
# [['定', '芝', '乏', '淀', '実'], ['倉', '衾', '斜', '会', '急']]
|
| 145 |
+
print(model.get_topk_labels(image_array2, k=3, return_probs=True)) # 上位k件の予測ラベルを返す (ラベルと確率)
|
| 146 |
+
# [[('定', 0.9979104399681091), ('芝', 0.0002953427319880575), ('乏', 0.00012814522779081017)], [('倉', 0.9862521290779114), ('衾', 0.0005956474924460053), ('斜', 0.00039981433656066656)]]
|
| 147 |
+
```
|
| 148 |
+
|
| 149 |
+
## 引用
|
| 150 |
+
```bibtex
|
| 151 |
+
@misc{Metom,
|
| 152 |
+
url = {[https://huggingface.co/SakanaAI/Metom](https://huggingface.co/SakanaAI/Metom)},
|
| 153 |
+
title = {Metom},
|
| 154 |
+
author = {Imajuku, Yuki and Clanuwat, Tarin}
|
| 155 |
+
}
|
| 156 |
+
```
|