question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PReLU layer not working with variable input size

See original GitHub issue

I want to implement a sample PNet model from this paper. For this purpose I am using the functional API instead of the Sequential model, since at the end I need to concatenate two outputs.

This is the code:

from  keras import backend as K
from keras.models import Sequential, Model, Input
from keras.layers import Conv2D, MaxPooling2D, Dense, Activation
from keras.layers.advanced_activations import PReLU
K.set_image_dim_ordering('tf')
def get_p_net():
    main_input = Input(shape=(None, None, 3), dtype='float32', name='main_input')
    x = Conv2D(10, (3, 3), strides=(1, 1), padding='valid', name='conv1')(main_input)
    x = PReLU(name='PReLU1', alpha_constraint=None)(x)
    x = MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same', name='pool1')(x)
    x = Conv2D(16, (3, 3), strides=(1,1), padding='valid', name='conv2')(x)
    x = PReLU(name='PReLU2')(x)
    x = Conv2D(32, (3, 3), strides=(1,1), padding='valid', name='conv3')(x)
    x = PReLU(name='PReLU3')(x)
    binary_face_output = Conv2D(2, (1, 1), strides=(1,1), padding='same', name='conv4-1')(x)
    binary_face_output = Dense(2, activation='softmax', name='prob1')(binary_face_output)
    bbox_output = Conv2D(4, (1, 1), strides=(1,1), padding='same', name='conv4-2')(x)
    model = Model(inputs=[main_input], outputs=[binary_face_output, bbox_output])
    model.summary()
    # model.compile(optimizer='rmsprop', loss='binary_crossentropy',
    #           loss_weights=[1., 1.])

get_p_net()

Everything looks ok, except when I try running the code I receive the following error.

  File "p_net.py", line 23, in <module>
    get_p_net()
  File "p_net.py", line 9, in get_p_net
    x = PReLU(name='PReLU1', alpha_constraint=None)(x)
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 569, in __call__
    self.build(input_shapes[0])
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\keras\layers\advanced_activations.py", line 111, in build
    constraint=self.alpha_constraint)
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\keras\engine\topology.py", line 391, in add_weight
    weight = K.variable(initializer(shape), dtype=dtype, name=name)
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\keras\initializers.py", line 29, in __call__
    return K.constant(0, shape=shape, dtype=dtype)
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\keras\backend\tensorflow_backend.py", line 358, in constant
    return tf.constant(value, dtype=dtype, shape=shape, name=name)
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\framework\constant_op.py", line 102, in constant
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 371, in make_tensor_proto
    if np.prod(shape) == 0:
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\numpy\core\fromnumeric.py", line 2518, in prod
    out=out, **kwargs)
  File "D:\Anaconda2\envs\tensorflow\lib\site-packages\numpy\core\_methods.py", line 35, in _prod
    return umr_prod(a, axis, dtype, out, keepdims)
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'

Fixed the problem and I get the model summary. I want to ask if there is a way of processing a variable size inputs through a network which contains a PReLU layers. It is clear that somehow it does not like the input shape of the layer (output of the last convolutional layer). So, changing the very first line to:

main_input = Input(shape=(12, 12, 3), dtype='float32', name='main_input')

I want to ask if there is a way of processing variable size inputs through a network which contains PReLU layers.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

4reactions
singhaycommented, Jul 15, 2018

This is a bug

  • ✅ Check that you are up-to-date with the master branch of Keras. You can update with: pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps

  • ✅ If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.

Related Issue: https://github.com/keras-team/keras/issues/7694

The bug most likely starts in Line 109 build() function of PReLU class of file advanced_activations.py line

        param_shape = list(input_shape[1:])

which assumes that one the first position will contain None representing batch size, however for a greater tensor that has None at index 1 will let this being passed further resulting in TypeError

0reactions
mukaiNO1commented, Mar 21, 2021

The problem is still there. tensorflow==2.4.1 keras == 2.4.3 ValueError: Dimensions must be equal, but are 16 and 500 for '{{node sequential_2/p_re_lu_14/mul}} = Mul[T=DT_FLOAT](sequential_2/p_re_lu_14/Neg, sequential_2/p_re_lu_14/Relu_1)' with input shapes: [16,16,56], [1,500,500,56].

Read more comments on GitHub >

github_iconTop Results From Across the Web

PReLU with variable input size - tensorflow - Stack Overflow
Use PreLU(shared_axes=[1,2]) so shape is computed on compilation.
Read more >
neural networks - Is PReLU superfluous with respect to ReLU?
Lets assume we have 3 Dense layers, where the activations are x0→x1→x2, such that x2=ψPReLU(x1)+γ and x1=PReLU(Ax0+b).
Read more >
Define Custom Deep Learning Layer with Learnable Parameters
Specify any variables required to create the layer as inputs to the constructor function. The PReLU layer constructor function requires one optional argument...
Read more >
Why cautiously initializing deep neural networks matters?
If m is the input size and nh is number of hidden units, then weights can be ... This phenomenon is called vanishing...
Read more >
Functions — Neural Network Libraries 1.32.0 documentation
To implement the naive multiplicaiton function of two variables using PythonFunction ... If None, dummy variable is created and running mean is not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found