DEV Community

Hassan Ahmed
Hassan Ahmed

Posted on

I Built a CNN to Detect Skin Cancer from Images (Beginner ML Project)

I Built a CNN to Detect Skin Cancer from Images (Beginner ML Project)

Hey everyone šŸ‘‹

Just wanted to share a machine learning project I recently built as part of my learning journey. It's a basic skin cancer detection model using a Convolutional Neural Network (CNN). The model classifies skin lesion images as benign or malignant, and I tested it locally with a Streamlit app.


Why I Picked This Project

I’m still learning machine learning, and I wanted to try something practical something where I could take an idea, build a model, and test it with real images. Skin cancer is a serious health issue, and early detection helps a lot, so I thought this would be a good starting point for a classification task.

āš ļø Disclaimer: This is an educational project only not for real medical use.


Tools I Used

  • Python
  • TensorFlow + Keras
  • Streamlit
  • Pillow / NumPy
  • Jupyter Notebook

What the Model Does

  • Loads a skin lesion image
  • Preprocesses it (resize, normalize)
  • Predicts if the image is benign or malignant
  • Shows the result in a local Streamlit interface

CNN Architecture (Simplified)

model = Sequential([
    Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),
    MaxPooling2D((2, 2)),
    Conv2D(64, (3, 3), activation='relu'),
    MaxPooling2D((2, 2)),
    Conv2D(128, (3, 3), activation='relu'),
    MaxPooling2D((2, 2)),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(1, activation='sigmoid')
])
Enter fullscreen mode Exit fullscreen mode

Trained with binary cross-entropy since it's a binary classification task.


Prediction Function (Streamlit)

def predict_skin_cancer(image_path, model):
    img = image.load_img(image_path, target_size=(224, 224))
    img_array = image.img_to_array(img) / 255.0
    img_array = np.expand_dims(img_array, axis=0)

    prediction = model.predict(img_array)
    return "Malignant" if prediction > 0.5 else "Benign"
Enter fullscreen mode Exit fullscreen mode

How It Looks

The Streamlit interface is basic — upload an image, and it shows the prediction.

App Screenshot
Result Screenshot


What I Learned

  • How CNNs work for image classification
  • Preprocessing is super important
  • Saving and loading trained models
  • How to create quick tools with Streamlit for testing models

Future Improvements

  • Better dataset (mine was small)
  • Try transfer learning (e.g. MobileNet or EfficientNet)
  • Add Grad-CAM for model explainability
  • Deploy the app online (Streamlit Cloud or Hugging Face Spaces)

šŸ”— GitHub Repo

Full code is here if you want to check it out or test it locally:
šŸ‘‰ https://github.com/Hassan123j/Skin-cancer-detection-using-CNN


If you're learning ML like me, feel free to reach out, ask questions, or give feedback. I'd love to hear from others doing similar stuff!


Top comments (0)