Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert TensorFlow string to python string

Tags:

I am aware that in TensorFlow, a tf.string tensor is basically a byte string. I need to do some operation with a filename which is stored in a queue using tf.train.string_input_producer().

A small snippet is shown below :

 key, value = reader.read(filename_queue)  filename = value.eval(session=sess)  print(filename) 

However as a byte string it gives an output like the following :

b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08' 

I tried to convert using

filename = tf.decode_raw(filename, tf.uint8) filename = ''.join(chr(i) for i in filename) 

However Tensor objects are not iterable and hence this fails.

Where am I going wrong ?

Is it a missing feature in TensorFlow that tf.string be converted to a Python string easily , or is there some other feature I am not aware about ?

More Info

The filename_queue has been prepared as follows :

train_set = ['file1.jpg', 'file2.jpg'] # Truncated for illustration filename_queue = tf.train.string_input_producer(train_set, num_epochs=10, seed=0, capacity=1000)                   
like image 379
Ujjwal Avatar asked Feb 09 '17 19:02

Ujjwal


People also ask

Can tensor be string?

# Tensors can be strings, too here is a scalar string. # If you have three string tensors of different lengths, this is OK. # Note that the shape is (3,). The string length is not included.


Video Answer


1 Answers

In tensorflow 2.0.0, it can be done in the following way:

import tensorflow as tf  my_str = tf.constant('Hello World') my_str_npy = my_str.numpy()  print(my_str_npy) type(my_str_npy) 

This converts a string tensor into a string of 'bytes' class

like image 128
Aalok_G Avatar answered Sep 20 '22 08:09

Aalok_G