# Machine Learning in Recognition of Basic Pulmonary Pathologies
## 2 Methodology  
The algorithms used in this study were trained on data obtained from the CheXpert database containing 224,316 accurately described chest radiographs from 65,240 patients. This dataset is remarkably large, and it provides labels made by experienced doctors. All images have good resolution and satisfying quality, and each one has 14 labels that describe common pulmonary pathologies. The label can contain '1' for presence, '0' for absence, and '-' for no information provided [22]. A considerably important part of the whole algorithm is lung segmentation. Because of the planar type of imaging, it is much harder to perform this process on X-ray images than on computed tomography images. There were attempts to make this process by image processing, but they failed. This is why the primary tool that performs lung segmentation here, is a previously trained neural network. It was downloaded from the GitHub repository [23]. It consists of 32 layers divided into convolutional blocks and has 7,759,521 trained weights. It was trained on a set of lung photos and corresponding masks created by radiologists. It accepts photos of 512 \(\times\) 512 px as input, and each photo was previously scaled and presented as a NumPy array. This network returns a binary matrix of ones and zeros that make up a mask that only lets the pixels that contain the lungs through when placed over the original image. Masks were improved algorithmically and by applying morphological operations, such as binary opening, closing, and erosion. Sample improved segmentation was presented in Figure 1. The number of images after segmentation is presented in Table 1.  
Figure 1: Sample lung segmentation on X-ray image. The neural network was built using transfer learning, i.e., adding a few more neural layers to ResNet-50 architecture, as explained in Figure 2. The weight optimization gradient is propagated back from the last layer to the initial layer in neural networks. Unfortunately, the more layers there are, the more this gradient disappears, which affects the weights in the first layers less and less. This phenomenon is known as the vanishing gradient effect and is dealt well with ResNet networks that use shortcuts to propagate the gradient. As a result, this network architecture uses hundreds of layers simultaneously, achieving outstanding performance. This network consists of the first block, which has a convolutional, batch normalization, activation (ReLU) and MaxPooling layer, and then in later stages has convolutional blocks and identity blocks, which have the shortcuts as mentioned earlier. All of them add up to 178 layers. The output indicates the probability of the presence of pathology in the image. This network has 31,956,481 weights that will process the input image after training [24]. The data were divided into training sets, validation sets, and test sets in a ratio of 8:1:1. Since it is impossible to upload all the photos to the computer's RAM simultaneously, special data generators were used, which, in addition to delivering them to the network, appropriately formatted them. The network was built to accept images with dimensions of 224 \(\times\) 224 px at the entrance, so each photo was scaled to this format. Data augmentation \begin{table}
\begin{tabular}{l l l} \hline \hline \multirow{2}{*}{**Pathology**} & \multicolumn{2}{c}{**Number of Cases**} \\ \cline{2-3}  & **Positive** & **Negative** \\ \hline Pneumothorax & 17,322 & 63,776 \\ Pneumonia & 4586 & 18,704 \\ Lung Lesion & 6904 & 17,618 \\ Lung Consolidation & 12,796 & 36,345 \\ \hline \hline \end{tabular}
\end{table} 
Table 1: Number of patients after segmentation. Figure 2: An explanation how new network was build using Transfer Learning. algorithms were used to prevent the network from overfitting: each image from the training set before entering the network was rotated by a random, slight angle. Thus, the network never received the same data twice.