# A Fabric Defect Segmentation Model Based on Improved Swin-Unet with Gabor Filter
## 3 Proposed Method
### Model Design  
To solve the local optimal fitting problem of transformer-based models in fabric defect segmentation, we designed a network based on Swin-UNet. The overall architecture of the model is shown in Figure 2. In this section, we introduce the three stages of the model separately: (1) the fixed Gabor filter for preprocessing fabric texture features; (2) the U  
Figure 1: Swin Transformer block.  
shaped Swin Transformer Nntwork for defect segmentation; and (3) multi-stage result fusion for precisely locating defects.  
Stage 1. First, the input of the network is an RGB image. Considering that the Gabor filter is designed as a \(5\times 5\) kernel size convolution layer, the \(d\) attribute is the number of directions. When \(d=4\), the value of \(\theta\) of the Gabor filter is [0,45,90,135]; the schematic diagram of the filter kernel with different parameters \(\theta\) is shown in Figure 3. In addition, the Gabor filter processes RGB channels separately; thus, setting the convolution group as the number of input channels can ensure that RGB do not affect each other through Gabor filtering. In addition, the unfiltered original image is merged together as part of the output. Overall, the number of output channels can be calculated as \(c_{out}=c_{in}*d+c_{in}\). Although the Gabor filter is part of the network, its parameters are fixed, and do not participate in model training.  
Stage 2. After preprocessing, the U-shaped network is the main part of the model. After four times downsampling and upsampling, the features of the fabric are compressed and reconstructed. When Swin-Unet is used for fabric defect segmentation, two problems should be noted:  
1. The repetitive texture feature learning is much harder for Swin Transformer than CNN;  
2. Due to imbalanced positive and negative samples, Swin-Unet is more likely to stay at the local optimal solution, identifying all results as background, when using the BCE-Dice loss as the loss function.  
Through our experiments, we found that in order to maintain a basically consistent structure of the Swin Transformer in the downsampling stage, using a lighter structure in  
Figure 3: Gabor kernel under the condition of \(\theta=[0,45,90,135],kernel=256,\lambda=15,\gamma=0.5\), \(\sigma=25\).  
Figure 2: The architecture of our model.  
the upsampling stage can make model training easier. Therefore, we improved Swin-Unet by making it not completely symmetrical. The architecture hyperparameters of these model variants are:  
* Swin-Unet-T: C = 96, N = 6
* Swin-Unet-S: C = 96, N = 18
* Swin-Unet-B: C = 128, N = 18
* Swin-Unet-L: C = 192, N = 18  
where C is the channel number of the hidden layers in the first block and N is the layer number of the third block. With this setting, the pretraining parameters of the Swin Transformer can be loaded for transfer learning. The patch expanding layer is the same as in [PERSON]'s work on Swin-Unet [34]. It is very similar to the reverse patch merging operation. The difference is that we concatenate the features from the bottom and left in the skip connections in order to reduce the loss of spatial information caused by downsampling. In the U-shaped network, the output of each layer is used as the basis for the final decision.  
Stage 3. From a simple point of view, each Fusion Head is an upsampling module with different upsampling ratios. This multi-scale output prediction is common in target detection algorithms such as Faster RCNN. However, the difference is that we use the segmentation results of each layer as a reference instead of a simple superposition to select the final segmentation results.