added force scale
Browse filesFiles changed (2)
- S2FApp/app.py+30-14
- models/s2f_model.py-2
110 unmodified lines11111211311411511679 unmodified lines1961971981992002012 unmodified lines2042052062072082092105 unmodified lines21621721821922022122222322422522622722822923023112 unmodified lines24424524624724824925025122 unmodified lines2742752762772782792 unmodified lines2822832842852862872883 unmodified lines2922932942952962972982993003013023033049 unmodified lines314315316317318319320110 unmodified linesexcept FileNotFoundError:st.error("config/substrate_settings.json not found")# Main area: image inputimg_source = st.radio("Image source", ["Upload", "Example"], horizontal=True, label_visibility="collapsed")img = None79 unmodified linesst.success("Prediction complete!")# Visualization - Plotly with zoom/pan, annotated (titles in Streamlit to avoid clipping)tit1, tit2 = st.columns(2)with tit1:2 unmodified linesst.markdown('<p style="font-size: 1.1rem; color: black; font-weight: 600;">Output: Predicted traction force map</p>', unsafe_allow_html=True)fig_pl = make_subplots(rows=1, cols=2)fig_pl.add_trace(go.Heatmap(z=img, colorscale="gray", showscale=False), row=1, col=1)fig_pl.add_trace(go.Heatmap(z=heatmap, colorscale="Jet", zmin=0, zmax=1, showscale=True,colorbar=dict(len=0.4, thickness=12)), row=1, col=2)fig_pl.update_layout(height=400,5 unmodified linesfig_pl.update_yaxes(showticklabels=False, autorange="reversed")st.plotly_chart(fig_pl, use_container_width=True)# Metrics with help (below plot)col1, col2, col3, col4 = st.columns(4)with col1:st.metric("Sum of all pixels", f"{pixel_sum:.2f}", help="Raw sum of all pixel values in the force map")with col2:st.metric("Cell force (scaled)", f"{force:.2f}", help="Total traction force in physical units")with col3:st.metric("Heatmap max", f"{np.max(heatmap):.4f}", help="Peak force intensity in the map")with col4:st.metric("Heatmap mean", f"{np.mean(heatmap):.4f}", help="Average force intensity")# How to read (below numbers)with st.expander("ℹ️ How to read the results"):12 unmodified lines- **Heatmap max/mean:** Peak and average force intensity in the map""")# Downloadheatmap_uint8 = (np.clip(heatmap, 0, 1) * 255).astype(np.uint8)heatmap_rgb = cv2.applyColorMap(heatmap_uint8, cv2.COLORMAP_JET)heatmap_rgb = cv2.cvtColor(heatmap_rgb, cv2.COLOR_BGR2RGB)pil_heatmap = Image.fromarray(heatmap_rgb)22 unmodified lines# Show cached results (e.g. after clicking Download)r = st.session_state["prediction_result"]img, heatmap, force, pixel_sum = r["img"], r["heatmap"], r["force"], r["pixel_sum"]st.success("Prediction complete!")tit1, tit2 = st.columns(2)with tit1:2 unmodified linesst.markdown('<p style="font-size: 1.1rem; color: black; font-weight: 600;">Output: Predicted traction force map</p>', unsafe_allow_html=True)fig_pl = make_subplots(rows=1, cols=2)fig_pl.add_trace(go.Heatmap(z=img, colorscale="gray", showscale=False), row=1, col=1)fig_pl.add_trace(go.Heatmap(z=heatmap, colorscale="Jet", zmin=0, zmax=1, showscale=True,colorbar=dict(len=0.4, thickness=12)), row=1, col=2)fig_pl.update_layout(height=400, margin=dict(l=10, r=10, t=10, b=10),xaxis=dict(scaleanchor="y", scaleratio=1),3 unmodified linesst.plotly_chart(fig_pl, use_container_width=True)col1, col2, col3, col4 = st.columns(4)with col1:st.metric("Sum of all pixels", f"{pixel_sum:.2f}", help="Raw sum of all pixel values in the force map")with col2:st.metric("Cell force (scaled)", f"{force:.2f}", help="Total traction force in physical units")with col3:st.metric("Heatmap max", f"{np.max(heatmap):.4f}", help="Peak force intensity in the map")with col4:st.metric("Heatmap mean", f"{np.mean(heatmap):.4f}", help="Average force intensity")with st.expander("ℹ️ How to read the results"):st.markdown("""**Input (left):** Bright-field microscopy image of a cell or spheroid on a substrate.9 unmodified lines- **Cell force (scaled):** Total traction force in physical units (scaled by substrate stiffness)- **Heatmap max/mean:** Peak and average force intensity in the map""")heatmap_uint8 = (np.clip(heatmap, 0, 1) * 255).astype(np.uint8)heatmap_rgb = cv2.applyColorMap(heatmap_uint8, cv2.COLORMAP_JET)heatmap_rgb = cv2.cvtColor(heatmap_rgb, cv2.COLOR_BGR2RGB)pil_heatmap = Image.fromarray(heatmap_rgb)110 unmodified lines11111211311411511611711811912012112212312412512612712879 unmodified lines2082092102112122132142152162 unmodified lines2192202212222232242255 unmodified lines23123223323423523623723823924024124224324424524612 unmodified lines25926026126226326426526622 unmodified lines2892902912922932942952 unmodified lines2982993003013023033043 unmodified lines3083093103113123133143153163173183193209 unmodified lines330331332333334335336110 unmodified linesexcept FileNotFoundError:st.error("config/substrate_settings.json not found")st.divider()st.header("Display options")force_scale = st.slider("Force scale",min_value=0.0,max_value=1.0,value=1.0,step=0.01,format="%.2f",help="Scale the displayed force values. 1 = full intensity, 0.5 = half the pixel values.",)# Main area: image inputimg_source = st.radio("Image source", ["Upload", "Example"], horizontal=True, label_visibility="collapsed")img = None79 unmodified linesst.success("Prediction complete!")# Apply force scale to displayed heatmapscaled_heatmap = heatmap * force_scale# Visualization - Plotly with zoom/pan, annotated (titles in Streamlit to avoid clipping)tit1, tit2 = st.columns(2)with tit1:2 unmodified linesst.markdown('<p style="font-size: 1.1rem; color: black; font-weight: 600;">Output: Predicted traction force map</p>', unsafe_allow_html=True)fig_pl = make_subplots(rows=1, cols=2)fig_pl.add_trace(go.Heatmap(z=img, colorscale="gray", showscale=False), row=1, col=1)fig_pl.add_trace(go.Heatmap(z=scaled_heatmap, colorscale="Jet", zmin=0, zmax=1, showscale=True,colorbar=dict(len=0.4, thickness=12)), row=1, col=2)fig_pl.update_layout(height=400,5 unmodified linesfig_pl.update_yaxes(showticklabels=False, autorange="reversed")st.plotly_chart(fig_pl, use_container_width=True)# Metrics with help (below plot) - use scaled valuescol1, col2, col3, col4 = st.columns(4)with col1:st.metric("Sum of all pixels", f"{pixel_sum * force_scale:.2f}", help="Raw sum of all pixel values in the force map")with col2:st.metric("Cell force (scaled)", f"{force * force_scale:.2f}", help="Total traction force in physical units")with col3:st.metric("Heatmap max", f"{np.max(scaled_heatmap):.4f}", help="Peak force intensity in the map")with col4:st.metric("Heatmap mean", f"{np.mean(scaled_heatmap):.4f}", help="Average force intensity")# How to read (below numbers)with st.expander("ℹ️ How to read the results"):12 unmodified lines- **Heatmap max/mean:** Peak and average force intensity in the map""")# Download (scaled heatmap)heatmap_uint8 = (np.clip(scaled_heatmap, 0, 1) * 255).astype(np.uint8)heatmap_rgb = cv2.applyColorMap(heatmap_uint8, cv2.COLORMAP_JET)heatmap_rgb = cv2.cvtColor(heatmap_rgb, cv2.COLOR_BGR2RGB)pil_heatmap = Image.fromarray(heatmap_rgb)22 unmodified lines# Show cached results (e.g. after clicking Download)r = st.session_state["prediction_result"]img, heatmap, force, pixel_sum = r["img"], r["heatmap"], r["force"], r["pixel_sum"]scaled_heatmap = heatmap * force_scalest.success("Prediction complete!")tit1, tit2 = st.columns(2)with tit1:2 unmodified linesst.markdown('<p style="font-size: 1.1rem; color: black; font-weight: 600;">Output: Predicted traction force map</p>', unsafe_allow_html=True)fig_pl = make_subplots(rows=1, cols=2)fig_pl.add_trace(go.Heatmap(z=img, colorscale="gray", showscale=False), row=1, col=1)fig_pl.add_trace(go.Heatmap(z=scaled_heatmap, colorscale="Jet", zmin=0, zmax=1, showscale=True,colorbar=dict(len=0.4, thickness=12)), row=1, col=2)fig_pl.update_layout(height=400, margin=dict(l=10, r=10, t=10, b=10),xaxis=dict(scaleanchor="y", scaleratio=1),3 unmodified linesst.plotly_chart(fig_pl, use_container_width=True)col1, col2, col3, col4 = st.columns(4)with col1:st.metric("Sum of all pixels", f"{pixel_sum * force_scale:.2f}", help="Raw sum of all pixel values in the force map")with col2:st.metric("Cell force (scaled)", f"{force * force_scale:.2f}", help="Total traction force in physical units")with col3:st.metric("Heatmap max", f"{np.max(scaled_heatmap):.4f}", help="Peak force intensity in the map")with col4:st.metric("Heatmap mean", f"{np.mean(scaled_heatmap):.4f}", help="Average force intensity")with st.expander("ℹ️ How to read the results"):st.markdown("""**Input (left):** Bright-field microscopy image of a cell or spheroid on a substrate.9 unmodified lines- **Cell force (scaled):** Total traction force in physical units (scaled by substrate stiffness)- **Heatmap max/mean:** Peak and average force intensity in the map""")heatmap_uint8 = (np.clip(scaled_heatmap, 0, 1) * 255).astype(np.uint8)heatmap_rgb = cv2.applyColorMap(heatmap_uint8, cv2.COLORMAP_JET)heatmap_rgb = cv2.cvtColor(heatmap_rgb, cv2.COLOR_BGR2RGB)pil_heatmap = Image.fromarray(heatmap_rgb)
173 unmodified lines17417517617717817918057 unmodified lines238239240241242243244173 unmodified linespsi = F.interpolate(psi, size=x.shape[2:], mode='bilinear', align_corners=False)return x * psiclass SpheroidAttentionGate(nn.Module):"""Attention Gate from ForceNet2WithAttention (s2f_spheroid). Checkpoint-compatible for ckp_spheroid_FN.pth."""def __init__(self, F_g, F_l, F_int):57 unmodified linesx = x * self.attention(x)return self.output_conv(x)class S2FGenerator(nn.Module):"""S2F (Shape2Force) model: U-Net generator for force map prediction.173 unmodified lines17417517617717817957 unmodified lines237238239240241242173 unmodified linespsi = F.interpolate(psi, size=x.shape[2:], mode='bilinear', align_corners=False)return x * psiclass SpheroidAttentionGate(nn.Module):"""Attention Gate from ForceNet2WithAttention (s2f_spheroid). Checkpoint-compatible for ckp_spheroid_FN.pth."""def __init__(self, F_g, F_l, F_int):57 unmodified linesx = x * self.attention(x)return self.output_conv(x)class S2FGenerator(nn.Module):"""S2F (Shape2Force) model: U-Net generator for force map prediction.
S2FApp/app.py+30-14
models/s2f_model.py-2