-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_dataset.py
134 lines (104 loc) · 4.21 KB
/
prepare_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Import
import numpy as np
import tensorflow as tf
# My import
import constants as const
# load data into keras dataset
def load_dataset(train_data_dir, test_data_dir):
"""
Load data into Keras datasets.
Note:
- The function uses TensorFlow's `image_dataset_from_directory` utility to load the datasets.
- Training and validation datasets are split from the `train_data_dir` with a validation split of 0.2.
- The datasets are shuffled for training purposes.
- The image size and batch size are determined by constants defined in the `constant` module.
:param train_data_dir: The directory path containing the training data.
:param test_data_dir: The directory path containing the test data.
:returns: A tuple containing three datasets:
- The training dataset.
- The validation dataset.
- The test dataset.
"""
# train and validation dataset
print("\n> Training and Validation: ")
train_ks_dataset, val_ks_dataset = tf.keras.utils.image_dataset_from_directory(
train_data_dir,
color_mode="rgb",
batch_size=const.BATCH_SIZE,
image_size=const.IMG_SIZE,
shuffle=True,
seed=123,
validation_split=0.2,
subset="both",
)
# test dataset
print("\n> Test:")
test_ks_dataset = tf.keras.utils.image_dataset_from_directory(
test_data_dir,
batch_size=const.BATCH_SIZE,
image_size=const.IMG_SIZE,
shuffle=False,
)
return train_ks_dataset, val_ks_dataset, test_ks_dataset
# Data Augmentation
def perform_data_augmentation():
"""
Perform data augmentation.
Note:
- It is performed using a TensorFlow Sequential model with the following transformations:
- RandomFlip: str, Specifies the type of random flip to be applied.
- RandomRotation: float, Specifies the maximum angle of rotation in degrees.
- RandomZoom: float, Specifies the maximum zoom factor.
:return: tf.keras.Sequential, A sequential model representing the data augmentation transformations.
"""
# Perform data augmentation
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip("horizontal"),
tf.keras.layers.RandomRotation(0.1),
tf.keras.layers.RandomZoom(0.2),
])
return data_augmentation
# Data Normalization
def data_normalization(tf_dataset, augment):
"""
Normalize the data and optionally apply data augmentation.
Note:
- Standardizes the data by rescaling pixel values to the range [0, 1].
- The dataset is configured for performance by prefetching data using TensorFlow's AUTOTUNE mechanism.
:param tf_dataset: tf.data.Dataset, The dataset to be normalized.
:param augment: bool, Whether to apply data augmentation.
If True, augmentation is performed only on the training set.
:return: tf.Dataset.data object.
"""
# Standardize the data
normalization_layer = tf.keras.layers.Rescaling(1. / 255)
ds = tf_dataset.map(lambda x, y: (normalization_layer(x), y))
# Use data augmentation only on the training set
if augment:
data_augmentation = perform_data_augmentation()
ds = ds.map(lambda x, y: (data_augmentation(x, training=True), y))
# Configure the dataset for performance
ds = ds.prefetch(buffer_size=const.AUTOTUNE)
return ds
# Convert tensorflow dataset object into an array
def image_to_array(tf_dataset):
"""
Convert a TensorFlow dataset object into NumPy arrays.
Note:
- The function iterates through the TensorFlow dataset to extract images and labels.
- Images and labels are appended to separate lists and then converted into NumPy arrays.
:param tf_dataset: tf.Dataset.data object.
:return: A tuple containing two NumPy arrays:
- X_array: The input images.
- y_array: The corresponding class labels.
"""
X_array = [] # Images
y_array = [] # Labels
for image, label in tf_dataset.unbatch().map(lambda x, y: (x, y)):
X_array.append(image)
y_array.append(label)
# Input values
X_array = np.array(X_array)
# Target values
y_array = np.array(y_array)
return X_array, y_array