Machine LearningLesson 17 of 22

Deep Learning with CNNs

Convolutional Neural Networks are built for images. They slide small filters to find edges, textures, then parts, then objects. This is why they beat plain forests on photos.

Convolutional Neural Networks are built for images. They slide small filters to find edges, textures, then parts, then objects. This is why they beat plain forests on photos.

You will see convolution and pooling. Convolution finds patterns, pooling shrinks the map and keeps the strongest. Data augmentation artificially expands your dataset by flipping and cropping.

You do not need a supercomputer. With transfer learning you can fine-tune a pretrained model on your data in minutes on Colab GPU.

Convolution and pooling

A filter is a tiny weight matrix that slides. Pooling downsamples. Together they build hierarchy.

PYTHON
# Keras example
# model = Sequential([Conv2D(32,3,activation="relu"), MaxPooling2D(), Flatten(), Dense(10,activation="softmax")])

Transfer learning

Reuse a model trained on ImageNet and fine-tune your classes. Much faster than from scratch.

PYTHON
# base = EfficientNetB0(weights="imagenet", include_top=False)
# base.trainable = False

Syntax

PYTHON
from tensorflow import keras
model = keras.Sequential([keras.layers.Conv2D(32,3,activation="relu"), keras.layers.MaxPooling2D(), keras.layers.Flatten(), keras.layers.Dense(10, activation="softmax")])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

Colab GPU vs local CPU

Training CNN on CPU can take hours, on Colab GPU minutes. In Colab set Runtime -> Change runtime type -> GPU. Forgetting this is the top r/learnmachinelearning post.

PYTHON
# Colab: Runtime -> Change runtime type -> T4 GPU
# Check: import tensorflow as tf; print(tf.config.list_physical_devices("GPU"))

Good to know

  • CNNs use filters and pooling for images.
  • Data augmentation expands small datasets.
  • Transfer learning saves time and GPU.

Keep building your ML models

Copy the code, run it locally with pip install scikit-learn pandas, and continue to the next lesson.

→ Next: NLP Basics: Text to Vectors