1

As you can read in the title my keras.utils.Sequence return 3 arrays. How can I use model.fit(..) with 3 outputs from keras.utils.Sequence object.

    def __getitem__(self, index):
        'Generate one batch of data'
        # Generate indexes of the batch
        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]

        # Find list of IDs
        list_IDs_temp = [self.list_IDs[k] for k in indexes]

        # Generate data
        X,Xt,y= self.__data_generation(list_IDs_temp)

        return X,Xt,y

X is my original image, Xt is the transformed image, and y is the label. I want to compile the code below:

model_englobe.fit(generated_image_train,batch_size=1,epochs=2)

My model take two inputs X my original image, Xt. generated_image_train[i] is the batch number i generated_image_train[i][0] I select here all original image, generated_image_train[i][1] I select here all transformed image, generated_image_train[i][2] I select here all labels,

The display error:

    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step  **
        outputs = model.train_step(data)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:754 train_step
        y_pred = self(x, training=True)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py:207 assert_input_compatibility
        ' input tensors. Inputs received: ' + str(inputs))

    ValueError: Layer model expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, None, None, None) dtype=float32>]

Thank you very much

kodi
  • 51
  • 9

1 Answers1

2

If your generator returns 3 variables, then keras assumes that you are returning X, y and sample_weights. To pass two inputs, you have to return X and Xt as a list like this:

    def __getitem__(self, index):
        'Generate one batch of data'
        # Generate indexes of the batch
        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]

        # Find list of IDs
        list_IDs_temp = [self.list_IDs[k] for k in indexes]

        # Generate data
        X,Xt,y= self.__data_generation(list_IDs_temp)

        return [X,Xt],y

For more information, check the x parameters in the fit function documentation.

Coderji
  • 7,655
  • 5
  • 37
  • 51
  • Thank you, however I have this error:ValueError: in user code: /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function * return step_function(self, iterator) :17 call * self.total[i,j].assign(tensor_sub) /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/array_ops.py:1229 assign ** raise ValueError("Sliced assignment is only supported for variables") ValueError: Sliced assignment is only supported for variables. Do you know what it means? – kodi Mar 11 '21 at 18:49
  • maybe I am mistaken, can you try to return a list instead of tuple? like [X, Xt].. I have edited my answer. as Tuple does not support slicing and that is why it throw an error. – Coderji Mar 11 '21 at 19:01
  • I have the same error:ValueError: Sliced assignment is only supported for variables – kodi Mar 11 '21 at 19:11
  • I tried it now with a dummy model and it worked. I think it is something related on how you are generating the data. are you sure that the data you generated are numpy arrays. – Coderji Mar 11 '21 at 19:15
  • 1
    I think this is my layer according to this post:https://stackoverflow.com/questions/57377905/why-does-this-error-happenssliced-assignment-is-only-supported-for-variables Can I write :tensor_sub = tf.math.reduce_sum( tf.math.abs(tf.subtract(in1[0,i,j,:],in2[0,:])) ) if in1, in2 are tensors – kodi Mar 11 '21 at 19:17
  • I see, if you are still struggling with this error. I suggest to open a new issue, as it is not related to the subject of this ticket and it would be really helpful to add the summary of your model to know how are you feeding your inputs. – Coderji Mar 11 '21 at 19:20