1

I have the following code:

def map_per_image(class_index_array):
    def index_to_label_f(index):
        index_to_label = {0:label_Vehicles, 1:label_Roads, 2:label_None}
        #print(index.eval())
        label = index_to_label[index]
        return label
    translated = tf.map_fn(index_to_label_f, class_index_array)
    return translated

Outside of Tensorflow environment, using ordinary map it would have worked. But the value of the argument class_index_array is a tensor, thus is its element, index also tensor.

I need somehow to convert the tensor index to an int value, in order to look up the dictionary. I tried index.eval() but got an error:

ValueError: Operation 'map_8/while/map/while/TensorArrayReadV3' has been marked as not fetchable.

Without such conversion, I would get a different error due to using a tensor value to look up the dictionary:

KeyError: <tf.Tensor 'map_9/while/map/while/TensorArrayReadV3:0' shape=() dtype=int32>

I wonder what's the proper way to express my above implementation logic in Tensorflow execution (session) environment?

Yu Shen
  • 2,770
  • 3
  • 33
  • 48
  • 2
    This might be useful: https://stackoverflow.com/questions/35316250/tensorflow-dictionary-lookup-with-string-tensor – Elliot Roberts May 31 '18 at 21:01
  • Thanks for the pointer! Yes, it solved my problem. I had to think and code in tensor not iterating over array elements. – Yu Shen May 31 '18 at 23:52

0 Answers0