# Zero-Shot Detection of Visual Food Safety Hazards via Knowledge-Enhanced Feature Synthesis
## 3 Methodology
### Knowledge-Enhanced Feature Synthesizer
#### 3.3.2 Region Feature Diffusion Model  
RFDM generates visual features for unseen hazard classes conditioned on the knowledge representations from the MSGF module. Unlike traditional generative models such as GANs, the diffusion model offers more stable training and generates more diverse features, which is crucial for effective zero-shot detection.  
Let \(\mathbf{h}\in\mathbb{R}^{d}\) be a region feature vector. The diffusion process consists of two phases: a forward process that gradually adds noise to the data, and a reverse process that learns to recover the original data distribution.  
The forward diffusion process is defined as a Markov chain that gradually adds Gaussian noise to the region feature:  
\[\mathbf{h}_{t}=\sqrt{1-\gamma_{t}}\mathbf{h}_{t-1}+\sqrt{\gamma_{t}}\mathbf{z}_ {t} \tag{12}\]  
where \(\mathbf{h}_{0}=\mathbf{h}\) is the original region feature, \(\mathbf{h}_{t}\) is the feature at timestep \(t\), \(\mathbf{z}_{t}\sim\mathcal{N}(\mathbf{0},\mathbf{I})\) is Gaussian noise, and \(\gamma_{t}\in(0,1)\) is a noise schedule that determines the amount of noise added at each timestep.  
The reverse denoising process, parameterized by our model, aims to recover the original feature by predicting the noise component:  
\[p_{\theta}(\mathbf{h}_{t-1}|\mathbf{h}_{t},\mathbf{s})=\mathcal{N}(\mathbf{h} _{t-1}|\mu_{\theta}(\mathbf{h}_{t},t,\mathbf{s}),\Sigma_{\theta}(\mathbf{h}_{ t},t)\mathbf{I}) \tag{13}\]  
where \(\mathbf{s}\) is the knowledge representation from the MSGF module, \(\mu_{\theta}\) is the predicted mean, and \(\Sigma_{\theta}\) is a fixed covariance.  
The mean prediction is given by the following:  
\[\mu_{\theta}(\mathbf{h}_{t},t,\mathbf{s})=\frac{1}{\sqrt{\beta_{t}}}\left( \mathbf{h}_{t}-\frac{1-\beta}{\sqrt{1-\beta_{t}}}\mathbf{z}_{\theta}(\mathbf{ h}_{t},t,\mathbf{s})\right) \tag{14}\]  
where \(\beta_{t}=1-\gamma_{t}\), \(\tilde{\beta}_{t}=\prod_{i=1}^{t}\beta_{i}\), and \(\mathbf{z}_{\theta}(\mathbf{h}_{t},t,\mathbf{s})\) is the predicted noise component conditioned on the knowledge representation \(\mathbf{s}\).  
The model is trained by minimizing the mean squared error between the actual noise and the predicted noise:  
\[\mathcal{L}_{R}=\mathbb{E}_{\mathbf{h},\mathbf{z},\mathbf{s}}\left[\sum_{t=1} ^{T}\|\mathbf{z}_{t}-\mathbf{z}_{\theta}(\mathbf{h}_{t},t,\mathbf{s})\|^{2}\right] \tag{15}\]  
To ensure that the synthesized features are well-structured and discriminative, we introduce a graph denoising loss:  
\[\mathcal{L}_{G}=\mathbb{E}\left[-\frac{1}{\mathcal{C}}\sum_{k=1}^{3}\sum_{i=1} ^{\mathcal{C}}y_{i}\log(\hat{s}_{i})-\alpha\hat{s}_{i}\log(\sigma(\hat{ \mathbf{b}}_{i}^{k}))\right] \tag{16}\]  
where \(y_{i}\) is the class label, \(\hat{s}_{i}=\sigma(s_{i})\) with \(s_{i}\in\mathbb{R}^{C}\) being the \(i\)-th row vector of knowledge representation matrix \(\mathbf{S}\), \(\hat{\mathbf{b}}_{i}^{k}\in\mathbb{R}^{C}\) is the \(i\)-th row vector of matrix \(\mathbf{A}^{k}\mathbf{S}\), \(\sigma(\cdot)\) is the sigmoid function, and \(\alpha\) is a trade-off factor.