# DScanNet: Packaging Defect Detection Algorithm Based on Selective State Space Models
## 3 Proposed Method
### Mamba Block  
The design of Mamba Block is inspired by State Space Models (SSM). State Space Models are mathematical models used to map a one-dimensional input vector \(\mathrm{x}(\mathrm{t})\) to an N-dimensional potential state variable \(\mathrm{h}(\mathrm{t})\), which is then mapped to a one-dimensional output vector \(\mathrm{y}(\mathrm{t})\). SSMs are usually represented as linear ordinary differential equations (ODEs):  
\[\left\{ \begin{array}{l}\mathrm{h}^{\prime}(\mathrm{t})=\mathrm{Ah}(\mathrm{t })+\mathrm{Bx}(\mathrm{t})\\ \mathrm{y}(\mathrm{t})=\mathrm{Ch}(\mathrm{t})+\mathrm{Dx}(\mathrm{t})\end{array} \right. \tag{2}\]  
Deep learning cannot use State Space Models directly, and to solve this problem, ODEs need to be discretized into discrete functions. This conversion effectively aligns the model with the sampling rate of the underlying signals in the input data, thereby improving computational efficiency. Considering the input \(\mathrm{x}_{\mathrm{k}}\in\mathbb{R}^{\mathrm{L}\times\mathrm{D}}\), i.e., a vector of length L sampled in the signal stream, the equations can be discretized as follows according to the Zero Order Holding (ZOH) rule:  
\[\left\{ \begin{array}{l}\mathrm{h}_{\mathrm{k}}=\overline{\mathrm{A}}\mathrm{ h}_{\mathrm{k}-1}+\overline{\mathrm{B}}\mathrm{x}_{\mathrm{k}}\\ \mathrm{y}(\mathrm{k})=\overline{\mathrm{C}}\mathrm{h}_{\mathrm{k}}+\mathrm{Dx} _{\mathrm{k}}\end{array} \right. \tag{3}\]  
\[ \begin{array}{l}\overline{\mathrm{A}}=\mathrm{e}^{\mathrm{A}\mathrm{A}}\\ \overline{\mathrm{B}}=(\mathrm{A}\mathrm{A})^{-1}(\mathrm{e}^{\mathrm{A}\mathrm{ A}}-\mathrm{I})\mathrm{\Delta B}\\ \overline{\mathrm{C}}=\mathrm{C}\end{array}  \tag{4}\]  
where \(\mathrm{B},\mathrm{C}\in\mathbb{R}\mathrm{D}\) and \(\mathrm{I}\) is the unit matrix. After discretization, the SSM is computed by global convolution with a structured convolution kernel \(\overline{\mathrm{K}}\in\mathbb{R}\mathrm{D}\):  
\[ \begin{array}{l}\mathrm{y}=\mathrm{x}\ast\overline{\mathrm{K}}\\ \overline{\mathrm{K}}=(\mathrm{C}\overline{\mathrm{B}},\overline{\mathrm{C} \mathrm{A}\mathrm{B}},\cdots,\mathrm{C}\overline{\mathrm{A}}^{\mathrm{L}-1} \overline{\mathrm{B}})\end{array}  \tag{5}\]  
Based on the above equations, Mamba has devised a simple selection mechanism to parameterize the SSM parameters based on the inputs. The model filters out irrelevant information being this approach and remembers the relevant information indefinitely. However, the direct application of Mamba to visual detection tasks leads to the loss of information in the global receptive field. To address this challenge, VMamba proposes the VSS Block module. By introducing a two-dimensional selective scanning mechanism (SS2D), linear complexity can be preserved while maintaining the global sensory field. As shown in Figure 4, the core module of this paper is Mamba Block. The feature map is input followed by a series of feature extraction modules that enable the network to capture deeper features while keeping the training inference process efficient and stable through batch normalization. LFEM is used here to refer to the LFEM Block (see Section Local Feature Extraction Module (LFEM Block) for details). SS2D is composed of three parts: the scan expansion operation, the S6 module, and the scan merging operation. The scanning expansion operation expands the input image into sequences along four different directions (top left to bottom right, bottom right to top left, top right to bottom left, and bottom left to top right), which allows each pixel to capture information from other pixels in different directions. These sequences are then processed by the S6 module to extract features, ensuring that the information from each direction is fully scanned so that diverse features are captured. Subsequently, a scan-and-merge operation sums and merges the sequences from different directions [32].