## 4 Materials and Methods
### Model Architecture are Experiment Description
We arbitrarily decided to use 1000 elements in FC1 and FC2 each, as our PCB images are not so varied by its nature, in comparison with ImageNet images of 1000 classes. The length of dense SoftMax layer corresponds to the number of patches in PCB. The training of the modified network is done using normal PCBs (66 grids per PCB). We tried several ways to organize the training process and force our model to learn semantic image features from patches [36]. The most promising approach appears to be the following:  
Figure 5: Modified VGG16 deep network (architecture A1). Figure 6: The number in each patch is a distance to the normal cluster. Blue number areas are identified as normal, red ones are identified defective (the distance is above a threshold). * To choose the size of the patch comparable to the size of individual elements, as too small patch size will lead to extra sensitivity to normal minor changes, and too large patch size will result in a lack of discriminative power to detect and localize the defect. The patch size will affect the number of classes K, in which our PCB will be divided. * To feed the patches randomly rotated by 0, 90, 180, and 270 degrees to the described configuration of the network. In our implementation for PCB analysis, the input image (patch) size of \(100\times 100\) pixels was chosen. It gave us the flatten layer output tensor of 4608 elements (Figure 4) and the number of network parameters (before FC layers) as 14.7M. The number of trainable parameters in FC layers is 5.8M. The network was trained to match the patch (potentially rotated) to its location on PCB, i.e., to match it to the correct number of class K, which is the output of the SoftMax layer, using the categorical cross-entropy loss function. After training the new model by using only the normal patches, we remove the FC2 and the SoftMax layers, and the output of the FC1 layer is used as the feature vector for a given input for the architecture A1. Layers FC1 and FC2 extract different information patterns. Later we will check which layer's feature vector contains more relevant information about the input image structures to distinguish a patch with a normal design from a defective one. The other architectures (A2 and A0) are the same as A1, with the following modifications: the output feature vectors are output of FC2 (see Figure 5) and FC1 (Figure 4) layers, respectively. In other words, in A0 architecture we use the feature vector (the output of FC1 layer) of the original VGG16 without any training by the RotNet. The difference between A1 and A2 is in the choice of which fully-connected layer outputs to use. The FC1 layer extracts more generic features of the image processed, and FC2 would operate with more specific information. It will be shown that the choice between FC1 and FC2 is crucial. In [35], the authors trained RotNet without usage of transfer learning model as the pre-trained feature extractor, opposite to this article's proposed approach. For the training of the class K patches (K from 1 to 66 in this case), we calculate a set of normal signatures (representations) for the class K, containing multidimensional feature vectors. This set defines a cluster, which contains all representations for normal samples for this class. This cluster may consist of disjoint sets. When a patch (belonging to a certain class K) of the testing image is analyzed, the feature vector of the patch is calculated first, and then its distance from the cluster of normal representations of the same class is found. If this distance exceeds a certain threshold, the patch is considered defective. This process is repeated for all the patches of the test image, and the location of a defective patch is marked on the PCB. Currently, the threshold value is set manually and is same for all the classes. The threshold value cannot be defined automatically as it depends on the product itself, and our system has no prior information about the nature of the defects and acceptable deviations.