My code doesn't takes in json as an input if it takes it gets this error

Data dictionary: Tensor(“EagerPyFunc:0”, dtype=string, device=/job:localhost/replica:0/task:0)

TypeError Traceback (most recent call last)
in <cell line: 75>()
73 return json_file, read_annotations(json_file)
74
—> 75 dataset = json_files.map(process_json_file)
76 dataset = dataset.map(lambda x, y: (y, tf.strings.regex_replace(x, annotations_dir, images_dir)))
77 dataset = dataset.map(lambda x, y: (x, read_image(y)))

23 frames
/tmp/autograph_generated_fileug2g849v.py in ()
27 ag
.ld(print)(‘Data dictionary:’, ag__.ld(data_dict))
28 data_dict = ag__.converted_call(ag__.ld(tf).py_function, (ag__.autograph_artifact(lambda x: ag__.converted_call(ag__.ld(json).loads, (ag__.converted_call(ag__.converted_call(ag__.ld(x).numpy, (), None, fscope).decode, (‘utf-8’,), None, fscope),), None, fscope)), [ag__.ld(data)], ag__.ld(tf).string), None, fscope)
—> 29 image_width = ag__.converted_call(ag__.ld(tf).cond, (ag__.converted_call(ag__.ld(tf).size, (ag__.ld(data_dict),), None, fscope) > 0, ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(tf).strings.to_number, (ag__.converted_call(ag__.ld(tf).io.decode_json_example, (ag__.ld(data),), None, fscope)[‘imageWidth’],), None, fscope)), ag__.autogra…
30 image_height = ag__.converted_call(ag__.ld(tf).cond, (ag__.converted_call(ag__.ld(tf).size, (ag__.ld(data_dict),), None, fscope) > 0, ag__.autograph_artifact(lambda : ag__.converted_call(ag__.ld(tf).strings.to_number, (ag__.converted_call(ag__.ld(tf).io.decode_json_example, (ag__.ld(data),), None, fscope)[‘imageHeight’],), None, fscope)), ag__.auto…
31 shapes = ag__.converted_call(ag__.ld(parse_shapes), (ag_.ld(data),), None, fscope)

TypeError: in user code:

File "<ipython-input-4-e27d038179b2>", line 73, in process_json_file  *
    return json_file, read_annotations(json_file)
File "<ipython-input-6-1b7b1ee735b2>", line 23, in read_annotations  *
    image_width = tf.cond(tf.size(data_dict) > 0,

TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got 'imageWidth'

It seems like there’s a TypeError caused by trying to perform operations with incompatible data types in a TensorFlow function. This can happen when accessing or manipulating TensorFlow data structures incorrectly, such as trying to index with non-integers or perform operations with unexpected types.

To fix this, check:

  • Whether data_dict is being converted to the expected type before using it in tf.size.
  • If tf.cond logic is implemented correctly, ensuring the conditions and returned results are valid.
  • Ensure that all operations involving TensorFlow data structures or tensors are handled appropriately.

Review the data types and shapes at each step to identify where the type mismatch occurs. Modified by moderator