Switch viewer and training to patient1 fixed flat CT
This commit is contained in:
81
app.py
81
app.py
@@ -31,7 +31,7 @@ from metrics import crop_to_common_shape, ddf_summary, registration_metrics, sli
|
||||
st.set_page_config(
|
||||
page_title="VoxelMorph 颈部 CT 配准工作台",
|
||||
layout="wide",
|
||||
initial_sidebar_state="expanded",
|
||||
initial_sidebar_state="collapsed",
|
||||
)
|
||||
|
||||
|
||||
@@ -608,31 +608,70 @@ def run_inference_from_ui(moving_path: str, fixed_path: str, checkpoint_path: st
|
||||
)
|
||||
|
||||
|
||||
def run_training_from_ui(moving_path: str, fixed_path: str, checkpoint_path: str) -> None:
|
||||
from model_and_train import train_pair
|
||||
|
||||
train_pair(
|
||||
moving_path=moving_path,
|
||||
fixed_path=fixed_path,
|
||||
checkpoint_path=checkpoint_path,
|
||||
epochs=80,
|
||||
learning_rate=1e-4,
|
||||
smooth_weight=0.01,
|
||||
image_loss="mse",
|
||||
nb_features=(8, 8, 8, 8, 8),
|
||||
save_every=20,
|
||||
)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
st.markdown(CUSTOM_CSS, unsafe_allow_html=True)
|
||||
st.title("VoxelMorph 颈部 CT 配准工作台")
|
||||
|
||||
candidates = discover_nifti_files()
|
||||
checkpoint_candidates = discover_checkpoint_files()
|
||||
with st.sidebar:
|
||||
st.header("输入")
|
||||
moving_path = choose_path("Moving", DEFAULT_MOVING_NIFTI, candidates)
|
||||
fixed_path = choose_path("Fixed", DEFAULT_FIXED_NIFTI, candidates)
|
||||
checkpoint_path = choose_path("模型权重", DEFAULT_CHECKPOINT, checkpoint_candidates)
|
||||
out_dir = st.text_input("输出目录", value=str(INFERENCE_DIR))
|
||||
display_max_voxels = st.slider("Web显示体素上限", 2_000_000, 30_000_000, 14_000_000, 1_000_000)
|
||||
moving_path = str(DEFAULT_MOVING_NIFTI)
|
||||
fixed_path = str(DEFAULT_FIXED_NIFTI)
|
||||
checkpoint_path = str(DEFAULT_CHECKPOINT)
|
||||
out_dir = str(INFERENCE_DIR)
|
||||
display_max_voxels = 30_000_000
|
||||
|
||||
start = st.button("开始推理", type="primary", width="stretch")
|
||||
if start:
|
||||
with st.spinner("推理运行中"):
|
||||
try:
|
||||
load_nifti_cached.clear()
|
||||
result = run_inference_from_ui(moving_path, fixed_path, checkpoint_path, out_dir)
|
||||
load_nifti_cached.clear()
|
||||
st.session_state["last_result"] = result
|
||||
st.success("推理完成")
|
||||
except Exception as exc:
|
||||
st.error(str(exc))
|
||||
st.caption(
|
||||
"患者1专用:固定图像 = 平扫CT;移动图像 = 仰头CT。"
|
||||
"推理前会重采样、归一化并裁剪/填充到同一模型网格。"
|
||||
)
|
||||
info_cols = st.columns(4)
|
||||
info_cols[0].metric("Fixed", "患者1-平扫CT")
|
||||
info_cols[1].metric("Moving", "患者1-仰头CT")
|
||||
info_cols[2].metric("模型", Path(checkpoint_path).name)
|
||||
info_cols[3].metric("输出", Path(out_dir).name)
|
||||
|
||||
action_cols = st.columns([1, 1, 4])
|
||||
with action_cols[0]:
|
||||
train_now = st.button("重新训练模型", type="primary", width="stretch")
|
||||
with action_cols[1]:
|
||||
start = st.button("开始推理", width="stretch")
|
||||
|
||||
if train_now:
|
||||
with st.spinner("患者1模型训练中"):
|
||||
try:
|
||||
load_nifti_cached.clear()
|
||||
run_training_from_ui(moving_path, fixed_path, checkpoint_path)
|
||||
result = run_inference_from_ui(moving_path, fixed_path, checkpoint_path, out_dir)
|
||||
load_nifti_cached.clear()
|
||||
st.session_state["last_result"] = result
|
||||
st.success("训练和推理完成")
|
||||
except Exception as exc:
|
||||
st.error(str(exc))
|
||||
|
||||
if start:
|
||||
with st.spinner("推理运行中"):
|
||||
try:
|
||||
load_nifti_cached.clear()
|
||||
result = run_inference_from_ui(moving_path, fixed_path, checkpoint_path, out_dir)
|
||||
load_nifti_cached.clear()
|
||||
st.session_state["last_result"] = result
|
||||
st.success("推理完成")
|
||||
except Exception as exc:
|
||||
st.error(str(exc))
|
||||
|
||||
result_paths = load_result_paths(out_dir)
|
||||
last_result = st.session_state.get("last_result", {})
|
||||
|
||||
Reference in New Issue
Block a user