I'm trying to parse directories on Google Storage into strings, but I keep getting errors. I want to find the directory of each file and return a numerical encoding of the directory name as a Dataset. This would be trivial in sklearn using LabelEncoder, but I'm having trouble doing this in Tensorflow.
CLASS_NAMES = [b'class_1', b'class_2', b'class_3']
labeler = tfds.features.ClassLabel(names=CLASS_NAMES)
def parse_filenames(filename):
label = tf.strings.split(tf.expand_dims(filename, axis=-1), sep='/')
label = label.values[-2]
# Problem is in the two lines below
position_feature = tf.feature_column.categorical_column_with_vocabulary_list('label_names', CLASS_NAMES)
label = tf.io.parse_example(label, features=position_feature)
return label
folder = b'gs://<bucket>/train/*/*.jpg'
filenames_dataset = tf.data.Dataset.list_files(folder)
label_dataset = filenames_dataset.map(parse_filenames)
next(iter(label_dataset))
I get an error ValueError: dictionary update sequence element #0 has length 16; 2 is required
If I take out the two lines under the "# Problem is here" comment, it works fine, except it returns a string instead of an integer. I've tried other non-tensorflow options, such as <list_name>.index(label), but those of course fail because everything is a tensor instead of a string. Is there another way to do this?