# Balanced Loss Function for Accurate Surface Defect Segmentation
## 1 Introduction  
The automatic visual inspection of surface defects has significant value for industrial operation safety and manufacturing quality control. Based on DCNN (Deep Convolutional Neural Networks), surface defect segmentation provides pixel-level defectiveness recognition and enables accurate analysis on surface defects, which is important for applications such as the evaluation of metallic surface defectiveness severity. Surface defect segmentation has been recently receiving increasing interest from researchers for its practical value in application. Most works adapt existing methods of natural scene image semantic segmentation to surface defect segmentation. For example, [PERSON] et al. [1] propose a feature enhancement network in addition to the segmentation network, where the feature map resolution is maintained in the encoder. Similarly, TLUNet [2] makes an independent classification prediction from the bottleneck features in U-Net. [PERSON] et al. [3] applies class-activation mapping to a classification model to address a weak supervision problem in defect segmentation. Some other mature models and techniques in semantic segmentation are also introduced to this field, such as saliency detection [4], feature pyramid [5] and autolabeling [6]. In this paper, we focus on the segmentation loss function to improve the accuracy of surface defect segmentation. We aim to tackle the loss imbalance problem that occurs in the training phase of segmentation models. The loss imbalance problem is a serial of imbalance problems observed from the training loss of surface defect segmentation models. We qualitatively define the loss imbalance problem of segmentation as follows: when loss imbalance occurs in model training, the loss value is dominated by a (often small) group of points, which has unequally minor influence for the segmentation accuracy. Ideally, the loss function is consistent withsegmentation accuracy metrics, such as intersection-over-union (IoU) or Dice coefficient. With proper regularization and the gradient descent algorithm, the loss value will drop and the segmentation accuracy (at least on training data) will improve simultaneously. However, with imbalanced loss, the reduced loss does not signify better segmentation accuracy, because the model parameters are updated to fit some unimportant examples. It usually incurs over-fitting on a portion of examples. Even worse, if the dominant examples have noisy labels, the model training hardly converges. Our investigation reveals three kinds of loss imbalance that emerge from the training of surface defect segmentation models: label imbalance, easy-hard example imbalance and boundary imbalance. Our experimental findings show that the three types of loss imbalance cause stagnation of training or over-fitting in different manners. The motivation of this paper is tackling the loss imbalance problem by a balanced loss function. To the best of our knowledge, no existing segmentation loss function addresses different loss imbalance problems in a unified loss function. The loss imbalance problem exists in many commonly employed segmentation loss functions. Cross-entropy loss, first used by [PERSON] et al. [7] in semantic segmentation, has been the most widely used loss function for segmentation so far. It treats segmentation as a normal pixel-wise classification problem. However, the label imbalance problem, often the imbalance between foreground and background pixels, impels people to use weighted cross-entropy loss [8] to manually amplify the influence of under-represented classes in training. Focal loss [9], which is designed to reinforce the focus on hard examples, is also introduced to segmentation. However, focal loss will aggravate the easy-hard example imbalance, which is contradictive against our aim. Specialized for segmentation tasks, IoU loss [10], Dice loss [11] and Tversky loss [12] are proposed to approximate the segmentation performance metrics, such as IoU and Dice score.