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.

Multi output network generator

See original GitHub issue

model.fit support multi input/output network, but if data-set is large enough and one have to use model.fit_generator, its complicated to generate tuple for such case, is there any plan to make it more simpler like model.fit.

I have network that take one input and produce two outputs, i created separate generator for each, but i am not able to run network on this. my input should be of form x, [y1, y2].

i think i need to extend generator for such case ?

image_datagen = ImageDataGenerator(
          rotation_range=15.,
          width_shift_range=0.2,
          height_shift_range=0.2,
          rescale=1./255,
          shear_range=0.2,
          zoom_range=0.2,
          horizontal_flip=True,
          vertical_flip = True,
          fill_mode='nearest')
  
  mask_datagen = ImageDataGenerator(
          rotation_range=15.,
          width_shift_range=0.2,
          height_shift_range=0.2,
          rescale=1./255,
          shear_range=0.2,
          zoom_range=0.2,
          horizontal_flip=True,
          vertical_flip = True,
          fill_mode='nearest')
  shading_datagen = ImageDataGenerator(
          rotation_range=15.,
          width_shift_range=0.2,
          height_shift_range=0.2,
          rescale=1./255,
          shear_range=0.2,
          zoom_range=0.2,
          horizontal_flip=True,
          vertical_flip = True,
          fill_mode='nearest')
  test_datagen = ImageDataGenerator(rescale=1./255)
  seed_validation = 1
  validation_image_generator = test_datagen.flow_from_directory(
          os.path.join(gen_path,'clean/test'),
          target_size=(128, 256),class_mode=None,classes=None,
          batch_size=20,seed=seed_validation)
  validation_mask_generator = test_datagen.flow_from_directory(
          os.path.join(gen_path,'albedo/test'),
          target_size=(128, 256),class_mode=None,classes=None,
          batch_size=20,seed=seed_validation)
  validation_shading_generator = test_datagen.flow_from_directory(
          os.path.join(gen_path,'gray_shading/test'),
          target_size=(128, 256),class_mode=None,classes=None,
          batch_size=20,seed=seed_validation)
  #testDataGen = ImageDataGenerator(rescale=1./255)
  seed = 5
  #image_datagen.fit(image_datagen, augment=True, seed=seed)
  #mask_datagen.fit(mask_datagen, augment=True, seed=seed)
  
  image_generator = image_datagen.flow_from_directory(
      os.path.join(gen_path,'clean/train'),
      class_mode=None,target_size=(128,256),batch_size=20,classes=None,
      seed=seed)
  
  mask_generator = mask_datagen.flow_from_directory(
      os.path.join(gen_path,'albedo/train'),
      class_mode=None,target_size=(128,256),batch_size=20,classes = None,
      seed=seed)
  shading_generator = shading_datagen.flow_from_directory(
      os.path.join(gen_path,'gray_shading/train'),
      class_mode=None,target_size=(128,256),batch_size=20,classes = None,
      seed=seed)
  
  train_generator = itertools.izip(image_generator, mask_generator, shading_generator)

Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on StackOverflow or join the Keras Slack channel and ask there instead of filing a GitHub issue.

Thank you!

  • Check that you are up-to-date with the master branch of Keras. You can update with: pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps

  • If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.

  • If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with: pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps

  • Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
zafaralicommented, Jun 15, 2017

Sorry if question sound silly I am not able to understand input for this join_generator ? its single zipped three generators ??

Don’t worry, I didn’t include a doc string. It’s just a list of the generators. Each generator must return (x, y). Therefore if you have:

g1 --> (x1, y1)
g2 --> (x2, y2)
join_generators(g1, g2) --> ([x1, x2], [y1, y2])

and with that code y ll still lead to one input, should i write separate y1 and y2 and yield x, [y1,y2]

This was just a skeleton of a possible implementation. For example, if your generators only return one element you could just:

def join_generators(xgenerators, ygenerator):
    while True: # keras requires all generators to be infinite
        data = [next(g) for g in xgenerators]

        yield x, next(ygenerator)

Or vary the code accordingly.

1reaction
0x00b1commented, Jun 15, 2017

I like that trick. In my experience, writing a custom generator that yields both inputs is easier to manage than zipping two generators. It is especially evident when managing complex augmentation and sampling methods.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Building a multi-output Convolutional Neural Network with Keras
Building a multi-output Convolutional Neural Network with Keras. In this post, we will be exploring the Keras ... Data generator for the UTKFace...
Read more >
Multiple-Output Network Clock Generator - MAX9489
The MAX9489 clock generator provides multiple clock outputs, ideal for network routers. The MAX9489 provides 15 buffered clock outputs, each independently ...
Read more >
Data augmentation for multiple output heads in Keras
But, I am not able to apply data augmentation. I am trying to do the following: datagen = ImageDataGenerator(horizontal_flip=True, vertical_flip ...
Read more >
Keras: How to use fit_generator with multiple outputs of ...
In a Keras model with the Functional API I need to call fit_generator to train on augmented images data using an ImageDataGenerator ......
Read more >
Building Multi Output Cnn With Keras - Kaushal Shah
Now let's explore CNN with multiple outputs in detail. ... So we need to create a custom training data generator which generates the...
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