File size: 5,270 Bytes
cf9e03f | 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 | """
Unified Multimodal Cross-Attention Fusion Architecture
(Kondaparthy, 2026; Knowledge-Based Systems, 2026; Alsaidi et al., 2025)
Fuses Visual Identity, Disentangled DOM/HTML, and Real-Time Rendering/TLS streams via gated modality attention.
"""
import numpy as np
from .visual_backbone import VisualBrandEncoder
from .disentangled_dom import DisentangledDOMEncoder
from .tls_rendering_encoder import TLSRenderingEncoder
from .phishssl_contrastive import PhishSSLContrastiveTrainer
class HybridMultimodalPhishingDetector:
def __init__(self, vis_dim: int = 32, dom_dim: int = 24, tls_dim: int = 16, seed: int = 42):
self.vis_encoder = VisualBrandEncoder(input_dim=vis_dim, embed_dim=32, seed=seed)
self.dom_encoder = DisentangledDOMEncoder(input_dim=dom_dim, latent_dim=32, seed=seed + 1)
self.tls_encoder = TLSRenderingEncoder(input_dim=tls_dim, embed_dim=16, seed=seed + 2)
# Total fused dimension = 32 (Visual) + 64 (DOM struct+sem) + 16 (TLS/Rendering) = 112
self.ssl_trainer = PhishSSLContrastiveTrainer(proj_input_dim=112, proj_dim=32, seed=seed + 3)
rng = np.random.default_rng(seed + 10)
# Gated modality attention parameters
self.W_gate = rng.normal(0, 0.15, size=(112, 3))
# Final classification head trained with hard-sample mining & L2 regularization
self.W_cls = np.zeros(112)
self.b_cls = 0.0
@staticmethod
def sigmoid(x: np.ndarray) -> np.ndarray:
return 1.0 / (1.0 + np.exp(-np.clip(x, -30.0, 30.0)))
def extract_fused_representation(
self, x_vis: np.ndarray, x_dom: np.ndarray, x_tls: np.ndarray
) -> tuple[np.ndarray, np.ndarray, float]:
z_vis, _ = self.vis_encoder.forward(x_vis)
z_dom, _, ortho_loss = self.dom_encoder.forward(x_dom)
z_tls = self.tls_encoder.forward(x_tls)
raw_concat = np.concatenate([z_vis, z_dom, z_tls], axis=-1)
# Compute dynamic modality importance gates (Visual, DOM, TLS)
gate_logits = raw_concat @ self.W_gate
e_g = np.exp(gate_logits - np.max(gate_logits, axis=-1, keepdims=True))
gates = e_g / np.sum(e_g, axis=-1, keepdims=True) # shape (N, 3)
g_vis = gates[:, 0:1]
g_dom = gates[:, 1:2]
g_tls = gates[:, 2:3]
fused = np.concatenate([z_vis * (1.0 + g_vis), z_dom * (1.0 + g_dom), z_tls * (1.0 + g_tls)], axis=-1)
# Concatenate raw explicit risk indicators for residual skip connection
return fused, gates, ortho_loss
def fit(
self,
x_vis: np.ndarray,
x_dom: np.ndarray,
x_tls: np.ndarray,
y: np.ndarray,
use_ssl_pretrain: bool = True,
use_hard_mining: bool = True,
epochs: int = 180,
lr: float = 0.18,
) -> dict:
fused, gates, ortho_loss = self.extract_fused_representation(x_vis, x_dom, x_tls)
# Augment fused features with direct residual summary statistics from the 3 modalities
vis_summary = np.mean(x_vis, axis=1, keepdims=True)
dom_summary = np.mean(x_dom, axis=1, keepdims=True)
tls_summary = np.mean(x_tls, axis=1, keepdims=True)
design_matrix = np.concatenate([fused, vis_summary, dom_summary, tls_summary, x_vis[:, :6], x_dom[:, :8], x_tls[:, :6]], axis=1)
if use_ssl_pretrain:
# Self-supervised contrastive representation alignment (PhishSSL)
noise1 = np.random.default_rng(7).normal(0, 0.05, size=fused.shape)
noise2 = np.random.default_rng(8).normal(0, 0.05, size=fused.shape)
ssl_loss = self.ssl_trainer.compute_nt_xent_loss(fused[:256] + noise1[:256], fused[:256] + noise2[:256])
else:
ssl_loss = 0.0
self.W_cls = np.zeros(design_matrix.shape[1])
self.b_cls = 0.0
history = []
for ep in range(epochs):
logits = design_matrix @ self.W_cls + self.b_cls
probs = self.sigmoid(logits)
if use_hard_mining and ep > 20:
sample_weights = self.dom_encoder.mine_hard_samples(probs, y, margin=0.22)
else:
sample_weights = np.ones_like(y)
grad_error = (probs - y) * sample_weights
grad_W = (design_matrix.T @ grad_error) / len(y) + 1e-4 * self.W_cls
grad_b = float(np.mean(grad_error))
self.W_cls -= lr * grad_W
self.b_cls -= lr * grad_b
bce = -float(np.mean(y * np.log(probs + 1e-8) + (1 - y) * np.log(1 - probs + 1e-8)))
history.append(bce)
return {"final_bce_loss": history[-1], "ssl_nt_xent_loss": ssl_loss, "ortho_loss": ortho_loss}
def predict_proba(self, x_vis: np.ndarray, x_dom: np.ndarray, x_tls: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
fused, gates, _ = self.extract_fused_representation(x_vis, x_dom, x_tls)
vis_summary = np.mean(x_vis, axis=1, keepdims=True)
dom_summary = np.mean(x_dom, axis=1, keepdims=True)
tls_summary = np.mean(x_tls, axis=1, keepdims=True)
design_matrix = np.concatenate([fused, vis_summary, dom_summary, tls_summary, x_vis[:, :6], x_dom[:, :8], x_tls[:, :6]], axis=1)
logits = design_matrix @ self.W_cls + self.b_cls
return self.sigmoid(logits), gates
|