0

I would like to append the images which are procurred by the ImageDataGenerators to two different lists. I believed I could do that with a lambda layer but I am getting an error message. For a toy example see the code below. You can use any set of images to run the code. I used the cats and dogs dataset found here: "https://download.microsoft.com/download/3/E/1/3E1C3F21-ECDB-4869-8368-6DEBA77B919F/kagglecatsanddogs_3367a.zip"

imgs1 = []
imgs2 = []

train_datagen = ImageDataGenerator(rescale = 1./255.,
                                   rotation_range = 40,
                                   width_shift_range = 0.2,
                                   height_shift_range = 0.2,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

train_generator1 = train_datagen.flow_from_directory(train_dir1,
                                                    batch_size = 1,
                                                    class_mode = 'binary', 
                                                    target_size = (150, 150), shuffle = False)  

train_generator2 = train_datagen.flow_from_directory(train_dir2,
                                                    batch_size = 1,
                                                    class_mode = 'binary', 
                                                    target_size = (150, 150), shuffle = False)    

inputs1 = Input(shape=(150, 150, 3))
inputs2 = Input(shape=(150, 150, 3))

l1 = Lambda(lambda x: imgs1.append(x), name = 'lambda1')(inputs1)
l2 = Lambda(lambda x: imgs2.append(x), name = 'lambda2')(inputs2)

x1 = Flatten()(inputs1)

x1 = Dense(1024, activation='relu')(x1)

x1 = Dropout(0.2)(x1)  

outputs1 = Dense(1, activation='sigmoid')(x1)    


x2 = Flatten()(inputs1)

x2 = Dense(1024, activation='relu')(x2)

x2 = Dropout(0.2)(x2)  

outputs2 = Dense(1, activation='sigmoid')(x2)  

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-31-88211ebc88dd> in <module>
      3 inputs2 = Input(shape=(150, 150, 3))
      4 
----> 5 l1 = Lambda(lambda x: imgs1.append(x), name = 'lambda1')(inputs1)
      6 l2 = Lambda(lambda x: imgs2.append(x), name = 'lambda2')(inputs2)
      7 

~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
    865             raise ValueError('A layer\'s `call` method should return a '
    866                              'Tensor or a list of Tensors, not None '
--> 867                              '(layer: ' + self.name + ').')
    868           if base_layer_utils.have_all_keras_metadata(inputs):
    869             if training_arg_passed_by_framework:

ValueError: A layer's `call` method should return a Tensor or a list of Tensors, not None (layer: lambda1).
halfer
  • 19,824
  • 17
  • 99
  • 186
user8270077
  • 4,621
  • 17
  • 75
  • 140

1 Answers1

0

I guess you did not use Lambda correctly here, imgs1.append(x) will give you nothing and that's why tf saying it expects tensors but got None type.

You can check tf doc as how to use it.

https://www.tensorflow.org/api_docs/python/tf/keras/layers/Lambda

I think it is also similar in python:

>>> test=[]
>>> zzz=test.append(2)
>>> print(zzz)
None
donglinjy
  • 1,104
  • 6
  • 14