0

我想将 ImageDataGenerators 获取的图像附加到两个不同的列表中。我相信我可以使用 lambda 层做到这一点,但我收到一条错误消息。有关玩具示例,请参见下面的代码。您可以使用任何一组图像来运行代码。我使用了这里找到的猫狗数据集:“ 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).
4

1 回答 1

0

我猜你在这里没有正确使用 Lambda,imgs1.append(x)不会给你任何东西,这就是为什么 tf 说它需要张量但没有类型。

您可以查看 tf doc 以了解如何使用它。

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

我认为它在python中也类似:

>>> test=[]
>>> zzz=test.append(2)
>>> print(zzz)
None
于 2019-10-17T15:14:02.813 回答