Breast cancer classification with Keras and Deep Learning

In this tutorial, you will learn how to train a Keras deep learning model to predict breast cancer in breast histology images.

Back 2012-2013 I was working for the National Institutes of Health (NIH) and the National Cancer Institute (NCI) to develop a suite of image processing and machine learning algorithms to automatically analyze breast histology images for cancer risk factors, a task that took trained pathologists hours to complete. Our work helped facilitate further advancements in breast cancer risk factor prediction

Back then deep learning was not as popular and “mainstream” as it is now. For example, the ImageNet image classification challenge had only launched in 2009 and it wasn’t until 2012 that Alex Krizhevsky, Ilya Sutskever, and Geoffrey Hinton won the competition with the now infamous AlexNet architecture.

To analyze the cellular structures in the breast histology images we were instead leveraging basic computer vision and image processing algorithms, but combining them in a novel way. These algorithms worked really well — but also required quite a bit of work to put together.

Today I thought it would be worthwhile to explore deep learning in the context of breast cancer classification.

Just last year a close family member of mine was diagnosed with cancer. And similarly, I would be willing to bet that every single reader of this blog knows someone who has had cancer at some point as well.

As deep learning researchers, practitioners, and engineers it’s important for us to gain hands-on experience applying deep learning to medical and computer vision problems — this experience can help us develop deep learning algorithms to better aid pathologists in predicting cancer.

To learn how to train a Keras deep learning model for breast cancer prediction, just keep reading!

Looking for the source code to this post?
Jump right to the downloads section.

Breast cancer classification with Keras and Deep Learning

In the first part of this tutorial, we will be reviewing our breast cancer histology image dataset.

From there we’ll create a Python script to split the input dataset into three sets:

  1. A training set
  2. A validation set
  3. A testing set

Next, we’ll use Keras to define a Convolutional Neural Network which we’ll appropriately name “CancerNet”.

Finally, we’ll create a Python script to train CancerNet on our breast histology images.

We’ll wrap the blog post by reviewing our results.

The breast cancer histology image dataset

Figure 1: The Kaggle Breast Histopathology Images dataset was curated by Janowczyk and Madabhushi and Roa et al. The most common form of breast cancer, Invasive Ductal Carcinoma (IDC), will be classified with deep learning and Keras.

The dataset we are using for today’s post is for Invasive Ductal Carcinoma (IDC), the most common of all breast cancer.

The dataset was originally curated by Janowczyk and Madabhushi and Roa et al. but is available in public domain on Kaggle’s website.

The original dataset consisted of 162 slide images scanned at 40x.

Slide images are naturally massive (in terms of spatial dimensions), so in order to make them easier to work with, a total of 277,524 patches of 50×50 pixels were extracted, including:

  • 198,738 negative examples (i.e., no breast cancer)
  • 78,786 positive examples (i.e., indicating breast cancer was found in the patch)

There is clearly an imbalance in the class data with over 2x the number of negative data points than positive data points.

Each image in the dataset has a specific filename structure. An example of an image filename in the dataset can be seen below:


We can interpret this filename as:

  • Patient ID: 10253_idx5
  • x-coordinate of the crop: 1,351
  • y-coordinate of the crop: 1,101
  • Class label: 0 (0 indicates no IDC while 1 indicates IDC)

Figure 1 above shows examples of both positive and negative samples — our goal is to train a deep learning model capable of discerning the difference between the two classes.

Preparing your deep learning environment for Cancer classification

All of the Python packages you will use here today are installable via pip, a Python package manager.

I recommend that you install them into a virtual environment for this project, or that you add to one of your existing data science environments. Virtual environments are outside the scope of today’s blog post, but all of my installation guides will show you how to set them up.

If you need to set up a full blown deep learning system using recent OS’es, including macOS Mojave or Ubuntu 18.04, visit the respective links.

Here’s the gist of what you’ll need after your system prerequisites and virtual environment are ready (provided you are using a Python virtual environment, of course):

Note: None of our scripts today require OpenCV, but imutils  has an OpenCV dependency.

Project structure

Go ahead and grab the “Downloads” for today’s blog post.

From there, unzip the file:

Now that you have the files extracted, it’s time to put the dataset inside of the directory structure.

Go ahead and make the following directories:

Then, head on over to Kaggle’s website and log-in. From there you can click the following link to download the dataset into your project folder:

Click here to download the data from Kaggle.

Note: You will need create an account on Kaggle’s website (if you don’t already have an account) to download the dataset.

Be sure to save the .zip file in the breast-cancer-classification/datasets/orig  folder.

Now head back to your terminal, navigate to the directory you just created, and unzip the data:

And from there, let’s go back to the project directory and use the tree  command to inspect our project structure:

As you can see, our dataset is in the datasets/orig  folder and is then broken out by faux patient ID. These images are separated into either benign ( 0/ ) or malignant ( 1/ ) directories.

