Segmentation / code /scripts /plot_kvasir_loss.py
MaybeRichard's picture
Upload PixelGen code: cross-attention mask mode + multi-scale ablation configs
01fdb75 verified
raw
history blame
2.75 kB
"""Plot Kvasir-SEG training loss curves from log file."""
import sys
sys.path.insert(0, "/data/sichengli/Code/PixelGen")
import re
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
with open('/data/sichengli/Code/PixelGen/training_medical_kvasir.log', 'r') as f:
text = f.read()
# Extract epoch-end lines (100% completion lines with loss values)
pattern = r'Epoch\s+(\d+):\s+100%.*?fm_loss=([\d.]+),\s*lpips_loss=([\d.]+),\s*loss=([\d.]+)'
matches = re.findall(pattern, text)
epochs = []
fm_losses = []
lpips_losses = []
total_losses = []
seen = set()
for m in matches:
epoch = int(m[0])
if epoch in seen:
continue
seen.add(epoch)
epochs.append(epoch)
fm_losses.append(float(m[1]))
lpips_losses.append(float(m[2]))
total_losses.append(float(m[3]))
# Convert epoch to step (8 steps per epoch)
steps = [e * 8 for e in epochs]
print(f"Total data points: {len(steps)}")
print(f"Step range: {steps[0]} - {steps[-1]}")
# Smooth with moving average
def smooth(values, window=50):
if len(values) < window:
return values
kernel = np.ones(window) / window
return np.convolve(values, kernel, mode='valid')
fig, axes = plt.subplots(1, 3, figsize=(18, 5))
for ax, data, label, color in zip(
axes,
[fm_losses, lpips_losses, total_losses],
['FM Loss (MSE)', 'LPIPS Loss', 'Total Loss'],
['#2196F3', '#FF9800', '#4CAF50']
):
s = np.array(steps)
d = np.array(data)
# Raw data (light)
ax.plot(s, d, alpha=0.15, color=color, linewidth=0.5)
# Smoothed
w = min(100, max(1, len(d) // 5))
if w > 1:
d_smooth = smooth(d, w)
s_smooth = s[w-1:][:len(d_smooth)]
ax.plot(s_smooth, d_smooth, color=color, linewidth=2, label=label + " (smoothed)")
ax.set_xlabel('Training Steps', fontsize=12)
ax.set_ylabel('Loss', fontsize=12)
ax.set_title(label, fontsize=14, fontweight='bold')
ax.legend(fontsize=10)
ax.grid(True, alpha=0.3)
ax.set_xlim(0, 100000)
plt.suptitle('Kvasir-SEG Training Loss Curves (100k steps)', fontsize=16, fontweight='bold', y=1.02)
plt.tight_layout()
out_path = '/data/sichengli/Code/PixelGen/medical_workdirs/exp_PixelGen_Medical_Kvasir/val_samples/loss_curves.png'
plt.savefig(out_path, dpi=150, bbox_inches='tight')
print(f"Saved: {out_path}")
# Print key milestones
milestones = [0, 10000, 20000, 30000, 50000, 70000, 100000]
print()
header = f"{'Step':>8s} | {'FM Loss':>10s} | {'LPIPS':>10s} | {'Total':>10s}"
print(header)
print('-' * len(header))
for ms in milestones:
idx = min(range(len(steps)), key=lambda i: abs(steps[i] - ms))
print(f"{steps[idx]:>8d} | {fm_losses[idx]:>10.4f} | {lpips_losses[idx]:>10.4f} | {total_losses[idx]:>10.4f}")