我正在尝试使用tensorflow.keras模型和层类设计一个GAN。我制作了一个鉴别器,它接收2张图片的列表,并输出密集的乙状结肠激活的相似性百分比:
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_从_目录()创建了一个自定义生成器,用于加载图片:
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.
我确定了inNoise、fake和prediction的形状:
<class 'tensorflow.python.framework.ops.Tensor'> (None, 16) Tensor("input_4:0", shape=(None, 16), dtype=float32)
<class 'tensorflow.python.framework.ops.Tensor'> (None, 256, 256, 3) Tensor("model_1/Identity:0", shape=(None, 256, 256, 3), dtype=float32)
<class 'tensorflow.python.framework.ops.Tensor'> (1, 1) Tensor("dense_2/Identity:0", shape=(1, 1), dtype=float32)
但我仍然无法找出是什么导致了错误,在谷歌上查找也没有给我任何提示。有人能帮忙吗
在核心部分,这里的问题是您试图使numpy数组成为计算图的一部分。这可能导致未定义的行为,具体取决于您如何使用它。对您的代码进行一些小的更改可能会有所帮助:
如您所见,真实图像需要作为模型的输入提供,而不是作为模型的一部分导出
相关问题 更多 >
编程相关推荐