0

I'm trying to use tf.data.experimental.TFRecordWriter to save dataset on Google cloud bucket using TPU. The code from the example in documentation works:

dataset = tf.data.Dataset.range(3)
dataset = dataset.map(tf.io.serialize_tensor)
writer = tf.data.experimental.TFRecordWriter("gs://oleg-zyablov/test.tfrec")
writer.write(dataset)

But I have dataset of tuples (string, int64), where first is jpg-encoded image and second is label. When I pass it to writer.write() method, it says: 'tuple' object has no attribute 'is_compatible_with'.

I guess I have to pack image and label into tf.train.Example to make it work. I use the following code:

def serialize(image, class_idx):
  tfrecord = tf.train.Example(features = tf.train.Features(feature = {
    'image': tf.train.Feature(bytes_list = tf.train.BytesList(value = [image.numpy()])),
    'class': tf.train.Feature(int64_list = tf.train.Int64List(value = [class_idx.numpy()]))
  }))
  return tfrecord.SerializeToString()

#saving_pipeline is a dataset of (string, int64) tuples
saving_pipeline_serialized = saving_pipeline.map(serialize)

writer = tf.data.experimental.TFRecordWriter("gs://oleg-zyablov/car-classification/train_tfrecords/test.tfrecord")
writer.write(saving_pipeline_serialized)

But I get the following error:

'Tensor' object has no attribute 'numpy'

Although I didn't turn off eager mode and this code tf.constant([], dtype = float).numpy() works. Maybe TPU works not in eager mode? Ok, I changed .numpy() to .eval() in the code above. Then I get the foloowing error:

Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`

What session does TPU use and how do I specify it? When I run the code below:

with tf.compat.v1.Session():
  saving_pipeline_serialized = saving_pipeline.map(serialize)

I get an error:

Cannot use the default session to evaluate tensor: the tensor's graph is different from the session's graph. Pass an explicit session to `eval(session=sess)`.

But I don't know how to get the current graph and pass it to tf.compat.v1.Session(). When I go another way and type:

image.eval(session = tf.compat.v1.get_default_session())

It says:

Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`

Is it possible to use .eval() on TPU? Or how do I perform my task another way?

Bob Smith
  • 36,107
  • 11
  • 98
  • 91
Oleg S
  • 11
  • 1
  • I am also trying to get this to work with some spectrogram data. Tensorflow is particularly impossible to figure out with TPU. Very frustrating – Nikki Apr 14 '21 at 19:39
  • .numpy() is supposed to work on TF 2.4. Maybe try tf.compat.v1.enable_eager_execution() according to this thread https://stackoverflow.com/questions/34097281/convert-a-tensor-to-numpy-array-in-tensorflow – Nikki Apr 14 '21 at 19:42

0 Answers0