0

my method i thought that the problem from it is

 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 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}}]]

Edit

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(np.array(photo).astype(np.float32))
            X2.append(np.array(in_seq).astype(np.float32))
            y.append(np.array(out_seq).astype(np.float32))
    return array(X1), array(X2), array(y)
5
  • try to print the shape of the photo, in_seq, out_seq: print (photo.shape, photo.dtype) Commented Jun 26, 2021 at 13:02
  • i got X1.append(np.array(photo).astype(np.float32)) TypeError: float() argument must be a string or a number, not 'dict' Commented Jun 27, 2021 at 20:12
  • What is the output of this? print(type(photo)) and tell me the shape Commented Jun 27, 2021 at 21:09
  • i got this <class 'numpy.ndarray'> (1000,) Commented Jun 27, 2021 at 21:33
  • The photo variable is a dictionary. Add your code where you get this variable and also some example data of this value. Commented Jun 28, 2021 at 10:54

1 Answer 1

0

This error indicates some values or all values in your data does not have a valid data type to convert.


Reason:

Common reason for this error is that values of the array are not of given dtype in graph mode. It may be because some values are NaN or None or all values are in a format which is not supported to convert to a tensor, such as a python dictionary.


Solution:

This issue would be fixed by converting the data to the expected dtype, for example applying methods such as x=np.asarray(x).astype(np.float32) to the input data before feeding to the model. It also supports the NaN values issue. Note that it's better to do some preprocessing to None data values, and replace them with methods like DataFrame.fillna().

But in scenarios where your data type is something unsupported (like python dictionary) you can not solve the problem with the above approach. You should change the data structure then feed it to the model. Note that even np.asarray can not change the structure. You may get the type of data numpy array, but the structure remains the same and can not be handled by network. So don't get the type as an evidence for not being dict. Here is some example:

x = {1:1,2:2,3:3,4:4,5:5}
print(type(x))      #<class 'dict'>
x = np.asarray(x)
print(type(x))      #<class 'numpy.ndarray'> #the type is changed
print(x)            #{1: 1, 2: 2, 3: 3, 4: 4, 5: 5} #the structure has not been changed
    

Your Scenario:

As your code reflects, you converted the data to the numpy array and float. So, even you have None variables, you won't get error. So, as the error reflects (Unsupported object type dict), one of your input variables ([in_img, in_seq], out_word) is a dictionary. Based on your code, in_seq and out_seq are lists. So, it should be in_img which initiate from photo variable. So, check this variable data. It is likely holds a dictionary like data. Don't pay attention to the type (print(type(photo))), because as my above code, it could be a numpy.ndarray, but hold a dictionary data.

Sign up to request clarification or add additional context in comments.

2 Comments

You have said in the comments that X1.append(np.array(photo).astype(np.float32)) TypeError: float() argument must be a string or a number, not 'dict'.how did you solve it?
i didn't solve it .. i just print the data that those parameters holds .. but the basic error still

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.