Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import numpy as np
|
| 3 |
import torch
|
| 4 |
-
import torch.nn as nn
|
| 5 |
import torch.nn.functional as F
|
| 6 |
-
import timm
|
| 7 |
import gradio as gr
|
|
|
|
|
|
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
img = Image.open(image_path).convert("L")
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
with torch.no_grad():
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
rot_probs = F.softmax(rot_logits, dim=1)[0].cpu().numpy()
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
#
|
| 60 |
base = os.path.splitext(os.path.basename(image_path))[0]
|
| 61 |
try:
|
| 62 |
-
|
| 63 |
except ValueError:
|
| 64 |
-
|
| 65 |
|
| 66 |
-
#
|
| 67 |
warn_html = ""
|
| 68 |
-
if
|
| 69 |
warn_html += "<p style='color:red'>⚠ Potential mislabeled projection</p>"
|
| 70 |
-
if
|
| 71 |
warn_html += "<p style='color:red'>⚠ Potential mislabeled rotation</p>"
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
f"<p><strong>
|
|
|
|
| 76 |
f"{warn_html}"
|
| 77 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
# --- Gradio UI ---
|
| 80 |
-
with gr.Blocks() as demo:
|
| 81 |
with gr.Row():
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
-
demo.launch()
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
import os
|
|
|
|
| 5 |
import torch
|
|
|
|
| 6 |
import torch.nn.functional as F
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
+
import numpy as np
|
| 9 |
+
import torchvision.transforms as T
|
| 10 |
from PIL import Image
|
| 11 |
|
| 12 |
+
from lib.framework import create_model
|
| 13 |
+
from lib.options import ParamSet, _retrieve_parameter, _dispatch_by_group
|
| 14 |
+
from lib.dataloader import ImageMixin
|
| 15 |
+
|
| 16 |
+
# ===========================================
|
| 17 |
+
# 1) パス設定
|
| 18 |
+
# ===========================================
|
| 19 |
+
WEIGHT_PATH = "./weight_epoch-011_best.pt"
|
| 20 |
+
PARAMETER_JSON = "./parameters.json"
|
| 21 |
+
|
| 22 |
+
# ===========================================
|
| 23 |
+
# 2) クラスラベル定義
|
| 24 |
+
# ===========================================
|
| 25 |
+
LABEL_APorPA = ["AP", "PA", "Lateral"]
|
| 26 |
+
LABEL_ROUND = ["Upright", "Inverted", "Left rotation", "Right rotation"]
|
| 27 |
+
|
| 28 |
+
# ===========================================
|
| 29 |
+
# 3) 前処理クラス
|
| 30 |
+
# ===========================================
|
| 31 |
+
class ImageHandler(ImageMixin):
|
| 32 |
+
def __init__(self, params):
|
| 33 |
+
self.params = params
|
| 34 |
+
self.transform = T.Compose([
|
| 35 |
+
# 256×256 前提なら Resize は不要
|
| 36 |
+
# T.Resize((256, 256)),
|
| 37 |
+
T.ToTensor(),
|
| 38 |
+
])
|
| 39 |
+
|
| 40 |
+
def set_image(self, image: Image.Image):
|
| 41 |
+
tensor = self.transform(image) # [C,H,W], float32 in [0,1]
|
| 42 |
+
return {"image": tensor.unsqueeze(0)} # バッチ次元追加
|
| 43 |
+
|
| 44 |
+
# ===========================================
|
| 45 |
+
# 4) パラメータロード
|
| 46 |
+
# ===========================================
|
| 47 |
+
def load_parameter(parameter_path):
|
| 48 |
+
_args = ParamSet()
|
| 49 |
+
params = _retrieve_parameter(parameter_path)
|
| 50 |
+
for k, v in params.items():
|
| 51 |
+
setattr(_args, k, v)
|
| 52 |
+
# 推論用に上書き
|
| 53 |
+
_args.augmentation = "no"
|
| 54 |
+
_args.sampler = "no"
|
| 55 |
+
_args.pretrained = False
|
| 56 |
+
_args.mlp = None
|
| 57 |
+
_args.net = _args.model
|
| 58 |
+
_args.device = torch.device("cpu")
|
| 59 |
+
return (
|
| 60 |
+
_dispatch_by_group(_args, "model"),
|
| 61 |
+
_dispatch_by_group(_args, "dataloader"),
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
args_model, args_dataloader = load_parameter(PARAMETER_JSON)
|
| 65 |
+
|
| 66 |
+
# ===========================================
|
| 67 |
+
# 5) モデル作成&重みロード
|
| 68 |
+
# ===========================================
|
| 69 |
+
model = create_model(args_model)
|
| 70 |
+
print(f"Loading weights from {WEIGHT_PATH}")
|
| 71 |
+
model.load_weight(WEIGHT_PATH)
|
| 72 |
+
model.eval()
|
| 73 |
+
|
| 74 |
+
# ===========================================
|
| 75 |
+
# 6) 推論+HTML生成
|
| 76 |
+
# ===========================================
|
| 77 |
+
def predict_html(image_path: str) -> str:
|
| 78 |
+
# 画像読み込み
|
| 79 |
img = Image.open(image_path).convert("L")
|
| 80 |
+
handler = ImageHandler(args_dataloader)
|
| 81 |
+
batch = handler.set_image(img)
|
| 82 |
|
| 83 |
with torch.no_grad():
|
| 84 |
+
outputs = model(batch)
|
| 85 |
+
# raw logits
|
| 86 |
+
logits_proj = outputs.get("label_APorPA")
|
| 87 |
+
logits_rot = outputs.get("label_round")
|
|
|
|
| 88 |
|
| 89 |
+
# argmax でラベル選択
|
| 90 |
+
idx_proj = int(torch.argmax(logits_proj, dim=1).item())
|
| 91 |
+
idx_rot = int(torch.argmax(logits_rot, dim=1).item())
|
| 92 |
+
pred_proj = LABEL_APorPA[idx_proj]
|
| 93 |
+
pred_rot = LABEL_ROUND[idx_rot]
|
| 94 |
|
| 95 |
+
# ファイ���名から元ラベル取得(例: "AP_Upright.png")
|
| 96 |
base = os.path.splitext(os.path.basename(image_path))[0]
|
| 97 |
try:
|
| 98 |
+
orig_proj, orig_rot = base.split("_", 1)
|
| 99 |
except ValueError:
|
| 100 |
+
orig_proj = orig_rot = None
|
| 101 |
|
| 102 |
+
# 警告HTML
|
| 103 |
warn_html = ""
|
| 104 |
+
if orig_proj and orig_proj != pred_proj:
|
| 105 |
warn_html += "<p style='color:red'>⚠ Potential mislabeled projection</p>"
|
| 106 |
+
if orig_rot and orig_rot != pred_rot:
|
| 107 |
warn_html += "<p style='color:red'>⚠ Potential mislabeled rotation</p>"
|
| 108 |
|
| 109 |
+
# 結果表示用HTML
|
| 110 |
+
html = (
|
| 111 |
+
f"<p><strong>Projection :</strong> {pred_proj}</p>"
|
| 112 |
+
f"<p><strong>Rotation :</strong> {pred_rot}</p>"
|
| 113 |
f"{warn_html}"
|
| 114 |
)
|
| 115 |
+
return html
|
| 116 |
+
|
| 117 |
+
# ===========================================
|
| 118 |
+
# 7) Gradio UI
|
| 119 |
+
# ===========================================
|
| 120 |
+
html_header = """
|
| 121 |
+
<div style="padding:10px;border:1px solid #ddd;border-radius:5px">
|
| 122 |
+
<h2>Chest X‑ray Projection & Rotation Classification</h2>
|
| 123 |
+
<p>Upload a 256×256 grayscale PNG. The model predicts projection (AP/PA/Lateral)
|
| 124 |
+
and rotation (Upright/Inverted/Left/Right) and warns if filename label differs.</p>
|
| 125 |
+
</div>
|
| 126 |
+
"""
|
| 127 |
+
|
| 128 |
+
with gr.Blocks(title="CXR Projection & Rotation") as demo:
|
| 129 |
+
gr.HTML(html_header)
|
| 130 |
|
|
|
|
|
|
|
| 131 |
with gr.Row():
|
| 132 |
+
input_image = gr.Image(
|
| 133 |
+
label="Upload PNG (256×256)",
|
| 134 |
+
type="filepath",
|
| 135 |
+
image_mode="L"
|
| 136 |
+
)
|
| 137 |
+
output_html = gr.HTML()
|
| 138 |
+
|
| 139 |
+
send_btn = gr.Button("Run Inference")
|
| 140 |
+
send_btn.click(
|
| 141 |
+
fn=predict_html,
|
| 142 |
+
inputs=input_image,
|
| 143 |
+
outputs=output_html
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
# サンプル例
|
| 147 |
+
gr.Examples(
|
| 148 |
+
examples=[
|
| 149 |
+
"./sample/sample_AP_Upright.png",
|
| 150 |
+
"./sample/sample_PA_Inverted.png",
|
| 151 |
+
"./sample/sample_Lateral_Right rotation.png",
|
| 152 |
+
],
|
| 153 |
+
inputs=input_image
|
| 154 |
+
)
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
| 157 |
+
demo.launch(debug=True)
|