Let’s Find Out If You’ve Got Breast Cancer — Using Artificial Intelligence

Sohail Sayed
May 22 · Unlisted

Using Deep Convolutional Neural Networks To Detect Breast Cancer In Mammography

If you’re a woman reading this, you’re not safe anymore. You’re in danger.

1 in 8 women will be diagnosed with breast cancer.

At least one of these women will run into breast cancer.

When you think of danger, what scares you the most? Is it a car accident, or being murdered? How about a plane crash?

Chances Of Dying: A Small list💀

Car Accident | 1 in 103

Murder | 1 in 16,000

Plane Crash | 1 in 11,000,000


Breast Cancer for women | 1 in 8

Aren’t you silly, worrying about nothing.

Okay, maybe murder isn’t nothing. But just looks at how LITTLE it matters compared to breast cancer. That goes for us guys too 👴:

(1 in 1000 odds for breast cancer in men — still much higher than murder).

Does this scare you more than a gun?

You’re a lot more likely to die from breast cancer than be murdered.

Breast Cancer is a Painful Process

So here’s the long and short of it. Unlike a murder, or plane crash — which are horrifying in their own rights — cancer is a long, painful process. Suffering lasts months, even YEARS. It’s terrifying and drags on😱.

Chemotherapy— which is the best cure we’ve currently got —doesn’t discriminate between cancer cells and your healthy cells. That mean’s otherwise healthy parts of your body become…well, not so healthy.

It’s a lose-lose situation

An Early Diagnoses is KEY To Survive and Reduce Suffering

Ok, so you’ve got breast cancer. It’s tough. But the fact is that a majority of victims will survive if they’ve been diagnosed early. This isn’t the case once the cancer’s spread too much.

So it seems that an early and accurate diagnosis is key. Who is the person responsible for this possibly life-saving decision🤔? It’s the pathologist.

Pathologists Make Deadly Mistakes

Pathologists aren’t perfect. Most pathologists have a success rate of 96–98% success rate for diagnosing cancer. That isn’t too bad, right? Not bad, except for the fact that 2–4% means THOUSANDS of people. THOUSANDS of people that have cancer that’s never detected.

That’s not the only thing about pathology that can be improved. The average pathology report takes 2 to 3 weeks to complete.

What the 🤯!!! That means cancer patients have to wait almost a month to find out if they’ve got cancer or not. I can’t even imagine how much stress this causes.

AI Can Diagnose Cancer Better Than Pathologists

Alright, so pathologist = decent but not good enough. But if a highly trained professional with years of studying pathology isn’t good enough, no other person can be, right?


Now here comes the good part.

There happens to be this thing (not person) that is good enough. The thing = Artificial Intelligence. AI doesn’t have the emotional or physical limitations a pathologist has.

The only time Grey’s Anatomy has ever been useful to me.

So it can train A LOT harder, smarter and faster without getting upset every time it’s favorite Grey’s Anatomy character dies, unlike us humans. AI doesn’t even care about Grey’s Anatomy.

Using Keras, I made a convolutional neural network which can identify breast cancer in a mammogram.

The model I made uses a convolutional neural network — or CNN — to:

a. Learn what a mammogram with cancer looks like


b. Classify additional mammograms you feed it as cancerous or benign

It’s a simple classification task, but it can literally save lives 🚀.

Mammogram: An x-ray photo of the breast 🏥

← Like the one on the left

But Wait — What’s a Convolutional Neural Network

Ohhh, let me explain what a convolutional neural network — or CNN — is first.

Not that CNN

CNN’s are a type of neural network that are really good at identifying the features of an image. Hell, they can even teach a car to drive on its own (🚨shameless promo alert🚨 — check out my article on that too though 🚗)

Let me tell you about how it’s done. It starts small.

I won’t get too deep into the math. However, just know that this is how edges are detected.

The CNN begins by literally scanning a grid of 4x4 or 5x5 grid of pixels for edges. Yes, literal edges. The edges are detected by running a kernel of numbers, or a filter on top of the pixels to map the pixel values on a separate feature map.

But the CNN doesn’t stop there. In the next layer, it combines edges to form features.

Examples of literal edges. The CNN combines these to form larger features, which are also combined to form even larger features.

And before long, the CNN is smart enough to know what features make up a certain class — or not. Like it knows that dogs have large tails while cats have short snouts. Pretty cool right? And that way, it can actually know what a dog vs cat looks like.

That's essentially what my CNN did — but with breast cancer mammograms.

Let’s Take a Look At The Code

Alright, so first of we’ve got to import the needed libraries for this model.

OK, cool! We’ve imported the libraries that will be used for this CNN. What next?

How about — data preprocessing! Yay 🎉🎉(doesn’t sound quite as much fun as I thought it would). This is the section where the training data is actually prepared and cleaned out 💨.

Do you remember that CNN thing I explained earlier — well here it is in flesh. My CNN has 6 layers — 4 dense and one activation — which drive the model’s ability to learn what a malignant mammogram looks like.

Oh, and my model uses the Adam optimizer which uses gradient descent to tweak parameters and get more accurate. Check out my article on the future of cancer prediction using ML’s description of gradient descent if you’re still confused btw.

Then we predict if the sample is benign or malignant!


print( "\n Accuracy of breast cancer detection system:" , np.sum(y_prediction == test_Y) / float(len(test_Y)))#server here
import time
import pymysql
while True:
db = pymysql.connect("localhost","root","","cancer")
cursor = db.cursor()
cursor.execute("SELECT * FROM patient_data WHERE diagnosis = ''")
data = cursor.fetchall()
for row in data:
p_id = row[0]
data = []
temp = []
for i in range(1,31):
data = np.array(data)
y_prediction = model.predict_classes(scaler.transform(data))
if y_prediction[0] == 1:
diagnosis = "Benign"
diagnosis = "Malignant"
cursor.execute("UPDATE patient_data SET diagnosis = %s WHERE p_id = %s",(diagnosis, str(p_id)))

All using CNN’s, we’re able to create a simple model which can do a more than a simple job. I mean just think about what kind of models can be created as ML gets better and better, at a crazy fast rate.

I wouldn’t be surprised if my next checkup with the doctor involved a bit of machine learning — if it isn’t already being used.


  • Breast cancer is a BIG and really common problem. You’re MUCH more likely to develop breast cancer than be murdered, or die in a plane crash even if you’re a guy.
  • Pathologists are awesome, but AI is soon going to be more awesome at a pathologists job.
  • Convolutional Neural Networks are a super neat tool used in ML to recognize features within an image and learn the features to classify images. It’s what I used to create a breast cancer detection model.

Before You Go!


Sohail Sayed

Written by

16 year old Activator @The Knowledge Society. Check out my website! sohailsayed.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade