"""OMEGA-TAG Mini v0.5: tiny, OFFLINE multilabel spectral-event model. Model shipped with project uses *synthetic* two-stage training: isolated/pair pretraining, then overlapping-mixture fine-tuning. It has NEVER been trained on real labelled music, so it must not claim reliable instrument identities. All scores are uncalibrated classifier activations, not probabilities. No remote downloads or unsafe pickle deserialization. """ from __future__ import annotations from pathlib import Path import hashlib import json import math import numpy as np import soundfile as sf from scipy.signal import resample_poly LABELS = ("bass_sustain", "kick_like", "snare_like", "hi_hat_like", "harmonic_mid", "noise_texture") FRIENDLY = { "bass_sustain": "Tieffrequente, anhaltende Töne / Bass-Verdacht", "kick_like": "Tieffrequente Impulse / Kick-Verdacht", "snare_like": "Breitbandige Mitten-Impulse / Snare-Verdacht", "hi_hat_like": "Helle, kurze Impulse / Hi-Hat-Verdacht", "harmonic_mid": "Harmonische Mitten / Melodie-Verdacht", "noise_texture": "Rauschähnliche Textur (kann musikalisch sein)", } SR = 8000 N = 4096 MODEL_FILE = Path(__file__).resolve().parent / "models" / "omega_tag_mini_v1.npz" BANDS = ((25,80),(80,180),(180,400),(400,900),(900,1900),(1900,3500),(3500,3950)) def features(x: np.ndarray) -> np.ndarray: """32 stable feature components from 0.512s mono resampled audio. Features are descriptive and deliberately do not identify instruments through semantic embedding / pretrained audio foundation features. """ x = np.asarray(x, dtype=np.float64).reshape(-1) if len(x) != N: raise ValueError(f"Expected {N} samples, got {len(x)}") if not np.isfinite(x).all(): raise ValueError("Non-finite features input") # Fixed frames. Use per-frame relative energies for spectral activity. windows = np.stack([x[i:i+1024] for i in range(0, N-1024+1, 384)], axis=0) fft = np.abs(np.fft.rfft(windows*np.hanning(1024), axis=-1))**2+1e-15 hz = np.fft.rfftfreq(1024, 1./SR) bands=np.stack([fft[:,(hz>=lo)&(hz 35 result=np.r_[avg,variability,np.clip(logenergy,-10,7), np.clip(logenergy-logenergy.mean(),-12,12), logtotal, flatness,zcr,np.log1p(novelty), np.log1p(fluctuation), np.log1p(crest),spectral_flux] return np.asarray(result,dtype=np.float32) FEATURE_COUNT = len(features(np.zeros(N,dtype=np.float32))) def synthetic_signal(label: str, rng: np.random.Generator) -> np.ndarray: """Generated test/training signals, NOT copyrighted music nor real stems.""" t=np.arange(N)/SR x=np.zeros(N,dtype=np.float64) if label=="bass_sustain": freq=rng.uniform(46,160) for harmonic in (1,2,3): x+=np.sin(2*np.pi*(harmonic*freq*t+rng.uniform(0,1)))*(.6/harmonic**1.3) x*=.4+.6*np.sin(2*np.pi*rng.uniform(.3,2)*t+rng.uniform(0,6))**2 elif label=="kick_like": for loc in rng.choice(np.arange(0,N-850,300),size=int(rng.integers(1,4)),replace=False): k=np.arange(min(1500,N-loc)); time=k/SR phase=2*np.pi*(50*time+90*(1-np.exp(-time*32))/32) env=np.exp(-time*rng.uniform(17,35)) x[loc:loc+len(k)] += np.sin(phase)*env elif label=="snare_like": for loc in rng.choice(np.arange(0,N-450,280),size=int(rng.integers(1,4)),replace=False): k=np.arange(min(850,N-loc)); time=k/SR pulse=rng.standard_normal(len(k)) pulse=np.r_[0,np.diff(pulse)] x[loc:loc+len(k)] += pulse*np.exp(-time*27)*.3 elif label=="hi_hat_like": for loc in rng.choice(np.arange(0,N-150,170),size=int(rng.integers(2,7)),replace=False): k=np.arange(min(380,N-loc));time=k/SR noise=rng.standard_normal(len(k)) noise=np.r_[0,np.diff(noise)] x[loc:loc+len(k)]+=noise*np.exp(-time*85)*.35 elif label=="harmonic_mid": for freq in rng.uniform(200,900,size=int(rng.integers(1,4))): x+=np.sin(2*np.pi*(freq*t+rng.uniform(0,1)))*.4 x*=.3+.7*(np.sin(np.pi*np.clip(t/rng.uniform(.25,.85),0,1))**2) elif label=="noise_texture": noise=rng.standard_normal(N) if rng.random()<.6: noise=np.convolve(noise,np.ones(37)/37,mode='same') x += noise*.3 return x.astype(np.float32) def synthetic_case(rng:np.random.Generator, phase: str) -> tuple[np.ndarray,np.ndarray]: max_sources=2 if phase=="pretrain" else 5 count=int(rng.integers(1,max_sources+1)) indices=rng.choice(len(LABELS),size=count,replace=False) y=np.zeros(len(LABELS),dtype=np.float32) x=np.zeros(N,dtype=np.float32) for idx in indices: y[idx]=1. x+=synthetic_signal(LABELS[idx],rng)*rng.uniform(.2,1.0) # Apply nonstationary gain, external hum/noise and polarity changes to test # robustness; all remain synthetic. if rng.random()<.4:x*=np.linspace(rng.uniform(.35,1),rng.uniform(.35,1),N) if rng.random()<.5:x+=rng.standard_normal(N).astype(np.float32)*rng.uniform(0,.016) if rng.random()<.5:x*=-1 peak=max(float(np.max(np.abs(x))),1e-8) x*=float(rng.uniform(.15,.8))/peak return x,y def generate_training(n:int,seed:int,phase:str): rng=np.random.default_rng(seed) X=np.empty((n,FEATURE_COUNT),np.float32) Y=np.empty((n,len(LABELS)),np.float32) for i in range(n): x,y=synthetic_case(rng,phase) X[i]=features(x);Y[i]=y return X,Y def _sigmoid(z): return 1/(1+np.exp(-np.clip(z,-35,35))) def _net(X,W1,b1,W2,b2): h=np.tanh(X@W1+b1) return h,_sigmoid(h@W2+b2) def train_model(destination:Path=MODEL_FILE,seed:int=25,epochs1:int=90,epochs2:int=105, pretrain_count:int=650,finetune_count:int=1100) -> dict: """Offline reproducible 2-stage neural training, using only synthetic audio. Stage 2 is real parameter fine-tuning of stage 1 weights but NOT transfer learning from a commercial foundation model or real instrument recordings. """ rng=np.random.default_rng(seed) xp,yp=generate_training(pretrain_count,seed,"pretrain") xf,yf=generate_training(finetune_count,seed+1,"finetune") xt,yt=generate_training(300,seed+2,"finetune") mu=xp.mean(axis=0) sigma=np.maximum(xp.std(axis=0),.08) xp=np.clip((xp-mu)/sigma,-8,8) xf=np.clip((xf-mu)/sigma,-8,8) xt=np.clip((xt-mu)/sigma,-8,8) hidden=32 W1=(rng.standard_normal((FEATURE_COUNT,hidden))*.15).astype(np.float64) b1=np.zeros(hidden) W2=(rng.standard_normal((hidden,len(LABELS)))*.15).astype(np.float64) b2=np.zeros(len(LABELS)) params=[W1,b1,W2,b2] mt=[np.zeros_like(p) for p in params] vt=[np.zeros_like(p) for p in params] step=0 def loop(X,Y,epochs,lr): nonlocal step for epoch in range(epochs): for batchidx in np.array_split(rng.permutation(len(X)),max(1,len(X)//96)): data,labels=X[batchidx],Y[batchidx] h,prob=_net(data,*params) grad=(prob-labels)/len(batchidx) g2=h.T@grad gb2=grad.sum(axis=0) gh=(grad@W2.T)*(1-h*h) g1=data.T@gh gb1=gh.sum(axis=0) for idx, g in enumerate((g1,gb1,g2,gb2)): step+=1 if idx==0 else 0 mt[idx][:]=.9*mt[idx]+.1*g vt[idx][:]=.999*vt[idx]+.001*g*g mhat=mt[idx]/(1-.9**step) vhat=vt[idx]/(1-.999**step) params[idx]-=lr*mhat/(np.sqrt(vhat)+1e-8) loop(xp,yp,epochs1,.012) loop(xf,yf,epochs2,.006) h,pred=_net(xt,*params) estimates=pred>=.5 precision=float(np.sum((estimates==1)&(yt==1))/max(1,np.sum(estimates==1))) recall=float(np.sum((estimates==1)&(yt==1))/max(1,np.sum(yt==1))) acc=float(np.mean(estimates==yt)) destination.parent.mkdir(parents=True,exist_ok=True) np.savez_compressed(destination, W1=W1.astype(np.float32),b1=b1.astype(np.float32), W2=W2.astype(np.float32),b2=b2.astype(np.float32), mu=mu.astype(np.float32),sigma=sigma.astype(np.float32), model_schema=np.array("omega_mini_synthetic_v1"), labels=np.asarray(LABELS)) return {"stage1":"synthetic isolated/pair pretraining", "stage2":"fine-tuning on synthetic overlapping mixtures", "heldout_synthetic_precision":round(precision,3), "heldout_synthetic_recall":round(recall,3), "heldout_synthetic_label_accuracy":round(acc,3), "validation_dataset":"300 held-out synthetic mixtures, NOT real-world benchmark", "checkpoint":str(destination), "checkpoint_sha256":hashlib.sha256(destination.read_bytes()).hexdigest(), "architecture":f"feature({FEATURE_COUNT}) -> tanh(32) -> sigmoid({len(LABELS)})", "train_examples":pretrain_count+finetune_count} def load_model(path:Path=MODEL_FILE)->dict: if not path.is_file(): raise FileNotFoundError("Mini model missing. Run python -m app.mini_model --train") with np.load(path,allow_pickle=False) as f: params={k:f[k].copy() for k in ("W1","b1","W2","b2","mu","sigma")} schema=str(f["model_schema"].item()) labels=tuple(str(x) for x in f["labels"]) if schema!="omega_mini_synthetic_v1" or labels!=LABELS: raise ValueError("Unsupported/incompatible mini model weights") if params["W1"].shape!=(FEATURE_COUNT,32) or params["W2"].shape!=(32,len(LABELS)): raise ValueError("Corrupt mini model dimensions") if not all(np.isfinite(v).all() for v in params.values()): raise ValueError("Non-finite model weights") return params def predict_features(x:np.ndarray,model:dict)->np.ndarray: scaled=np.clip((x-model["mu"])/model["sigma"],-8,8) return _net(scaled,model["W1"],model["b1"],model["W2"],model["b2"])[1] def inspect_file(path:Path,max_windows:int=12, model_path:Path=MODEL_FILE)->dict: """Inspect strategically sampled 0.512s segments; bounded RAM, any duration.""" model=load_model(model_path) scores=[] with sf.SoundFile(str(path)) as source: if source.frames<=0: raise ValueError("Empty audio") count=min(max_windows,max(1,math.ceil(source.frames/source.samplerate/.65))) positions=np.linspace(0,max(0,source.frames-int(.55*source.samplerate)),count,dtype=int) for pos in positions: source.seek(int(pos)) block=source.read(min(source.frames-int(pos),int(.65*source.samplerate)), dtype="float32",always_2d=True) if not np.isfinite(block).all():raise ValueError("Invalid audio samples") mono=block.mean(axis=1) if len(mono)>0: g=math.gcd(source.samplerate,SR) mono=resample_poly(mono,SR//g,source.samplerate//g) if len(mono)=.55), "note":"Hypothesis only. May be wrong for real recordings."} for label,prob in zip(LABELS,score)], "limitations":["No pretrained music foundation model", "No isolated real music-stem training data", "Scores are not calibrated for real recordings", "Short sampled windows may miss rare or late events"], } if __name__=="__main__": import sys if sys.argv[1:]==["--train"]: details=train_model() print(json.dumps(details,indent=2)) else: print("Use: python -m app.mini_model --train")