File size: 11,634 Bytes
01d4d83 14b82f8 01d4d83 7d79d81 01d4d83 14b82f8 01d4d83 14b82f8 01d4d83 14b82f8 01d4d83 14b82f8 01d4d83 14b82f8 01d4d83 14b82f8 01d4d83 14b82f8 aa490af 14b82f8 01d4d83 14b82f8 | 1 2 3 4 5 6 7 8 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | import streamlit as st
import os
import torch
import numpy as np
import pandas as pd
import plotly.express as px
import scipy.io.wavfile
import io
BASE_PATH = 'Models'
st.set_page_config(layout="wide", page_title="Audio Source Separation Inspector")
def process_audio(file_path, gain_factor):
try:
# 1. FIX: Check if file is actually a Git LFS pointer (text file)
with open(file_path, 'rb') as f:
header = f.read(50)
if header.startswith(b'version https://git-lfs'):
st.error(f"❌ **LFS Error:** `{os.path.basename(file_path)}` is a Git LFS pointer, not a WAV file. Run `git lfs pull` in your terminal.")
return None
sample_rate, data = scipy.io.wavfile.read(file_path)
if data.dtype == np.int16:
data = data.astype(np.float32) / 32768.0
elif data.dtype == np.int32:
data = data.astype(np.float32) / 2147483648.0
data = data * gain_factor
data = np.clip(data, -1.0, 1.0)
data = (data * 32767).astype(np.int16)
virtual_file = io.BytesIO()
scipy.io.wavfile.write(virtual_file, sample_rate, data)
return virtual_file
except Exception as e:
st.error(f"Error processing audio: {e}")
return file_path
def get_subdirs(path):
if not os.path.exists(path):
return []
return [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))]
def load_spectrogram_interactive(pt_path, title="Spectrogram"):
try:
# 2. FIX: Added weights_only=False to fix PyTorch 2.6+ error
spec_tensor = torch.load(pt_path, map_location='cpu', weights_only=False)
if spec_tensor.dim() == 4:
spec_tensor = spec_tensor[0]
if spec_tensor.dim() == 3:
spec_data = spec_tensor.mean(dim=0).numpy()
else:
spec_data = spec_tensor.numpy()
if spec_data.min() >= 0:
spec_data = np.log1p(spec_data)
fig = px.imshow(
spec_data,
origin='lower',
aspect='auto',
color_continuous_scale='Viridis',
labels=dict(x="Time Frame", y="Frequency Bin", color="Log Magnitude"),
title=title
)
fig.update_layout(margin=dict(l=0, r=0, t=30, b=0), height=300)
return fig
except Exception as e:
st.error(f"Error loading spectrogram: {e}")
return None
def load_feature_map_interactive(pt_path):
try:
# 3. FIX: Added weights_only=False here as well
feat_tensor = torch.load(pt_path, map_location='cpu', weights_only=False)
if feat_tensor.dim() == 4:
feat_tensor = feat_tensor[0]
mean_activation = feat_tensor.mean(dim=0).numpy()
fig = px.imshow(
mean_activation,
origin='lower',
aspect='auto',
color_continuous_scale='Viridis',
labels=dict(x="Time", y="Freq/Feature", color="Activation"),
title=f"Mean Activation (Shape: {list(feat_tensor.shape)})"
)
fig.update_layout(margin=dict(l=0, r=0, t=40, b=0))
return fig
except Exception as e:
return None
st.title("🎵 Audio Source Separation Inspector")
st.markdown("""
### Model Interpretation Guide
This tool helps you evaluate how well the model separates audio sources.
* **Audio Quality:** Listen for "artifacts" (robotic sounds or clicking) in the Prediction compared to the Target.
* **Spectrogram Clarity:** In the visuals below, distinct horizontal lines represent clear tones. Vertical smear usually indicates percussion or noise.
* **Error Analysis:** If the Prediction looks "blurry" compared to the Target, the model is losing high-frequency details.
""")
if not os.path.exists(BASE_PATH):
st.error(f"Models directory not found at {BASE_PATH}. Please ensure your data was uploaded correctly.")
st.stop()
models = get_subdirs(BASE_PATH)
selected_model = st.sidebar.selectbox("Select Model", models)
st.sidebar.markdown("### Audio Settings")
volume_boost = st.sidebar.slider(
"Volume Boost (Gain)",
min_value=1.0,
max_value=20.0,
value=1.0,
step=0.5,
help="Digitally increases the amplitude of the audio signal."
)
if selected_model:
model_path = os.path.join(BASE_PATH, selected_model)
artifacts_path = os.path.join(model_path, "test_artifacts")
if os.path.exists(artifacts_path):
samples = get_subdirs(artifacts_path)
samples.sort(key=lambda x: int(x.split('_')[-1]) if '_' in x else 0)
selected_sample = st.sidebar.selectbox("Select Sample ID", samples)
if selected_sample:
sample_path = os.path.join(artifacts_path, selected_sample)
audio_dir = os.path.join(sample_path, "audio")
specs_dir = os.path.join(sample_path, "specs")
feats_dir = os.path.join(sample_path, "feats")
all_files = os.listdir(audio_dir)
target_files = [f for f in all_files if f.startswith("target_") and f.endswith(".wav")]
classes = [f.replace("target_", "").replace(".wav", "") for f in target_files]
selected_class = st.sidebar.selectbox("Focus Class", classes)
tab1, tab2, tab3 = st.tabs(["🎧 Audio & Spectrograms", "🧠 Internal Activations", "📊 Model Metadata"])
with tab1:
st.header(f"Sample {selected_sample} | Focus: {selected_class.capitalize()}")
st.subheader("1. Mixture (Input)")
st.markdown("The raw input containing all sound sources mixed together.")
mix_audio = os.path.join(audio_dir, "mixture.wav")
mix_spec = os.path.join(specs_dir, "mixture.pt")
c1, c2 = st.columns([1, 3])
with c1:
if os.path.exists(mix_audio):
st.markdown("**Audio:**")
processed_mix = process_audio(mix_audio, volume_boost)
if processed_mix:
st.audio(processed_mix, format='audio/wav')
with c2:
if os.path.exists(mix_spec):
fig = load_spectrogram_interactive(mix_spec, title="Mixture Mel-Spectrogram")
if fig: st.plotly_chart(fig, width='stretch')
st.divider()
st.subheader(f"2. Target: {selected_class}")
st.markdown(f"**Interpretation:** This is the 'Ground Truth'. Look at the spectrogram structure here—this is the ideal output.")
tgt_audio = os.path.join(audio_dir, f"target_{selected_class}.wav")
tgt_spec = os.path.join(specs_dir, f"target_{selected_class}.pt")
c1, c2 = st.columns([1, 3])
with c1:
if os.path.exists(tgt_audio):
st.markdown("**Audio:**")
processed_tgt = process_audio(tgt_audio, volume_boost)
if processed_tgt:
st.audio(processed_tgt, format='audio/wav')
with c2:
if os.path.exists(tgt_spec):
fig = load_spectrogram_interactive(tgt_spec, title=f"Target Mel-Spectrogram ({selected_class})")
if fig: st.plotly_chart(fig, width='stretch')
st.divider()
st.subheader(f"3. Prediction: {selected_class}")
st.markdown(f"**Interpretation:** Compare this to the Target above. If you see 'fuzziness' in the dark areas, the model is not silencing background noise correctly.")
pred_audio = os.path.join(audio_dir, f"pred_{selected_class}.wav")
pred_spec = os.path.join(specs_dir, f"pred_{selected_class}.pt")
c1, c2 = st.columns([1, 3])
with c1:
if os.path.exists(pred_audio):
st.markdown("**Audio:**")
processed_pred = process_audio(pred_audio, volume_boost)
if processed_pred:
st.audio(processed_pred, format='audio/wav')
with c2:
if os.path.exists(pred_spec):
fig = load_spectrogram_interactive(pred_spec, title=f"Predicted Mel-Spectrogram ({selected_class})")
if fig: st.plotly_chart(fig, width='stretch')
with tab2:
st.header("Internal Feature Maps")
st.markdown("These heatmaps visualize the neural network's internal state. Bright spots indicate features the model considers important for separation.")
if os.path.exists(feats_dir):
feat_files = sorted(os.listdir(feats_dir))
if feat_files:
selected_layer = st.selectbox("Select Probed Layer", feat_files)
if selected_layer:
st.write(f"Layer: **{selected_layer.replace('.pt', '')}**")
fig = load_feature_map_interactive(os.path.join(feats_dir, selected_layer))
if fig:
st.plotly_chart(fig, width='stretch')
else:
st.warning("No feature maps found for this sample.")
else:
st.error("Features directory not found.")
with tab3:
st.header("Training and Testing Logs")
st.markdown("Use these graphs to check for **Overfitting**. If Training Loss decreases but Test Metrics stagnate or drop, the model is memorizing data rather than learning general features.")
c1, c2 = st.columns(2)
with c1:
results_csv = os.path.join(model_path, "test_results.csv")
if os.path.exists(results_csv):
st.subheader("Test Metrics")
df = pd.read_csv(results_csv)
x_axis = 'Batch_Index' if 'Batch_Index' in df.columns else df.index
numeric_cols = df.select_dtypes(include=np.number).columns
fig = px.line(df, title="Test Metrics", x=x_axis, y=numeric_cols)
st.plotly_chart(fig, width='stretch')
st.dataframe(df, width='stretch')
else:
st.info("No `test_results.csv` found.")
with c2:
loss_csv = os.path.join(model_path, "loss.csv")
if os.path.exists(loss_csv):
st.subheader("Training Loss")
try:
df_loss = pd.read_csv(loss_csv)
x_axis = 'epoch' if 'epoch' in df_loss.columns else df_loss.index
numeric_cols = df_loss.select_dtypes(include=np.number).columns
fig = px.line(df_loss, x=x_axis, y=numeric_cols, title="Loss Curves")
st.plotly_chart(fig, width='stretch')
st.dataframe(df_loss, width='stretch')
except Exception as e:
st.write("Could not parse `loss.csv`.", e)
else:
st.info("No `loss.csv` found.")
else:
st.warning(f"No 'test_artifacts' folder found in {selected_model}") |