光山容仁
commited on
Commit
·
22be147
1
Parent(s):
8a2e9e8
Add Gradio app, requirements and model
Browse files- .gitattributes +1 -0
- app.py +100 -0
- cxp_projection_rotation.pt +3 -0
- requirements.txt +4 -0
- sample_images/AP_Right90.png +0 -0
- sample_images/AP_Upright.png +0 -0
- sample_images/Lateral_Left90.png +0 -0
- sample_images/PA_Inverted.png +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
cxp_projection_rotation.pt filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
import onnxruntime as ort
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# モデルをONNX Runtimeで読み込む
|
| 8 |
+
session = ort.InferenceSession("cxp_projection_rotation.pt", None)
|
| 9 |
+
input_name = session.get_inputs()[0].name
|
| 10 |
+
# 出力ヘッド名(順序に注意)
|
| 11 |
+
projection_name = session.get_outputs()[0].name
|
| 12 |
+
rotation_name = session.get_outputs()[1].name
|
| 13 |
+
|
| 14 |
+
# クラスラベルの定義
|
| 15 |
+
PROJ_LABELS = ["AP", "PA", "Lateral"]
|
| 16 |
+
ROT_LABELS = ["Upright", "Inverted", "Left90", "Right90"]
|
| 17 |
+
|
| 18 |
+
def softmax(x: np.ndarray) -> np.ndarray:
|
| 19 |
+
"""数値安定版ソフトマックス"""
|
| 20 |
+
e = np.exp(x - np.max(x))
|
| 21 |
+
return e / np.sum(e)
|
| 22 |
+
|
| 23 |
+
def predict(image_path: str) -> str:
|
| 24 |
+
# 画像をグレースケールで読み込み(Lモード)
|
| 25 |
+
img = Image.open(image_path).convert("L")
|
| 26 |
+
arr = np.array(img, dtype=np.float32) / 255.0 # 正規化
|
| 27 |
+
arr = arr[np.newaxis, np.newaxis, :, :] # [1,1,H,W] バッチ&チャネル次元追加
|
| 28 |
+
|
| 29 |
+
# 推論
|
| 30 |
+
proj_logits, rot_logits = session.run(
|
| 31 |
+
[projection_name, rotation_name],
|
| 32 |
+
{input_name: arr}
|
| 33 |
+
)
|
| 34 |
+
proj_probs = softmax(proj_logits[0])
|
| 35 |
+
rot_probs = softmax(rot_logits[0])
|
| 36 |
+
|
| 37 |
+
# 予測結果と確率
|
| 38 |
+
proj_idx = int(np.argmax(proj_probs))
|
| 39 |
+
rot_idx = int(np.argmax(rot_probs))
|
| 40 |
+
proj_lbl = PROJ_LABELS[proj_idx]
|
| 41 |
+
rot_lbl = ROT_LABELS[rot_idx]
|
| 42 |
+
proj_p = proj_probs[proj_idx]
|
| 43 |
+
rot_p = rot_probs[rot_idx]
|
| 44 |
+
|
| 45 |
+
# ファイル名から元ラベルを推定(例: "AP_Upright.png" → ["AP","Upright"])
|
| 46 |
+
base = os.path.splitext(os.path.basename(image_path))[0]
|
| 47 |
+
try:
|
| 48 |
+
orig_proj, orig_rot = base.split("_", 1)
|
| 49 |
+
except ValueError:
|
| 50 |
+
orig_proj = orig_rot = None
|
| 51 |
+
|
| 52 |
+
# 警告メッセージ(必要なら赤字で表示)
|
| 53 |
+
warnings_html = ""
|
| 54 |
+
if orig_proj and orig_proj != proj_lbl:
|
| 55 |
+
warnings_html += "<p style='color:red'>⚠ Potential mislabeled projection</p>"
|
| 56 |
+
if orig_rot and orig_rot != rot_lbl:
|
| 57 |
+
warnings_html += "<p style='color:red'>⚠ Potential mislabeled rotation</p>"
|
| 58 |
+
|
| 59 |
+
# HTML形式で結果を返す
|
| 60 |
+
html = (
|
| 61 |
+
f"<p><strong>Projection :</strong> {proj_lbl} (p={proj_p:.3f})</p>"
|
| 62 |
+
f"<p><strong>Rotation :</strong> {rot_lbl} (p={rot_p:.3f})</p>"
|
| 63 |
+
f"{warnings_html}"
|
| 64 |
+
)
|
| 65 |
+
return html
|
| 66 |
+
|
| 67 |
+
# Gradio UI構築
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
with gr.Row():
|
| 70 |
+
with gr.Column():
|
| 71 |
+
# 画像アップロード(PNG/L, 256×256前提)
|
| 72 |
+
image_input = gr.Image(
|
| 73 |
+
label="Upload PNG (256×256)",
|
| 74 |
+
type="filepath",
|
| 75 |
+
tool=None
|
| 76 |
+
)
|
| 77 |
+
# sample_imagesフォルダから4枚まで例示
|
| 78 |
+
sample_list = sorted(
|
| 79 |
+
[os.path.join("sample_images", f)
|
| 80 |
+
for f in os.listdir("sample_images")
|
| 81 |
+
if f.lower().endswith(".png")]
|
| 82 |
+
)
|
| 83 |
+
gr.Examples(
|
| 84 |
+
examples=sample_list,
|
| 85 |
+
inputs=image_input,
|
| 86 |
+
label="Sample Images"
|
| 87 |
+
)
|
| 88 |
+
with gr.Column():
|
| 89 |
+
# 推論結果用HTML表示エリア
|
| 90 |
+
result = gr.HTML()
|
| 91 |
+
|
| 92 |
+
# 画像選択・アップロード時に自動でpredictを実行
|
| 93 |
+
image_input.change(
|
| 94 |
+
fn=predict,
|
| 95 |
+
inputs=image_input,
|
| 96 |
+
outputs=result
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
demo.launch()
|
cxp_projection_rotation.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:67d5e766adf1acec37c112e87d568ffda2c8a1fe9334f1c0ab3ef3dbfe35aae4
|
| 3 |
+
size 71017114
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
onnxruntime
|
| 3 |
+
numpy
|
| 4 |
+
Pillow
|
sample_images/AP_Right90.png
ADDED
|
sample_images/AP_Upright.png
ADDED
|
sample_images/Lateral_Left90.png
ADDED
|
sample_images/PA_Inverted.png
ADDED
|