# Zero-Shot Detection of Visual Food Safety Hazards via Knowledge-Enhanced Feature Synthesis
## 3 Methodology
### Zero-Shot Detector Training
#### 3.4.4 Detector Integration  
Finally, we integrate the trained unseen classifier into the original detector by replacing or extending the classification layer. For GZSD, where both seen and unseen classes may be present, we calibrate the confidence scores of seen and unseen predictions to address the bias towards seen classes:  
\[\hat{p}_{i}=\begin{cases}\gamma p_{i},&\text{if }i\in\mathcal{Y}_{s}\\ p_{i},&\text{if }i\in\mathcal{Y}_{u}\end{cases} \tag{21}\]  
where \(p_{i}\) is the original confidence score for class \(i\), \(\hat{p}_{i}\) is the calibrated score, and \(\gamma\in(0,1)\) is a calibration factor that reduces the confidence scores of seen classes to balance the detection performance.  
Our training process follows a systematic sequence of operations designed to efficiently transfer knowledge from seen to unseen hazard classes, as detailed in Algorithm 1. The process begins with training a standard object detector on the set of seen hazard classes, using annotated bounding boxes to learn visual representations of known food safety issues. Once the detector is trained, we extract region features from the training images, which serve as the real visual representations that our generative model aims to emulate. We then initialize our Knowledge-Enhanced Feature Synthesizer with the Food Safety Knowledge Graph, establishing the domain-specific prior knowledge that will guide feature synthesis. The KEFS is subsequently trained using the extracted region features, semantic vectors for seen classes, and semantic vectors for unseen classes, optimizing the combined loss function that ensures both feature quality and semantic consistency. After training, the KEFS generates synthetic region features for unseen hazard classes based on their semantic descriptions, effectively creating visual representations for safety issues that were never seen during training. Using these synthesized features, we train a specialized classifier for unseen hazard classes, which is then integrated into the original detector framework. This integration enables the detector to recognize both seen and unseen hazard classes simultaneously, completing the knowledge transfer from semantic space to visual space. This methodical approach ensures robust performance in zero-shot detection scenarios by leveraging structured domain knowledge to generate discriminative features for novel food safety hazards.  
```
0: Training set \(\mathcal{X}_{s}\), semantic vectors \(\mathcal{V}_{s}\) and \(\mathcal{V}_{u}\), food safety knowledge graph \(\mathcal{G}\)
0: Zero-shot detector with parameters \(\omega_{d}\)
1:\(\omega_{d}\leftarrow\) Train detector on \(\mathcal{X}_{s}\) with annotations
2:\(\mathbf{H}_{s}\leftarrow\) Extract region features from \(\mathcal{X}_{s}\) using detector \(\omega_{d}\)
3:\(\mathcal{G}\leftarrow\) Initialize KEFS with knowledge graph \(\mathcal{G}\)
4:\(\mathcal{G}\leftarrow\) Train KEFS on \(\mathbf{H}_{s}\), \(\mathcal{V}_{s}\), and \(\mathcal{V}_{u}\) by optimizing \(\mathcal{L}_{KEFS}\)
5:\(\mathbf{H}_{u}\leftarrow\) Synthesize region features for unseen classes using trained KEFS, \(\mathcal{V}_{u}\)
6:\(\omega_{cls}^{u}\leftarrow\) Train unseen classifier using \(\mathbf{H}_{u}\) and labels \(\mathcal{Y}_{u}\)
7:\(\omega_{d}\leftarrow\) Update detector parameters with unseen classifier \(\omega_{cls}^{u}\)
8:return\(\omega_{d}\)
```  
**Algorithm 1** Training procedure for Zero-Shot Food Safety Hazard Detection