Updated March 16, 2023
Introduction to Keras Utils Sequence
Keras utils sequence data generator is useful in a variety of situations, such as when we need advanced control over sample generation or when simple data does not fit into memory and must be loaded dynamically. It is the root class in data generators, and it will contain a few methods that were overridden for implementing a custom data loader. It is defined in the data generator.
The method of utils sequence is returning the complete batch. It is the safest way for doing multi-processing. This structure ensures that our network is trained from a single sample from each epoch that does not contain case generators. For defining the utils sequence we need to import the different types of dependencies as numpy and keras. The state of the art configuration was defined in memory space to define the process data that we were using to do it. We are generating multiple cores in real time to feed the same in the proper way for our deep learning model.
Key Takeaways
- Every sequence is implemented getitem, len, and init methods. If we want to modify the dataset between epochs which we are implementing from epoch end.
- It is used in safely multiprocessing. At the time of using the utils sequence, we need to define a class.
How to Use Keras Utils Sequence?
We need to follow the below steps as follows. First, we need to import the specified module which is required for the utils sequence.
1. In the first step we are importing the imread, resize, numpy, keras, and math model. We are importing all the modules by using the import keyword as follows.
Code:
import numpy as np
import math
from skimage.io import imread
from skimage.transform import resize
from tensorflow import keras
Output:
2. After importing the required module, now in this step we are creating the class of utils sequence.
Code:
class seq (tf.keras.utils.Sequence):
Output:
3. After creating the class now in this step we are using the init method as follows.
Code:
class seq (tf.keras.utils.Sequence):
def __init__( …..):
self.x, self.y = x_set, y_set
self.batch_size = batch_size
Output:
4. After defining the init method now in this step we are using the len method.
Code:
def __len__(self):
return math.ceil(len()
Output:
5. After defining the init method now in this step we are using getitem method.
Code:
def __getitem__(self, idx):
batch_x = self.x[ ]
batch_y = self.y[ ]
Output:
6. After defining the getitem method now in this step we are defining the return statement by using an array with utils sequence.
Code:
return np.array([
resize(imread())
for file_name in batch_x]), np.array(batch_y)
Output:
Custom Data Keras Utils Sequence
The custom data generator is useful in multiple cases. We require advanced control over data generation that was sampled or simply data that does not fit into memory and must be loaded dynamically. As we know, the utils sequence is nothing more than the root class of the data generator, and it will contain multiple methods that were overridden in order to implement the custom data loader.
Below is the structure of the custom implementation of utils sequence as follows.
Code:
class custom_seq (tf.keras.utils.Sequence):
def __init__(self, x_set, y_set, b_size):
self.x, self.y = x_set, y_set
self.batch_size = b_size
def __len__(self):
return math.ceil()
def __getitem__(self, idx):
batch_x = self.x[]
batch_y = self.y[) * self.b_size]
return np.array([
resize(imread(file_name), (300, 300))
for file_name in batch_x]), np.array(batch_y)
Output:
We can easily pass the object of the class by using a custom generator, as shown in the example below.
Code:
t_ds = custom_seq(X_col={'path':'plot.csv', 'bbox': 'keras_utils_sequence'},
y_col = {'name': 'sequence', 'type': 'sequence'},
batch_size = b_size, input_size = t_size)
Output:
The second object for passing the trained model dataset is as follows:
Code:
v_ds = custom_seq(val_df,
X_col={'path':'plot.csv', 'bbox': 'keras_utils_sequence'},
y_col={'name': 'sequence', 'type': 'sequence'},
batch_size = b_size, input_size = t_size)
Output:
The model of fit to use the object is defined below.
Code:
model.fit (
t_ds,
validation_data = v_ds,
epochs = num_epochs)
Output:
Keras Utils Sequence TmpKerasModel
We are using the below method to define the sequence in utils. In that, we need to import the sequential model and need to design the model by using a sequential function. The below example shows how we can use the TmpKerasModel utils sequence. In the below example, we are using the init method as follows.
Code:
def __init__(self, x_set, y_set, b_size):
self.x, self.y = x_set, y_set
self.batch_size = b_size
Output:
The below example shows how we can use the TmpKerasModel utils sequence. In the below example, we are using the len method.
Code:
def __len__(self):
return math.ceil(len(self.x) / self.b_size)
Output:
The below example shows how we can use the TmpKerasModel utils sequence. We are using getitem method.
Code:
def __getitem__(self, idx):
batch_x = self.x [ ]
batch_y = self.y[idx * self.b_size:(idx + 1) *
self.b_size]
Output:
Examples of Keras Utils Sequence
Given below are the examples mentioned:
Example #1
In the below example, we are using getitem method to define the utils sequence.
Code:
import numpy as np
import math
from skimage.io import imread
from skimage.transform import resize
from tensorflow import keras
class custom_seq (tf.keras.utils.Sequence):
def __getitem__(self, idx):
batch_x = self.x[ ]
batch_y = self.y[]
return np.array([
resize(imread(file_name), (300, 300))
for file_name in batch_x]), np.array(batch_y)
Output:
Example #2
In the below example, we are using the init method to define the utils sequence.
Code:
import numpy as np
import math
from skimage.io import imread
from skimage.transform import resize
from tensorflow import keras
class custom_seq (tf.keras.utils.Sequence):
def __init__(self, x_set, y_set, b_size):
self.x, self.y = x_set, y_set
self.batch_size = b_size
return np.array([
resize(imread(file_name), (300, 300))
for file_name in batch_x]), np.array(batch_y)
Output:
FAQ
Given below are the FAQs mentioned:
Q1. What is the use of keras utils sequence?
Answer: Basically, it is used in multiprocessing. At the time of doing multiprocessing in our application then we use the keras utils sequence.
Q2. Which module do we need to import at the time of using the utils sequence?
Answer: We need to import the math, keras, numpy, sequential, resize, tensorflow, and imread modules while using the keras utils sequence.
Q3. Which methods we are using with utils sequence?
Answer: At the time of using it first we are defining the class after defining the class we are using the len, init, and getitem methods.
Conclusion
For defining the utils sequence we need to import the different types of dependencies as numpy and keras. The data generator is useful in multiple cases, we need advanced control of the sample generation or simple data is not fitting into the memory and it will be loaded dynamically.
Recommended Articles
This is a guide to Keras Utils Sequence. Here we discuss the introduction, and how to use the keras utils sequence. examples and FAQ. You may also have a look at the following articles to learn more –