Today’s pyimagesearch/  module contains our configuration and CancerNet.

Today we’ll review the following Python files in this order:

  • : Contains our configuration that will be used by both our dataset builder and model trainer.
  • : Builds our dataset by splitting images into training, validation, and testing sets.
  • : Contains our CancerNet breast cancer classification CNN.
  • : Responsible for training and evaluating our Keras breast cancer classification model.

The configuration file

Before we can build our dataset and train our network let’s review our configuration file.

For deep learning projects that span multiple Python files (such as this one), I like to create a single Python configuration file that stores all relevant configurations.

Let’s go ahead and take a look at :

First, our configuration file contains the path to the original input dataset downloaded from Kaggle (Line 5).

From there we specify the base path to where we’re going to store our image files after creating the training, testing, and validation splits (Line 9).

Using the BASE_PATH , we derive paths to training, validation, and testing output directories (Lines 12-14).

Our TRAIN_SPLIT  is the percentage of data that will be used for training (Line 17). Here I’ve set it to 80%, where the remaining 20% will be used for testing.

Of the training data, we’ll reserve some images for validation. Line 21 specifies that 10% of the training data (after we’ve split off the testing data) will be used for validation.

We’re now armed with the information required to build our breast cancer image dataset, so let’s move on.

Building the breast cancer image dataset

Figure 2: We will split our deep learning breast cancer image dataset into training, validation, and testing sets. While this 5.8GB deep learning dataset isn’t large compared to most datasets, I’m going to treat it like it is so you can learn by example. Thus, we will use the opportunity to put the Keras ImageDataGenerator to work, yielding small batches of images. This eliminates the need to have the whole dataset in memory.

Our breast cancer image dataset consists of 198,783 images, each of which is 50×50 pixels.

If we were to try to load this entire dataset in memory at once we would need a little over 5.8GB.

For most modern machines, especially machines with GPUs, 5.8GB is a reasonable size; however, I’ll be making the assumption that your machine does not have that much memory.

Instead, we’ll organize our dataset on disk so we can use Keras’ ImageDataGenerator class to yield batches of images from disk without having to keep the entire dataset in memory.

But first we need to organize our dataset. Let’s build a script to do so now.

Open up the  file and insert the following code:

This script requires that we import  our config  settings and paths  for collecting all the image paths. We also will use random  to randomly shuffle our paths, shutil  to copy images, and os  for joining paths and making directories. Each of these imports is listed on Lines 2-6.

To begin, we’ll grab all the imagePaths  for our dataset and shuffle  them (Lines 10-12).

We then compute the index of the training/testing split (Line 15). Using that index, i , our trainPaths  and testPaths  are constructed via slicing the imagePaths  (Lines 16 and 17).

Our trainPaths  are further split, this time reserving a portion for validation, valPaths  (Lines 20-22).

Lines 25-29 define a list called datasets . Inside are three tuples, each with the information required to organize all of our imagePaths  into training, validation, and testing data.

Let’s go ahead and loop over the datasets  list now:

On Line 32, we define a loop over our dataset splits. Inside, we:

  • Create the base output directory (Lines 37-39).
  • Implement a nested loop over all input images in the current split (Line 42):
    • Extract the filename  from the input path (Line 45) and then extract the class label  from the filename (Line 46).
    • Build our output labelPath  as well as create the label output directory (Lines 49-54).
    • And finally, copy each file into its destination (Lines 58 and 59).

Now that our script is coded up, go ahead and create the training, testing, and validation split directory structure by executing the following command:

The output of our script is shown under the command.

I’ve also executed the tree  command again so you can see how our dataset is now structured into our training, validation, and testing sets.

Note: I didn’t bother expanding our original datasets/orig/  structure — you can scroll up to the “Project Structure” section if you need a refresher.

CancerNet: Our breast cancer prediction CNN

Figure 3: Our Keras deep learning classification architecture for predicting breast cancer (click to expand)

The next step is to implement the CNN architecture we are going to use for this project.

To implement the architecture I used the Keras deep learning library and designed a network appropriately named “CancerNet” which:

  1. Uses exclusively 3×3 CONV filters, similar to VGGNet
  2. Stacks multiple 3×3 CONV filters on top of each other prior to performing max-pooling (again, similar to VGGNet)
  3. But unlike VGGNet, uses depthwise separable convolution rather than standard convolution layers

Depthwise separable convolution is not a “new” idea in deep learning.

In fact, they were first utilized by Google Brain intern, Laurent Sifre in 2013.

Andrew Howard utilized them in 2015 when working with MobileNet.

And perhaps most notably, Francois Chollet used them in 2016-2017 when creating the famous Xception architecture.

