I am writing a mapping function for a dataset in Tensorflow 2. The dataset contains several images and the corresponding labels, more specifically there are only three possible values for the labels, 13, 17 and 34. The mapping function is supposed to take the labels and convert them into categorical labels.
There might be better ways to implement this function (please feel free to suggest), but this is my implementation:
def map_labels(dataset):
def convert_labels_to_categorical(image, labels):
labels = [1.0, 0., 0.] if labels == 13 else [0., 1.0, 0.] if labels == 17 else [0., 0., 1.0]
return image, labels
categorical_dataset = dataset.map(convert_labels_to_categorical)
return categorical_dataset
The main issue is that I get the error below:
OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed: AutoGraph is disabled in this function. Try decorating it directly with @tf.function.
I really have no idea about what this error means, and there are not so many other sources on the internet documenting the same error. Any idea?
EDIT (new non-working implementation):
def map_labels(dataset):
def convert_labels_to_categorical(image, labels):
labels = tf.Variable([1.0, 0., 0.]) if tf.reduce_any(tf.math.equal(labels, tf.constant(0,dtype=tf.int64))) \
else tf.Variable([0., 1.0, 0.]) if tf.reduce_any(tf.math.equal(labels, tf.constant(90,dtype=tf.int64))) \
else tf.Variable([0., 0., 1.0])
return image, labels
categorical_dataset = dataset.map(convert_labels_to_categorical)
return categorical_dataset