TF-Notes: Difference between revisions

From Depth Psychology Study Wiki
mNo edit summary
Line 632: Line 632:
* '''Eager Execution'''
* '''Eager Execution'''
** Executes operations immediately rather than building a static graph.
** Executes operations immediately rather than building a static graph.
** Makes TensorFlow more '''intuitive''' and Pythonic, enabling straightforward/improved debugging.
Makes TensorFlow more '''intuitive''' and Pythonic, enabling straightforward/improved debugging.
*** Immediate Feedback  
* Immediate Feedback
 
** Facilitates interactive programming
** Facilitates interactive programming
** Simplified Code
** Simplified Code
Line 661: Line 662:
**TensorBoard
**TensorBoard
***Visualization toolkit for TensorFlow
***Visualization toolkit for TensorFlow
== '''Convolutional Neural Networks (CNNs)''' ==
'''Purpose''':
CNNs analyze visual data by leveraging patterns in the spatial structure of images, mimicking the hierarchical structure of the human visual system.
'''Key Components of CNNs''':
# '''Convolutional Layers'''
#* Perform feature extraction.
#* Learn spatial hierarchies of features through kernels (filters) applied to the input data.
#* Generate feature maps by convolving the input with a set of filters.
# '''Pooling Layers'''
#* Perform downsampling to reduce the spatial dimensions of feature maps.
#* Types:
#** '''Max Pooling''': Retains the maximum value in a pooling window.
#** '''Average Pooling''': Retains the average value in a pooling window.
#* Reduces computational complexity and prevents overfitting by acting as a form of regularization.
# '''Fully Connected (FC) Layers'''
#* Flatten the high-level features from convolutional and pooling layers into a single vector.
#* Use the vector for classification or regression tasks by passing it through dense layers and applying an activation function like softmax or sigmoid.
----
=== Workflow of CNNs: ===
# '''Input Image'''
#* Raw pixel data of the image is input into the network.
# '''Convolutional Layers'''
#* Extract low-level features like edges, corners, and textures. With deeper layers, higher-level features like shapes and objects are learned.
# '''Activation Function'''
#* Non-linear activation functions (e.g., ReLU) are applied after convolution to introduce non-linearity and enable the network to learn complex patterns.
# '''Pooling Layers'''
#* Reduce dimensionality while retaining key features. This decreases computational costs and improves model generalization.
# '''Fully Connected Layers'''
#* Use learned features to make final predictions. Typically followed by a softmax or sigmoid activation for classification.
----
=== Expanded CNN Architecture Example: ===
* Input -> Convolution -> ReLU -> Pooling -> Convolution -> ReLU -> Pooling -> Fully Connected -> Output.
----
=== Additional Notes for Understanding: ===
# '''Padding'''
#* Used to preserve spatial dimensions after convolution. Common padding types are:
#** '''Valid Padding''': No padding; reduces output size.
#** '''Same Padding''': Pads input so that output dimensions match the input dimensions.
# '''Stride'''
#* The number of pixels by which the filter moves during convolution. Larger strides reduce the spatial size of the output.
# '''Regularization Techniques'''
#* '''Dropout''': Randomly sets a fraction of input units to zero during training to prevent overfitting.
#* '''Batch Normalization''': Normalizes intermediate activations to improve stability and training speed.
# '''Data Augmentation'''
#* Techniques like flipping, rotating, cropping, and scaling are used to increase the diversity of the training data and improve generalization.
# '''Applications of CNNs''':
#* Image classification.
#* Object detection.
#* Semantic segmentation.
#* Medical imaging.
#* Style transfer.
# '''Popular Architectures''':
#* '''LeNet''': Early architecture for handwritten digit recognition.
#* '''AlexNet''': Introduced deeper networks with ReLU and dropout.
#* '''VGGNet''': Deep but simple networks with small filters.
#* '''ResNet''': Introduced residual connections to combat vanishing gradients.
#* '''Inception''': Used modules to increase depth and width efficiently.
Code Example: Basic CNN:
----from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
<nowiki>#</nowiki> Create a Sequential model
model = Sequential([
    Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),  # First Conv2D layer
    MaxPooling2D((2, 2)),  # First MaxPooling layer
    Conv2D(64, (3, 3), activation='relu'),  # Second Conv2D layer
    MaxPooling2D((2, 2)),  # Second MaxPooling layer
    Flatten(),  # Flatten the feature maps
    Dense(128, activation='relu'),  # Fully connected dense layer
    Dense(10, activation='softmax')  # Output layer for 10 classes
])
<nowiki>#</nowiki> Compile the model
model.compile(optimizer='adam',  # Use Adam optimizer
              loss='categorical_crossentropy',  # Loss function for multi-class classification
              metrics=['accuracy'])  # Metrics to monitor during training
<nowiki>#</nowiki> Print a summary of the model
model.summary()
----
* '''Model Explanation''':
** <code>Conv2D(32, (3, 3))</code>: Adds a convolutional layer with 32 filters of size (3x3).
** <code>MaxPooling2D((2, 2))</code>: Reduces spatial dimensions by taking the max value in a (2x2) window.
** <code>Flatten()</code>: Flattens the 2D feature maps into a 1D vector.
** <code>Dense(128, activation='relu')</code>: Fully connected layer with 128 units and ReLU activation.
** <code>Dense(10, activation='softmax')</code>: Output layer for classification into 10 classes using softmax activation.
* '''Input Shape''':
** <code>input_shape=(64, 64, 3)</code> assumes input images are 64x64 pixels with 3 color channels (RGB).
----
=== '''Advanced CNN Architectures''' ===
==== '''1. VGG (Visual Geometry Group Networks)''' ====
* '''Key Idea''': Uniform small 3×3 convolutional filters.
** Max-Pooling Layers
** Fully Connected Layers
* '''Structure''': Deep networks (e.g., VGG-16: 13 convolutional + 3 dense layers).
* '''Pros''': Simplicity and depth for feature extraction.
* '''Cons''': High computational cost.
----from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
<nowiki>#</nowiki> Create a Sequential model
model = Sequential([
    Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),  # First Conv2D layer
    Conv2D(32, (3, 3), activation='relu'),  # Second Conv2D layer
    MaxPooling2D((2, 2)),  # First MaxPooling layer
    Conv2D(128, (3, 3), activation='relu'),  # Third Conv2D layer
    Conv2D(128, (3, 3), activation='relu'),  # Fourth Conv2D layer
    MaxPooling2D((2, 2)),  # Second MaxPooling layer
    Conv2D(256, (3, 3), activation='relu'),  # Fifth Conv2D layer
    Conv2D(256, (3, 3), activation='relu'),  # Sixth Conv2D layer
    MaxPooling2D((2, 2)),  # Third MaxPooling layer
    Flatten(),  # Flatten the feature maps
    Dense(512, activation='relu'),  # First Fully connected dense layer
    Dense(512, activation='relu'),  # Second Fully connected dense layer
    Dense(10, activation='softmax')  # Output layer for 10 classes
])
<nowiki>#</nowiki> Compile the model
model.compile(optimizer='adam',  # Use Adam optimizer
              loss='categorical_crossentropy',  # Loss function for multi-class classification
              metrics=['accuracy'])  # Metrics to monitor during training
