Loss functions for defect detection