A detailed explanation of the differences between standard convolution layers and depthwise separable convolution is outside the scope of this tutorial (for that, refer to this guide), but the gist is that depthwise separable convolution:

  1. Is more efficient.
  2. Requires less memory.
  3. Requires less computation.
  4. Can perform better than standard convolution in some situations.

I haven’t used depthwise separable convolution in any tutorials here on PyImageSearch so I thought it would be fun to play with it today.

With that said, let’s get started implementing CancerNet!

Open up the  file and insert the following code:

Our Keras imports are listed on Lines 2-10. We’ll be using Keras’ Sequential  API to build CancerNet .

An import you haven’t seen on the PyImageSearch blog is SeparableConv2D . This convolutional layer type allows for depthwise convolutions. For further details, please refer to the documentation.

The remaining imports/layer types are all discussed in both my introductory Keras Tutorial and in even greater detail inside of Deep Learning for Computer Vision with Python.

Let’s go ahead and define our CancerNet  class on Line 12 and then proceed to build  it on Line 14.

The build  method requires four parameters:

  • width , height , and depth : Here we specify the input image volume shape to our network, where depth  is the number of color channels each image contains.
  • classes : The number of classes our network will predict (for CancerNet , it will be 2 ).

We go ahead and initialize our model  on Line 17 and subsequently, specify our inputShape  (Line 18). In the case of using TensorFlow as our backend, we’re now ready to add layers.

Other backends that specify "channels_first"  require that we place the depth  at the front of the inputShape  and image dimensions following (Lines 23-24).

Let’s define our DEPTHWISE_CONV => RELU => POOL  layers:

Three DEPTHWISE_CONV => RELU => POOL  blocks are defined here with increasing stacking and number of filters. I’ve applied  BatchNormalization  and Dropout  as well.

Let’s append our fully connected head:

Our FC => RELU  layers and softmax classifier make the head of the network.

The output of the softmax classifier will be the prediction percentages for each class our model will predict.

Finally, our model  is returned to the training script.

Our training script

The last piece of the puzzle we need to implement is our actual training script.

Create a new file named , open it up, and insert the following code:

Our imports come from 7 places:

  1. matplotlib : A scientific plotting package that is the de-facto standard for Python. On Line 3 we set matplotlib to use the "Agg"  backend so that we’re able to save our training plots to disk.
  2. keras : We’ll be taking advantage of the ImageDataGenerator , LearningRateScheduler , Adagrad  optimizer, and np_utils .
  3. sklearn : From scikit-learn we’ll need its implementation of a classification_report  and a confusion_matrix .
  4. pyimagesearch : We’re going to be putting our newly defined CancerNet to use (training and evaluating it). We’ll also need our config to grab the paths to our three data splits. This module is not pip-installable; it is included the “Downloads” section of today’s post.
  5. imutils : I’ve made my convenience functions publicly available as a pip-installable package. We’ll be using the paths  module to grab paths to each of our images.
  6. numpy : The typical tool used by data scientists for numerical processing with Python.
  7. Python: Both argparse  and os  are built into Python installations. We’ll use argparse to parse a command line argument.

Let’s parse our one and only command line argument, --plot . With this argument provided in a terminal at runtime, our script will be able to dynamically accept different plot filenames. If you don’t specify a command line argument with the plot filename, a default of plot.png  will be used.

Now that we’ve imported the required libraries and we’ve parsed command line arguments, let’s define training parameters including our training image paths and account for class imbalance:

Lines 28-30 define the number of training epochs, initial learning rate, and batch size.

From there, we grab our training image paths and determine the total number of images in each of the splits (Lines 34-37).

We’ll go ahead and compute the classWeight  for our training data to account for class imbalance/skew.

Let’s initialize our data augmentation object:

Data augmentation, a form of regularization, is important for nearly all deep learning experiments to assist with model generalization. The method purposely perturbs training examples, changing their appearance slightly, before passing them into the network for training. This partially alleviates the need to gather more training data, though more training data will rarely hurt your model.

Our data augmentation object, trainAug  is initialized on Lines 46-55. As you can see, random rotations, shifts, shears, and flips will be applied to our data as it is generated. Rescaling our image pixel intensities to the range [0, 1] is handled by the trainAug  generator as well as the valAug  generator defined on Line 58.

Let’s initialize each of our generators now:

Here we initialize the training, validation, and testing generator. Each generator will provide batches of images on demand, as is denoted by the batch_size  parameter.

Let’s go ahead and initialize our model  and start training!

Our model is initialized with the Adagrad  optimizer on Lines 88-90.

We then  compile  our model with a "binary_crossentropy"  loss  function (since we only have two classes of data), as well as learning rate decay (Lines 91 and 92).

Making a call to the Keras fit_generator method, our training process is initiated. Using this method, our image data can reside on disk and be yielded in batches rather than having the whole dataset in RAM throughout training. While not 100% necessary for today’s 5.8GB dataset, you can see how useful this is if you had a 200GB dataset, for example.

