The embodiment of the invention provides a defect sample generation method and device. In the embodiment of the invention, a query original image set and a prompt small image set of a defect image can be obtained, and
image query is performed on the query original image set based on each prompt small image in the prompt small image set. Obtaining a first segmentation
mask of a defect image of each query original image in the query original image set and a corresponding defect category; performing expansion operation on the segmentation
mask of the defect image of each query original image in the query original image set, and generating a second segmentation
mask of the expanded defect image of each query original image in the query original image set; and generating a defect
diffusion editing image corresponding to each query original image in the query original image set based on a starting point and an end point determined by a plurality of rays uniformly emitted in a plurality of specified directions in the first segmentation mask and the second segmentation mask, and the second segmentation mask, and generating a defect sample based on the defect
diffusion editing image and the corresponding defect category corresponding to each query original image in the query original image set.