myLewysG
myLewysG

Reputation: 3

tensorflow.python.framework.errors_impl.InvalidArgumentError: cannot convert a Tensor of dtype resource to a numpy array

I am trying to design a GAN using tensorflow.keras models and layers classes. I made a discriminator that takes in a list of 2 pictures and outputs a Dense sigmoid activated percentage of similarity:

prediction = Dense(1, activation = "sigmoid")(Flatten()(conv4))
model = Model(inputs = [firstImage, secondImage], outputs = prediction)

Then a generator that takes in a random one dimension vector and returns a picture out of it:

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)

I made a custom generator using a keras.ImageDataGenerator.flow_from_directory() to load in pictures:

def loadRealImages(batch):
   for gen in pixGen.flow_from_directory(picturesPath, target_size = (256, 256),
                                    batch_size = batch, class_mode = "binary"):`
      yield gen

I didn't have any trouble compiling any of these two but then when I try to link them together into an adversarial model with this code:

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

I get this error on the last line:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot convert a Tensor of dtype resource to a NumPy array.

I ascertained the shape of inNoise, fake and 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)

But I still can't figure out what is raising the error and looking it up on google didn't really give me any pointers either. Can anyone help with this?

Upvotes: 0

Views: 1360

Answers (1)

Susmit Agrawal
Susmit Agrawal

Reputation: 3764

At the core, the issue here is that you're trying to make a numpy array a part of the computation graph. This can lead to undefined behaviour depending on how you use it. Some minor changes to you code can help:

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

As you can see, the real image needs to be provided as an input to the model, not derived as a part of it.

Upvotes: 0

Related Questions