Segmentation / code /scripts /plot_refuge2_loss.py
MaybeRichard's picture
Upload PixelGen code: cross-attention mask mode + multi-scale ablation configs
01fdb75 verified
raw
history blame
2.48 kB
"""Plot REFUGE2 training loss curves from log file."""
import re
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
with open('/data/sichengli/Code/PixelGen/training_medical_refuge2.log', 'r') as f:
text = f.read()
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]))
steps = [e * 6 for e in epochs]
print(f"Total data points: {len(steps)}")
print(f"Step range: {steps[0]} - {steps[-1]}")
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)
ax.plot(s, d, alpha=0.15, color=color, linewidth=0.5)
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('REFUGE2 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_REFUGE2/val_samples/loss_curves.png'
plt.savefig(out_path, dpi=150, bbox_inches='tight')
print(f"Saved: {out_path}")
milestones = [0, 10000, 20000, 30000, 50000, 70000, 100000]
header = f"{'Step':>8s} | {'FM Loss':>10s} | {'LPIPS':>10s} | {'Total':>10s}"
print(f"\n{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}")