<nowiki>#</nowiki> Print a summary of the model
model.summary()
----
=== Explanation of Key Components: ===
# '''Conv2D Layers''':
#* 32,128,256 filters represent increasing feature extraction depth.
#* 3×3 kernels are used consistently for feature extraction.
# '''MaxPooling2D Layers''':
#* Downsample the feature maps to reduce spatial dimensions and computational costs.
# '''Flatten Layer''':
#* Converts the 3D feature maps into a 1D vector for dense layer input.
# '''Dense Layers''':
#* Two fully connected layers with 512 neurons each, followed by a softmax layer for 10-class classification.
# '''Compilation''':
#* '''Adam optimizer''': Adaptive learning rate optimization for faster convergence.
#* '''Categorical Crossentropy''': Suitable for multi-class classification problems.
----
==== '''2. ResNet (Residual Networks)''' ====
* '''Key Idea''': '''Residual connections''' (skip connections) to combat vanishing gradients.
* '''Structure''': Residual block: Output=F(Input)+Input.
* '''Pros''': Enables very deep networks (e.g., ResNet-50, ResNet-101).
* '''Cons''': Increased complexity.
----from tensorflow.keras.layers import Input, Conv2D, BatchNormalization, Activation, Add, MaxPooling2D, Flatten, Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
<nowiki>#</nowiki> Residual Block
def residual_block(x, filters, kernel_size=3, stride=1):
shortcut = x
    x = Conv2D(filters, kernel_size, strides=stride, padding='same')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(filters, kernel_size, strides=1, padding='same')(x)
    x = BatchNormalization()(x)
    x = Add()([x, shortcut]) 
    x = Activation('relu')(x)
    return x
<nowiki>#</nowiki> ResNet-like Model
def resnet_like(input_shape=(64, 64, 3), num_classes=10):
    inputs = Input(shape=input_shape)
    # Initial Conv Layer
    x = Conv2D(64, (7, 7), strides=2, padding='same')(inputs)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=2, padding='same')(x)
    # Residual Blocks
    x = residual_block(x, 64)
    x = residual_block(x, 64)
    x = residual_block(x, 128, stride=2)
    x = residual_block(x, 128)
    x = residual_block(x, 256, stride=2)
    x = residual_block(x, 256)
    # Global Average Pooling and Output
    x = GlobalAveragePooling2D()(x)
    x = Dense(num_classes, activation='softmax')(x)
    # Create Model
    model = Model(inputs, x)
    return model
<nowiki>#</nowiki> Create and compile the ResNet-like model
model = resnet_like()
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
<nowiki>#</nowiki> Print the model summary
model.summary()
----
==== '''3. Inception Networks''' ====
* '''Key Idea''': '''Multi-scale feature extraction''' with filters of different sizes (e.g., 1×1, 3×3, 5×5).
* '''Structure''': Inception modules use 1×1 bottleneck layers to reduce dimensionality.
* '''Pros''': Efficient and versatile.
* '''Cons''': Complex architecture design.
== Data Augmentation Techniques ==
'''Purpose''':
* Enhance the robustness and generalization of models.
* Artificially expand the training dataset by introducing variations.
* Reduce overfitting by exposing the model to diverse examples.
* Improve performance on unseen data by mimicking real-world variations.
----
==== '''Common Data Augmentation Techniques''' ====
# '''Rotation''':
#* Rotate images by a random degree within a specified range (e.g., ±30°).
#* Helps the model learn invariance to orientation changes.
# '''Translations''':
#* Shift the image horizontally or vertically by a certain number of pixels.
#* Useful for objects that might not always be centered in the frame.
# '''Flipping''':
#* Horizontal flipping: Mirrors the image along the vertical axis.
#* Vertical flipping (less common): Mirrors the image along the horizontal axis.
#* Effective for symmetrical objects or scenes.
# '''Scaling''':
#* Resize the image by a random factor, either zooming in or out.
#* Helps the model learn invariance to object sizes.
# '''Adding Noise''':
#* Inject random noise (e.g., Gaussian noise, salt-and-pepper noise) to simulate variations in image quality.
#* Helps the model become robust to noisy or low-quality inputs.
----
==== '''Other Useful Techniques''': ====
# '''Shearing''':
#* Distort the image by slanting it along an axis.
#* Simulates different perspectives or viewing angles.
# '''Cropping''':
#* Randomly crop parts of the image to simulate zoomed-in views or partial occlusions.
# '''Brightness, Contrast, and Color Adjustments''':
#* Alter the brightness, contrast, saturation, or hue of the image.
#* Makes the model robust to varying lighting conditions.
# '''Random Erasing''':
#* Randomly mask out parts of the image.
#* Forces the model to rely on other parts of the image for prediction.
# '''CutMix and MixUp''':
#* '''CutMix''': Combines parts of two images and mixes their labels.
#* '''MixUp''': Mixes two images and their labels linearly.
----from tensorflow.keras.preprocessing.image import ImageDataGenerator
<nowiki>#</nowiki> Create an ImageDataGenerator instance with augmentation techniques
datagen = ImageDataGenerator(
    rotation_range=30,       # Rotate images by up to 30 degrees
    width_shift_range=0.2,   # Translate images horizontally by 20% of width
    height_shift_range=0.2,  # Translate images vertically by 20% of height
    horizontal_flip=True,    # Randomly flip images horizontally
    zoom_range=0.2,          # Zoom in/out by up to 20%
    brightness_range=[0.8, 1.2],  # Adjust brightness (80% to 120%)
    fill_mode='nearest'      # Fill missing pixels after transformations
)
<nowiki>#</nowiki> Example: Applying augmentations to a single image
from tensorflow.keras.preprocessing.image import img_to_array, load_img
image = load_img('example.jpg', target_size=(64, 64))  # Load image
image_array = img_to_array(image)  # Convert to array
image_array = image_array.reshape((1,) + image_array.shape)  # Add batch dimension
<nowiki>#</nowiki> Generate augmented images
i = 0
for batch in datagen.flow(image_array, batch_size=1):
plt.figure(i)
imgplot = plt.imshow(image.array to img(batch[0]))
----
=== '''Feature-Wise Normalization''' ===
* '''Definition''': Normalize the entire dataset feature-by-feature, based on the dataset's mean and standard deviation.
* '''Use Case''': Ensures all features have similar ranges, which helps models converge faster during training.
* '''How It Works''':
** Compute the mean and standard deviation for each feature (e.g., pixel intensity) across all images in the dataset.
** Subtract the mean and divide by the standard deviation for each feature.
----datagen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True)
<nowiki>#</nowiki> Fit the generator on the dataset to compute statistics
datagen.fit(training_data)  # training_data is a NumPy array of images
----
=== '''Sample-Wise Normalization''' ===
* '''Definition''': Normalize each individual sample (image) independently by subtracting the sample's mean and dividing by its standard deviation.
* '''Use Case''': Useful when you want to normalize each image separately, especially if the dataset contains images with different intensity distributions.
* '''How It Works''':
** For each image, compute its mean and standard deviation.
** Normalize the image by subtracting its mean and dividing by its standard deviation.
----datagen = ImageDataGenerator(samplewise_center=True, samplewise_std_normalization=True)
<nowiki>#</nowiki> Use the generator as part of the training process
datagen.flow(training_data, training_labels)
----ImageDataGenerator.fit(training_images)
i = 0
for batch in ImageDataGenerator.flow(training_images, batch_size=32):
plt.figure(i)
imgplot = plt.imshow(image.array_ti_image(batch[0]))
i += 1
if i % 4 == 0
break
plt.show()
----
=== '''Custom Augmentation Functions''' ===
* '''Definition''': Define your own augmentation logic when the built-in transformations are insufficient or you need specialized augmentations.
* '''How It Works''':
** Write a custom function that modifies the input data (e.g., applying specific transformations or injecting custom noise).
** Pass the function as part of a data preprocessing pipeline.
----import numpy as np
<nowiki>#</nowiki> Define a custom function to add random noise
def add_noise(image):
    noise = np.random.normal(loc=0, scale=0.1, size=image.shape)  # Gaussian noise
    return np.clip(image + noise, 0, 1)  # Ensure pixel values remain in [0, 1]
<nowiki>#</nowiki> Create a DataGenerator with a custom preprocessing function
datagen = ImageDataGenerator(preprocessing_function=add_noise)
<nowiki>#</nowiki> Use the generator
i = 0
for batch in datagen.flow(training_images, batch_size=32):
plt.figure(i)
imgplot = plt.imshow(image.array_ti_image(batch[0]))
i += 1
if i % 4 == 0
break
plt.show()
----

Revision as of 03:58, 28 January 2025

Shallow Neural Networks v.s. Deep Neural Networks

Shallow Neural Networks:

  • Consist of one hidden layer (occasionally two, but rarely more).
  • Typically take inputs as feature vectors, where preprocessing transforms raw data into structured inputs.

Deep Neural Networks:

  • Consist of three or more hidden layers.
  • Handle larger numbers of neurons per layer depending on model design.
  • Can process raw, high-dimensional data such as images, text, and audio directly, often using specialized architectures like Convolutional Neural Networks (CNNs) for images or Recurrent Neural Networks (RNNs) for sequences.

Why deep learning took off: Advancements in the field

  • ReLU Activation Function:
    • Solves the vanishing gradient problem, enabling the training of much deeper networks.
  • Availability of More Data:
    • Deep learning benefits from large datasets which have become accessible due to the growth of the internet, digital media, and data collection tools.
  • Increased Computational Power:
    • GPUs and specialized hardware (e.g., TPUs) allow faster training of deep models.
    • What once took days or weeks can now be trained in hours or days.
  • Algorithmic Innovations:
    • Advancements such as batch normalization, dropout, and better weight initialization have improved the stability and efficiency of training.
  • Plateau of Conventional Machine Learning Algorithms:
    • Traditional algorithms often fail to improve after a certain data or model complexity threshold.
    • Deep learning continues to scale with data size, improving performance as more data becomes available.

In short:

The success of deep learning is due to advancements in the field, the availability of large datasets, and powerful computation.

CNNs (supervised tasks) vs. Traditional Neural Networks:

  • CNNs are considered deep neural networks. They are specialized architectures designed for tasks involving grid-like data, such as images and videos. Unlike traditional fully connected networks, CNNs utilize convolutions to detect spatial hierarchies in data. This enables them to efficiently capture patterns and relationships, making them better suited for image and visual data processing. Key Features of CNNs:
    • Input as Images: CNNs directly take images as input, which allows them to process raw pixel data without extensive manual feature engineering.
    • Efficiency in Training: By leveraging properties such as local receptive fields, parameter sharing, and spatial hierarchies, CNNs make the training process computationally efficient compared to fully connected networks.
    • Applications: CNNs excel at solving problems in image recognition, object detection, segmentation, and other computer vision tasks.
CNN Architecture.png

Convolutional Layers and ReLU

  • Convolutional Layers apply a filter (kernel) over the input data to extract features such as edges, textures, or patterns.
  • After the convolution operation, the resulting feature map undergoes a non-linear activation function, commonly ReLU (Rectified Linear Unit).

Pooling Layers

Pooling layers are used to down-sample the feature map, reducing its size while retaining important features.

Why Use Pooling?

  • Reduces dimensionality, decreasing computation and preventing overfitting.
  • Keeps the most significant information from the feature map.
  • Increases the spatial invariance of the network (i.e., the ability to recognize patterns regardless of location).

Keras Code

One Set of Convolutional and Pooling Layers:

model = Sequential()

model.add(Input(shape=(28, 28, 1)))

model.add(Conv2D(16, (5, 5), strides=(1, 1), activation='relu'))

model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

   

model.add(Flatten())

model.add(Dense(100, activation='relu'))

model.add(Dense(num_classes, activation='softmax'))

   

    # compile model

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


Two Sets of Convolutional and Pooling Layers:

model = Sequential()

model.add(Input(shape=(28, 28, 1)))

model.add(Conv2D(16, (5, 5), activation='relu'))

model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

   

model.add(Conv2D(8, (2, 2), activation='relu'))

model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

   

model.add(Flatten())

model.add(Dense(100, activation='relu'))

model.add(Dense(num_classes, activation='softmax'))   

# Compile model

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

Recurrent Neural Networks (supervised tasks)

  • Recurrent Neural Networks (RNNs) are a class of neural networks specifically designed to handle sequential data. They have loops that allow information to persist, meaning they don't just take the current input at each time step but also consider the output (or hidden state) from the previous time step. This enables them to process data where the order and context of inputs matter, such as time series, text, or audio.

How RNNs Work:

  1. RNN's.png
    Sequential Data Processing:
    • At each time step t, the network takes:
      • The current input xt​,
      • A bias bt​,
      • And the hidden state (output) from the previous time step, at−1​.
    • These are combined to compute the current hidden state at​.
  2. Recurrent Connection:
    • The feedback loop, as shown in the diagram, allows information to persist across time steps. This is the "memory" mechanism of RNNs.
    • The connection between at−1​ (previous state) and zt​ (current computation) captures temporal dependencies in sequential data.
  3. Activation Function:
    • The raw output zt​ passes through a non-linear activation function f (commonly tanh or ReLU) to compute at​, the current output or hidden state.

Long Short-Term Memory (LSTM) Model

Popular RNN variant (LSTM for short)

LSTMs are a type of Recurrent Neural Network (RNN) that solve the vanishing gradient problem, allowing them to capture long-term dependencies in sequential data. They achieve this through a memory cell and a system of gates that regulate the flow of information.


Applications Include:

  1. Image Generation:
    • LSTMs generate images pixel by pixel or feature by feature.
    • Often combined with Convolutional Neural Networks (CNNs) for spatial feature extraction.
  2. Handwriting Generation:
    • Learn sequences of strokes and predict future strokes to generate realistic handwriting.
    • Can mimic human writing styles given text input.
  3. Automatic Captioning for Images and Videos:
    • Combines CNNs (for extracting visual features) with LSTMs (for generating captions).
    • Example: "A group of people playing soccer in a field."

Key Features of LSTMs

  1. Memory Cell:
    • Stores information for long durations, addressing the short-term memory limitations of standard RNNs.
  2. Gating Mechanism:
    • Forget Gate: Decides which parts of the memory to discard.
    • Input Gate: Determines what new information to add to the memory.
    • Output Gate: Controls what part of the memory is output at the current step.
  3. Handles Long-Term Dependencies:
    • LSTMs can maintain context over hundreds of time steps, making them suitable for tasks like text generation, speech synthesis, and video processing.
  4. Flexible Sequence Modeling:
    • Suitable for variable-length input sequences, such as sentences, time-series data, or video frames.

Additional Notes

  • Variants:
    • GRUs (Gated Recurrent Units): A simplified version of LSTMs with fewer parameters, often faster to train but sometimes less effective for longer dependencies.
    • Bi-directional LSTMs: Process sequences in both forward and backward directions, capturing past and future context simultaneously.
  • Use Cases Beyond the Above:
    • Speech Recognition: Transcribe spoken words into text.
    • Music Generation: Compose music by predicting the sequence of notes.
    • Anomaly Detection: Detect unusual patterns in time-series data, like network intrusion or equipment failure.
  • Challenges:
    • LSTMs are computationally more expensive compared to simpler RNNs.
    • They require careful hyperparameter tuning (e.g., learning rate, number of layers, units per layer).

Autoencoders (unsupervised learning)

Autoencoder.png

Autoencoding is a type of data compression algorithm where the compression (encoding) and decompression (decoding) functions are learned automatically from the data, typically using neural networks.

Key Features of Autoencoders

  1. Unsupervised Learning:
    • Autoencoders are unsupervised neural network models.
    • They use backpropagation, but the input itself acts as the output label during training.
  2. Data-Specific:
    • Autoencoders are specialized for the type of data they are trained on and may not generalize well to data that is significantly different.
  3. Nonlinear Transformations:
    • Autoencoders can learn nonlinear representations, which makes them more powerful than techniques like Principal Component Analysis (PCA) that can only handle linear transformations.
  4. Applications:
    • Data Denoising: Removing noise from images, audio, or other data.
    • Dimensionality Reduction: Reducing the number of features in data while preserving key information, often used for visualization or preprocessing.

Types of Autoencoders

  • Standard Autoencoders:
    • Composed of an encoder-decoder structure with a bottleneck layer for compressed representation.
  • Denoising Autoencoders:
    • Designed to reconstruct clean data from noisy inputs.
  • Variational Autoencoders (VAEs):
    • A probabilistic extension of autoencoders that can generate new data points similar to the training data.
  • Sparse Autoencoders:
    • Include a sparsity constraint to learn compressed representations efficiently.

Clarifications on Specific Points

  1. Restricted Boltzmann Machines (RBMs):
    • While RBMs are related to autoencoders, they are not the same.
    • RBMs are generative models used for pretraining deep networks, and they are not autoencoders.
    • A more closely related concept is Deep Belief Networks (DBNs), which combine stacked RBMs.
  2. Fixing Imbalanced Datasets:
    • While autoencoders can be used to oversample minority classes by learning the structure of the minority class and generating synthetic samples, this is not their primary application.

Applications of Autoencoders

  1. Data Denoising:
    • Remove noise from images, audio, or other forms of data.
  2. Dimensionality Reduction:
    • Learn compressed representations for data visualization or feature extraction.
  3. Estimating Missing Values:
    • Reconstruct missing data by leveraging patterns in the dataset.
  4. Automatic Feature Extraction:
    • Extract meaningful features from unstructured data like text, images, or time-series data.
  5. Anomaly Detection:
    • Learn normal patterns in the data and identify deviations, useful for fraud detection, network intrusion, or manufacturing defect identification.
  6. Data Generation:
    • Variational autoencoders can generate new, similar data points, such as synthetic images.

Keras Advanced Features

  • Sequential API: Used for simple, linear stacks of layers.
  • Functional API: Designed for more flexibility and control, enabling the creation of intricate models such as:
    • Models with multiple inputs and outputs.
    • Shared layers that can be reused.
    • Models with non-sequential data flows, like multi-branch or residual networks.

