EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Keras Tutorial Keras Utils Sequence
 

Keras Utils Sequence

Updated March 16, 2023

Keras Utils Sequence

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Keras Utils Sequence - Importing

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:

Keras Utils Sequence - Class

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:

using the init method

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:

using len method

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:

Using getitem method

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:

getitem method

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:

Keras Utils Sequence Structure

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:

Keras Utils Sequence Generator

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:

Keras Utils Sequence Trained Model

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 11

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:

Keras Utils Sequence init method

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:

Keras Utils Sequence 13

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:

Keras Utils Sequence getitem method

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:

Defining Keras Utils Sequence

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:

Define init method

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 –

  1. Keras Tuner
  2. What is Keras GPU?
  3. Keras Embedding
  4. Keras predict

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW