LungAI is an AI-powered platform designed to assist healthcare professionals in the early detection of lung cancer through the analysis of CT scan images. Using deep learning models like ResNet50, it classifies images with 90% accuracy and integrates techniques such as data augmentation and image preprocessing. Additionally, LungAI incorporates Grad-CAM, a tool for generating heatmaps that highlight areas of interest, aiding doctors in interpreting model predictions and identifying potential tumors. The platform also allows for the generation of detailed medical reports, which can be personalized and downloaded. Serving both educational and diagnostic purposes, LungAI demonstrates the potential of AI in healthcare, helping to optimize resources, reduce diagnostic delays, and ultimately improve early cancer detection and patient outcomes.
According to data from the Spanish Cancer Registry (REDECAN) and the National Statistics Institute (INE), Lung cancer remains the leading causes of death not only in Spain but worldwide. In 2022, lung cancer was the most frequently diagnosed cancer, with 2.5 million new cases globally, and it also had the highest mortality rate, with approximately 1.8 million deaths. In Spain, more than 70% of lung cancer cases are diagnosed at an advanced stage, which significantly reduces the chances of successful treatment.
This is where my project, LungAI, comes in. LungAI is a predictive model that aims to identify high-risk patients more efficiently and with high accuracy. It enhances the speed, cost, and accessibility of lung cancer diagnosis, ultimately contributing to earlier detection and better patient outcomes.
The methodology for developing LungAI involved the integration of advanced machine learning techniques, including deep learning and data augmentation, to create a model capable of accurately identifying lung cancer in CT scan images. This section describes the approach taken in detail, including the preprocessing of data, the architecture of the model, the training process, and the evaluation methods used.
The dataset used for training, validation, and testing consists of CT scan images. These images were preprocessed to ensure compatibility with the model architecture and to improve the model's performance. The images were resized to 224x224 pixels to match the input size required by the pretrained ResNet50 model, which has an input size of 224x224 RGB images.
In addition to resizing, the pixel values of the images were normalized to a range between 0 and 1 by dividing the pixel values by 255. This normalization step is important because it standardizes the input data, ensuring that the model does not focus on the raw pixel values but instead learns the features that matter.
To prevent overfitting and make the model more robust, data augmentation was applied to the training set. This technique artificially increases the size of the dataset by generating variations of the images during training. The following augmentation transformations were applied:
The validation and test sets did not undergo data augmentation, ensuring that the evaluation of the model was based on original, unaltered data. Only image normalization was applied to these datasets.
train_datagen = ImageDataGenerator( rescale=1./255, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest' ) valid_test_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory( os.path.join(dataset_path, 'train'), target_size=(224, 224), batch_size=32, class_mode='binary' ) valid_generator = valid_test_datagen.flow_from_directory( os.path.join(dataset_path, 'valid'), target_size=(224, 224), batch_size=32, class_mode='binary' ) test_generator = valid_test_datagen.flow_from_directory( os.path.join(dataset_path, 'test'), target_size=(224, 224), batch_size=32, class_mode='binary', shuffle=False )
The core of LungAI is based on the ResNet50 model, a deep convolutional neural network (CNN) architecture known for its powerful performance in image classification tasks. ResNet50 was initially trained on the ImageNet dataset, which contains millions of labeled images from various classes. This pretrained model has learned general features that are applicable across various image domains, making it a suitable choice for transfer learning.
ResNet50 was chosen because of its skip connections, which allow the model to skip certain layers and help avoid the vanishing gradient problem in deep neural networks. This architecture is well-suited for tasks that require learning deep representations from complex data like medical images.
The model was initialized with ImageNet weights, which were kept frozen in the initial layers of the network. The top layers of ResNet50 (the fully connected layers responsible for classification) were removed, and custom layers were added for the binary classification task (normal vs. cancerous).
Fine-Tuning
The next step was fine-tuning the model to make it specialized for lung cancer detection. Fine-tuning involved unfreezing a portion of the layers in the ResNet50 model to allow the model to adapt the general features learned from ImageNet to the specific characteristics of lung CT scans. The first 50 layers of the ResNet50 model were kept frozen, and the layers starting from the 50th layer onward were unfrozen for training. This approach preserves the general features learned from ImageNet while allowing the model to learn task-specific features related to lung cancer detection.
After the ResNet50 base, additional fully connected layers were added to the model:
The model was compiled using the Adam optimizer, a popular optimization algorithm that adapts the learning rate during training. The learning rate was set to 0.00005 to ensure stable fine-tuning, preventing the model from updating weights too drastically in the early stages of training.
The loss function chosen was binary cross-entropy, which is standard for binary classification tasks. The performance metric used to evaluate the model was accuracy, which indicates how well the model is classifying images into the correct categories (normal vs. cancerous).
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.00005), loss='binary_crossentropy', metrics=['accuracy'])
The model was trained using the ImageDataGenerator class in Keras, which allows real-time data augmentation during training. The train generator applies the data augmentation transformations to the images on the fly, creating new versions of each image for each epoch.
The validation data was used to evaluate the model's performance during training, but it was not augmented to ensure that the validation results reflect the model's performance on unaltered images.
The model was trained for 20 epochs with a batch size of 32 images. During training, a ReduceLROnPlateau callback was used to decrease the learning rate if the validation loss stopped improving, which helps the model converge faster and avoid overshooting the optimal solution.
reduce_lr = ReduceLROnPlateau( monitor='val_loss', factor=0.1, patience=2, min_lr=1e-6 ) model.fit( train_generator, validation_data=valid_generator, epochs=20, callbacks=[reduce_lr], steps_per_epoch=train_generator.samples // train_generator.batch_size, validation_steps=valid_generator.samples // valid_generator.batch_size )
After training, the model's performance was evaluated on the test dataset. The test generator was used to load and preprocess the test data. The final evaluation metrics were accuracy, ROC AUC score, and the confusion matrix.
Below is a demonstration of the LungAI project:
The model was evaluated using the test set, and the performance metrics were calculated to assess its effectiveness. The classification report, which includes precision, recall, and F1-score, is as follows:
Class 0 (Cancer):
Class 1 (Normal):
Overall accuracy of the model on the test set was 88%. In addition to the per-class performance, the macro and weighted averages are as follows:
Macro Average:
Weighted Average:
The model's ROC AUC score, which evaluates the model's ability to distinguish between the two classes (Cancer vs. Normal), was 0.96, indicating excellent discriminatory power.
Additionally, a confusion matrix was generated to visualize the model's performance on the test data. The matrix revealed the following results:
It is particularly important that the model achieved 0 false negatives, meaning that the model did not misclassify any cancer cases (Class 0) as normal (Class 1). This is a crucial outcome, as false negatives can have severe consequences in cancer diagnosis, where early detection is essential for effective treatment.
The LungAI model demonstrates several notable strengths:
Despite its strengths, the LungAI model has certain limitations:
To address these limitations and enhance the model's capabilities, the following steps are planned:
Improving Recall for the "Cancer" Class
Efforts will be made to reduce false negatives further, even in datasets with more diverse cancer presentations. This may involve fine-tuning hyperparameters, rebalancing the dataset, or employing techniques such as focal loss.
Transition to Multiclass Classification
Developing a more complex model to classify not only the presence of cancer but also its type. Early experiments in this direction have yielded a precision of 40%, indicating a promising area for further exploration.
Enhancing Grad-CAM Visualizations
Debugging and resolving issues with Grad-CAM implementation will be prioritized. This includes identifying why CT scan images fail to display the colored heatmaps indicating tumor regions, ensuring that clinicians can interpret the model's predictions effectively.
Model Optimization
Continued optimization, including experimenting with advanced architectures (e.g., vision transformers) or ensemble methods, could further enhance the model's accuracy and interpretability.
LungAI represents a promising advance in early cancer detection, achieving 88% accuracy and a 0.96 AUC-ROC score. Its key strength is zero false negatives, ensuring no cancer cases go undetected. Future improvements include boosting recall, transitioning to multiclass classification, and resolving Grad-CAM issues. These advancements will improve the model's performance and clinical application.
This model has the potential to save lives, optimize resources, and reduce healthcare costs. With an initial investment in infrastructure and training, it could be integrated into healthcare systems delivering measurable results. LungAI showcases the transformative potential of AI in medical diagnostics, enabling faster, more accurate cancer detection.
Spanish Cancer Registry (REDECAN)
https://redecan.org/es
National Statistics Institute (INE)
https://www.ine.es/
Spanish Association Against Cancer
https://observatorio.contraelcancer.es/
4.Chest CT-Scan images Dataset
https://www.kaggle.com/datasets/mohamedhanyyy/chest-ctscan-images