Advantages of the Functional API:

  1. Flexibility: Enables construction of complex architectures like multi-branch and hierarchical models.
  2. Clarity: Provides an explicit and clear representation of the model structure.
  3. Reusability: Layers or models can be reused across different parts of the architecture.

Real-World Applications:

  1. Healthcare:
    • Medical image analysis for disease detection (e.g., pneumonia detection from chest X-rays).
  2. Finance:
    • Predicting market trends using time-series data.
  3. Autonomous Driving:
    • Object detection for pedestrians or vehicles.
    • Lane detection for road safety systems.
Keras Functional API and Subclassing API

from tensorflow.keras.models import Model

from tensorflow.keras.layers import Input, Dense

  1. Define the input

inputs = Input(shape=(784,))

  1. Define the layers

x = Dense(64, activation='relu')(inputs)

outputs = Dense(10, activation='softmax')(x)

  1. Create the model

model = Model(inputs=inputs, outputs=outputs)

model.summary()

Handling multiple inputs and outputs

from tensorflow.keras.models import Model

from tensorflow.keras.layers import Input, Dense, concatenate

# Define two sets of inputs

inputA = Input(shape=(64,))

inputB = Input(shape=(128,))

# The first branch operates on the first input

x = Dense(8, activation='relu')(inputA)

x = Dense(4, activation='relu')(x)

# The second branch operates on the second input

y = Dense(16, activation='relu')(inputB)

y = Dense(4, activation='relu')(y)

# Combine the outputs of the two branches

combined = concatenate([x, y])

# Add fully connected (FC) layers and a regression output

z = Dense(2, activation='relu')(combined)

z = Dense(1, activation='linear')(z)

# Create the model

model = Model(inputs=[inputA, inputB], outputs=z)

model.summary()

Shared layers and complex architectures

from tensorflow.keras.models import Model

from tensorflow.keras.layers import Input, Dense, Lambda

# Define the input layer

input = Input(shape=(28, 28, 1))

# Define a shared convolutional base

conv_base = Dense(64, activation='relu')

# Process the input through the shared layer

processed_1 = conv_base(input)

processed_2 = conv_base(input)

# Create a model using the shared layer

model = Model(inputs=input, outputs=[processed_1,processed_2])

model.summary()

Practical example: Implementing a complex model

from tensorflow.keras.models import Model

from tensorflow.keras.layers import Input, Dense, Lambda, Conv2D, MaxPooling2D, Flatten

from tensorflow.keras.activations import relu, linear

# First input model

inputA = Input(shape=(32, 32, 1))

x = Conv2D(32, (3, 3), activation=relu)(inputA)

x = MaxPooling2D((2, 2))(x)

x = Flatten()(x)

x = Model(inputs=inputA, outputs=x)

# Second input model

inputB = Input(shape=(32, 32, 1))

y = Conv2D(32, (3, 3), activation=relu)(inputB)

y = MaxPooling2D((2, 2))(y)

y = Flatten()(y)

y = Model(inputs=inputB, outputs=y)

# Combine the outputs of the two branches

combined = concatenate([x.output, y.output])

# Add fully connected (FC) layers and a regression output

z = Dense(64, activation='relu')(combined)

z = Dense(1, activation='linear')(z)

# Create the model

model = Model(inputs=[x.input, y.input], outputs=z)

model.summary()

Keras subclassing API
  • Offers flexibility
  • Defines custom and dynamic models
  • Implements subclassing of the model class and call method
  • Used in research and development for sturom training loops and non standard architectures.

import tensorflow as tf

# Define your model by subclassing

class MyModel(tf.keras.Model):

def __init__(self):

super(MyModel, self).__init__()

# Define layers

self.dense1 = tf.keras.layers.Dense(64, activation='relu')

self.dense2 = tf.keras.layers.Dense(10, activation='softmax')

def call(self, inputs):

# Forward pass

x = self.dense1(inputs)

y = self.dense2(x)

return y

# Instantiate the model

model = MyModel()

model.summary()

# Define loss function

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy()

optimizer = tf.keras.optimizers.Adam()

# Compile the model

model.compile(optimizer=optimizer, loss=loss_fn, metrics=['accuracy'])

Use Cases for the Keras Subclassing API:

  1. Models with Dynamic Architecture:
    • The Subclassing API is ideal for creating models with architectures that vary dynamically based on the input or intermediate computations.
    • Example: Recurrent neural networks (RNNs) with variable sequence lengths or architectures that depend on runtime conditions.
  2. Custom Training Loops:
    • The Subclassing API offers fine-grained control over the training process, allowing you to implement custom training loops using GradientTape.
    • Example: Adversarial training (e.g., GANs) or reinforcement learning models where standard .fit() may not suffice.
  3. Research and Prototyping:
    • It is particularly suited for rapid experimentation in research scenarios, enabling you to define and test novel model architectures.
    • Example: Exploring new layer types, loss functions, or combining multiple types of inputs and outputs.
  4. Dynamic Graphs:
    • The Subclassing API allows you to implement eager execution with dynamic computation graphs, offering flexibility to perform different operations at each step of the forward pass.
    • Example: Models that use conditional logic or operations like if/else during the forward pass, such as graph neural networks or decision-tree-inspired neural networks.


Dropout Layers

Dropout is a regularization technique that randomly zeroes out a fraction of neuron outputs during each training step. This reduces the network’s reliance on specific neurons and fosters more robust feature learning—helping mitigate overfitting.

Training vs. Inference

  • Dropout is active only during training, where it stochastically drops neurons.
  • At inference (testing/prediction), dropout is effectively disabled (or rescaled to preserve expected outputs).

Dropout Rate (Hyperparameter)

  • Determines the probability of zeroing out each neuron’s output.
  • Common values range from 0.2 to 0.5, but it’s dataset and architecture dependent.

TensorFlow Keras Example

from tensorflow.keras.layers import Dropout

# Apply 50% dropout

dropout_layer = Dropout(rate=0.5)(hidden_layer)

Overall, dropout promotes generalization by preventing co-adaptation of neurons. It is an easy-to-use, widely adopted method for combating overfitting in deep networks.

Summary

  • Dropout randomly “turns off” neurons during training.
  • It is disabled at inference, ensuring consistent outputs.
  • The dropout rate sets the fraction of neurons dropped (typical ranges: 0.2–0.5).
  • TensorFlow (and other frameworks) make it straightforward to implement.

