WikiGalaxy

Personalize

What are CNNs?

Introduction to CNNs

Convolutional Neural Networks (CNNs) are a class of deep learning algorithms primarily used for image processing, classification, segmentation, and other computer vision tasks. They are designed to automatically and adaptively learn spatial hierarchies of features from images.

  • Convolutional Layer: The core building block of a CNN which performs convolution operations.
  • Pooling Layer: Reduces the dimensionality of each feature map while retaining important information.
  • Fully Connected Layer: Connects neurons in one layer to every neuron in the next layer, similar to a traditional neural network.
  • Activation Functions: Introduce non-linearities into the network, commonly using ReLU (Rectified Linear Unit).
  • Dropout: A regularization technique used to prevent overfitting by randomly setting a fraction of input units to zero at each update during training time.

Example: Image Classification with CNNs

Image Classification

One of the most common applications of CNNs is image classification. This involves assigning a label to an image from a predefined set of categories.


import tensorflow as tf
from tensorflow.keras import layers, models

# Load dataset
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()

# Normalize pixel values
train_images, test_images = train_images / 255.0, test_images / 255.0

# Define a simple CNN model
model = models.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10)
])

# Compile the model
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

# Train the model
model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))
        

Explanation of Code

  • Loading Dataset: CIFAR-10 dataset is used, which consists of 60,000 32x32 color images in 10 different classes.
  • Normalization: Pixel values are scaled to the range [0, 1] for better convergence.
  • Model Architecture: The model consists of three convolutional layers followed by pooling layers, a flattening layer, and two dense layers.
  • Compilation and Training: The model is compiled with Adam optimizer and trained for 10 epochs.

Example: Object Detection with CNNs

Object Detection

CNNs can be extended for object detection tasks where the goal is to identify and locate objects within an image.


# Assume we have a pre-trained model like YOLO or SSD
# Load pre-trained model (pseudo-code)
model = load_pretrained_object_detection_model()

# Load and preprocess image
image = preprocess_image('path/to/image.jpg')

# Perform object detection
detections = model.detect_objects(image)

# Display results
for detection in detections:
    print(f"Detected {detection['label']} with confidence {detection['confidence']}")
        

Explanation of Code

  • Pre-trained Models: Models like YOLO (You Only Look Once) or SSD (Single Shot MultiBox Detector) are commonly used for object detection.
  • Preprocessing: Input images are preprocessed to match the input size and format required by the model.
  • Detection: The model outputs bounding boxes and labels for detected objects.

Example: Semantic Segmentation with CNNs

Semantic Segmentation

Semantic segmentation involves labeling each pixel in an image with a class, which is a more granular task than object detection.


# Assume we have a pre-trained segmentation model like U-Net
# Load pre-trained model (pseudo-code)
model = load_pretrained_segmentation_model()

# Load and preprocess image
image = preprocess_image('path/to/image.jpg')

# Perform segmentation
segmentation_map = model.segment_image(image)

# Display segmentation map
display_segmentation_map(segmentation_map)
        

Explanation of Code

  • Pre-trained Models: Models like U-Net are effective for semantic segmentation tasks.
  • Preprocessing: Input images are resized and normalized as per the model's requirements.
  • Segmentation Map: The model outputs a segmentation map where each pixel is classified into a category.

Example: Image Generation with CNNs

Image Generation

CNNs are also used in generative models like GANs (Generative Adversarial Networks) for creating new images.


# Assume we have a GAN model
# Load pre-trained GAN model (pseudo-code)
generator = load_pretrained_gan_generator()

# Generate random noise
noise = generate_random_noise()

# Generate image from noise
generated_image = generator.generate_image(noise)

# Display generated image
display_image(generated_image)
        

Explanation of Code

  • GANs: Consist of two networks, a generator and a discriminator, that work against each other.
  • Random Noise: The generator uses random noise as input to produce new images.
  • Image Generation: The generator creates an image that tries to mimic real images as closely as possible.

Example: Transfer Learning with CNNs

Transfer Learning

Transfer learning involves using a pre-trained model on a new task, leveraging the knowledge it has already acquired.


# Load a pre-trained model like VGG16
base_model = tf.keras.applications.VGG16(input_shape=(224, 224, 3), include_top=False, weights='imagenet')

# Freeze the base model
base_model.trainable = False

# Add custom layers on top
model = tf.keras.Sequential([
    base_model,
    layers.Flatten(),
    layers.Dense(1024, activation='relu'),
    layers.Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train the model on new data
model.fit(new_train_images, new_train_labels, epochs=5, validation_data=(new_test_images, new_test_labels))
        

Explanation of Code

  • Pre-trained Model: VGG16 is used as the base model, pre-trained on ImageNet.
  • Freezing Layers: The layers of the base model are frozen to retain pre-learned features.
  • Custom Layers: Additional layers are added to adapt the model to the new task.
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025