我正在尝试使用tensorflow.keras模型和图层类设计GAN。我创建了一个鉴别器,该鉴别器接收2张图片的列表,并输出Dense Sigmoid激活的相似性百分比:
prediction = Dense(1, activation = "sigmoid")(Flatten()(conv4))
model = Model(inputs = [firstImage, secondImage], outputs = prediction)
然后是一个生成器,它接受一个随机的一维矢量并从中返回图片:
generated = Conv2D(3, kernel_size = (4, 4), padding = "same",
kernel_initializer = kernelInit, activation = "sigmoid")(conv5) # output shape (256, 256, 3)
model = Model(inputs = noise, outputs = generated)
我使用keras.ImageDataGenerator.flow_from_directory()创建了一个自定义生成器,以加载图片:
def loadRealImages(batch):
for gen in pixGen.flow_from_directory(picturesPath, target_size = (256, 256),
batch_size = batch, class_mode = "binary"):`
yield gen
编译这两个代码中的任何一个都没有问题,但是当我尝试使用以下代码将它们链接到一个对抗模型中时:
inNoise = Input(shape = (generatorInNoise,))
fake = generator(inNoise) # get one fake
real = np.array(next(loadRealImages(1))[0], dtype = np.float32) # get one real image
discriminator.trainable = False # lock discriminator weights
prediction = discriminator([real, fake]) # check similarity
adversarial = Model(inputs = inNoise, outputs = [fake, prediction]) # set adversarial model
我在最后一行收到此错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot convert a Tensor of dtype resource to a NumPy array.
我确定了噪声,假和预测的形状:
<类'tensorflow.python.framework.ops.Tensor'>(无,16)Tensor(“ input_4:0”,shape =(无,16),dtype = float32)
<类'tensorflow.python.framework.ops.Tensor'>(无,256,256,3)Tensor(“ model_1 / Identity:0”,shape =(无,256,256,3),dtype = float32)
<类'tensorflow.python.framework.ops.Tensor'>(1,1)Tensor(“ dense_2 / Identity:0”,shape =(1,1),dtype = float32)
但是我仍然不知道是什么引起了错误,并且在Google上查找它也没有给我任何指示。有人可以帮忙吗?
答案 0 :(得分:0)
从根本上讲,这里的问题是您试图将numpy数组作为计算图的一部分。这可能导致不确定的行为,具体取决于您如何使用它。对您的代码进行一些小的更改可以帮助您:
inNoise = Input(shape = (generatorInNoise,))
fake = generator(inNoise) # get one fake
real = Input((real_image_shape)) # get one real image
discriminator.trainable = False # lock discriminator weights
prediction = discriminator([real, fake]) # check similarity
adversarial = Model(inputs = [inNoise, real], outputs = [fake, prediction]) # set adversarial model
如您所见,实际图像需要作为模型的输入提供,而不是作为模型的一部分提供。