Batch Normalization

Batch Normalization (BatchNorm) stabilizes and accelerates neural network training by normalizing activations across the current mini-batch. This reduces internal covariate shift—i.e., shifts in input distributions over time—and often enables using larger learning rates for faster convergence.

  • Operation
    • Normalizes each channel to have near-zero mean and unit variance (based on the current mini-batch statistics).
    • During training, BatchNorm uses batch statistics; during inference, it uses running averages of mean and variance.
  • Learnable Scale & Shift
    • Each BatchNorm layer introduces two trainable parameters: γ (scale) and β (shift). These parameters allow the model to adjust or “undo” normalization if needed, preserving representational power.
  • TensorFlow Keras Example

from tensorflow.keras.layers import BatchNormalization

batch_norm_layer = BatchNormalization()(hidden_layer)


Overall, BatchNorm often yields faster training, improved stability, and can help networks converge in fewer epochs.

  • Summary
    • BatchNorm normalizes activations, reducing internal covariate shift.
    • It’s used both in training (via batch statistics) and inference (via running averages).
    • Introduces two learnable parameters for scaling and shifting post-normalization.
    • Can allow higher learning rates and faster convergence.

Keras Custom Layers

Custom layers allow developers to extend the functionality of deep learning frameworks and tailor models to specific needs:

  • Novel Research Ideas
    • Integrate new algorithms or experimental methods.
    • Rapidly prototype and evaluate cutting-edge techniques.
  • Performance Optimization
    • Fine-tune execution for specialized hardware or data structures.
    • Reduce memory or computational overhead by customizing layer operations.
  • Flexibility
    • Define unique behaviors not covered by standard library layers.
    • Experiment with unconventional architectures or parameterization.
  • Reusability & Maintenance
    • Bundle functionality into modular, readable components.
    • Simplify debugging and collaboration by centralizing custom code.
Basics of creating a custom layers

import tensorflow as tf

from tensorflow.keras.layers import Layer

class MyCustomLayer(Layer):

    def __init__(self, units=32, **kwargs):

        super(MyCustomLayer, self).__init__(**kwargs)

        self.units = units

    def build(self, input_shape):

        # Define trainable weights (e.g., kernel and bias)

        self.w = self.add_weight(

            shape=(input_shape[-1], self.units),

            initializer='random_normal',

            trainable=True

        )

        self.b = self.add_weight(

            shape=(self.units,),

            initializer='zeros',

            trainable=True

        )

    def call(self, inputs):

        # Forward pass using the custom weight parameters

        return tf.matmul(inputs, self.w) + self.b

print(tf.executing_eagerly()) # Should return True in TF 2.x

a = tf.constant([1, 2, 3])

b = tf.constant([4, 5, 6])

result = tf.add(a, b)

print(result)

# Outputs: tf.Tensor([5 7 9], shape=(3,), dtype=int32)

Custom dense Layer

class CustomDenseLayer(Layer):

    def __init__(self, units=32):

        super(CustomDenseLayer, self).__init__()

        self.units = units

    def build(self, input_shape):

        # Define trainable weights (e.g., kernel and bias)

        self.w = self.add_weight(

            shape=(input_shape[-1], self.units),

            initializer='random_normal',

            trainable=True

        )

        self.b = self.add_weight(

            shape=(self.units,),

            initializer='zeros',

            trainable=True

        )

    def call(self, inputs):

        # Forward pass using the custom weight parameters

        return tf.nn.relu(tf.matmul(inputs, self.w) + self.b)

import tensorflow.keras.model import Sequential

model = Sequential([

CustomDenseLayer(64),

CustomDenseLayer(10)

])

model.compile(optimizer='adam', loss='categorical_crossentropy')

TensorFlow 2.x

  • Eager Execution
    • Executes operations immediately rather than building a static graph.

Makes TensorFlow more intuitive and Pythonic, enabling straightforward/improved debugging.

  • Immediate Feedback
    • Facilitates interactive programming
    • Simplified Code
  • High-Level APIs
    • Simplifies model building via Keras, offering an approachable, layer-based API.
      • User Friendly
      • Modular and composable
      • Extensive documentation
    • Integrates seamlessly with eager execution to facilitate rapid experimentation.
  • Cross-Platform Support
    • Compatible with various hardware backends (e.g., CPU, GPU, TPU).
    • Embedded devices. (TensorFlow Lite)
    • Web (TensorFlow.js)
    • Production ML Pipelines (TensorFlow Extended (TFX))
  • Scalability & Performance
    • Optimizations under the hood handle large-scale training.
    • Suited to both research prototyping and production systems.
  • Rich Ecosystem
    • Extends TensorFlow with additional libraries (e.g., TensorFlow Extended, TensorFlow Lite, TensorFlow.js).
    • Offers a broad set of tools for data processing, visualization, and distributed training.
    • TensorFlow Hub
      • Repository for reusable machine learning modules.
    • TensorBoard
      • Visualization toolkit for TensorFlow

Convolutional Neural Networks (CNNs)

Purpose: CNNs analyze visual data by leveraging patterns in the spatial structure of images, mimicking the hierarchical structure of the human visual system.

Key Components of CNNs:

  1. Convolutional Layers
    • Perform feature extraction.
    • Learn spatial hierarchies of features through kernels (filters) applied to the input data.
    • Generate feature maps by convolving the input with a set of filters.
  2. Pooling Layers
    • Perform downsampling to reduce the spatial dimensions of feature maps.
    • Types:
      • Max Pooling: Retains the maximum value in a pooling window.
      • Average Pooling: Retains the average value in a pooling window.
    • Reduces computational complexity and prevents overfitting by acting as a form of regularization.
  3. Fully Connected (FC) Layers
    • Flatten the high-level features from convolutional and pooling layers into a single vector.
    • Use the vector for classification or regression tasks by passing it through dense layers and applying an activation function like softmax or sigmoid.

Workflow of CNNs:

  1. Input Image
    • Raw pixel data of the image is input into the network.
  2. Convolutional Layers
    • Extract low-level features like edges, corners, and textures. With deeper layers, higher-level features like shapes and objects are learned.
  3. Activation Function
    • Non-linear activation functions (e.g., ReLU) are applied after convolution to introduce non-linearity and enable the network to learn complex patterns.
  4. Pooling Layers
    • Reduce dimensionality while retaining key features. This decreases computational costs and improves model generalization.
  5. Fully Connected Layers
    • Use learned features to make final predictions. Typically followed by a softmax or sigmoid activation for classification.

