# Frequency Range Optimization for Continuous Wave Terahertz Imaging
## 4 CNN for Material Classification
### CNN Structure and Machine Learning  
Classification of different materials depends on their spectral characteristics. Each material has its spectral footprints, and this paper aims to detect the intersection of those spectral footprints. The advantage of CNN in the case of classification is recognition robustness, which can mimic the eye's natural phenomena of cortex vision [25; 26]. In the preliminary work [19], the WSD algorithm proved superior to algorithms using 1D THz-FDS data. One of CNN advantages is that the features can be extracted even if they are distributed randomly spatially. As described in Section 2, pseudo random spatial distribution is one of the shortcomings of THz-FDS systems, which is the result of the frequency tuning method.  
The efficiency of the CNN depends on the prepared training dataset, network structure, and hyperparameter selection [27]. This paper's single CNN structure was designed for moderate resolution, benefiting from short training time and high classification speed while maintaining high classification accuracy. Figure 7 shows the proposed CNN structure, where Conv, ReLu, Maxpool, FC and Cross are abbreviations for convolution operation, rectified linear activation function, max pooling, fully connected layer and cross-entropy, respectively. The number after the forward slash in Figure 7 represents the stride layer.  
The CNN's structure starts with the normalization function for scaling 2D input into an arbitrary span. The first layer in the proposed CNN structure starts with convolutional filters (CF) or kernels. The CF is a fixed-value moving window with a preselected structure and stride. Repeated pattern extraction is allowed with the convolution operation over the inspected image. The output from the convolution operation is a feature map. The CF selection is a trade-off between network complexity and extraction potential.  
The rectified linear activation function (ReLu) is used as the activation function. ReLu speeds up the learning process and is not prone to a vanishing gradient. The second layer in proposed CNN is the pooling layer. The pooling layer reduces the data dimensions by extracting maximal values from a certain area. A max-pooling layer of size 2 \(\times\) 2 was used in the proposed CNN. In the preliminary work it was determined that, if WSD was used, the pooling layer could be omitted, due to the down-sampling procedure of the WSD  
Figure 6: WSD transforms series into a two-dimensional image.  
Figure 7: Proposed CNN structure.  
algorithm. However, max-pooling is used to minimize the proposed CNN's complexity. Another convolution operation layer, ReLu and max pooling layer follows. The last layer of the proposed CNN is a fully connected layer. The fully connected layer turns 2D data into a single dimension vector. A cross-entropy layer's dimension is the number of classes. In the case of this paper, the CNN was used to classify between three different plastic materials, as described in Section 4.1.  
A The CNN's hyperparameters are determined through training. The proposed network was trained using the ADAM optimizer [28]. The classification accuracy and reliability are related strongly to the number and types of layers and the preprocessing algorithm. The proposed CNN training parameters were similar as in [19]. Training dataset consisted of the 1D THz-FDS data acquired from different batches with three different plastic materials. Each batch contained 50 images. In the 1D THz-FDS data acquisition process, it was desired to have a limited temperature, atmospheric pressure, and absolute humidity fluctuations. Limited fluctuations in environmental parameters will increase prediction reliability and could reduce overfit. Ten batches were used for CNN training, representing 500 samples for each plastic material. The samples were divided into training (50% of samples), validation (30% of samples), and testing (20% of samples) groups. The CNN's learning time and achieved accuracy are presented in Table 1.