WikiGalaxy

Personalize

Deep Learning in Computer Vision

Introduction to Deep Learning in Computer Vision

Deep learning has revolutionized the field of computer vision by enabling machines to interpret and understand visual data. This technology is employed in various applications, from facial recognition to autonomous vehicles.

  • Utilizes neural networks to process visual data.
  • Capable of learning complex patterns in images.
  • Widely used in image classification, object detection, and segmentation.

Image Classification

Image Classification

Image classification involves assigning a label to an image from a predefined set of categories. Deep learning models, particularly Convolutional Neural Networks (CNNs), have significantly improved the accuracy of image classification tasks.

  • Uses CNNs for feature extraction and classification.
  • Trained on large datasets like ImageNet.
  • Applications in medical imaging, autonomous vehicles, and more.

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

# Load and preprocess data
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0

# Define the 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.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

This example demonstrates a basic CNN model for classifying images from the CIFAR-10 dataset. The model is trained over 10 epochs and uses layers such as Conv2D and MaxPooling2D to extract features from images.

  • The model architecture includes convolutional layers followed by pooling layers for feature extraction.
  • The final Dense layer outputs predictions for each of the 10 classes in CIFAR-10.
  • Adam optimizer and sparse categorical crossentropy loss are used for training.

Object Detection

Object Detection

Object detection aims to identify and locate objects within an image. It extends image classification by providing bounding boxes around detected objects. Popular models include YOLO and Faster R-CNN.

  • Combines classification and localization tasks.
  • Uses bounding boxes to specify object locations.
  • Applications in surveillance, autonomous driving, and robotics.

import torch
import torchvision
from torchvision.models.detection import fasterrcnn_resnet50_fpn

# Load a pre-trained Faster R-CNN model
model = fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()

# Load an image
from PIL import Image
from torchvision.transforms import functional as F
image = Image.open('path/to/image.jpg')
image = F.to_tensor(image)

# Perform object detection
with torch.no_grad():
    prediction = model([image])

# Display results
for element in prediction[0]['boxes']:
    print(element)
        

Explanation

This example illustrates how to use a pre-trained Faster R-CNN model for object detection. The model predicts bounding boxes for objects in the input image.

  • Faster R-CNN is a two-stage object detection model.
  • The first stage proposes candidate object regions, and the second stage classifies these regions.
  • PIL is used to load images, and Torchvision provides utilities for model inference.

Semantic Segmentation

Semantic Segmentation

Semantic segmentation assigns a class label to each pixel in an image. This technique is essential for understanding the structure and context of scenes in images.

  • Labels every pixel with a class.
  • Used in medical imaging and autonomous vehicles.
  • Popular models include U-Net and DeepLab.

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

# Define a simple U-Net model
def unet_model(input_shape):
    inputs = tf.keras.Input(shape=input_shape)
    x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(inputs)
    x = layers.MaxPooling2D((2, 2))(x)
    x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
    x = layers.UpSampling2D((2, 2))(x)
    outputs = layers.Conv2D(1, (1, 1), activation='sigmoid')(x)
    return models.Model(inputs, outputs)

# Instantiate and compile the model
model = unet_model((128, 128, 3))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
        

Explanation

This code defines a simplified U-Net architecture for semantic segmentation. The model processes an input image and predicts a segmentation mask.

  • U-Net is widely used for medical image segmentation.
  • The model consists of contracting and expanding paths for feature extraction and reconstruction.
  • Binary crossentropy loss is used for binary segmentation tasks.

Generative Adversarial Networks (GANs)

Generative Adversarial Networks (GANs)

GANs are used to generate realistic images by training two networks: a generator and a discriminator. The generator creates images, while the discriminator evaluates them.

  • Used for image synthesis and style transfer.
  • Consists of a generator and a discriminator.
  • Applications in art creation, image enhancement, and more.

import torch
import torch.nn as nn
import torch.optim as optim

# Define the generator model
class Generator(nn.Module):
    def __init__(self):
        super(Generator, self).__init__()
        self.main = nn.Sequential(
            nn.Linear(100, 256),
            nn.ReLU(True),
            nn.Linear(256, 512),
            nn.ReLU(True),
            nn.Linear(512, 1024),
            nn.ReLU(True),
            nn.Linear(1024, 784),
            nn.Tanh()
        )

    def forward(self, input):
        return self.main(input)

# Instantiate generator
generator = Generator()

# Define loss and optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(generator.parameters(), lr=0.0002)
        

Explanation

This example demonstrates a simple GAN architecture where a generator network is defined to create images from random noise.

  • The generator uses linear layers to transform random noise into images.
  • ReLU activations are used for non-linearity, and Tanh is applied to the output layer.
  • BCELoss is used for training the GAN with an Adam optimizer.

Transfer Learning

Transfer Learning

Transfer learning leverages pre-trained models on large datasets to improve performance on specific tasks. It is particularly useful when labeled data is scarce.

  • Uses pre-trained models like VGG, ResNet, etc.
  • Fine-tunes models on new tasks with limited data.
  • Speeds up training and improves accuracy.

import tensorflow as tf
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Flatten

# Load pre-trained VGG16 model + higher level layers
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

# Freeze base model
base_model.trainable = False

# Add custom layers on top
x = Flatten()(base_model.output)
x = Dense(1024, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

# Create new model
model = Model(inputs=base_model.input, outputs=predictions)

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

Explanation

This example demonstrates transfer learning using the VGG16 model. The base model's layers are frozen, and new layers are added for a specific task.

  • VGG16 is used as a feature extractor, and its weights are not updated during training.
  • Custom layers are added to adapt the model for a new classification task.
  • Categorical crossentropy loss is used for multi-class classification.
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025