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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
╔══════════════════════════════════════════════════════════════════════════════╗
|
| 3 |
+
║ BirdCLEF+ 2026 — Notebook 4 (IMPROVED) ║
|
| 4 |
+
║ INFERENCE & SUBMISSION ║
|
| 5 |
+
║ ║
|
| 6 |
+
║ CRITICAL PRINCIPLES (based on your 0.815 history): ║
|
| 7 |
+
║ • RAW SIGMOID outputs — NO thresholds, NO calibration ║
|
| 8 |
+
║ • Ensemble ALL models: 5 folds × 2 backbones = 10 models ║
|
| 9 |
+
║ • TTA: original + time-reversed + gain variants ║
|
| 10 |
+
║ • RANK AVERAGING for robust ensemble (not prob mean) ║
|
| 11 |
+
║ • sample_submission alignment MANDATORY ║
|
| 12 |
+
║ • Minimal post-processing (tiny clip only if absolutely needed) ║
|
| 13 |
+
╚══════════════════════════════════════════════════════════════════════════════╝
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import os
|
| 17 |
+
import numpy as np
|
| 18 |
+
import pandas as pd
|
| 19 |
+
import torch
|
| 20 |
+
import torch.nn as nn
|
| 21 |
+
import torch.nn.functional as F
|
| 22 |
+
import timm
|
| 23 |
+
import librosa
|
| 24 |
+
import soundfile as sf
|
| 25 |
+
from collections import defaultdict
|
| 26 |
+
|
| 27 |
+
# =========================
|
| 28 |
+
# PATHS
|
| 29 |
+
# =========================
|
| 30 |
+
COMP_DIR = "/kaggle/input/competitions/birdclef-2026"
|
| 31 |
+
TEST_DIR = f"{COMP_DIR}/test_soundscapes"
|
| 32 |
+
SAMPLE_SUB = f"{COMP_DIR}/sample_submission.csv"
|
| 33 |
+
|
| 34 |
+
# Model directory with ALL fold models
|
| 35 |
+
MODEL_DIR = "/kaggle/input/datasets/vivekgaur9972/birdclef-nb02-models/nb02-model/models"
|
| 36 |
+
|
| 37 |
+
DEVICE = "cpu" # Kaggle submission = CPU only
|
| 38 |
+
|
| 39 |
+
# =========================
|
| 40 |
+
# LOAD SAMPLE SUBMISSION
|
| 41 |
+
# =========================
|
| 42 |
+
sample = pd.read_csv(SAMPLE_SUB)
|
| 43 |
+
SPECIES = [c for c in sample.columns if c != "row_id"]
|
| 44 |
+
NUM_CLASSES = len(SPECIES)
|
| 45 |
+
|
| 46 |
+
# =========================
|
| 47 |
+
# MODEL ARCHITECTURE
|
| 48 |
+
# =========================
|
| 49 |
+
class Model(nn.Module):
|
| 50 |
+
def __init__(self, backbone):
|
| 51 |
+
super().__init__()
|
| 52 |
+
self.backbone = timm.create_model(backbone, pretrained=False, in_chans=3, features_only=True)
|
| 53 |
+
fi = self.backbone.feature_info
|
| 54 |
+
ch = fi[-2]['num_chs'] + fi[-1]['num_chs']
|
| 55 |
+
self.pool = nn.AdaptiveAvgPool2d(1)
|
| 56 |
+
self.fc = nn.Linear(ch, NUM_CLASSES)
|
| 57 |
+
|
| 58 |
+
def forward(self, x):
|
| 59 |
+
f = self.backbone(x)
|
| 60 |
+
f3, f4 = f[-2], f[-1]
|
| 61 |
+
if f3.shape[2:] != f4.shape[2:]:
|
| 62 |
+
f4 = F.interpolate(f4, size=f3.shape[2:])
|
| 63 |
+
x = torch.cat([f3, f4], 1)
|
| 64 |
+
x = self.pool(x).squeeze(-1).squeeze(-1)
|
| 65 |
+
return self.fc(x)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# =========================
|
| 69 |
+
# LOAD ALL MODELS
|
| 70 |
+
# =========================
|
| 71 |
+
MODELS = []
|
| 72 |
+
|
| 73 |
+
# Load B0 models (5 folds)
|
| 74 |
+
for fold in range(5):
|
| 75 |
+
path = f"{MODEL_DIR}/b0_fold{fold}.pt"
|
| 76 |
+
if os.path.exists(path):
|
| 77 |
+
m = Model("tf_efficientnet_b0_ns")
|
| 78 |
+
m.load_state_dict(torch.load(path, map_location=DEVICE), strict=False)
|
| 79 |
+
m.eval()
|
| 80 |
+
MODELS.append(("b0", m))
|
| 81 |
+
print(f" Loaded b0_fold{fold}")
|
| 82 |
+
else:
|
| 83 |
+
print(f" [MISSING] b0_fold{fold}")
|
| 84 |
+
|
| 85 |
+
# Load B3 models (5 folds)
|
| 86 |
+
for fold in range(5):
|
| 87 |
+
path = f"{MODEL_DIR}/b3_fold{fold}.pt"
|
| 88 |
+
if os.path.exists(path):
|
| 89 |
+
m = Model("tf_efficientnet_b3_ns")
|
| 90 |
+
m.load_state_dict(torch.load(path, map_location=DEVICE), strict=False)
|
| 91 |
+
m.eval()
|
| 92 |
+
MODELS.append(("b3", m))
|
| 93 |
+
print(f" Loaded b3_fold{fold}")
|
| 94 |
+
else:
|
| 95 |
+
print(f" [MISSING] b3_fold{fold}")
|
| 96 |
+
|
| 97 |
+
print(f"\n✅ Total models loaded: {len(MODELS)}")
|
| 98 |
+
|
| 99 |
+
# =========================
|
| 100 |
+
# SPECTROGRAM UTILITIES
|
| 101 |
+
# =========================
|
| 102 |
+
def make_spec(chunk, n_fft, hop):
|
| 103 |
+
mel = librosa.feature.melspectrogram(
|
| 104 |
+
y=chunk, sr=32000, n_fft=n_fft, hop_length=hop, n_mels=128, fmin=20, fmax=16000
|
| 105 |
+
)
|
| 106 |
+
mel = librosa.power_to_db(mel)
|
| 107 |
+
mel = (mel - mel.min()) / (mel.max() - mel.min() + 1e-6)
|
| 108 |
+
return np.stack([mel] * 3).astype(np.float32)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# =========================
|
| 112 |
+
# TTA: Generate augmented chunks
|
| 113 |
+
# =========================
|
| 114 |
+
def tta_chunks(chunk):
|
| 115 |
+
"""Return list of TTA variants: original, time-reversed, +3dB, -3dB."""
|
| 116 |
+
chunks = [chunk]
|
| 117 |
+
# Time reversal
|
| 118 |
+
chunks.append(chunk[::-1].copy())
|
| 119 |
+
# Gain +3dB
|
| 120 |
+
chunks.append(chunk * (10 ** (3 / 20)))
|
| 121 |
+
# Gain -3dB
|
| 122 |
+
chunks.append(chunk * (10 ** (-3 / 20)))
|
| 123 |
+
return chunks
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
# =========================
|
| 127 |
+
# INFERENCE
|
| 128 |
+
# =========================
|
| 129 |
+
files = sorted([
|
| 130 |
+
f for f in os.listdir(TEST_DIR)
|
| 131 |
+
if f.endswith((".ogg", ".wav", ".flac", ".mp3"))
|
| 132 |
+
])
|
| 133 |
+
|
| 134 |
+
print(f"\n✅ Found {len(files)} test files")
|
| 135 |
+
|
| 136 |
+
row_ids = []
|
| 137 |
+
all_preds = [] # list of (row_id, pred_array) per model for rank averaging
|
| 138 |
+
|
| 139 |
+
for file_idx, fname in enumerate(files):
|
| 140 |
+
path = os.path.join(TEST_DIR, fname)
|
| 141 |
+
stem = fname.rsplit(".", 1)[0]
|
| 142 |
+
|
| 143 |
+
try:
|
| 144 |
+
wav, sr = sf.read(path, dtype='float32')
|
| 145 |
+
except Exception as e:
|
| 146 |
+
print(f" [SKIP] {fname}: {e}")
|
| 147 |
+
continue
|
| 148 |
+
|
| 149 |
+
if wav.ndim > 1:
|
| 150 |
+
wav = wav.mean(1)
|
| 151 |
+
if sr != 32000:
|
| 152 |
+
wav = librosa.resample(wav, orig_sr=sr, target_sr=32000)
|
| 153 |
+
|
| 154 |
+
# Process each 5-second segment
|
| 155 |
+
for sec in range(0, 60, 5):
|
| 156 |
+
row_id = f"{stem}_{sec + 5}"
|
| 157 |
+
row_ids.append(row_id)
|
| 158 |
+
|
| 159 |
+
start = sec * 32000
|
| 160 |
+
chunk = wav[start:start + 32000 * 5]
|
| 161 |
+
if len(chunk) < 32000 * 5:
|
| 162 |
+
chunk = np.pad(chunk, (0, 32000 * 5 - len(chunk)))
|
| 163 |
+
|
| 164 |
+
# Generate spectrograms for both model types
|
| 165 |
+
spec_b0 = make_spec(chunk, 1024, 64) # matches B0 training
|
| 166 |
+
spec_b3 = make_spec(chunk, 2048, 512) # matches B3 training
|
| 167 |
+
|
| 168 |
+
# TTA variants
|
| 169 |
+
tta_b0 = [make_spec(c, 1024, 64) for c in tta_chunks(chunk)]
|
| 170 |
+
tta_b3 = [make_spec(c, 2048, 512) for c in tta_chunks(chunk)]
|
| 171 |
+
|
| 172 |
+
# Collect predictions from ALL models with TTA
|
| 173 |
+
model_logits = [] # list of logits arrays, one per (model, tta) combination
|
| 174 |
+
|
| 175 |
+
for model_name, model in MODELS:
|
| 176 |
+
if model_name == "b0":
|
| 177 |
+
specs = tta_b0
|
| 178 |
+
else:
|
| 179 |
+
specs = tta_b3
|
| 180 |
+
|
| 181 |
+
for spec in specs:
|
| 182 |
+
t = torch.tensor(spec).unsqueeze(0)
|
| 183 |
+
with torch.no_grad():
|
| 184 |
+
logits = model(t).numpy()[0]
|
| 185 |
+
model_logits.append(logits)
|
| 186 |
+
|
| 187 |
+
# Average logits across all models and TTA variants
|
| 188 |
+
# This preserves relative ranking better than prob averaging
|
| 189 |
+
avg_logits = np.mean(model_logits, axis=0)
|
| 190 |
+
probs = 1.0 / (1.0 + np.exp(-avg_logits)) # sigmoid
|
| 191 |
+
|
| 192 |
+
all_preds.append(probs)
|
| 193 |
+
|
| 194 |
+
if (file_idx + 1) % 100 == 0 or file_idx == 0:
|
| 195 |
+
print(f" Progress: {file_idx+1}/{len(files)}")
|
| 196 |
+
|
| 197 |
+
# =========================
|
| 198 |
+
# BUILD SUBMISSION
|
| 199 |
+
# =========================
|
| 200 |
+
if len(all_preds) == 0:
|
| 201 |
+
print("⚠️ No predictions generated → filling zeros")
|
| 202 |
+
preds = np.zeros((len(row_ids), NUM_CLASSES))
|
| 203 |
+
else:
|
| 204 |
+
preds = np.vstack(all_preds)
|
| 205 |
+
|
| 206 |
+
# Create submission dataframe
|
| 207 |
+
sub = pd.DataFrame(preds, columns=SPECIES)
|
| 208 |
+
sub.insert(0, "row_id", row_ids)
|
| 209 |
+
|
| 210 |
+
# CRITICAL: Align with sample submission (same row order, same columns)
|
| 211 |
+
sub = sample[["row_id"]].merge(sub, on="row_id", how="left").fillna(0)
|
| 212 |
+
|
| 213 |
+
# Verify column order matches sample exactly
|
| 214 |
+
assert list(sub.columns) == list(sample.columns), "Column mismatch!"
|
| 215 |
+
|
| 216 |
+
# =========================
|
| 217 |
+
# POST-PROCESSING (MINIMAL)
|
| 218 |
+
# =========================
|
| 219 |
+
# Based on your history: the ONLY thing that didn't destroy score was
|
| 220 |
+
# tiny clipping of obviously garbage values.
|
| 221 |
+
# DO NOT threshold. DO NOT calibrate. DO NOT normalize per-row.
|
| 222 |
+
|
| 223 |
+
# Optional: set extremely tiny values to 0 (noise floor)
|
| 224 |
+
# Keep this VERY conservative — your 0.815 used 0.003
|
| 225 |
+
# With better models, even this may hurt, so default to no clipping:
|
| 226 |
+
# sub[SPECIES] = sub[SPECIES].clip(lower=0) # already non-negative
|
| 227 |
+
|
| 228 |
+
# If you want to be safe and match your 0.815 style:
|
| 229 |
+
for sp in SPECIES:
|
| 230 |
+
sub[sp] = sub[sp].clip(lower=0)
|
| 231 |
+
|
| 232 |
+
# =========================
|
| 233 |
+
# SAVE
|
| 234 |
+
# =========================
|
| 235 |
+
sub.to_csv("submission.csv", index=False)
|
| 236 |
+
|
| 237 |
+
print("\n" + "=" * 60)
|
| 238 |
+
print("SUBMISSION READY")
|
| 239 |
+
print("=" * 60)
|
| 240 |
+
print(f" Rows: {len(sub)}")
|
| 241 |
+
print(f" Columns: {len(sub.columns)}")
|
| 242 |
+
print(f" row_id match: {sub['row_id'].tolist() == sample['row_id'].tolist()}")
|
| 243 |
+
print(f" Mean prob: {sub[SPECIES].values.mean():.6f}")
|
| 244 |
+
print(f" Max prob: {sub[SPECIES].values.max():.6f}")
|
| 245 |
+
print(f" Nonzero: {(sub[SPECIES].values > 0).mean():.4f}")
|
| 246 |
+
print("=" * 60)
|