## 2 Materials and Methods
### Methodology for Building and Improving Defect Detection Systems
#### 2.1.1 Stage 1--Creating a Set of Images for Training and Developing an Initial Neural Network Model  
Using the first set of 100 (25% of the total) boards (50 pcs of which were defective, and 50 pcs of which were not faulty), the color component thresholds were corrected so that every blue stain was found. The color range covered 100% of the defect areas on the surface of the boards. This area was allowed to be slightly wider than the actual defect areas, but could not be narrower. Approximately 20,000 (+/\(-\)1000) samples were collected from the area marked in \"color\". Creating a set involved cutting out square images representing the defect, spaced every 3 to 10 mm (distance between subsequent squares).  
Then, the first training of the neural network was performed. We present the following sample results obtained during one of the implementations at a sawmill:  
* Accuracy: 95%.
* Loss: 15%.
* Epoch: 10.  
Then, the images in the set were mixed and divided again into subsets: training and validation. The proportions of the sizes of the training and validation set were 3/4 and 1/4. This means that 3/4 of the set, after mixing the photos, was included in the training set. The remaining images were for validation. The same division applied to images representing 'no fault'. The collection size was 40,000.  
The next step was to verify the effectiveness of the obtained network model when scanning on subsequent boards as follows:  
1. The model correctly classifies defects and non-defects in approximately 50% of the cases.
2. The model programmed in the Keras environment returns values from 0 (for defect) to 1 (for non-defect).  
Below is a fragment of the code that built the model in the Keras environment (Listing 1). The input layer of the neural network model accepts images with dimensions of 32 \(\times\) 32. The activation functions of hidden layers are linear in a range \(>\) 0 of the input value (name: 'ReLU', \(max\) (0, \(x\))):  
\[y(x)=max(0,x)=\begin{cases}0&x<0\\ x&x\geq 0\end{cases} \tag{1}\]  
The last layer has two output neurons, and their activation functions have a sigmoid form:  
\[y(x)=\sigma(x)=\frac{1}{(1+e^{-x})}. \tag{2}\]``` sz=32 model=Sequential() model.add(Conv2D(32,(2,2),input\_shape=(sz,sz,3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Conv2D(32,(2,2))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Conv2D(64,(2,2))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Flatten()) model.add(Dense(64)) model.add(Activation('relu')) model.add(Dropout (0.5)) model.add(Dense(outputs))#OUTPUTSD  
Network training was performed, and the following statistics were obtained:  
1. The results for the first round of training were as follows: Accuracy: 85%; Loss: 21%; Epoch: 10.
2. The results for the second round of training were as follows: Accuracy: 91% Loss: 12% Epoch: 20.  
The network returned the following results:  
* 95% of places that are blue stain have an _NNDiff_ value > 50%;
* 3% of places that are non-blue stain have an _NNDiff_ value > 50%;
* 91% of places that are non-blue stain have an _NNDiff_ value < -50%;
* 4% of places that are blue stain have _NNDiff_ value < -50%.