faustomorales / vit-keras

Keras implementation of ViT (Vision Transformer)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ValueError: Can't convert Python sequence with mixed types to Tensor

Sunmmming opened this issue · comments

I use the tensorlfow2.0.0 to run the code.But in layers.py line35, there is an error ValueError: Can't convert Python sequence with mixed types to Tensor. This may be due to input_ Shape is a nonetype, and the initializers shape got an nonetype input.

Hi there! Thanks for raising this issue. To help reproduce/diagnose, could you please provide the following?

  • A full traceback of the error
  • The output of the following code snippet
import vit_keras
import tensorflow as tf
import tensorflow_addons as tfa

print("vit_keras", vit_keras.__version__)
print("tensorflow", tf.__version__)
print("tensorflow-addons", tfa.__version__)

As a quick fix in the meantime, I suggest trying tensorflow >= 2.3.0 with a compatible version of tensorflow-addons (see compatibility matrix).

Certainly.
I will try,the information is followed.

  • The full traceback of the error

 File "vit-keras-master/vit_keras/vit.py", line 146, in vit_b16
    representation_size=768 if weights == "imagenet21k" else None,
  File "vit-keras-master/vit_keras/vit.py", line 77, in build_model
    y = layers.AddPositionEmbs(y.shape,name="Transformer/posembed_input")(y)
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 817, in __call__
    self._maybe_build(inputs)
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 2141, in _maybe_build
    self.build(input_shapes)
  File "vit-keras-master/vit_keras/layers.py", line 38, in build
    shape=(1,input_shape[1],input_shape[2])
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/ops/init_ops_v2.py", line 296, in __call__
    dtype)
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/ops/init_ops_v2.py", line 791, in random_normal
    shape=shape, mean=mean, stddev=stddev, dtype=dtype, seed=self.seed)
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/ops/random_ops.py", line 69, in random_normal
    shape_tensor = tensor_util.shape_tensor(shape)
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/framework/tensor_util.py", line 964, in shape_tensor
    return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 1184, in convert_to_tensor
    return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 1242, in convert_to_tensor_v2
    as_ref=False)
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 1296, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/framework/constant_op.py", line 286, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/framework/constant_op.py", line 227, in constant
    allow_broadcast=True)
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/framework/constant_op.py", line 235, in _constant_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "anaconda3/envs/vit3/lib/python3.7/site-packages/tensorflow_core/python/framework/constant_op.py", line 96, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Can't convert Python sequence with mixed types to Tensor.
  • The output of the following code snippet
vit_keras 0.0.0
tensorflow 2.0.0
tensorflow-addons 0.6.0

I haven't been able to reproduce this yet. Could you provide the code that yields the exception (i.e., the arguments you're providing to vit_b16)?

Environment:tensorflow2.0.0 cuda:10.0 cudnn:7.6.4

I just run the demo on the https://github.com/faustomorales/vit-keras, don't change anything.

from vit_keras import vit, utils

image_size = 384
classes = utils.get_imagenet_classes()
model = vit.vit_b16(
    image_size=image_size,
    activation='sigmoid',
    pretrained=True,
    include_top=True,
    pretrained_top=True
)
url = 'https://upload.wikimedia.org/wikipedia/commons/d/d7/Granny_smith_and_cross_section.jpg'
image = utils.read(url, image_size)
X = vit.preprocess_inputs(image).reshape(1, image_size, image_size, 3)
y = model.predict(X)
print(classes[y[0].argmax()]) # Granny smith

The frist error i got is the “tf.initializers.random_normal” ,the module is not found.

So i change the module to "tf.random_normal_initializer".
I got the error  “ValueError: Can't convert Python sequence with mixed types to Tensor. ” as mentioned above.
Also,i try some other modules like "tf.random.normal", but still get the error  “ValueError: Can't convert Python sequence with mixed types to Tensor. ”

But i have found the reason.

In vit.py line 76,the output shape of the 'tf.keras.layers.Reshape((-1, hidden_size))(y)' is 

**[None,None,768]**

after the 'layers.ClassToken(name="class_token")(y)'  line 77, the output is still

**[None,None,768]**

The second element dimension is None,so in the “  y = layers.AddPositionEmbs(name="Transformer/posembed_input")(y)”,
the error “ValueError: Can't convert Python sequence with mixed types to Tensor. ” happens. So i change the 

**tf.keras.layers.Reshape((-1, hidden_size))(y) to y = tf.keras.layers.Reshape((y.shape[1]*y.shape[2], hidden_size))(y)**

And it works

Nice! This fix has been applied as of a59b999 and is in v0.0.10 on PyPi.

I have encountered a problem showing the same error as the below example. I would appreciate any help in this matter.

Summary:

  1. List of Tuple containing (1,2) can be converted to a TensorFlow variable.
  2. List of Tuple containing (3, (6, 9)) can NOT be converted to a TensorFlow variable.

t1 = []
for i in range(10):
... t1.append((i, 2i))
...
t1
[(0, 0), (1, 2), (2, 4), (3, 6), (4, 8), (5, 10), (6, 12), (7, 14), (8, 16), (9, 18)]
t1_tv = tf.Variable(t1, dtype=tf.int32)
t1_tv
<tf.Variable 'Variable:0' shape=(10, 2) dtype=int32, numpy=
array([[ 0, 0],
[ 1, 2],
[ 2, 4],
[ 3, 6],
[ 4, 8],
[ 5, 10],
[ 6, 12],
[ 7, 14],
[ 8, 16],
[ 9, 18]], dtype=int32)>
t2 = []
for i in range(10):
... t2.append((i, (2
i, i**2)))
...
t2
[(0, (0, 0)), (1, (2, 1)), (2, (4, 4)), (3, (6, 9)), (4, (8, 16)), (5, (10, 25)), (6, (12, 36)), (7, (14, 49)), (8, (16, 64)), (9, (18, 81))]
t2_tv = tf.Variable(t2, dtype=tf.int32)
Traceback (most recent call last):
File "", line 1, in
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variables.py", line 268, in call
return cls._variable_v2_call(*args, **kwargs)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variables.py", line 250, in _variable_v2_call
return previous_getter(
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variables.py", line 243, in
previous_getter = lambda **kws: default_variable_creator_v2(None, **kws)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py", line 2662, in default_variable_creator_v2
return resource_variable_ops.ResourceVariable(
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variables.py", line 270, in call
return super(VariableMetaclass, cls).call(*args, **kwargs)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 1602, in init
self._init_from_args(
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 1745, in _init_from_args
initial_value = ops.convert_to_tensor(initial_value,
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/profiler/trace.py", line 163, in wrapped
return func(*args, **kwargs)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1566, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 346, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 271, in constant
return _constant_impl(value, dtype, shape, name, verify_shape=False,
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 283, in _constant_impl
return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 308, in _constant_eager_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 106, in convert_to_eager_tensor
return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Can't convert Python sequence with mixed types to Tensor.

b = [[float('nan'), 2, 3, 0, "s"],
         [0, 0, float('nan'), 4, 5],
         [45, float('nan'), 3, 4, float('nan')]]
    b = tf.strings.as_string(b)
    print(b)

Can't convert Python sequence with mixed types to Tensor.