Chapter 7: Feature Engineering for Deep Learning
7.3 Practical Exercises: Chapter 7
In this exercise section, we’ll apply feature engineering techniques within TensorFlow/Keras pipelines, focusing on preparing data, handling preprocessing layers, and constructing end-to-end workflows with tf.data
. These exercises are designed to reinforce the concepts of embedding preprocessing steps into the model pipeline.
Exercise 1: Normalizing and Encoding Data Using Keras Preprocessing Layers
Objective: Use Keras preprocessing layers to normalize numeric data and one-hot encode categorical data within a model pipeline.
Instructions:
- Create a small dataset with two numeric and one categorical feature.
- Apply the
Normalization
layer for the numeric data and theStringLookup
withCategoryEncoding
layer for the categorical data. - Build a simple model that integrates these preprocessing steps.
Solution:
import tensorflow as tf
from tensorflow.keras.layers import Normalization, StringLookup, CategoryEncoding, Input, Dense, concatenate
from tensorflow.keras.models import Model
import numpy as np
# Sample data
numeric_data = np.array([[25.0, 50000.0], [30.0, 60000.0], [35.0, 70000.0], [40.0, 80000.0]])
categorical_data = np.array(['A', 'B', 'A', 'C'])
# Define preprocessing layers
normalizer = Normalization()
normalizer.adapt(numeric_data)
string_lookup = StringLookup(vocabulary=['A', 'B', 'C'])
category_encoder = CategoryEncoding(num_tokens=string_lookup.vocabulary_size())
# Define model inputs
numeric_input = Input(shape=(2,), name="numeric_input")
categorical_input = Input(shape=(1,), dtype="string", name="categorical_input")
# Apply preprocessing
normalized_numeric = normalizer(numeric_input)
encoded_category = category_encoder(string_lookup(categorical_input))
# Combine features and build a simple model
combined_features = concatenate([normalized_numeric, encoded_category])
output = Dense(1, activation="sigmoid")(combined_features)
model = Model(inputs=[numeric_input, categorical_input], outputs=output)
# Display model summary
model.summary()
In this exercise:
- We use the
Normalization
layer to standardize the numeric data andStringLookup
followed byCategoryEncoding
for one-hot encoding categorical data. - This integrated pipeline allows the model to process raw data, handling all preprocessing within the model.
Exercise 2: Building an Image Data Augmentation Layer with Keras
Objective: Use Keras image data augmentation layers to preprocess image data directly in the model.
Instructions:
- Load a set of sample images.
- Apply data augmentation layers, including rotation, flipping, and zooming, to create variations of the input images.
- Build a simple CNN model that includes these augmentation layers.
Solution:
from tensorflow.keras.layers import RandomFlip, RandomRotation, RandomZoom, Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
# Define data augmentation layers
data_augmentation = Sequential([
RandomFlip("horizontal"),
RandomRotation(0.2),
RandomZoom(0.1),
])
# Build a CNN model with data augmentation
model = Sequential([
Input(shape=(224, 224, 3)),
data_augmentation,
Conv2D(32, (3, 3), activation="relu"),
MaxPooling2D(),
Conv2D(64, (3, 3), activation="relu"),
MaxPooling2D(),
Flatten(),
Dense(128, activation="relu"),
Dense(10, activation="softmax") # Assuming 10 classes
])
# Display model summary
model.summary()
In this exercise:
- The data augmentation layer is integrated as part of the model, applying random transformations to images during training.
- The CNN model can generalize better to new images, as it’s trained on augmented data, making it more robust.
Exercise 3: Constructing a tf.data
Pipeline for Mixed Data
Objective: Build a tf.data
pipeline that preprocesses both image and structured data, integrating normalization for numeric features and resizing for images.
Instructions:
- Load sample image paths, numeric data, and categorical data.
- Create preprocessing functions for images (resizing and normalizing) and numeric data.
- Combine them into a single
tf.data
pipeline and verify the output.
Solution:
import tensorflow as tf
import numpy as np
# Sample image paths, numeric, and categorical data
image_paths = ["path/to/image1.jpg", "path/to/image2.jpg"]
numeric_data = np.array([[25.0, 50000.0], [30.0, 60000.0]])
categorical_data = np.array(["A", "B"])
# Define image processing function
def load_and_preprocess_image(path):
image = tf.io.read_file(path)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, [224, 224])
return image / 255.0
# Define numeric preprocessing
normalizer = Normalization()
normalizer.adapt(numeric_data)
# Define categorical encoding
string_lookup = StringLookup(vocabulary=["A", "B", "C"], output_mode="one_hot")
# Process data function
def process_data(image_path, numeric, category):
image = load_and_preprocess_image(image_path)
numeric = normalizer(numeric)
category = string_lookup(category)
return {"image_input": image, "numeric_input": numeric, "categorical_input": category}
# Combine data into a tf.data.Dataset
dataset = tf.data.Dataset.from_tensor_slices((image_paths, numeric_data, categorical_data))
dataset = dataset.map(process_data).batch(2)
# Verify output
for batch in dataset.take(1):
print(batch)
In this exercise:
- We create a
tf.data
pipeline that preprocesses image paths (loading, resizing, and normalizing) and structured data (normalizing numeric and encoding categorical). - The output format matches model input requirements, making it easier to feed the data directly into a neural network.
Exercise 4: Combining Multiple Inputs with Keras Preprocessing Layers in a Model
Objective: Create a model that integrates multiple input types (images, numeric, and categorical) with feature engineering directly embedded in the model.
Instructions:
- Define separate inputs for images, numeric, and categorical data.
- Use Keras preprocessing layers to normalize and encode inputs.
- Combine the preprocessed inputs in a neural network model.
Solution:
from tensorflow.keras.layers import Input, concatenate, Dense, Flatten
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Model
# Define image input and ResNet model for feature extraction
image_input = Input(shape=(224, 224, 3), name="image_input")
resnet = ResNet50(weights="imagenet", include_top=False, input_tensor=image_input)
flattened_image = Flatten()(resnet.output)
# Define numeric input and apply normalization
numeric_input = Input(shape=(2,), name="numeric_input")
normalized_numeric = normalizer(numeric_input)
# Define categorical input and apply encoding
categorical_input = Input(shape=(1,), dtype="string", name="categorical_input")
encoded_category = string_lookup(categorical_input)
# Concatenate all processed inputs
combined = concatenate([flattened_image, normalized_numeric, encoded_category])
# Final model layers
output = Dense(128, activation="relu")(combined)
output = Dense(10, activation="softmax")(output) # Assuming 10 classes
# Build the model
model = Model(inputs=[image_input, numeric_input, categorical_input], outputs=output)
# Display model summary
model.summary()
In this exercise:
- The model handles multiple input types: an image processed by ResNet, numeric data normalized through
Normalization
, and categorical data encoded withStringLookup
. - All inputs are concatenated, enabling a comprehensive view of the data in the model.
These exercises illustrate practical applications of feature engineering for deep learning, from integrating preprocessing layers to building efficient data pipelines. By embedding these steps in TensorFlow/Keras, data transformations become part of the model, creating an end-to-end pipeline that ensures data consistency and simplifies deployment.
7.3 Practical Exercises: Chapter 7
In this exercise section, we’ll apply feature engineering techniques within TensorFlow/Keras pipelines, focusing on preparing data, handling preprocessing layers, and constructing end-to-end workflows with tf.data
. These exercises are designed to reinforce the concepts of embedding preprocessing steps into the model pipeline.
Exercise 1: Normalizing and Encoding Data Using Keras Preprocessing Layers
Objective: Use Keras preprocessing layers to normalize numeric data and one-hot encode categorical data within a model pipeline.
Instructions:
- Create a small dataset with two numeric and one categorical feature.
- Apply the
Normalization
layer for the numeric data and theStringLookup
withCategoryEncoding
layer for the categorical data. - Build a simple model that integrates these preprocessing steps.
Solution:
import tensorflow as tf
from tensorflow.keras.layers import Normalization, StringLookup, CategoryEncoding, Input, Dense, concatenate
from tensorflow.keras.models import Model
import numpy as np
# Sample data
numeric_data = np.array([[25.0, 50000.0], [30.0, 60000.0], [35.0, 70000.0], [40.0, 80000.0]])
categorical_data = np.array(['A', 'B', 'A', 'C'])
# Define preprocessing layers
normalizer = Normalization()
normalizer.adapt(numeric_data)
string_lookup = StringLookup(vocabulary=['A', 'B', 'C'])
category_encoder = CategoryEncoding(num_tokens=string_lookup.vocabulary_size())
# Define model inputs
numeric_input = Input(shape=(2,), name="numeric_input")
categorical_input = Input(shape=(1,), dtype="string", name="categorical_input")
# Apply preprocessing
normalized_numeric = normalizer(numeric_input)
encoded_category = category_encoder(string_lookup(categorical_input))
# Combine features and build a simple model
combined_features = concatenate([normalized_numeric, encoded_category])
output = Dense(1, activation="sigmoid")(combined_features)
model = Model(inputs=[numeric_input, categorical_input], outputs=output)
# Display model summary
model.summary()
In this exercise:
- We use the
Normalization
layer to standardize the numeric data andStringLookup
followed byCategoryEncoding
for one-hot encoding categorical data. - This integrated pipeline allows the model to process raw data, handling all preprocessing within the model.
Exercise 2: Building an Image Data Augmentation Layer with Keras
Objective: Use Keras image data augmentation layers to preprocess image data directly in the model.
Instructions:
- Load a set of sample images.
- Apply data augmentation layers, including rotation, flipping, and zooming, to create variations of the input images.
- Build a simple CNN model that includes these augmentation layers.
Solution:
from tensorflow.keras.layers import RandomFlip, RandomRotation, RandomZoom, Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
# Define data augmentation layers
data_augmentation = Sequential([
RandomFlip("horizontal"),
RandomRotation(0.2),
RandomZoom(0.1),
])
# Build a CNN model with data augmentation
model = Sequential([
Input(shape=(224, 224, 3)),
data_augmentation,
Conv2D(32, (3, 3), activation="relu"),
MaxPooling2D(),
Conv2D(64, (3, 3), activation="relu"),
MaxPooling2D(),
Flatten(),
Dense(128, activation="relu"),
Dense(10, activation="softmax") # Assuming 10 classes
])
# Display model summary
model.summary()
In this exercise:
- The data augmentation layer is integrated as part of the model, applying random transformations to images during training.
- The CNN model can generalize better to new images, as it’s trained on augmented data, making it more robust.
Exercise 3: Constructing a tf.data
Pipeline for Mixed Data
Objective: Build a tf.data
pipeline that preprocesses both image and structured data, integrating normalization for numeric features and resizing for images.
Instructions:
- Load sample image paths, numeric data, and categorical data.
- Create preprocessing functions for images (resizing and normalizing) and numeric data.
- Combine them into a single
tf.data
pipeline and verify the output.
Solution:
import tensorflow as tf
import numpy as np
# Sample image paths, numeric, and categorical data
image_paths = ["path/to/image1.jpg", "path/to/image2.jpg"]
numeric_data = np.array([[25.0, 50000.0], [30.0, 60000.0]])
categorical_data = np.array(["A", "B"])
# Define image processing function
def load_and_preprocess_image(path):
image = tf.io.read_file(path)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, [224, 224])
return image / 255.0
# Define numeric preprocessing
normalizer = Normalization()
normalizer.adapt(numeric_data)
# Define categorical encoding
string_lookup = StringLookup(vocabulary=["A", "B", "C"], output_mode="one_hot")
# Process data function
def process_data(image_path, numeric, category):
image = load_and_preprocess_image(image_path)
numeric = normalizer(numeric)
category = string_lookup(category)
return {"image_input": image, "numeric_input": numeric, "categorical_input": category}
# Combine data into a tf.data.Dataset
dataset = tf.data.Dataset.from_tensor_slices((image_paths, numeric_data, categorical_data))
dataset = dataset.map(process_data).batch(2)
# Verify output
for batch in dataset.take(1):
print(batch)
In this exercise:
- We create a
tf.data
pipeline that preprocesses image paths (loading, resizing, and normalizing) and structured data (normalizing numeric and encoding categorical). - The output format matches model input requirements, making it easier to feed the data directly into a neural network.
Exercise 4: Combining Multiple Inputs with Keras Preprocessing Layers in a Model
Objective: Create a model that integrates multiple input types (images, numeric, and categorical) with feature engineering directly embedded in the model.
Instructions:
- Define separate inputs for images, numeric, and categorical data.
- Use Keras preprocessing layers to normalize and encode inputs.
- Combine the preprocessed inputs in a neural network model.
Solution:
from tensorflow.keras.layers import Input, concatenate, Dense, Flatten
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Model
# Define image input and ResNet model for feature extraction
image_input = Input(shape=(224, 224, 3), name="image_input")
resnet = ResNet50(weights="imagenet", include_top=False, input_tensor=image_input)
flattened_image = Flatten()(resnet.output)
# Define numeric input and apply normalization
numeric_input = Input(shape=(2,), name="numeric_input")
normalized_numeric = normalizer(numeric_input)
# Define categorical input and apply encoding
categorical_input = Input(shape=(1,), dtype="string", name="categorical_input")
encoded_category = string_lookup(categorical_input)
# Concatenate all processed inputs
combined = concatenate([flattened_image, normalized_numeric, encoded_category])
# Final model layers
output = Dense(128, activation="relu")(combined)
output = Dense(10, activation="softmax")(output) # Assuming 10 classes
# Build the model
model = Model(inputs=[image_input, numeric_input, categorical_input], outputs=output)
# Display model summary
model.summary()
In this exercise:
- The model handles multiple input types: an image processed by ResNet, numeric data normalized through
Normalization
, and categorical data encoded withStringLookup
. - All inputs are concatenated, enabling a comprehensive view of the data in the model.
These exercises illustrate practical applications of feature engineering for deep learning, from integrating preprocessing layers to building efficient data pipelines. By embedding these steps in TensorFlow/Keras, data transformations become part of the model, creating an end-to-end pipeline that ensures data consistency and simplifies deployment.
7.3 Practical Exercises: Chapter 7
In this exercise section, we’ll apply feature engineering techniques within TensorFlow/Keras pipelines, focusing on preparing data, handling preprocessing layers, and constructing end-to-end workflows with tf.data
. These exercises are designed to reinforce the concepts of embedding preprocessing steps into the model pipeline.
Exercise 1: Normalizing and Encoding Data Using Keras Preprocessing Layers
Objective: Use Keras preprocessing layers to normalize numeric data and one-hot encode categorical data within a model pipeline.
Instructions:
- Create a small dataset with two numeric and one categorical feature.
- Apply the
Normalization
layer for the numeric data and theStringLookup
withCategoryEncoding
layer for the categorical data. - Build a simple model that integrates these preprocessing steps.
Solution:
import tensorflow as tf
from tensorflow.keras.layers import Normalization, StringLookup, CategoryEncoding, Input, Dense, concatenate
from tensorflow.keras.models import Model
import numpy as np
# Sample data
numeric_data = np.array([[25.0, 50000.0], [30.0, 60000.0], [35.0, 70000.0], [40.0, 80000.0]])
categorical_data = np.array(['A', 'B', 'A', 'C'])
# Define preprocessing layers
normalizer = Normalization()
normalizer.adapt(numeric_data)
string_lookup = StringLookup(vocabulary=['A', 'B', 'C'])
category_encoder = CategoryEncoding(num_tokens=string_lookup.vocabulary_size())
# Define model inputs
numeric_input = Input(shape=(2,), name="numeric_input")
categorical_input = Input(shape=(1,), dtype="string", name="categorical_input")
# Apply preprocessing
normalized_numeric = normalizer(numeric_input)
encoded_category = category_encoder(string_lookup(categorical_input))
# Combine features and build a simple model
combined_features = concatenate([normalized_numeric, encoded_category])
output = Dense(1, activation="sigmoid")(combined_features)
model = Model(inputs=[numeric_input, categorical_input], outputs=output)
# Display model summary
model.summary()
In this exercise:
- We use the
Normalization
layer to standardize the numeric data andStringLookup
followed byCategoryEncoding
for one-hot encoding categorical data. - This integrated pipeline allows the model to process raw data, handling all preprocessing within the model.
Exercise 2: Building an Image Data Augmentation Layer with Keras
Objective: Use Keras image data augmentation layers to preprocess image data directly in the model.
Instructions:
- Load a set of sample images.
- Apply data augmentation layers, including rotation, flipping, and zooming, to create variations of the input images.
- Build a simple CNN model that includes these augmentation layers.
Solution:
from tensorflow.keras.layers import RandomFlip, RandomRotation, RandomZoom, Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
# Define data augmentation layers
data_augmentation = Sequential([
RandomFlip("horizontal"),
RandomRotation(0.2),
RandomZoom(0.1),
])
# Build a CNN model with data augmentation
model = Sequential([
Input(shape=(224, 224, 3)),
data_augmentation,
Conv2D(32, (3, 3), activation="relu"),
MaxPooling2D(),
Conv2D(64, (3, 3), activation="relu"),
MaxPooling2D(),
Flatten(),
Dense(128, activation="relu"),
Dense(10, activation="softmax") # Assuming 10 classes
])
# Display model summary
model.summary()
In this exercise:
- The data augmentation layer is integrated as part of the model, applying random transformations to images during training.
- The CNN model can generalize better to new images, as it’s trained on augmented data, making it more robust.
Exercise 3: Constructing a tf.data
Pipeline for Mixed Data
Objective: Build a tf.data
pipeline that preprocesses both image and structured data, integrating normalization for numeric features and resizing for images.
Instructions:
- Load sample image paths, numeric data, and categorical data.
- Create preprocessing functions for images (resizing and normalizing) and numeric data.
- Combine them into a single
tf.data
pipeline and verify the output.
Solution:
import tensorflow as tf
import numpy as np
# Sample image paths, numeric, and categorical data
image_paths = ["path/to/image1.jpg", "path/to/image2.jpg"]
numeric_data = np.array([[25.0, 50000.0], [30.0, 60000.0]])
categorical_data = np.array(["A", "B"])
# Define image processing function
def load_and_preprocess_image(path):
image = tf.io.read_file(path)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, [224, 224])
return image / 255.0
# Define numeric preprocessing
normalizer = Normalization()
normalizer.adapt(numeric_data)
# Define categorical encoding
string_lookup = StringLookup(vocabulary=["A", "B", "C"], output_mode="one_hot")
# Process data function
def process_data(image_path, numeric, category):
image = load_and_preprocess_image(image_path)
numeric = normalizer(numeric)
category = string_lookup(category)
return {"image_input": image, "numeric_input": numeric, "categorical_input": category}
# Combine data into a tf.data.Dataset
dataset = tf.data.Dataset.from_tensor_slices((image_paths, numeric_data, categorical_data))
dataset = dataset.map(process_data).batch(2)
# Verify output
for batch in dataset.take(1):
print(batch)
In this exercise:
- We create a
tf.data
pipeline that preprocesses image paths (loading, resizing, and normalizing) and structured data (normalizing numeric and encoding categorical). - The output format matches model input requirements, making it easier to feed the data directly into a neural network.
Exercise 4: Combining Multiple Inputs with Keras Preprocessing Layers in a Model
Objective: Create a model that integrates multiple input types (images, numeric, and categorical) with feature engineering directly embedded in the model.
Instructions:
- Define separate inputs for images, numeric, and categorical data.
- Use Keras preprocessing layers to normalize and encode inputs.
- Combine the preprocessed inputs in a neural network model.
Solution:
from tensorflow.keras.layers import Input, concatenate, Dense, Flatten
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Model
# Define image input and ResNet model for feature extraction
image_input = Input(shape=(224, 224, 3), name="image_input")
resnet = ResNet50(weights="imagenet", include_top=False, input_tensor=image_input)
flattened_image = Flatten()(resnet.output)
# Define numeric input and apply normalization
numeric_input = Input(shape=(2,), name="numeric_input")
normalized_numeric = normalizer(numeric_input)
# Define categorical input and apply encoding
categorical_input = Input(shape=(1,), dtype="string", name="categorical_input")
encoded_category = string_lookup(categorical_input)
# Concatenate all processed inputs
combined = concatenate([flattened_image, normalized_numeric, encoded_category])
# Final model layers
output = Dense(128, activation="relu")(combined)
output = Dense(10, activation="softmax")(output) # Assuming 10 classes
# Build the model
model = Model(inputs=[image_input, numeric_input, categorical_input], outputs=output)
# Display model summary
model.summary()
In this exercise:
- The model handles multiple input types: an image processed by ResNet, numeric data normalized through
Normalization
, and categorical data encoded withStringLookup
. - All inputs are concatenated, enabling a comprehensive view of the data in the model.
These exercises illustrate practical applications of feature engineering for deep learning, from integrating preprocessing layers to building efficient data pipelines. By embedding these steps in TensorFlow/Keras, data transformations become part of the model, creating an end-to-end pipeline that ensures data consistency and simplifies deployment.
7.3 Practical Exercises: Chapter 7
In this exercise section, we’ll apply feature engineering techniques within TensorFlow/Keras pipelines, focusing on preparing data, handling preprocessing layers, and constructing end-to-end workflows with tf.data
. These exercises are designed to reinforce the concepts of embedding preprocessing steps into the model pipeline.
Exercise 1: Normalizing and Encoding Data Using Keras Preprocessing Layers
Objective: Use Keras preprocessing layers to normalize numeric data and one-hot encode categorical data within a model pipeline.
Instructions:
- Create a small dataset with two numeric and one categorical feature.
- Apply the
Normalization
layer for the numeric data and theStringLookup
withCategoryEncoding
layer for the categorical data. - Build a simple model that integrates these preprocessing steps.
Solution:
import tensorflow as tf
from tensorflow.keras.layers import Normalization, StringLookup, CategoryEncoding, Input, Dense, concatenate
from tensorflow.keras.models import Model
import numpy as np
# Sample data
numeric_data = np.array([[25.0, 50000.0], [30.0, 60000.0], [35.0, 70000.0], [40.0, 80000.0]])
categorical_data = np.array(['A', 'B', 'A', 'C'])
# Define preprocessing layers
normalizer = Normalization()
normalizer.adapt(numeric_data)
string_lookup = StringLookup(vocabulary=['A', 'B', 'C'])
category_encoder = CategoryEncoding(num_tokens=string_lookup.vocabulary_size())
# Define model inputs
numeric_input = Input(shape=(2,), name="numeric_input")
categorical_input = Input(shape=(1,), dtype="string", name="categorical_input")
# Apply preprocessing
normalized_numeric = normalizer(numeric_input)
encoded_category = category_encoder(string_lookup(categorical_input))
# Combine features and build a simple model
combined_features = concatenate([normalized_numeric, encoded_category])
output = Dense(1, activation="sigmoid")(combined_features)
model = Model(inputs=[numeric_input, categorical_input], outputs=output)
# Display model summary
model.summary()
In this exercise:
- We use the
Normalization
layer to standardize the numeric data andStringLookup
followed byCategoryEncoding
for one-hot encoding categorical data. - This integrated pipeline allows the model to process raw data, handling all preprocessing within the model.
Exercise 2: Building an Image Data Augmentation Layer with Keras
Objective: Use Keras image data augmentation layers to preprocess image data directly in the model.
Instructions:
- Load a set of sample images.
- Apply data augmentation layers, including rotation, flipping, and zooming, to create variations of the input images.
- Build a simple CNN model that includes these augmentation layers.
Solution:
from tensorflow.keras.layers import RandomFlip, RandomRotation, RandomZoom, Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
# Define data augmentation layers
data_augmentation = Sequential([
RandomFlip("horizontal"),
RandomRotation(0.2),
RandomZoom(0.1),
])
# Build a CNN model with data augmentation
model = Sequential([
Input(shape=(224, 224, 3)),
data_augmentation,
Conv2D(32, (3, 3), activation="relu"),
MaxPooling2D(),
Conv2D(64, (3, 3), activation="relu"),
MaxPooling2D(),
Flatten(),
Dense(128, activation="relu"),
Dense(10, activation="softmax") # Assuming 10 classes
])
# Display model summary
model.summary()
In this exercise:
- The data augmentation layer is integrated as part of the model, applying random transformations to images during training.
- The CNN model can generalize better to new images, as it’s trained on augmented data, making it more robust.
Exercise 3: Constructing a tf.data
Pipeline for Mixed Data
Objective: Build a tf.data
pipeline that preprocesses both image and structured data, integrating normalization for numeric features and resizing for images.
Instructions:
- Load sample image paths, numeric data, and categorical data.
- Create preprocessing functions for images (resizing and normalizing) and numeric data.
- Combine them into a single
tf.data
pipeline and verify the output.
Solution:
import tensorflow as tf
import numpy as np
# Sample image paths, numeric, and categorical data
image_paths = ["path/to/image1.jpg", "path/to/image2.jpg"]
numeric_data = np.array([[25.0, 50000.0], [30.0, 60000.0]])
categorical_data = np.array(["A", "B"])
# Define image processing function
def load_and_preprocess_image(path):
image = tf.io.read_file(path)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, [224, 224])
return image / 255.0
# Define numeric preprocessing
normalizer = Normalization()
normalizer.adapt(numeric_data)
# Define categorical encoding
string_lookup = StringLookup(vocabulary=["A", "B", "C"], output_mode="one_hot")
# Process data function
def process_data(image_path, numeric, category):
image = load_and_preprocess_image(image_path)
numeric = normalizer(numeric)
category = string_lookup(category)
return {"image_input": image, "numeric_input": numeric, "categorical_input": category}
# Combine data into a tf.data.Dataset
dataset = tf.data.Dataset.from_tensor_slices((image_paths, numeric_data, categorical_data))
dataset = dataset.map(process_data).batch(2)
# Verify output
for batch in dataset.take(1):
print(batch)
In this exercise:
- We create a
tf.data
pipeline that preprocesses image paths (loading, resizing, and normalizing) and structured data (normalizing numeric and encoding categorical). - The output format matches model input requirements, making it easier to feed the data directly into a neural network.
Exercise 4: Combining Multiple Inputs with Keras Preprocessing Layers in a Model
Objective: Create a model that integrates multiple input types (images, numeric, and categorical) with feature engineering directly embedded in the model.
Instructions:
- Define separate inputs for images, numeric, and categorical data.
- Use Keras preprocessing layers to normalize and encode inputs.
- Combine the preprocessed inputs in a neural network model.
Solution:
from tensorflow.keras.layers import Input, concatenate, Dense, Flatten
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Model
# Define image input and ResNet model for feature extraction
image_input = Input(shape=(224, 224, 3), name="image_input")
resnet = ResNet50(weights="imagenet", include_top=False, input_tensor=image_input)
flattened_image = Flatten()(resnet.output)
# Define numeric input and apply normalization
numeric_input = Input(shape=(2,), name="numeric_input")
normalized_numeric = normalizer(numeric_input)
# Define categorical input and apply encoding
categorical_input = Input(shape=(1,), dtype="string", name="categorical_input")
encoded_category = string_lookup(categorical_input)
# Concatenate all processed inputs
combined = concatenate([flattened_image, normalized_numeric, encoded_category])
# Final model layers
output = Dense(128, activation="relu")(combined)
output = Dense(10, activation="softmax")(output) # Assuming 10 classes
# Build the model
model = Model(inputs=[image_input, numeric_input, categorical_input], outputs=output)
# Display model summary
model.summary()
In this exercise:
- The model handles multiple input types: an image processed by ResNet, numeric data normalized through
Normalization
, and categorical data encoded withStringLookup
. - All inputs are concatenated, enabling a comprehensive view of the data in the model.
These exercises illustrate practical applications of feature engineering for deep learning, from integrating preprocessing layers to building efficient data pipelines. By embedding these steps in TensorFlow/Keras, data transformations become part of the model, creating an end-to-end pipeline that ensures data consistency and simplifies deployment.