# A Development and Validation of an AI Model for Cardiomegaly Detection in Chest X-rays
## 3 Experiments
### Building the Model
#### 3.2.3 DenseNet121  
DenseNet concatenates the feature maps of all preceding layers, enabling each layer to access a diverse range of features from previous layers. This approach allows the network to learn more complex patterns and improves overall performance [21]. Due to the dense connections, DenseNet requires fewer parameters than traditional convolutional neural networks (CNNs), which helps reduce the risk of overfitting and computational complexity. The activation function performs a nonlinear transformation of the output values of the neurons in each layer. The use of ReLU in fully connected layers can enhance the model's performance [22]. Based on this, the structure of DenseNet includes basic convolution and pooling layers, Dense Block layers, transition layers, another Dense Block layer, and an average pooling layer prior to the classification layer. Figure 2 shows the structure of the AI model developed in this study. C1 represents the convolution layer; D1, D2, D3, and D4 represent the four Dense Block layers; and T1, T2, and T3 represent the three transition layers. The model concludes with a binary classification layer, Dense(Sigmoid).  
The accuracy of the DenseNet-based model in detecting cardiomegaly was evaluated as follows:  
\[y=\sigma(Wx+b) \tag{5}\]  
Figure 2: DenseNet-based model structure.  
\(y\) represents the probability of predicting cardiomegaly, indicating the likelihood of belonging to the cardiomegaly class. \(W\) represents the weight vector, \(x\) represents the input vector, and \(b\) represents the bias. \(\sigma(x)\) is the sigmoid function, a nonlinear function defined as:  
\[\sigma(x)=\frac{1}{1+e^{-x}} \tag{6}\]  
The input \(x\) is defined as:  
\[x=W_{Dense}\cdot GAP\cdot b_{Dense} \tag{7}\]  
\(W_{Dense}\) represents the weight vector of the final dense layer. \(GAP\) is the output of the global average pooling layer that aggregates the features extracted by the DenseNet backbone. \(b_{Dense}\) is the bias of the dense layer. A Dense Block layer consists of multiple convolutional layers, where each layer uses the outputs of all preceding layers as inputs. This allows the feature maps of each layer to be combined efficiently. The transition layer is composed of \(1\times 1\) convolutions and pooling layers [23]. The layers within a Dense Block are calculated as follows.  
\[x_{i+k}=H_{k}(\{x_{i},x_{i+1},\dots,x_{i+k-1}\}) \tag{8}\]  
Here, \(H_{\text{I}}\) denotes operations such as convolution, batch normalization, and ReLU. The final layer of a typical DenseNet uses the Softmax function for multi-class classification, which converts the input vector into a probability distribution. Each element of the given input vector is transformed into a value of between 0 and 1; however, in this study, the output value is transformed into 0 or 1 to determine cardiomegaly. Therefore, the sigmoid function was used instead of the Softmax function. The DenseNet121 model pretrained on ImageNet was selected as the backbone for classifying cardiomegaly in chest X-rays. The backbone refers to the base network used in a CNN to extract features from an image. The code implementation of this analysis is provided in Algorithm 3.  
```
1:functionDNCD
2:Variable
3:\(\text{disr}\leftarrow\) [\"train,\" \"validation\", \"test\"]
4:\(\text{train\_datagen}\leftarrow\text{ImageDataGenerator(rescale}=1.0/255\), zoom\(\_\)range = 0.2)
5:\(\text{validation\_test}\),\(\text{datagen}\leftarrow\text{ImageDataGenerator(rescale}=1.0/255\))
6:functionDENSENET\(\_\)MODELING
7:\(\text{base\_model}\leftarrow\text{DenseNet121(include\_top}=\text{False}\), weights = \"imagenet\", input.shape = (224, 224, 3))
8:\(\text{x}\leftarrow\text{GlobalAveragePooling2D()(base\_model.output)}\)
9:\(\text{predictions}\leftarrow\text{Dense(1, activation = \"sigmoid\") (x)}\)
10:\(\text{model}\leftarrow\text{Model(inputs = base\_model.input, outputs = predictions)}\)
11:endfunction
12:endfunction
```  
**Algorithm 3**DNCD (DenseNet for Cardiomegaly Detection)