Training neural networks for defect detection