Metastatic Adenocarcinoma Classification Using Convolutional Neural Networks

Andrew A Borkowski
Analytics Vidhya
Published in
5 min readAug 21, 2020

Keras vs fastai

Machine learning (ML) has the potential for numerous applications in the health care field. One promising application is in the area of anatomic pathology. ML allows representative images to be used to train a computer to recognize patterns from labeled photographs. Based on a set of images selected to represent a specific tissue or disease process, the computer can be trained to evaluate and recognize new and unique images from patients and render a diagnosis.

Lung and colon adenocarcinoma are some of the most common cancers affecting numerous patients throughout the world. They frequently spread to other sites of the body. It is not uncommon that the pathologist is faced with the question if biopsy showing adenocarcinoma originated from lung or colon primary site. Pathologists are often forced to use special stains to help them make this determination. In this post, I used two different machine learning libraries (fastai and Keras) to solve the origin of metastatic adenocarcinoma.

For this project, I used an image dataset containing 5000 color images of lung adenocarcinoma and 5000 color images of colon adenocarcinoma from the LC25000 dataset, which is freely available for ML researchers. I created a data folder with two subfolders for each class, lung adenocarcinoma class, and colon adenocarcinoma class.

My goal was not only to solve the metastatic adenocarcinoma classification problem but also to compare two deep learning libraries : Keras and fastai.

Both fastai and Keras are high-level APIs build on the top of PyTorch and TensorFlow, respectively. Keras was fully integrated into version 2 of TensorFlow.

Following seven coding steps were used to solve the problem of metastatic adenocarcinoma classification:

  1. Importing relevant libraries
  2. Specifying the path to the data directory
  3. Preparing batches of tensor image data for ML model
  4. Visualizing samples of training images (optional)
  5. Creating ML model
  6. Fitting ML model
  7. Visualizing results with confusion matrix

The Jupyter Notebooks for this post are available on my GitHub website.

Let us start with the fastai. The first step is to import relevant libraries.

Next, we specify the path to the directory with our images.

We create ImageDataBuch object with following parameters: the path to the data directory, validation split (in our case we pick 20% of images for validation and 80% for training), augmentation (False in our case since the LC25000 images are already augmented), image size (299x299 pixels is the native size for ResNet50) and batch size (I found 48 to be a right batch size for my 11 GB 2080Ti video card running fastai ResNet50 code on my Linux machine).

We visualize some of the images from the training batch with single line of code.

Here we create our ML model with the following parameters: data object, the model for transfer learning (ResNet50 in this case), and metrics (I pick the accuracy).

Fastai automatically freezes the ResNet50 classifier layer and replaces it with a fastai code. Below is the summary of the fastai classifier. The whole model has approximately 25 million trainable parameters.

Next, we fit and train the model. After only five epochs our model achieved 100% training accuracy

With only two lines of code, we visualize the confusion matrix of the validation dataset.

Next, let’s move to Keras code. We will use TensorFlow 2 as a backend. As you can see already, to achieve the same results as with fastai, we need many more lines of code. First we import relevant libraries.

We specify the path to our data directory.

We create ImageDataGenerator object with two parameters: validation split (20% images for validation and 80% for training) and normalization of images. We create two data objects: one for training and one for validation. We specify image size, batch size (had to pick the batch size of 32, my video card could not handle 48 with tf.keras version of the classifier). Since we don’t want to shuffle our validation set, we set shuffle to False.

We visualize some of the images from the training batch with the following lines of code.

We create our ML model. We freeze the classifier layer of the ResNet50 model and create our classifier (I tried to replicate the fastai classifier as much as I could; however, Keras does not have AdaptiveAvgPool2d layer, so I used global_max_pooling2d instead).

The whole model has approximately 25 million trainable parameters, just like fastai model.

We compile, fit, and train the ML model. After five epochs, our Keras model achieved 98% training accuracy.

With few lines of code, we visualize the confusion matrix of the validation dataset.

Conclusion

After only five epochs of training, both models achieved high training accuracy. The fastai required fewer lines of code to accomplish the same goals. Although both deep learning libraries are great for new practitioners of machine learning craft, the fastai library is more straightforward and user friendly. The fastai library has a great community of users, and you can find just about any answer on users’ forums. Ultimately, it is a personal preference as to which deep learning library to use.

Note: All fastai code is written in version 1 of the fastai library. Version 2 of the fastai library is officially being released today (8/24/2020).

Best wishes to everyone in these difficult times. Stay healthy and safe!
Andrew
@tampapath

--

--