'Loading Custom Image Dataset with TFX as TFRecord

I'm trying to use TFX on a custom dataset [Images/labels] So I'm using ImportExampleGen with .TFRecord file as follows

example_gen = ImportExampleGen(input_base=TFRecord_DIR_PATH)

context.run(example_gen)

artifact = example_gen.outputs['examples'].get()[0]

I got IndexError: list index out of range because example_gen.outputs['examples'].get() is []

Here is the Images to TFRecord code

...
for idx, d in enumerate(str_labels) # LABELS IS DIR NAME (STR):
    imgs = glob.glob(f"..\\PATH\\*.*g")
    str2int[d] = idx
    for img_path in tqdm.tqdm(imgs):
        image = cv2.imread(img_path)[:,:,::-1]
        all_imgs.append(cv2.resize(image, (144, 96)))
        labels.append(idx)

all_imgs = np.array(all_imgs)
labels = np.array(labels)[...,np.newaxis]

with tf.io.TFRecordWriter("TFRecord_DIR_PATH/NAME.tfrecord") as tfrecord:
  for lbl, img in zip(labels, all_imgs):
    label = lbl
    feature = tf.io.serialize_tensor(img)
    features = {
      "label" : tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),
      "feature" : tf.train.Feature(bytes_list=tf.train.BytesList(value=[feature.numpy()]))
    }
    example = tf.train.Example(features=tf.train.Features(feature=features))
    tfrecord.write(example.SerializeToString())

I'm using TF==2.7.1 and TFX==1.6.0

I can't find the mistake so I wish you can. Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source