manicman1999 / StyleGAN2-Tensorflow-2.0

StyleGAN 2 in Tensorflow 2.0
MIT License
486 stars 112 forks source link

ValueError: Layer model_3 expects 8 input(s), but it received 9 input tensors. #15

Closed ShivanshuPurohit closed 3 years ago

ShivanshuPurohit commented 3 years ago

I got this error when loading the images. The full error is

Traceback (most recent call last):
  File "stylegan_two.py", line 643, in <module>
    model.evaluate(0)
  File "stylegan_two.py", line 517, in evaluate
    generated_images = self.GAN.GMA.predict(n1 + [n2, trunc], batch_size = BATCH_SIZE)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 1629, in predict
    tmp_batch_outputs = self.predict_function(iterator)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 828, in __call__
    result = self._call(*args, **kwds)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 871, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 726, in _initialize
    *args, **kwds))
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 2969, in _get_concrete_function_internal_garbage_collected
    graph_function, _ = self._maybe_define_function(args, kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 3361, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 3206, in _create_graph_function
    capture_by_value=self._capture_by_value),
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py", line 990, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 634, in wrapped_fn
    out = weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py", line 977, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1478 predict_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1468 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1461 run_step  **
        outputs = model.predict_step(data)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1434 predict_step
        return self(x, training=False)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:207 assert_input_compatibility
        ' input tensors. Inputs received: ' + str(inputs))

    ValueError: Layer model_3 expects 8 input(s), but it received 9 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(16, 512) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(16, 512) dtype=float32>, <tf.Tensor 'IteratorGetNext:2' shape=(16, 512) dtype=float32>, <tf.Tensor 'IteratorGetNext:3' shape=(16, 512) dtype=float32>, <tf.Tensor 'IteratorGetNext:4' shape=(16, 512) dtype=float32>, <tf.Tensor 'IteratorGetNext:5' shape=(16, 512) dtype=float32>, <tf.Tensor 'IteratorGetNext:6' shape=(16, 512) dtype=float32>, <tf.Tensor 'IteratorGetNext:7' shape=(16, 256, 256, 1) dtype=float32>, <tf.Tensor 'IteratorGetNext:8' shape=(16, 1) dtype=float32>]

Is it because the blocks are written in functional form while the self.generator is a sequential model? When I change the def generator(self) to

self.S = Dense(512, input_shape = [latent_size])
        self.S = LeakyReLU(0.2)(self.S)
        self.S = Dense(512)(self.S)
        self.S = LeakyReLU(0.2)(self.S)
        self.S = Dense(512)(self.S)
        self.S = LeakyReLU(0.2)(self.S)
        self.S = Dense(512)(self.S)
        self.S = LeakyReLU(0.2)(self.S)

it says self.S can't be converted to a tensor

RhynoTime commented 3 years ago

I am also having this same issue - TF 2.4 / CUDA 11.1

elRaulito commented 3 years ago

Did you manage to solve the issue?

jhawgs commented 3 years ago

I'm getting this error too. I'm on macOS with M1 and Tensorflow is running on GPU as a Pluggable Device. Given that @RhynoTime had this issue with CUDA, perhaps it has to do with GPU? It doesn't seem likely but not something to rule out.

I've gotten this traced back to the GAN.GMA model. The input shapes match all of those shown in the error message, but the final input (<tf.Tensor 'IteratorGetNext:8' shape=(16, 1) dtype=float32>) does not. For reference, here's the model summary for GAN.GMA.

Model: "model_3"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_18 (InputLayer)           [(None, 512)]        0                                            
__________________________________________________________________________________________________
input_19 (InputLayer)           [(None, 512)]        0                                            
__________________________________________________________________________________________________
input_20 (InputLayer)           [(None, 512)]        0                                            
__________________________________________________________________________________________________
input_21 (InputLayer)           [(None, 512)]        0                                            
__________________________________________________________________________________________________
input_22 (InputLayer)           [(None, 512)]        0                                            
__________________________________________________________________________________________________
input_23 (InputLayer)           [(None, 512)]        0                                            
__________________________________________________________________________________________________
input_24 (InputLayer)           [(None, 512)]        0                                            
__________________________________________________________________________________________________
sequential (Sequential)         (None, 512)          1050624     input_18[0][0]                   
                                                                 input_19[0][0]                   
                                                                 input_20[0][0]                   
                                                                 input_21[0][0]                   
                                                                 input_22[0][0]                   
                                                                 input_23[0][0]                   
                                                                 input_24[0][0]                   
__________________________________________________________________________________________________
input_25 (InputLayer)           [(None, 256, 256, 1) 0                                            
__________________________________________________________________________________________________
model_1 (Functional)            (None, 256, 256, 3)  14269368    sequential[0][0]                 
                                                                 sequential[1][0]                 
                                                                 sequential[2][0]                 
                                                                 sequential[3][0]                 
                                                                 sequential[4][0]                 
                                                                 sequential[5][0]                 
                                                                 sequential[6][0]                 
                                                                 input_25[0][0]                   
==================================================================================================
Total params: 15,319,992
Trainable params: 15,319,992
Non-trainable params: 0
__________________________________________________________________________________________________

As stated in error message, the problem is at line 519, but a similar call also occurs at line 546. The tensor in question of dimension (16, 1) is called trunc, and its passed in a list with another tensor that's then added to another. Upon removing trunc in both calls, the error goes away, but I'm monitoring my system resources in Activity Monitor, and it doesn't seem like there's anything going on. I did get two new additions to the Results folder, so maybe that's a good sign?

thusinh1969 commented 3 years ago

Bug ! trunc = 0.5 n1 = noiseList(64) n2 = nImage(64) trunc = np.ones([64, 1]) * trunc trunc.shape (64,1)

len(n1) 7

n2.shape (64, 256, 256, 1)

len(n1 + [n2, trunc]) 9

==> Passing 9 inputs to the generator while the generator accepts only 8. Must be a bug and this code is completely not tested before releasing ...?

You can do this: (I am NOT sure if multiply is a correct ops though...)

trunc = tf.reshape(trunc, (trunc.shape[0], 1, 1, 1)) n2_trunc = tf.math.multiply(n2, trunc)

...and then

generated_images = self.GAN.GMA.predict(n1 + [n2_trunc], batch_size = BATCH_SIZE)

manicman1999 commented 3 years ago

Hello everyone! Thank you for your comments. It appears that before uploading the code after some updates, I forgot to remove the input variable 'trunc' on lines 493, 508 and 534. Sorry about that! Updated code, compatible with TF 2.5.x should be uploaded tonight. Thanks again.