{'fraction_non_alphanumeric': 0.028871477482222806, 'fraction_numerical': 0.011061364234922308, 'mean_word_length': 4.6389456098013735, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
SwinCheX: Multi-label classification on chest X-ray images with transformers

According to the considerable growth in the avail of chest X-ray images in diagnosing various diseases, as well as gathering extensive datasets, having an automated diagnosis procedure using deep neural networks has occupied the minds of experts. Most of the available methods in computer vision use a CNN backbone to acquire high accuracy on the classification problems. Nevertheless, recent researches show that transformers, established as the de facto method in NLP, can also outperform many CNN-based models in vision. This paper proposes a multi-label classification deep model based on the Swin Transformer as the backbone to achieve state-of-the-art diagnosis classification. It leverages Multi-Layer Perceptron, also known as MLP, for the head architecture. We evaluate our model on one of the most widely-used and largest x-ray datasets called"Chest X-ray14,"which comprises more than 100,000 frontal/back-view images from over 30,000 patients with 14 famous chest diseases. Our model has been tested with several number of MLP layers for the head setting, each achieves a competitive AUC score on all classes. Comprehensive experiments on Chest X-ray14 have shown that a 3-layer head attains state-of-the-art performance with an average AUC score of 0.810, compared to the former SOTA average AUC of 0.799. We propose an experimental setup for the fair benchmarking of existing methods, which could be used as a basis for the future studies. Finally, we followed up our results by confirming that the proposed method attends to the pathologically relevant areas of the chest.


Introduction
Disburdening medical experts of monotonous tasks such as medical imaging diagnosis have been a long-standing goal in bioinformatics. Moreover, with the emergence of a global pandemic, there is an unprecedented increasing interest in reliable, accurate, and fast methods for diagnosing Chest X-ray (CXR) images. In this pursuit, Neural Network-based Computer Vision algorithms have proven to be successful at similar tasks, by surpassing human-level performance, both in terms of accuracy and speed (Goodfellow et al. 1 , Srivastava et al. 2 , Sabour et al. 3 and Mazzia et al. 4 ). Training these networks for the CXR diagnosis usually requires large amounts of labeled/annotated data, which is recently provided to the community by the National Institute of Health (NIH). This multi-labeled dataset, "ChestX-ray14" 5 , consists of above 100k frontal/back-view X-ray images and spurred us to tackle this problem.
Several attempts have been made to detect abnormalities in ChestX-ray14. Wang et al. 5 assessed performance of some of the most well-known Convolutional Neural Network (CNN) architectures (e.g., AlexNet 6 , VGGNet 7 , ResNet 8 ) that are pre-trained on ImageNet 9 . Yao et al. 10 exploited dependencies among disparate labels with an RNN-based model using Long-Short Term Memory Networks (LSTM) 11 along with a CNN backbone adopting a variant of DenseNet 12 , which barely was trained entirely on ChestX-ray14 5 . Rajpurkar et al. 13 enhanced a DenseNet-121 12 with transfer-learning to predict multi-label outputs. Eventually, Guendel et al. 14 proposed a location-aware method called DNetLoc grounded on DenseNet-121 12 , where an early stopping procedure was employed to avoid overfitting.
Unfortunately, a trusty and fair comparison among all different methods has been virtually impossible. In many early works, dissimilar dataset splits of training and test sets have been employed. First and foremost, a particular patient could exist in both training and test data which, according to the correlation between different images of the same patient, could yield a wrong and inflated detection rate. As a result, Wang et al. 5 released a new official patient-wise split later. Yao et al. 10 , Guendel et al. 14 , and Baltruschat et al. 15 published their evaluations based on this official split. For instance, to highlight the effect of the training split on evaluations, Guendel et al. 14 state that average AUC score of their model falls roughly 3 percent when using the official data split. Figure 1 provides an overview of our approach. In this paper, we focus on multi-label classification using Vision Transformers (ViT) 16 . We adapt a pre-trained Swin Transfomer 17 model and attach multi-head feedforward neural network layers for each class. We assess our model on four distinct configurations (e.g., with no head, one head, two heads, and three heads). Furthermore, to validate the results further, we obtained Grad-CAM saliency heatmaps for few input images to confirm that the model pays attention to pathologically relevant areas of the chest.
According to evaluations on the aforenamed dataset (i.e., Chest X-ray14 5 ), quantitative results have shown that the proposed model achieves state-of-the-art result. In other words, we attain the best average AUC score, as well class-wise AUC on various classes, based on the reports on the Chest X-Ray14 dataset 5 .

Background
For transparency, in this section, we discuss some deep learning concepts. We start with RNNs and then discuss their existing drawbacks. We continue with transformers and how they solved these issues, and how we can use transformers in vision-based tasks such as ours.

Recurrent Neural Networks
RNNs are a class of neural networks that allow previous outputs to be used as inputs while summarizing all given information in the hidden states. They are broadly used to model sequential dependencies. There are various RNN methods introduced for solving the problem of memorizing long-term dependencies, such as Long short-term memory networks, also known as LSTM 11 , and gated recurrent units (GRU) 18 . Although RNNs are among the most substantial state-of-the-art architectures for sequence modeling, there are some issues regarding these networks: • Sequential processing of the input prohibits parallelization within instances.
• There is a famous problem in RNNs around vanishing and exploding gradients, which is an essential field of study in the research community.
• Long-range patterns are still poorly modeled.
• Distance between time positions are implicitly assumed to be linear.
• There is no explicit modeling of long and short-range dependencies.

Attention
The attention mechanism is one of the most valuable breakthroughs in the last decade, and has spawned the rise of so many recent advances in deep models such as transformers 19 . The attention mechanism attempts to selectively concentrate on a few relevant aspects of the input while ignoring others in deep neural networks. The idea is that not only all the inputs should participate in forming the context vector, but also their relative importance should be taken into account. Therefore, whenever the model generates an output, it searches the whole hidden states in the encoder to find the most pertinent information. 20 .

Self-Attention
Cheng et al. 21 defined self-attention as bonding different positions of a single sequence to attain a more explicit representation.
A key vector of dimension d k , a query vector of dimension d k , and a value vector of dimension d v , which are an abstraction of the embedding vectors in different subspaces, are defined for each embedding of the words. To compute the attention of a target word concerning the input embeddings, we multiply the query of the target and the key of the input word. Then, these matching scores act as the weights of the value vectors during the summation. In practice, we compute the attention function on a set of queries simultaneously, with three packed matrices Q, K, and V for queries, keys, and values as follows: To wrap the merits of the attention: • There is a constant path length between any two positions.
• It can replace the aligned recurrence entirely.

2/10
Transformers Transformers are self-attention-based architectures that are trying to solve the problem of parallelization by using convolutional neural networks in tandem with attention models 19 . In general, attention boosts how fast the model can translate from one sequence to another. The architecture of these networks is mainly composed of one encoder and one decoder section with a linear layer on top of them. The encoder comprises a stack of six layers, each consisting of two sub-layers. The first sub-layer is a multi-head self-attention, and the second is a basic, fully connected feed-forward layer. The decoder section is similar to the encoder with six analogous layers; each layer contains three sub-layers. The first is much like the encoder, a multi-head attention layer. Following these layers, we have a multi-head attention layer that performs attention over the output of the encoder stack. At last, there is a fully-connected feed-forward layer just like the encoder stack.
To be noticed, there is a sub-layer called multi-head attention in transformers, which is slightly different from the self-attention. The intuition behind it, which is inspired by filters in convolutional neural networks, is that one may pay extra attention to some specific words in translating a word based on the type of question being asked. In multi-head attention, a unique linear transformation with a different query, key, and value vector for each word embedding is represented for each head to learn different relationships.  16 . Vision Transformer uses the original architecture of Transformer in the work of Vaswani et al. 19 for machine translation with just dropping the decoder part. Transformers lack inductive bias in the form of translation equivariance. In fact, they are permutation invariant by design and cannot process grid-structure data. Dosovitskiy et al. 16 converted the non-sequential spatial images into sequences by defining patches. The standard transformer model in Vaswani et al. 19 processes a 1D sequence of token embeddings as input. To use this notion, for an input image x ∈ R H×W ×C and patch size p × p, we create N patches with N = H×W P×P (same as the sequence length in sentences). Each patch x p ∈ R P×P×C needs to be transformed into a 1D vector d ∈ R D with a linear transformation layer to form embeddings. The images are then transformed into 1D embedding vectors through the self-attention. To this end, we also add positional embeddings, to account for the patches relative order. To obtain the output, we feed the sequence as an input to the standard transformer encoder, and then pass the output to the final MLP layers for the classification. In practice, ViTs are pre-trained on huge fully-supervised datasets, and are fine-tuned to the downstream tasks. To successfully fine-tune the pre-trained model, essentially, the original prediction MLP layer is replaced by a zero-initialized D×K feedforward layer (with k = number of classes in the downstream task). It has been shown that ViT would beat or at least approach state-of-the-art CNN models in various vision tasks if pre-trained on massive datasets like ImageNet-21k and JFT-300M.

Swin Transformer
ViT showed promising performance, but there are still some challenges regarding transferring the exceptional performance of transformers in the language domain to the computer vision. These challenges can be demonstrated by the existing distinction between these two modalities. Unlike the tokens that work as essential language processing components, different visual elements can vary significantly in scale, a crucial problem in some vision tasks such as object detection. In previous transformerbased models, we have seen that the tokens are all of a fixed size, a property that is problematic for these vision tasks. Another critical difference between language and vision is the high resolution of pixels in images. Numerous computer vision tasks such as semantic segmentation need a pixel-level dense prediction. The computational complexity in existing transformer models is quadratic in the image size, and hence it is impossible to use them in such architectural forms. Swin transformers were proposed to deal with the mentioned challenges, and operate as a general-purpose backbone for various vision tasks 17 .

Dataset
In this study, we use the ChestX-ray14 dataset 5 . This dataset contains 112,120 frontal/back-view chest X-ray images with the size of 1024×1024 (8-bits gray-scale values) from 30,850 patients, with labels from 14 pathology classes. Each image can have multiple labels. The labels are collected by analyzing radiology reports, and are expected to have over 90% accuracy. For a more accurate and objective comparison, we apply the official patient-wise split gathered by Wang et al. 5

Proposed Method
An overview of our model is provided in the Figure 1. We use the Swin transformer as the component that is shared between the models that are predicting each label. To elaborate, the Swin transformer, like ViT, first splits the input RGB image into . . . Figure 1. The overall architecture of our method. Chest X-ray images after being converted to RGB and resized to 224 are passed through a Swin-L 17 transformer. This follows a Layer-Norm, and a 7 × 7 average pooling, before the shared section is finished. 14 MLP heads are branched from the shared section. Each MLP head contains 3 layers of 384, 48, and 48 neurons, respectively.
non-overlapping patches. Each resulting patch is defined as a token. We use a patch size of 4 × 4, and thus the feature dimension is 4 × 4 × 3 = 48. Then a linear embedding layer is applied to this raw-valued feature to project it to an arbitrary dimension of C. After that, we apply several Swin transformer blocks. These blocks maintain the number of tokens ( H 4 × W 4 ). As the network gets deeper in layers, the number of tokens decreases by patch merging layers after each stack of the Swin transformer blocks. The first patch merging layer concatenates the feature of each group of 2 × 2 neighboring patches and applies a linear layer on the connected features, and the output dimensions are set to 2C. Swin transformer blocks are applied afterward for feature transformation, with the resolution kept at H 8 × W 8 . This procedure is repeated two more times, resulting in resolutions of H 16 × W 16 and H 32 × W 32 , respectively.

Swin Transformer Block
A Swin transformer block is built by replacing the standard multi-head self-attention (MSA) module with a module based on shifted windows, keeping other layers the same. The standard transformer architecture leverages global self-attention, where the associations between a token and all other tokens are calculated. This procedure tends to have a quadratic complexity with respect to the number of tokens, which is immensely infeasible for many vision tasks that involve high-resolution images. To solve this issue, self-attention is computed within local windows. The windows divide the image in a non-overlapping way.
Assuming each window has M × M patches, the computational complexity of a global multi-head self-attention module and a window-based one of an image of h × w patches are: The global MSA has a time complexity that is quadratic to the number of patches, making it inapplicable for a high-resolution image. In contrast, the window-based technique runs in linear time in terms of the number of patches, when M is fixed. However, the lack of connection between windows in the window-based self-attention technique limits its power and flexibility. Shifted window partitioning method was suggested to additionally model the cross window connections. The first module leverages a regular window partitioning scheme, which starts from the top-left pixel. Then, the next module uses a windowing setup, shifted M/2 × M/2 pixels from that of the regularly partitioned windows of the proceeding layer. Consecutive Swin transformer blocks are computed as:ẑ whereẑ l and z l denote the output features of the (S)W-MSA modules, and the MLP module for the block l, respectively. Furthermore, LN denotes the layer normalization module.

Multi-task Learning
Motivated by the multi-prediction head architecture for the multi-task learning problems, a shared architecture is placed at the front and one head is considered for each pathology after the front section. We use Swin-L 17 transformer as the shared section. The model weights are initialized from ImageNet22k 9 pre-trained weights. Swin-L outputs 1536 channels of 7 × 7 feature vectors. To reduce the output to a 1-dimensional vector and utilize consequent fully-connected layers, a Layer-Norm and a 7 × 7 average pooling is applied to the output. The architecture used in the head sections is the Multi-Layer Perceptron. For our main model, the 3-layer headed SwinCheX, we use a 3-Layer MLP with 384, 48, 48 neurons in each layer, respectively. The last layer is connected to one output, which is normalized by a sigmoid function. Therefore, each head outputs a number between zero and one, showing the probability of that sample containing the corresponding disease. For training of the network, binary cross-entropy loss is considered as the loss function. 32 is used as the batch size in training and the learning rate is set to 3 × 10 −5 . Figure 1 shows the overall architecture of our proposed method.

Evaluation
Some of the previous works in this field used some hyper-parameters without providing any reasons for the choice of the specified values. For example, Guendel et al. 14 used a learning decay of 10x when the validation loss plateaued. Seyyed-Kalantari et al. 26 decreased the learning rate by a factor of 2 if the validation loss did not improve over 3 epochs, and they stopped the model if it did not improve over 10 epochs. Furthermore, they fine-tuned the degree of random rotation data augmentation from a set of pre-defined values. While the general idea behind these approaches seems reasonable, the problem is that these hyper-parameters are fine-tuned to perform well on the validation set and therefore, there is the possibility that these hyper-parameters overfit the validation set. Even more so, these unprincipled hyper-parameter setups may have also been optimized to perform well on the given test split. Therefore, by changing the dataset, slightly modified hyper-parameters result in better performance. Furthermore, code reproducibility is one of the important factors in practical problems. With more hyper-parameters and more conditions on early stopping, reproduction of the results becomes more challenging.
To prevent the mentioned problems, we define a general evaluation method. In our proposed evaluation protocol that is followed in all of our experiments, we split the official training data into training and validation sets, with 80 percent of the set forming the training split. No patient appears in both training and validation splits, for the purpose of prevention of biased prediction of the models. The area under the curve of the receiver operating characteristic (AUROC) is considered as the evaluation metric of the model. The model is trained on the aforementioned training split, and AUC is reported for the validation set after each epoch. Then, the network from the epoch with the maximum validation AUC is selected and its AUC on the official test set is reported. Therefore, in this approach, few hyper-parameters are used and no unclear values for early stopping hyper-parameters are utilized. In this matter, our model selection method is much simpler and can easily be applied to any other dataset. Our train validation split, as well as our code, are available in our Github repository (https://github.com/rohban-lab/SwinCheX), which allows for effortless reproduction of results.

Experiments
Several experiments are run on different transformers to evaluate transformers' performance on the Chest-xRay14 dataset. Moreover, we test the previous state-of-the-art work in the field on our data split to make a fair comparison.
In all of our experiments, we observe that AUC boosts up in the early epochs, but starts to decline beyond a certain point. This observation holds in all of our experiments, including the already known models in the field such as DNet 14 and ResNet 15 . This happens because these models consist of millions of parameters and after a point, they begin to overfit to the training set. Furthermore, a good starting point for the optimization process is important to speed up the process and find a better local minimum. In the following, we introduce these experiments and discuss the results.

ViT (Vision Transformer)
In this experiment, the ViT vision transformer 16 is tested on each pathology present in the ChestX-ray14 dataset. This experiment is used to give an insight on how well transformers can operate on chest X-ray images, and a comparison is made with our proposed method and other works in table 1.
A single neuron is considered as the vision transformer output, and a sigmoid activation function is applied so as to represent the probability of that pathology existence. Weights are initialized from the network that is pre-trained on ImageNet22k 9 . The weights are then fine-tuned during training on CXR images. Batch size and learning rate are set to 32 and 3 × 10 −2 , respectively.

Swin Transformer
Swin transformer constitutes the shared section of our proposed model. Therefore, multiple scenarios are tested with the Swin transformer. First, a plain Swin transformer with no head attachments is tested. We may also refer to this model as the headless SwinCheX model in the following sections. In the headless case, 14 neurons are considered as the Swin transformer output, and the binary cross-entropy loss is considered as the loss function. In this approach, the whole network is shared for all of the pathologies, and therefore the model flexibility for learning specific functions for each pathology becomes significantly limited.
In order for the model to learn different, and more complex functions for each pathology, an MLP head structure is attached to the output of the Swin transformer. The head structures have no shared parameters in common and therefore, enable the model to learn more complex functions that are specific to each pathology. In this case, the Swin transformer remains as the shared structure between all pathologies. This approach enables the model to learn low-level features of images, which are common between all pathologies, through the shared section, and learn high-level features that are specific to each pathology in the head sections.
Consequently, in the next set of experiments, the impact of different head structures on the prediction accuracy is investigated. We examine various structures with a different number of layers in the MLP head section. The general structure of the model in these experiments is similar to the main method, except for the MLP heads. In particular, removing MLP layers from our main method is experimented on, so as to check the effectiveness of having more layers. Comparisons between head structure containing one layer of 48 neurons, a head structure containing two layers of 384 and 48 neurons respectively, and the proposed method that contains three layers of 384, 48, and 48 neurons respectively, are provided in Table 1, alongside the aforementioned headless architecture. Hyper-parameters, such as learning rate and batch size, are set the same in all the experiments, in order to make a fair comparison.

DenseNet
DenseNets are widely used in the chest X-ray classification tasks. For instance, they are used in the work by Guendel et al. 14 and Seyyed-Kalantari et al. 26 . It was observed that in these methods, a significant number of hyper-parameters are present, which results in difficulty in reproduction and generalization. To have a fair comparison, we apply the DenseNet-121 variant model, which was introduced by Guendel et al. 14 , simply known as DNet, on our data split and evaluate this method with the mentioned evaluation approach in the Methods section. Other than the evaluation approach, settings are the same as the ones used in Guendel et al. 14

Localization
We provide Grad-CAM 27 heatmaps to assess what parts of the lungs our model pays the most attention to diagnose the disease. This mapping can help the expert to spot potentially important areas for each disease in the input image. To get the Grad-CAM images for the Swin Transformer, we calculate the gradient of the most dominant logit with respect to the activation map that is located after the first layer norm of the last Swin Transformer block.

Results
Detection AUC of existing models in the field, as well as our proposed models are presented in Table 1. The maximum value for each pathology, among all the models, is bolded. It is observed that our 3-layer headed SwinCheX model achieves   state-of-the-art AUC value of 0.81 in 14 pathologies on average. Furthermore, SwinCheX with three different head settings achieves state-of-the-art AUC in 11 pathologies, namely Emphysema, Edema, Pneumothorax, Mass, Fibrosis, Atelectasis, Consolidation, Pleural Thickening, Nodule, Pneumonia, and Infiltration, two of which are achieved by the 1-layer headed SwinCheX, six by the 2-layer headed SwinCheX and three by the 3-layer headed SwinCheX. Also, the ViT transformer reaches a 0.891 AUC score in Cardiomegaly, which is the highest score for this pathology. These results show the power of multi-resolution transformers in detecting lung diseases from the X-ray images. Figure 2 shows the AUC of SwinCheX with different head settings during the training. The orange line shows AUC reached in our validation split and the gray line shows the AUC if the model is evaluated on the test split. These charts are only drawn for demonstration purposes and we do not use any information related to the test split in our model selection. As stated in the evaluation section, we use the epoch with the highest validation AUC to select our model. In these charts, we can see that there is a high correlation between the validation AUC and the test AUC. Consequently, our approach for selecting the model with the highest validation AUC, would make the selected model perform well in unseen test sets.
The Grad-CAM mappings of our 3-layer headed SwinCheX for samples containing Consolidation, Atelectasis, Mass or Cardiomegaly are provided in the Figure 3. Lung consolidation can be easily seen on an X-ray image by the naked eye. The consolidated parts of the lung look white, or opaque, on a chest X-ray. In Figure 3a our model detects these parts and diagnoses the sample as positive. Furthermore, an X-ray image can be helpful in the diagnosis of Atelectasis. Findings on an X-ray suggestive of Atelectasis include displacement of fissures, rib crowding, the elevation of the ipsilateral diaphragm, volume loss on ipsilateral hemithorax, hilar displacement, and compensatory hyperlucency of the remaining lobes. In Figure 3b, our model has diagnosed this disease by looking at the bottom of the two lungs and their relative positions.
In Figure 3c the proposed model diagnoses the sample as a positive case for lung mass by detecting the right area. A lung mass is defined as an abnormal spot or area in the lungs larger than 3 centimeters (cm), about 1.5 inches, in size. Spots smaller than 3 cm in diameter are considered lung nodules. Finally, in Figure 3d, we have provided a positive case of Cardiomegaly, which is also known as an enlarged heart. In this case, the Grad-CAM mapping of our model shows that it was able to detect this condition by determining where the heart is located and especially, some edge parts of the heart.

Comparison with Previous State-of-the-Art
The DNet model proposed by Guendel et al. 14 reaches the 0.807 AUC, which presents the previous state-of-the-art in the task of multi-label classification on chest X-ray images. Our 3-layer headed SwinCheX model surpasses this by achieving an AUC score of 0.81. Furthermore, DNet only has a better performance in Cardiomegaly, Hernia, and Effusion than all the different variations of SwinCheX, and our models are dominant in the rest of the diseases. Another major advantage that our model has over DNet is that DNet uses an adaptive learning rate while our model does not utilize this method. Using adaptive learning rates add some hyper-parameters to the model, such as when and by how much one should decrease the learning rate. Finding a good value for these hyper-parameters not only needs a substantial amount of time to train and experiment, but it can also result in overfitting to the validation set, that is, values are selected so that the model performs better on the validation set. Our model does not have these problems and is simpler. To better compare the results of these two models, we tested a DNet with our presented evaluation protocol. The results are shown in the Table 2. We have also put the 3-layer headed SwinCheX results as a reference. It can be seen that DNet with our proposed evaluation method performs worse than DNet on average over all of the pathologies, and our 3-layer headed SwinCheX outperforms it by 1.01% margin on the AUC.

Discussion
In this paper, we proposed several models based on the vision transformers to perform multi-label classification on the ChestX-ray14 dataset, which contains 14 pathologies of the chest. We also compare the results of these methods and the previous models. We introduce an evaluation method, which we use to select and evaluate the model based on, and for a fair comparison, test the previous state-of-the-art by this evaluation method. We have shown the efficiency of vision transformers for the task of multi-label classification on the chest X-ray images. We particularly tested and experimented with ViT 16 and Swin 17 transformers. Our results suggest superiority of multi-resolution ViTs in a multi-task learning setup for this task. We propose multi-head MLPs to model the multi-task learning nature of this problem. For future works, other vision transformers can be used and tested. Moreover, the ChestX-ray14 dataset contains images with scattered lung positions, watermarks, and different contrasts. working on a pre-processing technique to remove these issues in an efficient manner before the training process.