Expanded CNN Architecture Example:

  • Input -> Convolution -> ReLU -> Pooling -> Convolution -> ReLU -> Pooling -> Fully Connected -> Output.

Additional Notes for Understanding:

  1. Padding
    • Used to preserve spatial dimensions after convolution. Common padding types are:
      • Valid Padding: No padding; reduces output size.
      • Same Padding: Pads input so that output dimensions match the input dimensions.
  2. Stride
    • The number of pixels by which the filter moves during convolution. Larger strides reduce the spatial size of the output.
  3. Regularization Techniques
    • Dropout: Randomly sets a fraction of input units to zero during training to prevent overfitting.
    • Batch Normalization: Normalizes intermediate activations to improve stability and training speed.
  4. Data Augmentation
    • Techniques like flipping, rotating, cropping, and scaling are used to increase the diversity of the training data and improve generalization.
  5. Applications of CNNs:
    • Image classification.
    • Object detection.
    • Semantic segmentation.
    • Medical imaging.
    • Style transfer.
  6. Popular Architectures:
    • LeNet: Early architecture for handwritten digit recognition.
    • AlexNet: Introduced deeper networks with ReLU and dropout.
    • VGGNet: Deep but simple networks with small filters.
    • ResNet: Introduced residual connections to combat vanishing gradients.
    • Inception: Used modules to increase depth and width efficiently.


Code Example: Basic CNN:


from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

# Create a Sequential model

model = Sequential([

    Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),  # First Conv2D layer

    MaxPooling2D((2, 2)),  # First MaxPooling layer

    Conv2D(64, (3, 3), activation='relu'),  # Second Conv2D layer

    MaxPooling2D((2, 2)),  # Second MaxPooling layer

    Flatten(),  # Flatten the feature maps

    Dense(128, activation='relu'),  # Fully connected dense layer

    Dense(10, activation='softmax')  # Output layer for 10 classes

])

# Compile the model

model.compile(optimizer='adam',  # Use Adam optimizer

              loss='categorical_crossentropy',  # Loss function for multi-class classification

              metrics=['accuracy'])  # Metrics to monitor during training

# Print a summary of the model

model.summary()


  • Model Explanation:
    • Conv2D(32, (3, 3)): Adds a convolutional layer with 32 filters of size (3x3).
    • MaxPooling2D((2, 2)): Reduces spatial dimensions by taking the max value in a (2x2) window.
    • Flatten(): Flattens the 2D feature maps into a 1D vector.
    • Dense(128, activation='relu'): Fully connected layer with 128 units and ReLU activation.
    • Dense(10, activation='softmax'): Output layer for classification into 10 classes using softmax activation.
  • Input Shape:
    • input_shape=(64, 64, 3) assumes input images are 64x64 pixels with 3 color channels (RGB).

Advanced CNN Architectures

1. VGG (Visual Geometry Group Networks)

  • Key Idea: Uniform small 3×3 convolutional filters.
    • Max-Pooling Layers
    • Fully Connected Layers
  • Structure: Deep networks (e.g., VGG-16: 13 convolutional + 3 dense layers).
  • Pros: Simplicity and depth for feature extraction.
  • Cons: High computational cost.

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

# Create a Sequential model

model = Sequential([

    Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),  # First Conv2D layer

    Conv2D(32, (3, 3), activation='relu'),  # Second Conv2D layer

    MaxPooling2D((2, 2)),  # First MaxPooling layer

    Conv2D(128, (3, 3), activation='relu'),  # Third Conv2D layer

    Conv2D(128, (3, 3), activation='relu'),  # Fourth Conv2D layer

    MaxPooling2D((2, 2)),  # Second MaxPooling layer

    Conv2D(256, (3, 3), activation='relu'),  # Fifth Conv2D layer

    Conv2D(256, (3, 3), activation='relu'),  # Sixth Conv2D layer

    MaxPooling2D((2, 2)),  # Third MaxPooling layer

    Flatten(),  # Flatten the feature maps

    Dense(512, activation='relu'),  # First Fully connected dense layer

    Dense(512, activation='relu'),  # Second Fully connected dense layer

    Dense(10, activation='softmax')  # Output layer for 10 classes

])

# Compile the model

model.compile(optimizer='adam',  # Use Adam optimizer

              loss='categorical_crossentropy',  # Loss function for multi-class classification

              metrics=['accuracy'])  # Metrics to monitor during training

# Print a summary of the model

model.summary()


Explanation of Key Components:

  1. Conv2D Layers:
    • 32,128,256 filters represent increasing feature extraction depth.
    • 3×3 kernels are used consistently for feature extraction.
  2. MaxPooling2D Layers:
    • Downsample the feature maps to reduce spatial dimensions and computational costs.
  3. Flatten Layer:
    • Converts the 3D feature maps into a 1D vector for dense layer input.
  4. Dense Layers:
    • Two fully connected layers with 512 neurons each, followed by a softmax layer for 10-class classification.
  5. Compilation:
    • Adam optimizer: Adaptive learning rate optimization for faster convergence.
    • Categorical Crossentropy: Suitable for multi-class classification problems.

2. ResNet (Residual Networks)

  • Key Idea: Residual connections (skip connections) to combat vanishing gradients.
  • Structure: Residual block: Output=F(Input)+Input.
  • Pros: Enables very deep networks (e.g., ResNet-50, ResNet-101).
  • Cons: Increased complexity.

from tensorflow.keras.layers import Input, Conv2D, BatchNormalization, Activation, Add, MaxPooling2D, Flatten, Dense, GlobalAveragePooling2D

from tensorflow.keras.models import Model

# Residual Block

def residual_block(x, filters, kernel_size=3, stride=1):

shortcut = x

    x = Conv2D(filters, kernel_size, strides=stride, padding='same')(x)

    x = BatchNormalization()(x)

    x = Activation('relu')(x)

    x = Conv2D(filters, kernel_size, strides=1, padding='same')(x)

    x = BatchNormalization()(x)

    x = Add()([x, shortcut]) 

    x = Activation('relu')(x)

    return x

# ResNet-like Model

