Keras: [Dense Layer Issue] TypeError: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64

Created on 6 Apr 2017  路  4Comments  路  Source: keras-team/keras

Environment:

$ uname -a
Linux tk1 4.4.0-64-generic x86_64 x86_64 x86_64 GNU/Linux
$nvcc --version
Cuda compilation tools, release 8.0, V8.0.62
````
**Software Version:**

$ pip show keras
Name: Keras
Version: 2.0.2
Summary: Deep Learning for Python
....


$ pip show tensorflow-gpu
Name: tensorflow-gpu
Version: 1.0.1
Summary: TensorFlow helps the tensors flow
Home-page: http://tensorflow.org/
Author: Google Inc.
Author-email: [email protected]
License: Apache 2.0

**Issue Description:**

Tensorflow throws an `TypeError` when my program tries to create a Dense layer with an input from a Flatten layer. 
`TypeError: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64`.

The Flatten layer's output is float32. However, it seems like the Dense layer only accepts int32, int64.

```shell
sz_L1_filters, nb_L1_filters, stride_L1 = layer1_params
sz_res_filters, nb_res_filters, nb_res_stages = res_layer_params
sz_pool_fin = (input_shape[0]) / stride_L1

#  INPUT LAYERS
#######################
frame = Input(shape=(HEIGHT, WIDTH, CHANNELS), name='video_stream')

# VISION MODEL - USING CNN
###########################
x = Lambda(lambda image: image/255.0 - 0.5, input_shape=(HEIGHT, WIDTH, CHANNELS))(frame)
x = Conv2D(filters=nb_L1_filters, kernel_size=(sz_L1_filters, sz_L1_filters),
           strides=(stride_L1, stride_L1),
           kernel_initializer=init,
           kernel_regularizer=l2(reg),
           use_bias=False,
           padding='same',
           name='conv0')(x)
x = BatchNormalization(axis=1, name='bn0')(x)
x = Activation('relu', name='relu0')(x)
x = Dropout(KEEP_PROP)(x)
....
x = AveragePooling2D((sz_pool_fin, sz_pool_fin), name='avg_pool')(x)
x = Flatten(name='flat')(x)

x = Dense(1024, name='fc1', activation='relu')(x)  # <=== ERROR HERE
x = Dropout(0.5)(x)

Error Output:

Traceback (most recent call last):
  File "/home/dat/bag/train/DatNet.py", line 208, in <module>
    dat = DatNet()
  File "/home/dat/bag/train/DatNet.py", line 23, in __init__
    init=init, reg=reg, use_shortcuts=use_shortcuts)
  File "/home/dat/bag/train/DatNet.py", line 176, in build
    x = Dense(1024, name='fc1')(x)
  File "/home/dat/anaconda3/envs/tensorflow/lib/python3.5/site-packages/keras/engine/topology.py", line 528, in __call__
    self.build(input_shapes[0])
  File "/home/dat/anaconda3/envs/tensorflow/lib/python3.5/site-packages/keras/layers/core.py", line 827, in build
    constraint=self.kernel_constraint)
  File "/home/dat/anaconda3/envs/tensorflow/lib/python3.5/site-packages/keras/engine/topology.py", line 364, in add_weight
    weight = K.variable(initializer(shape), dtype=K.floatx(), name=name)
  File "/home/dat/anaconda3/envs/tensorflow/lib/python3.5/site-packages/keras/initializers.py", line 205, in __call__
    dtype=dtype, seed=self.seed)
  File "/home/dat/anaconda3/envs/tensorflow/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 3146, in random_uniform
    dtype=dtype, seed=seed)
  File "/home/dat/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/random_ops.py", line 244, in random_uniform
    seed2=seed2)
  File "/home/dat/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/gen_random_ops.py", line 220, in _random_uniform
    seed=seed, seed2=seed2, name=name)
  File "/home/dat/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 585, in apply_op
    param_name=input_name)
  File "/home/dat/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 61, in _SatisfiesTypeConstraint
    ", ".join(dtypes.as_dtype(x).name for x in allowed_list)))
TypeError: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64

All help are appreciated. Thank you!

Most helpful comment

Hey @dat-ai, I think this is an issue with the params your passing to a layer, not the output of one layer going into the next. I had this error when I passed in a kernel_size argument of (1.0, 6.0, 1.0), so I think you've got something similar. I would check your kernel_size arguments as well as the (sz_pool_fin, sz_pool_fin) passed into the AveragePooling2D layer to make sure they are in fact integers, and not floats.

All 4 comments

Hey @dat-ai, I think this is an issue with the params your passing to a layer, not the output of one layer going into the next. I had this error when I passed in a kernel_size argument of (1.0, 6.0, 1.0), so I think you've got something similar. I would check your kernel_size arguments as well as the (sz_pool_fin, sz_pool_fin) passed into the AveragePooling2D layer to make sure they are in fact integers, and not floats.

@sallamander : You are awesome. Thank for pinpointing the exact error. The error indeed was at sz_pool_fin = (input_shape[0]) / stride_L1

Step to resolve:

sz_pool_fin = int((input_shape[0]) / stride_L1)

Yep good point in the right direction!!!
Broken

decoder_upsample = Dense(filters * img_rows / 2 * img_cols / 2, activation='relu')

if K.image_data_format() == 'channels_first':
    output_shape = (batch_size, filters, img_rows / 2, img_cols / 2)
else:
    output_shape = (batch_size, img_rows / 2, img_cols / 2, filters)

Working

decoder_upsample = Dense(int(filters * img_rows / 2 * img_cols / 2), activation='relu')

if K.image_data_format() == 'channels_first':
    output_shape = (batch_size, filters, int(img_rows / 2), int(img_cols / 2))
else:
    output_shape = (batch_size, int(img_rows / 2), int(img_cols / 2), filters)

thank a lot ,you perfectly solve my problem same as you

Was this page helpful?
0 / 5 - 0 ratings