Skip to content

Commit

Permalink
Merge pull request #43 from kotaro-kinoshita/feature/improve-text-det…
Browse files Browse the repository at this point in the history
…ector-postprocess

dynamic unclip ratio
  • Loading branch information
kotaro-kinoshita authored Nov 26, 2024
2 parents d6fa502 + afded86 commit 641cba7
Show file tree
Hide file tree
Showing 30 changed files with 635 additions and 105 deletions.
2 changes: 1 addition & 1 deletion src/yomitoku/configs/cfg_text_detector_dbnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PostProcess:
thresh: float = 0.2
box_thresh: float = 0.5
max_candidates: int = 1500
unclip_ratio: float = 2.0
unclip_ratio: float = 7.0


@dataclass
Expand Down
14 changes: 4 additions & 10 deletions src/yomitoku/document_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def extract_words_within_element(pred_words, element):
if len(contained_words) == 0:
return None, None, check_list

mean_width = word_sum_width / len(contained_words)
mean_height = word_sum_height / len(contained_words)
# mean_width = word_sum_width / len(contained_words)
# mean_height = word_sum_height / len(contained_words)

word_direction = [word.direction for word in contained_words]
cnt_horizontal = word_direction.count("horizontal")
Expand All @@ -120,18 +120,12 @@ def extract_words_within_element(pred_words, element):
if element_direction == "horizontal":
contained_words = sorted(
contained_words,
key=lambda x: (
x.points[0][1] // int(mean_height),
x.points[0][0],
),
key=lambda x: (sum([p[1] for p in x.points]) / 4),
)
else:
contained_words = sorted(
contained_words,
key=lambda x: (
x.points[1][0] // int(mean_width),
x.points[1][1],
),
key=lambda x: (sum([p[0] for p in x.points]) / 4),
reverse=True,
)

Expand Down
13 changes: 11 additions & 2 deletions src/yomitoku/postprocessor/dbnet_postporcessor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cv2
import math
import numpy as np
import pyclipper
from shapely.geometry import Polygon
Expand Down Expand Up @@ -80,9 +81,17 @@ def boxes_from_bitmap(self, pred, _bitmap, dest_width, dest_height):

return boxes, scores

def unclip(self, box, unclip_ratio=1.5):
def unclip(self, box, unclip_ratio=7):
# 小さい文字が見切れやすい、大きい文字のマージンが過度に大きくなる等の課題がある
# 対応として、文字の大きさに応じて、拡大パラメータを動的に変更する
# Note: こののルールはヒューリスティックで理論的根拠はない
poly = Polygon(box)
distance = poly.area * unclip_ratio / poly.length
width = box[:, 0].max() - box[:, 0].min()
height = box[:, 1].max() - box[:, 1].min()
box_dist = min(width, height)
ratio = unclip_ratio / math.sqrt(box_dist)

distance = poly.area * ratio / poly.length
offset = pyclipper.PyclipperOffset()
offset.AddPath(box, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
expanded = np.array(offset.Execute(distance))
Expand Down
Binary file modified static/out/demo_html.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 19 additions & 19 deletions static/out/in_demo_p1.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<body>
<p>AI の進化に伴う課題と現状の取組</p>
<p>Al の進化に伴う課題と現状の取組</p>
<p>第1節</p>
<table border="1" style="border-collapse: collapse">
<tr>
Expand All @@ -13,35 +13,35 @@
<tr>
<td rowspan="8" colspan="1">従来型AI<br/>から存在<br/>するリスク</td>
<td rowspan="1" colspan="2">バイアスのある結果及び差別的な結果の出力</td>
<td rowspan="1" colspan="1"> IT企業が自社で開発したAI人材採用システムが女性を差別するという機械学習面の欠陥を持<br/>ち合わせていた</td>
<td rowspan="1" colspan="1">●IT企業が自社で開発したAI人材採用システムが女性を差別するという機械学習面の欠陥を持<br/>ち合わせていた</td>
</tr>
<tr>
<td rowspan="1" colspan="2">フィルターバブル及びエコーチェンバー現象</td>
<td rowspan="1" colspan="1"> SNS等によるレコメンドを通じた社会の分断が生じている</td>
<td rowspan="1" colspan="1">●SNS等によるレコメンドを通じた社会の分断が生じている</td>
</tr>
<tr>
<td rowspan="1" colspan="2">多様性の喪失</td>
<td rowspan="1" colspan="1"> 社会全体が同じモデルを、同じ温度感で使った場合、導かれる意見及び回答がIIMによって<br/>収束してしまい、多様性が失われる可能性がある</td>
<td rowspan="1" colspan="1">●社会全体が同じモデルを、同じ温度感で使った場合、導かれる意見及び回答がLLMによって<br/>収束してしまい、多様性が失われる可能性がある</td>
</tr>
<tr>
<td rowspan="1" colspan="2">不適切な個人情報の取扱い</td>
<td rowspan="1" colspan="1"> 透明性を欠く個人情報の利用及び個人情報の政治利用も問題視されている</td>
<td rowspan="1" colspan="1">●透明性を欠く個人情報の利用及び個人情報の政治利用も問題視されている</td>
</tr>
<tr>
<td rowspan="1" colspan="2">生命,身体、財産の侵害</td>
<td rowspan="1" colspan="1"> Alが不適切な判断を下すことで、自動運転車が事故を引き起こし、生命や財産に深刻な損害<br/>を与える可能性がある<br/>●トリアージにおいては、Alが順位を決定する際に倫理的なバイアスを持つことで、公平性の<br/>喪失等が生じる可能性がある</td>
<td rowspan="1" colspan="2">生命身体、財産の侵害</td>
<td rowspan="1" colspan="1">●Alが不適切な判断を下すことで、自動運転車が事故を引き起こし、生命や財産に深刻な損害<br/>を与える可能性がある<br/>●トリアージにおいては、Alが順位を決定する際に倫理的なバイアスを持つことで、公平性の<br/>喪失等が生じる可能性がある</td>
</tr>
<tr>
<td rowspan="1" colspan="2">データ汚染攻撃</td>
<td rowspan="1" colspan="1"> AIの学習実施時及びサービス運用時には学習データへの不正データ混入、サービス運用時で<br/>はアプリケーション自体を狙ったサイバー攻撃等のリスクが存在する</td>
<td rowspan="1" colspan="1">●AIの学習実施時及びサービス運用時には学習データへの不正データ混入、サービス運用時で<br/>はアプリケーション自体を狙ったサイバー攻撃等のリスクが存在する</td>
</tr>
<tr>
<td rowspan="1" colspan="2">ブラックボックス化、判断に関する説明の要求</td>
<td rowspan="1" colspan="1">● AIの判断のブラックボックス化に起因する問題も生じている<br/> AIの判断に関する透明性を求める動きも上がっている</td>
<td rowspan="1" colspan="1">● AIの判断のブラックボックス化に起因する問題も生じている<br/>●AIの判断に関する透明性を求める動きも上がっている</td>
</tr>
<tr>
<td rowspan="1" colspan="2">エネルギー使用量及び環境の負荷</td>
<td rowspan="1" colspan="1"> Alの利用拡大により、計算リソースの需要も拡大しており、結果として、データセンターが<br/>増大しエネルギー使用量の増加が懸念されている</td>
<td rowspan="1" colspan="1">AIの利用拡大により、計算リソースの需要も拡大しており、結果として、データセンターが<br/>増大しエネルギー使用量の増加が懸念されている</td>
</tr>
<tr>
<td rowspan="7" colspan="1">生成AIで<br/>特に顕在化<br/>したリスク</td>
Expand All @@ -50,23 +50,23 @@
</tr>
<tr>
<td rowspan="1" colspan="2">機密情報の流出</td>
<td rowspan="1" colspan="1"> AIの利用においては、個人情報や機密情報がプロンプトとして入力され、そのAIからの出力<br/>等を通じて流出してしまうリスクがある</td>
<td rowspan="1" colspan="1">●AIの利用においては、個人情報や機密情報がプロンプトとして入力され、そのAIからの出力<br/>等を通じて流出してしまうリスクがある</td>
</tr>
<tr>
<td rowspan="1" colspan="2">ハルシネーション</td>
<td rowspan="1" colspan="1">牛成AIが事実と異なることをもっともらしく回答する「ハルシネーション」に関してはAI開<br/>発者·提供者への訴訟も起きている</td>
<td rowspan="1" colspan="1">生成Alが事実と異なることをもっともらしく回答する「ハルシネーション」に関してはAI開<br/>発者·提供者への訴訟も起きている</td>
</tr>
<tr>
<td rowspan="1" colspan="2">偽情報、誤情報を鵜呑みにすること</td>
<td rowspan="1" colspan="1"> 生成AIが生み出す誤情報を鵜呑みにすることがリスクとなりうる<br/>●ディープフェイクは、各国で悪用例が相次いでいる</td>
<td rowspan="1" colspan="1">●生成AIが生み出す誤情報を鵜呑みにすることがリスクとなりうる<br/>●ディープフェイクは、各国で悪用例が相次いでいる</td>
</tr>
<tr>
<td rowspan="1" colspan="2">著作権との関係</td>
<td rowspan="1" colspan="1"> 知的財産権の取扱いへの議論が提起されている</td>
<td rowspan="1" colspan="1">●知的財産権の取扱いへの議論が提起されている</td>
</tr>
<tr>
<td rowspan="1" colspan="2">資格等との関係</td>
<td rowspan="1" colspan="1">牛成AIの活用を通じた業法免許や資格等の侵害リスクも考えうる</td>
<td rowspan="1" colspan="1">生成AIの活用を通じた業法免許や資格等の侵害リスクも考えうる</td>
</tr>
<tr>
<td rowspan="1" colspan="2">バイアスの再生成</td>
Expand All @@ -77,10 +77,10 @@
<h1>1 主要なLLMの概要</h1>
<p>(出典)「AI事業者ガイドライン(第1.0版)」別添(概要)</p>
<p>生成AIの基盤となる大規模言語モデル(LLM) の開発では、マイクロソフトやグーグルなど米<br/>国ビックテック企業などが先行している状況にある。</p>
<p>しかし、日本以外の企業·研究機関がクローズに研究開発を進めたLLMを活用するだけでは<br/>LLM構築の過程がブラックボックス化してしまい、LLMを活用する際の権利侵害や情報漏えいな<br/>どの懸念を払拭できない。日本語に強いLLMの利活用のためには、構築の過程や用いるデータが<br/>明らかな、透明性の高い安心して利活用できる国産のLLM構築が必要となる 3。すでに日本の企業<br/>においても、独自にLLM開発に取り組んでおり、ここではその動向を紹介する。</p>
<p>ビッグテック企業が開発したI.Mと比べると、日本では、中規模モデルのI.Mが開発されてい<br/>る傾向が見られる(図表 I-4-1-2)。</p>
<p>*3 産業技術総合研究所プレスリリース「産総研の計算資源ABCIを用いて世界トップレベルの生成AIの開発を開始一産総研·東京工業大学·<br/>LLM-jp (国立情報学研究所主宰)が協力ー」(2023年10月17日), &lt;hittps://www.aist.go.jp/alst j/news/pr20231017.html&gt; (2///2<br/>参照)</p>
<p>しかし、日本以外の企業·研究機関がクローズに研究開発を進めたLLM を活用するだけでは<br/>LLM構築の過程がブラックボックス化してしまい、LLMを活用する際の権利侵害や情報漏えいな<br/>どの懸念を払拭できない。日本語に強いLLMの利活用のためには、構築の過程や用いるデータが<br/>明らかな、透明性の高い安心して利活用できる国産のLLM構築が必要となる*3。すでに日本の企業<br/>においても、独自にLLM開発に取り組んでおり、ここではその動向を紹介する。</p>
<p>ビッグテック企業が開発したLLMと比べると、日本では、中規模モデルのLLMが開発されてい<br/>る傾向が見られる(図表1-4-1-2)。</p>
<p>*3 産業技術総合研究所プレスリリース「産総研の計算資源ABCIを用いて世界トップレベルの生成AIの開発を開始一産総研·東京工業大学·<br/>LLM-jp (国立情報学研究所主宰)が協力ー」(2023年10月17日),&lt;https://www.aist.go.jp/aist_j/news/pr20231017.html&gt; (2024/3/<br/>参照)</p>
<p>令和6年版 情報通信白書 第1部 47</p>
<p>第4章<br/>デジタルテクノロジーの課題と現状の対応策</p>
<p>デジタルテクノロジーの課題と現状の対応策<br/>第4章</p>
</body>
</html>
Loading

0 comments on commit 641cba7

Please sign in to comment.