After training is complete, we’ll evaluate the model on the testing data:

Lines 107 and 108 make predictions on all of our testing data (again using a generator object).

The highest prediction indices are grabbed for each sample (Line 112) and then a classification_report  is printed conveniently to the terminal (Lines 115 and 116).

Let’s gather additional evaluation metrics:

Here we compute the confusion_matrix  and then derive the accuracy, sensitivity , and specificity  (Lines 120-124). The matrix and each of these values is then printed in our terminal (Lines 127-130).

Finally, let’s generate and store our training plot:

Our training history plot consists of training/validation loss and training/validation accuracy. These are plotted over time so that we can spot over/underfitting.

Breast cancer prediction results

We’ve now implemented all the necessary Python scripts!

Let’s go ahead and train CancerNet on our breast cancer dataset.

Before continuing, ensure you have:

  1. Configured your deep learning environment with the necessary libraries/packages listed in the “Preparing your deep learning environment for Cancer classification” section.
  2. Used the “Downloads” section of this tutorial to download the source code.
  3. Downloaded the breast cancer dataset from Kaggle’s website.
  4. Unzipped the dataset and executed the  script to create the necessary image + directory structure.

After you’ve ticked off the four items above, open up a terminal and execute the following command:

Figure 4: Our CancerNet classification model training plot generated with Keras.

Looking at our output you can see that our model achieved ~85% accuracy; however, that raw accuracy is heavily weighted by the fact that we classified “benign/no cancer” correctly 93% of the time.

To understand our model’s performance at a deeper level we compute the sensitivity and the specificity.

Our sensitivity measures the proportion of the true positives that were also predicted as positive (85.03%).

Conversely, specificity measures our true negatives (84.70%).

We need to be really careful with our false negative here — we don’t want to classify someone as “No cancer” when they are in fact “Cancer positive”.

Our false positive rate is also important — we don’t want to mistakenly classify someone as “Cancer positive” and then subject them to painful, expensive, and invasive treatments when they don’t actually need them.

There is always a balance between sensitivity and specificity that a machine learning/deep learning engineer and practitioner must manage, but when it comes to deep learning and healthcare/health treatment, that balance becomes extremely important.

For more information on sensitivity, specificity, true positives, false negatives, true negatives, and false positives, refer to this guide.


In this tutorial, you learned how to use the Keras deep learning library to train a Convolutional Neural Network for breast cancer classification.

To accomplish this task, we leveraged a breast cancer histology image dataset curated by Janowczyk and Madabhushi and Roa et al.

The histology images themselves are massive (in terms of image size on disk and spatial dimensions when loaded into memory), so in order to make the images easier for us to work with them, Paul Mooney, part of the community advocacy team at Kaggle, converted the dataset to 50×50 pixel image patches and then uploaded the modified dataset directly to the Kaggle dataset archive.

A total of 277,524 images belonging to two classes are included in the dataset:

  1. Positive (+): 78,786
  2. Negative (-): 198,738

Here we can see there is a class imbalance in the data with over 2x more negative samples than positive samples.

The class imbalance, along with the challenging nature of the dataset, lead to us obtaining ~86% classification accuracy, ~85% sensitivity, and ~85% specificity.

I invite you to use this code as a template for starting your own breast cancer classification experiments.

To download the source code to this post, and be notified when future tutorials are published here on PyImageSearch, just enter your email address in the form below!


If you would like to download the code and images used in this post, please enter your email address in the form below. Not only will you get a .zip of the code, I’ll also send you a FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL! Sound good? If so, enter your email address and I’ll send you the code immediately!

, , , ,

