Defect detection with high precision is of great significance for printed circuit board (PCB) fabrication. Due to the lack of priori knowledge of categories and shape features, detection of unknown defects faces greater challenges than that of common defects. Inspired by similarity measurement, this study proposes a multi-layer deep feature fusion method to calculate the similarity between template and defective circuit board. Compared with conventional methods which divide the whole detection into two independent parts of hand-designed features and similarity measurement, the authors end-to-end model is designed to combine these two parts for joint optimisation. First, the Siamese network is utilised as their backbone architecture for feature extraction of pairwise images. And then the spatial pyramid pooling network is incorporated into the feature maps of each convolutional module to fuse the multi-scale feature vectors. Finally, the discriminative feature embedding and similarity metric are obtained by using the contrastive loss during the training process. Experimental results show that the proposed model has better performance in detecting and locating unknown defects in bare PCB images than traditional similarity measurement methods. Moreover, our method is promising for further improvement of defect detection with less training image pairs and more accurate detection results.