This disclosure provides a surface defect detection method, apparatus, electronic device, and storage medium. The method includes
parsing an image to be inspected based on a texture analysis model to obtain a token set and a reconstructed image, and determining a residual map between the two. Residual features are extracted from the residual map, and a residual anomaly
score of the image to be inspected is determined based on the residual features. The shape, scale, orientation, and position of all tokens in the token set are statistically analyzed to obtain token statistical features, and a batch anomaly
score of the image to be inspected is determined based on the token statistical features. Based on the residual anomaly
score and the batch anomaly score, the surface defect detection result of the product to be inspected is determined and output. The surface defect detection result includes attribution explanation information. This method can accurately detect weak-contrast large-scale muras, reducing the false negative rate of muras, and can also provide traceable anomaly attribution information to facilitate process adjustments.