73 Responses to Breast cancer classification with Keras and Deep Learning

  1. Pradeep Singh February 18, 2019 at 10:46 am #

    Thanks for this blog!. In the blog you mentioned it twice that there is an imbalance in the dataset. What are some of the ways by which we can manage that imbalance to remove the bias towards one class. ?

    • Adrian Rosebrock February 18, 2019 at 2:06 pm #

      There are a few ways. The “best” method is to gather more training data but that’s not always possible, especially in medical dataset situations or when performing outlier detection. Some problems just lend themselves naturally to imbalanced datasets.

      In those cases you could try augmenting the class with less examples such that it equals the number of examples for other datasets. Not a great solution but can work in some situations.

      Otherwise, we try to compute the class imbalance and “weight” the NN weight updates such that the class with less examples contributes more to the update, thereby attempting to “balance out” the data.

      Additionally, here are some other tactics that you may try.

    • Pawan February 18, 2019 at 7:22 pm #

      In order to deal with data imbalance u need to deal with ua loss function u can try
      1) weighted binary cross entropy
      2) Dice coef loss + BCE loss
      3) Focal loss

  2. Dennis February 18, 2019 at 11:05 am #

    Hi Adrian! Thank u for giving us good examples of usefull aplications of deep learning cases to learn with. Kind Regards, Dennis

    • Adrian Rosebrock February 18, 2019 at 2:06 pm #

      Thanks Dennis!

  3. Enoch Tetteh February 18, 2019 at 11:15 am #

    Hi Adrian, thanks for yet another good work. My first question is how did you handle the data imbalance, and if not why?

    Also, since you’re a big fan of keras, I want to know when do I have to use max pooling, average pooling or global average pooling. To put it well, which of them is more efficient?

    Lastly, how do I improve the precision and recall of CNN models with medical data sets?
    Thanks buddy.

    • Adrian Rosebrock February 18, 2019 at 2:07 pm #

      1. See my reply to Pradeep Singh

      2. It’s not a matter efficiency, it’s a matter of when you use each one. Standard max-pooling is often used for CNNs with fixed input sizes. You’ll see average pooling/global average pooling used quite a bit in fully-convolutional networks, especially object detection and instance segmentation networks.

  4. Donald R. Poole, Jr. February 18, 2019 at 11:17 am #

    Long time reader, but first-time responder here! Very interesting article and very timely with the work we are currently doing:

    I really like the fact that you stressed the importance of paying attention to the sensitivity and specificity scores. For that reason, I prefer not to employ the F1-Score for machine learning binary classification tasks and instead use Matthew’s Correlation Coefficient (MCC). The MCC give you a much more representative evaluation of the performance of a Binary Classification machine learning model than the F1-Score because it takes into account the TP and TN.

    Love the post and all the helpful tutorials that you keep publishing. It helps out a lot!

    • Adrian Rosebrock February 18, 2019 at 2:08 pm #

      Thanks for commenting, Donald! Yes, I do agree with you the standard F1-Score for binary classification, especially in this context, isn’t all that useful.

  5. Poornachandra Sandur February 18, 2019 at 11:25 am #

    Hi Adrian
    Your tutorials are awesome .. thanks for sharing your invaluable knowledge from the bottom of my heart …

    • Adrian Rosebrock February 18, 2019 at 2:09 pm #

      You are welcome, I’m glad you are enjoying them 🙂

  6. Sriram February 18, 2019 at 12:09 pm #

    Thanks for open sourcing it. This would be of great help to a lot of ppl out there

    • Adrian Rosebrock February 18, 2019 at 2:09 pm #

      It’s my pleasure, Sriram!

  7. David Bonn February 18, 2019 at 1:03 pm #

    Great post, Adrian.

    “Confusion Matrix” — so that is what they are called. I rediscovered them over the weekend. One good trick related to that is to scatter plot your training dataset with the index of the data point on the y axis and the score obtained by your model on the x axis — it gives a nice way to visualize how your model converged.

    Out of curiosity, why don’t you use just one output for a binary classifier? You could label a positive result with 1 (0) and a negative result be 0 (1). Is there any particular advantage or disadvantage one approach has over the other?

    • Manolo Ruiz February 19, 2019 at 10:52 am #

      Great question! I’ve always wondered this myself. I think it depends on your binary classificacion. For example if you are classifying cats vs dogs, your approach would probably not be good (i. e. one output neuron) because a cat is not really the opposite of a dog. The weights from the last hidden layer to the output would be negative for cat-like features while positive for dog-like features. This would effectively split the last hidden layer. But what if some feature is important for both classes? like having ears for example? This approach would in that sense limit the power of the net.
      Now in the cancer problem in particular I don’t know if any of this things apply, I don´t know how tumors look like. But my guess is it could work if one class is very opposite to the other.
      This is just what I think, might be completely wrong.
      Would be nice to see what Adrian has to say about this.

  8. Hasif February 19, 2019 at 2:57 am #

    I always faced this type of problem when the image size is lower than normal architecture (ResNet 224, DenseNet 299 etc). To be honest, it is quite troublesome to design a new architecture from scratch as I will deal more with it. My question is, is it possible to reuse the pretrained model with the smaller input size? (probably upscale the size first and connect to pretrained model and trained the first few layers and last fc layers.. Maybe.. Haven’t tried yet) what do you think of?

    • Adrian Rosebrock February 20, 2019 at 12:21 pm #

      Have you tried simply resizing your input images to match the input dimensions of the network? How does that work for you?

  9. tola February 19, 2019 at 5:38 am #

    Your tutorials are awesome and thanks for sharing

    • Adrian Rosebrock February 20, 2019 at 12:19 pm #

      Thanks Tola!

  10. Thomas February 19, 2019 at 10:03 am #

    Hello Adrian, Great post as always.

    My question is not directly related to the deep learning aspect but instead I would like to understand the way you implement the CancerNet class. The thing is that when you initialize the CancerNet model and compile it on line 88 – 91, you simply write:
    >>> model.compile(loss=”binary_crossentropy”, optimizer=opt, metrics=[“accuracy”])

    Since it’s a @staticmethod there is no self keyword in order to refer to the instance of a class. But can you please explain how do you access the compile method of Sequential class() which was initially defined in —>

    Thanks again for the great blog post.

    • Adrian Rosebrock February 20, 2019 at 12:15 pm #

      The Sequential mode was built and then returned inside the “build” method. The keyword is returned — that Sequential class is available to any function that calls it.

  11. Peyman February 19, 2019 at 11:52 am #

    Great contribution Adrian,
    Wondering what technique would you take to increase the accuracy of the network given the same imbalance data set?

    • Adrian Rosebrock February 20, 2019 at 12:13 pm #

      I’ve answered that question in my reply to Pradeep Singh. Make sure you give the comments a read 🙂

      • Peyman February 22, 2019 at 1:08 pm #

        Thanks Adrian. I just read the link from Jason Brownlee. While the link is great, I was curious about deep learning treatments for imbalanced dataset. Is there any architecture design change that can cope with imbalanced dataset? What kind of architectural changes in deep learner help on imbalance data? batch normalization, drop out, adding more layers, using different deep learner or cost functions or activation function (non-linearity).. any idea?

  12. Rafal Firlejczyk February 21, 2019 at 4:09 pm #

    Great tutorial, thank you. It took some time to train for these 40 Epochs. Where do you save the model after training? I did not see it on my disk.

    • Adrian Rosebrock February 22, 2019 at 6:23 am #

      This particular example does not serialize the model to disk. If you want to save and load Keras models refer to this tutorial.

  13. winnie February 22, 2019 at 2:13 pm #

    Thank U for your share.
    I am using machine-learning classifiers improves optical coherence tomography (OCT) glaucoma detection.there r 3 classes ,Positive ,Negative,and high risk.
    I use VGG16net to classify these pictures.But the result is it can’t classify high risk .Now ,I have no ideal about what to do next for classify the high risk.
    Could u give me some suggestions for the next work

    • Adrian Rosebrock February 27, 2019 at 6:27 am #

      Hey Winnie — do you have a link to the dataset so I an take a look?

  14. ameya February 23, 2019 at 10:15 am #

    Hi Adrian,

    Thank you so much for this tutorial. Really appreciate the amount of details that you have included in this article. It is very good starting point for a person like me who is an amateur to deep learning


    • Adrian Rosebrock February 27, 2019 at 6:16 am #

      Thanks Ameya, I really appreciate that 🙂

  15. Peter Jackson February 23, 2019 at 10:14 pm #

    Thanks Adrian

    I really enjoy your blog.

    My question isn’t just related to this post, but any time you are building a model from scratch. Do you have a standard approach, a set of steps you go through, or is it more of an artistic process.

    Any insight would be helpful.

    • Adrian Rosebrock February 27, 2019 at 6:10 am #

      I have a standard approach, much of which is detailed inside Deep Learning for Computer Vision with Python. I start by examining the dataset and considering the problem. I ask myself if I’ve encountered a similar dataset in the past and consider which techniques worked well. My first experiment utilizes standard feature extraction to obtain a baseline. I may then try fine-tuning. From there I typically take a model I’ve implemented already and train it on the dataset, logging my experiments along the way, constantly refining my knowledge over what is and what is not working.

  16. Rishabh Shukla February 25, 2019 at 10:08 am #

    Hi Adrian,
    thanks for the amazing blog.
    I tried implementing the blog on my system.
    My 8 GB ram mac is taking 1 hour per epoch for the training.
    1. I wanted to know is that normal or am I doing something wrong in my implementation?
    2. I am assuming you did the training on your AMI instance.
    If that is the case then could you share the specifications of your instance and approximately how long it took for your model to train?

    • Adrian Rosebrock February 27, 2019 at 5:54 am #

      1. That’s normal for training on a CPU.
      2. I trained on a GPU. I provide a pre-configured AMI that you can use. See the details here.

  17. Waldemar February 27, 2019 at 6:38 pm #

    Hi Adrian,

    what’s your hardware, that you are using for training? You need around 255s/epoch. I have a GTX 1070 which needs ~35 minutes/epoch. Am I doing something wrong?


    • Adrian Rosebrock February 28, 2019 at 1:44 pm #

      I was using an NVIDIA K80. You should check your output of “nvidia-smi” to confirm your GPU is being properly utilized.

  18. Andy March 3, 2019 at 2:38 pm #

    Thanks for this post Adrian — I love the simplicity of your tutorials and this one is right on the mark again.

    I ran into a dependency error while trying to run the downloaded code, so I thought I’d post the error and the resolution in case any of the other readers are running into the same issue.

    I got the following error when running $ python
    StopIteration: Could not import PIL.Image. The use of array_to_img requires PIL.

    I was able to get past it by using $ pip install pillow

    Thanks again!

    • Adrian Rosebrock March 5, 2019 at 8:52 am #

      Thanks for sharing, Andy!

  19. Prathik March 6, 2019 at 5:35 am #

    what is advantage of using CancerNet model?

    • Adrian Rosebrock March 8, 2019 at 5:44 am #

      Could you be more specific? As compared to what? The purpose of the tutorial was to show you how to implement a custom CNN architecture. The “advantage” here isn’t necessarily scientific but more-so “educational”.

  20. Cathy March 7, 2019 at 12:58 am #

    Hi Adrian! Thanks for your post.
    And I would like to ask where I can download the pre-trained model (parameters).
    I am trying to use Google colab to train this model, however, the connection with cloud is too fragile to maintain the model training process.

    • Adrian Rosebrock March 7, 2019 at 4:24 pm #

      You can use the “Downloads” section of the post to download the code, model, etc.

  21. Sivarama Krishnan Rajaraman March 8, 2019 at 9:24 am #

    Hi Adrian! The code works perfectly fine. However, when I perform the splits in the Windows machine, the train, validation, and test split numbers do not match with that mentioned herewith. Could you please suggest why? Many thanks.

  22. Xu Zhang March 11, 2019 at 7:30 pm #

    Hi Adrian! Thank you for the post.

    Are you familiar with the progressive resizing method for image classification? I know that the fastai library has a method to call it directly. Some papers showed they have used this method in Keras successfully. I understand the mechanism but I don’t know how to implement it in Keras. Do you have any advice? Many thanks

    • Adrian Rosebrock March 13, 2019 at 3:25 pm #

      Could you share an example of what you’re referring to?

  23. Muneeb Ullah March 13, 2019 at 3:41 am #

    I Really appreciate you.
    can you tell me, how we find testing loss and accuracy

    • Adrian Rosebrock March 13, 2019 at 3:10 pm #

      This tutorial already shows you how to find the testing accuracy. See the output of the “classification_report” function.

      • Muneeb Ullah March 14, 2019 at 1:02 pm #


  24. Juan Carlos March 26, 2019 at 9:31 pm #

    Hey Adrian

    From your amazing book DL4CV practitioners bundle, you advise to use transfer learning , have you tried it? I am planning to do so.

    • Adrian Rosebrock March 27, 2019 at 8:32 am #

      I have not tried transfer learning for this particular dataset. If you try it please report the results, I would be curious to know!

  25. Sayak May 3, 2019 at 8:56 am #

    Hello Adrian. Excellent tutorial. Love the way you detail about the things that are really required in the whole process especially the way you explained the need to look at specificity and sensitivity. Kudos!

    I am just curious to know about the time taken to build CancerNet? Also, were you using your Titan GPUs for this?

    • Adrian Rosebrock May 8, 2019 at 1:34 pm #

      I’m not sure what you mean by “time taken to build CancerNet”? CancerNet is a VGG-like network but with depthwise separable convolution instead of standard convolution. It was trained on a NVIDIA K80.

  26. khaled moawad May 5, 2019 at 7:02 pm #

    all variables of training,validation and testing data are lists of zero size .. what is the problem prof ?

    • Adrian Rosebrock May 8, 2019 at 1:12 pm #

      99.9% likely that your paths to the input dataset is incorrect. Double-check your paths.

  27. HuanCst May 25, 2019 at 4:40 am #

    This blog is great, thank you for sharing it. “Why don’t you use docker to install your libraries?” I think it’s a good idea for you.

    • Adrian Rosebrock May 30, 2019 at 9:32 am #

      Docker is fine but it’s way overkill for this project.

  28. jeff June 7, 2019 at 1:33 pm #

    Thanks Adrian for sharing these info.. am new in machine learning and am trying to find data set that can classify different types of cancer. may be using individual info.

  29. Fernando Lima June 17, 2019 at 11:38 pm #

    Hello Adrian. Excellent tutorial.
    I am a beginner in machine learning and would like to know in practice how to check if a cancer cell image is positive or negative

  30. murali July 2, 2019 at 12:02 pm #

    HI, It is a great tutorial.
    the image name also have ROI co-ordinate’s but they were not used . what is the use of ROI at the time of training . if we can train using ROI ,can you please say how to do it?

  31. muralli mallikarjun July 10, 2019 at 4:36 am #

    why i am getting high loss and high accuracy … i haved used only 10 epochs since my gpu is taking longer . i a using GTX1060

  32. Louis Loonis August 30, 2019 at 5:37 am #

    Hi Adrian,

    First of all, I would like to thank you for pyimagesearch. When I start a new computer vision project I always start by looking if any tutorial are here.

    I’m just wondering why you’re using binary crossentropy as you’re considering 2 output neurons. It does’t have to be 1 output neuron + sigmoid + binary_crossentropy or 2 output neurons + softmax + categorical_crossentropy ?

    • Adrian Rosebrock September 5, 2019 at 10:36 am #

      If you’re using a softmax output you should use “binary_crossentropy” for 2 class problems and “categroical_crossentropy” for > 2 classes.

  33. Joseph September 11, 2019 at 10:36 pm #

    Hello Adrian, I really enjoyed this tutorials. Thanks for the good work. regarding the imbalance i found this datasets which was used o a similar project to this one where it perfomed better compared to this. so i want to ask can we use this datasets to train the model here.
    I’m a little bit confuse so pls forgive if my question is too basic

    • Adrian Rosebrock September 12, 2019 at 11:28 am #

      The dataset you linked to is not an image dataset — the dataset we are using here is an image dataset. They are two different modalities.

  34. LITON DEVNATH October 1, 2019 at 2:47 am #

    Hi Adrian,
    It is a good example of a small dataset with deep learning. I’m new in deep learning. I have a question for you. I have a very small Chest X-ray data (only 172 images including normal and abnormal). how can I use converted my dataset to 50×50 pixel image patches? Any tutorial please share with me. my original image size 1024×1024 pixels.

    • Adrian Rosebrock October 3, 2019 at 12:31 pm #

      It’s great that you are interested in studying deep learning!

      Before you get too far I would recommend reading Deep Learning for Computer Vision with Python so you can learn the fundamentals. That book will help you get up to speed, ensuring you can apply DL to your project.

  35. Jin October 11, 2019 at 1:05 pm #

    Hi Adrian,

    Thank you for a great tutorial. My friend and I used your CancerNet architecture for our deep learning course project.
    We would like to cite where we got the CancerNet architecture from… Do you have any publications or books that refer to CancerNet model you used in this tutorial or is it okay to cite this webpage? Let us know your thoughts. Thank you!

  36. Kwabena Gah October 14, 2019 at 9:34 am #

    Hello, I had this error after running the experiment. Wondering what might caused this. Help if you can please
    plt.plot(np.arange(0, N), H.history[“acc”], label=”train_acc”)
    KeyError: ‘acc’

    • Adrian Rosebrock October 17, 2019 at 7:55 am #

      It sounds like you’re using TensorFlow 2.0. In TF 2.0 they changed “acc” to accuracy” and “val_acc” to “validation_accuracy”.

  37. Mehdi November 27, 2019 at 5:16 am #

    Hi Adrian,

    It was one year ago that I got to know your website and still there are many new things to learn from this wonderful educational website. Thank you for your helpful tutorials.
    I have a technical question for you.
    Recently I was asked to start academic research to perform anomaly detection on metal plates. The aim is to detect dents and scratches on plates. There has been a lot of research in this domain, however, since the project aims to provide an affordable solution, we want to use Raspberry Pi and RP camera module for this purpose. The concern is that if RP camera is capable of providing high-quality images to be used for training the ML and CNN models? Because even with the human eye it is sometimes difficult to identify those defections. I searched online and mostly industrial cameras have been suggested for such applications and I can’t find any previous work that used RP. So, this is my question to you as a knowledgable expert in the CV field: do you think RP camera can be used for such purpose? I deeply appreciate knowing your idea about this subject.


