question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A preprocessing_function with parameters for ImageDataGenerator

See original GitHub issue

Hi, I’m training a semantic segmentation keras model on the COCO data-set. I want to create a custom preprocessing function that resets irrelevant objects to zero in the mask image, e.g. I want to train segmentation training and I only care of correctly segmenting ‘cars’, so I want to set to zero all other labels:

The preprocessing function:

def preprocess_mask(x, label):
    x = x.astype('int16')
    x[x!=label] = 0
    return x

The desired generator:

datagen = ImageDataGenerator(
        preprocessing_function = preprocess_mask(label=4),
        rescale=1./255,
        horizontal_flip=True)

But it doesn’t work

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
Dref360commented, Oct 9, 2018

You need to implement a Functor in this case.

class MyFunctor():
    def __init__(self, label):
        self.label = label
    def __call__(self, x):
        ...
0reactions
Young1906commented, Dec 15, 2022

What if I want to apply a different preprocessing function to each split? Ie. Augmentation on Train split but not on validation split?

Read more comments on GitHub >

github_iconTop Results From Across the Web

tf.keras.preprocessing.image.ImageDataGenerator - TensorFlow
Generate batches of tensor image data with real-time data augmentation.
Read more >
Custom Preprocessing Function with ImageDataGenerator
In this code snippet, I will show you, how to write a custom preprocessing function to use with ImageDataGenerator to extract image patches,...
Read more >
Extending the ImageDataGenerator in Keras and TensorFlow
This article is a tutorial on extending the ImageDataGenerator in Keras and TensorFlow using the preprocessing function.
Read more >
Custom Data Augmentation in Keras - Step Up AI
Implement your own custom preprocessing function for data ... extend ImageDataGenerator and specify our own augmentation parameters in the ...
Read more >
tf.keras - How to change pre_processing function execution ...
Keras team members said that the ImageDataGenerator class is legacy. They suggest me to use transformation layers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found