ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type dict)

history = model.fit_generator(train_generator, epochs=epochs, steps_per_epoch=train_steps, verbose=1, callbacks=[checkpoint], validation_data=val_generator, validation_steps=val_steps)

def create_sequences(tokenizer, max_length, desc_list, photo, vocab_size):
X1, X2, y = list(), list(), list()
for desc in desc_list:
seq = tokenizer.texts_to_sequences([desc])[0]
for i in range(1, len(seq)):
in_seq, out_seq = seq[:i], seq[i]
in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
X1.append(photo)
X2.append(in_seq)
y.append(out_seq)
return array(X1), array(X2), array(y)

def data_generator(descriptions, photos, tokenizer, max_length, imgsIds, vocab_size):
while 1:
for ind in range(len(imgsIds)):
photo = photos[ind]
key = imgsIds[ind]
desc_list = descriptions[str(key)]
in_img, in_seq, out_word = create_sequences(
tokenizer, max_length, desc_list, photo, vocab_size)
yield [in_img, in_seq], out_word

i got

Failed to convert a NumPy array to a Tensor (Unsupported object type dict).

if there is anything i should add it please comment … Thanks

Traceback (most recent call last):
  File "fit.py", line 271, in <module>
    main(sys.argv)
  File "fit.py", line 268, in main
    fit_model(train, train_descriptions, train_rnn_input, val, val_descriptions, val_rnn_input)
  File "fit.py", line 255, in fit_model
    history = model.fit_generator(train_generator, epochs=epochs, steps_per_epoch=train_steps, verbose=1, callbacks=[checkpoint], validation_data=val_generator, validation_steps=val_steps)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1479, in fit_generator
    initial_epoch=initial_epoch)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 872, in fit
    return_dict=True)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1057, in evaluate
    model=self)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1112, in __init__
    model=model)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 775, in __init__
    peek = _process_tensorlike(peek)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1013, in _process_tensorlike
    inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/util/nest.py", line 617, in map_structure
    structure[0], [func(*x) for x in entries],
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/util/nest.py", line 617, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1008, in _convert_numpy_and_scipy
    return ops.convert_to_tensor(x, dtype=dtype)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1341, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 262, in constant
    allow_broadcast=True)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 270, in _constant_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 96, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type dict).
2021-06-27 04:46:22.936001: W tensorflow/core/kernels/data/generator_dataset_op.cc:103] Error occurred when finalizing GeneratorDataset iterator: Failed precondition: Python interpreter state is not initialized. The process may be terminated.
	 [[{{node PyFunc}}]]

is there any help please ?

1 Like

Isn’t there any dict() data in your dataset?

1 Like

Dict is descriptions in method data generator

1 Like

Why don’t you check the type of X1, X2, y?
As the error message, I think tensorflow tried to convert a numpy array which is dict() in fact to tensor, so failed converting.
If you don’t make any function array() besides, how about using numpy.array()?

1 Like

The parameter ‘photo’ contains the characteristics vector and photo’s detections then i walk through each description for the image
for desc in desc_list:
# encode the sequence
seq = tokenizer.texts_to_sequences([desc])[0]
# split one sequence into multiple X,y pairs
for i in range(1, len(seq)):
# split into input and output pair
in_seq, out_seq = seq[:i], seq[i]
# pad input sequence
in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
# encode output sequence
out_seq = to_categorical(…)
This what iam doing … is there any wrong in types here or you need print(type) for each one ?

Excuse me I didn’t get what do you mean by numpy.array ()

I tried to handle photo by
X1.append(nd.array(photo)) but got

Value error : failed to convert numpy array to tensor (unsupported object type numpy.ndarray)

i check now data in X1 like

array([9.88089450e-05, 2.25199750e-04, 7.83673313e-05, 1.24953804e-04,
4.95471577e-05, … ,
dtype=float32)]

X2 like

[array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1], dtype=int32)

y like

[array([0., 0., 0., …, 0., 0., 0.], dtype=float32)

How about changing

return array(X1), array(X2), array(y)

to

return numpy.asarray(X1), numpy.asarray(X2), numpy.asarray(y)

Does it still cause the same error?

thanks for replying, i tried it the same error exists