def resnet_like(input_shape=(64, 64, 3), num_classes=10):

    inputs = Input(shape=input_shape)

    # Initial Conv Layer

    x = Conv2D(64, (7, 7), strides=2, padding='same')(inputs)

    x = BatchNormalization()(x)

    x = Activation('relu')(x)

    x = MaxPooling2D((3, 3), strides=2, padding='same')(x)

    # Residual Blocks

    x = residual_block(x, 64)

    x = residual_block(x, 64)

    x = residual_block(x, 128, stride=2)

    x = residual_block(x, 128)

    x = residual_block(x, 256, stride=2)

    x = residual_block(x, 256)

    # Global Average Pooling and Output

    x = GlobalAveragePooling2D()(x)

    x = Dense(num_classes, activation='softmax')(x)

    # Create Model

    model = Model(inputs, x)

    return model

# Create and compile the ResNet-like model

model = resnet_like()

model.compile(optimizer='adam',

              loss='categorical_crossentropy',

              metrics=['accuracy'])

# Print the model summary

model.summary()


3. Inception Networks

  • Key Idea: Multi-scale feature extraction with filters of different sizes (e.g., 1×1, 3×3, 5×5).
  • Structure: Inception modules use 1×1 bottleneck layers to reduce dimensionality.
  • Pros: Efficient and versatile.
  • Cons: Complex architecture design.

Data Augmentation Techniques

Purpose:

  • Enhance the robustness and generalization of models.
  • Artificially expand the training dataset by introducing variations.
  • Reduce overfitting by exposing the model to diverse examples.
  • Improve performance on unseen data by mimicking real-world variations.

Common Data Augmentation Techniques

  1. Rotation:
    • Rotate images by a random degree within a specified range (e.g., ±30°).
    • Helps the model learn invariance to orientation changes.
  2. Translations:
    • Shift the image horizontally or vertically by a certain number of pixels.
    • Useful for objects that might not always be centered in the frame.
  3. Flipping:
    • Horizontal flipping: Mirrors the image along the vertical axis.
    • Vertical flipping (less common): Mirrors the image along the horizontal axis.
    • Effective for symmetrical objects or scenes.
  4. Scaling:
    • Resize the image by a random factor, either zooming in or out.
    • Helps the model learn invariance to object sizes.
  5. Adding Noise:
    • Inject random noise (e.g., Gaussian noise, salt-and-pepper noise) to simulate variations in image quality.
    • Helps the model become robust to noisy or low-quality inputs.

Other Useful Techniques:

  1. Shearing:
    • Distort the image by slanting it along an axis.
    • Simulates different perspectives or viewing angles.
  2. Cropping:
    • Randomly crop parts of the image to simulate zoomed-in views or partial occlusions.
  3. Brightness, Contrast, and Color Adjustments:
    • Alter the brightness, contrast, saturation, or hue of the image.
    • Makes the model robust to varying lighting conditions.
  4. Random Erasing:
    • Randomly mask out parts of the image.
    • Forces the model to rely on other parts of the image for prediction.
  5. CutMix and MixUp:
    • CutMix: Combines parts of two images and mixes their labels.
    • MixUp: Mixes two images and their labels linearly.

from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Create an ImageDataGenerator instance with augmentation techniques

datagen = ImageDataGenerator(

    rotation_range=30,       # Rotate images by up to 30 degrees

    width_shift_range=0.2,   # Translate images horizontally by 20% of width

    height_shift_range=0.2,  # Translate images vertically by 20% of height

    horizontal_flip=True,    # Randomly flip images horizontally

    zoom_range=0.2,          # Zoom in/out by up to 20%

    brightness_range=[0.8, 1.2],  # Adjust brightness (80% to 120%)

    fill_mode='nearest'      # Fill missing pixels after transformations

)

# Example: Applying augmentations to a single image

from tensorflow.keras.preprocessing.image import img_to_array, load_img

image = load_img('example.jpg', target_size=(64, 64))  # Load image

image_array = img_to_array(image)  # Convert to array

image_array = image_array.reshape((1,) + image_array.shape)  # Add batch dimension

# Generate augmented images

i = 0

for batch in datagen.flow(image_array, batch_size=1):

plt.figure(i)

imgplot = plt.imshow(image.array to img(batch[0]))


Feature-Wise Normalization

  • Definition: Normalize the entire dataset feature-by-feature, based on the dataset's mean and standard deviation.
  • Use Case: Ensures all features have similar ranges, which helps models converge faster during training.
  • How It Works:
    • Compute the mean and standard deviation for each feature (e.g., pixel intensity) across all images in the dataset.
    • Subtract the mean and divide by the standard deviation for each feature.

datagen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True)

# Fit the generator on the dataset to compute statistics

datagen.fit(training_data)  # training_data is a NumPy array of images


Sample-Wise Normalization

  • Definition: Normalize each individual sample (image) independently by subtracting the sample's mean and dividing by its standard deviation.
  • Use Case: Useful when you want to normalize each image separately, especially if the dataset contains images with different intensity distributions.
  • How It Works:
    • For each image, compute its mean and standard deviation.
    • Normalize the image by subtracting its mean and dividing by its standard deviation.

datagen = ImageDataGenerator(samplewise_center=True, samplewise_std_normalization=True)

# Use the generator as part of the training process

datagen.flow(training_data, training_labels)


ImageDataGenerator.fit(training_images)

i = 0

for batch in ImageDataGenerator.flow(training_images, batch_size=32):

plt.figure(i)

imgplot = plt.imshow(image.array_ti_image(batch[0]))

i += 1

if i % 4 == 0

break

plt.show()


Custom Augmentation Functions

  • Definition: Define your own augmentation logic when the built-in transformations are insufficient or you need specialized augmentations.
  • How It Works:
    • Write a custom function that modifies the input data (e.g., applying specific transformations or injecting custom noise).
    • Pass the function as part of a data preprocessing pipeline.

import numpy as np

# Define a custom function to add random noise

def add_noise(image):

    noise = np.random.normal(loc=0, scale=0.1, size=image.shape)  # Gaussian noise

    return np.clip(image + noise, 0, 1)  # Ensure pixel values remain in [0, 1]

# Create a DataGenerator with a custom preprocessing function

datagen = ImageDataGenerator(preprocessing_function=add_noise)

# Use the generator

i = 0

for batch in datagen.flow(training_images, batch_size=32):

plt.figure(i)

imgplot = plt.imshow(image.array_ti_image(batch[0]))

i += 1

if i % 4 == 0

break

plt.show()