Before you leave a comment...

Hey, Adrian here, author of the PyImageSearch blog. I'd love to hear from you, but before you submit a comment, please follow these guidelines:

  1. If you have a question, read the comments first. You should also search this page (i.e., ctrl + f) for keywords related to your question. It's likely that I have already addressed your question in the comments.
  2. If you are copying and pasting code/terminal output, please don't. Reviewing another programmers’ code is a very time consuming and tedious task, and due to the volume of emails and contact requests I receive, I simply cannot do it.
  3. Be respectful of the space. I put a lot of my own personal time into creating these free weekly tutorials. On average, each tutorial takes me 15-20 hours to put together. I love offering these guides to you and I take pride in the content I create. Therefore, I will not approve comments that include large code blocks/terminal output as it destroys the formatting of the page. Kindly be respectful of this space.
  4. Be patient. I receive 200+ comments and emails per day. Due to spam, and my desire to personally answer as many questions as I can, I hand moderate all new comments (typically once per week). I try to answer as many questions as I can, but I'm only one person. Please don't be offended if I cannot get to your question
  5. Do you need priority support? Consider purchasing one of my books and courses. I place customer questions and emails in a separate, special priority queue and answer them first. If you are a customer of mine you will receive a guaranteed response from me. If there's any time left over, I focus on the community at large and attempt to answer as many of those questions as I possibly can.

Thank you for keeping these guidelines in mind before submitting your comment.

Leave a Reply