How I Detected Cancer Metastasis Using AI

My Process Using the PatchCamelyon Dataset With TensorFlow

Madison Page
Visionary Hub
7 min readJan 16, 2022

--

The image sampling display function to visualize the images in the dataset

For context and some background information about Artificial Intelligence in diagnostics, see the first article in this series, Using AI in Diagnostic Imaging. To see my first AI project, see my second article in this series, How I Created a Model that Identifies Cardiac Conditions. For the link to my GitHub repository for this project, click here.

Looking at the sample images above, are you able to discern the ones with cancerous cells and those without? Although I could recognize some loose patterns for the two classifications (contains metastatic tissue and does not contain metastatic tissue), I most certainly would not be confident enough to bet money on the label of any similar images — and this is to be expected. While I am immersed in the veterinary field, I am not a trained professional who is responsible for being able to identify cancerous and non-cancerous cells, and neither are the vast majority of people. This is one of the reasons why veterinarians and doctors are so valuable, but also one of the reasons why diagnostics can be so costly. Sometimes, diagnostic images similar to the ones above will be outsourced to specialty doctors who are specialized in performing different diagnostic imaging analyses. This often causes a further increase in price for diagnostics, with little to no additional profit on the side of your original doctor or veterinarian. In fact, if fewer patients or clients are opting to use diagnostic services, it can be detrimental to the profits of clinics and hospitals.

Artificial Intelligence, or AI, however, might soon overcome this issue through image recognition. Diagnostic images can be interpreted by machine learning models to predict a diagnosis, opening opportunities to reduce costs of outsourcing to clients. To develop my skills further in AI from my last project, I decided to start a new project that would be slightly more challenging. I chose to make an image recognition model to detect cancer metastasis from the PatchCamelyon dataset with TensorFlow. Again, I wrote my code in Python on Google Colab.

Steps for Creating the Model

Imports and Setup

First, I performed several imports to be able to use the libraries and modules necessary for my project. I imported TensorFlow, Keras (from TensorFlow), the tensorflow_datasets module, OS, and InceptionV3, as well as Input, Dense, GlobalAveragePooling2D, Model, and Sequential from Keras:

Next, as the PatchCamelyon dataset was one of the datasets already in the TensorFlow library, I was able to upload it directly, split it into training, validation, and testing sets:

Dataset Information

Once the imports were completed, I printed the information of the dataset from the TensorFlow library:

Which gave:

Background Information for the PatchCamelyon Dataset

To better visualize the image data, my next step was to use tfds.show_examples to see a few of the images and their labels:

Which shows:

Examples of images being used in the dataset

Preparing the Training, Validation, and Testing Sets

Before being able to set up a model, I next had to prepare the images in the training, validation, and testing sets and the sets themselves.

My first step in this process was to normalize the images through resizing them and normalizing their shape:

Next, I defined AUTOTUNE, which allows TensorFlow to optimize the productivity of actions when applied, and set the BATCH_SIZE to 16:

To increase the speed of training in the second-to-last step, I used “Counter” and applied it to ds_train:

I then applied some functions to ds_train:

The first line applies the normalize_img function that we defined earlier to the training set and sets the number of parallel calls in the dataset that can occur to AUTOTUNE. The second allows some of the data to be kept in memory so it will be loaded more quickly in subsequent runs. The third line shuffles the data randomly, and the fourth line applies the batch size of the training set to the variable BATCH_SIZE that we defined earlier. Lastly, the fifth line applies “prefetch” and sets it to AUTOTUNE, which increases efficiency by allowing images to be prepared in advance while the GPU is running.

Next, I applied some of these functions to the validation and test sets:

Preparing the Model

Once the datasets were prepared, the model for learning had to be built. To set up the actual architecture, I created a function named “create_model” with the parameter of image size (96 for the images in this dataset). I then defined the variable “inception”, which represented the pre-trained model InceptionV3 and defined some parameters within the model. In the “create_model” function, I also froze the InceptionV3 layers with “layer. trainable = False”. Next, I built the structure, starting with the Sequential model. Before coding the additional layers in the model, I used “model.add” to include InceptionV3 and GlobalAveragePooling2D, and set the expected shape of the input to the shape of the data. I then listed the dense, batch normalization, and drop layers, connecting each layer to the previous one. Lastly, I defined the output as a final dense layer, set the model inputs and outputs, and returned the model:

To view the architecture of the model before running it, I created a code block to print its summary:

Which printed:

The summary of the model

The last steps before being able to train the model were to compile the model and set its parameters, and define the variable “callbacks” to enable the model to stop running early if the loss on the validation set does not improve after 5 consecutive epochs:

Training the Model

As the last step before evaluating the effectiveness of the model, I had to train it on the training data and run it on the validation data to estimate its accuracy on the testing data and detect possible overfitting or underfitting:

These were the results of the training:

Accuracy and loss on training and validation sets during training

Evaluating the Model

Lastly, to evaluate the accuracy of the model on the testing data, I ran the following code:

Which gave:

Testing accuracy and loss

Conclusion

In the end, I achieved an accuracy of approximately 80% on this model. This is comparable with the results of other developers on this dataset, such as Geert Litjens, who achieved an accuracy of 85.63% on his model for the PatchCamelyon dataset. For this project, I experimented for several hours testing out different optimizers, learning rates, and adding and removing dense, and dropout layers, particularly within the “create_model” function and the model compilation. I found this very valuable, as I began to realize that while it may be portrayed more simplistically and be more standardized in the mainstream media and ML guides, different optimizers and parameters work better for certain projects. I believe that because of the sometimes seemingly insignificant differences between the images where cancer metastasis is detected and images where it is not, this was especially true for my model. It was definitely helpful to learn this through this project, as my next project will not be as guided or inspired by previous projects, and it will be important to be more creative when resolving issues such as low accuracy.

GitHub | LinkedIn | Twitter | Medium | Newsletter | Email

--

--

Madison Page
Visionary Hub

Working on Lynx to reduce veterinary diagnostic costs