Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a string tensor to a python string in Tensorflow?

Tags:

tensorflow

The following code is a batch data provider for .mat files, but has the following problem when running it:

    TypeError: expected str, bytes or os.PathLike object, not FIFOQueue

The code is:

    import numpy as np
    import tensorflow as tf
    from tensorflow.python.framework import ops
    from tensorflow.python.framework import dtypes
    import h5py

    def Reader(filename):
        with h5py.File(filename, 'r') as f:
            image = np.transpose(np.array(f.get('patch_x'), dtype=np.float32))
            label = np.transpose(np.array(f.get('patch_y'), dtype=np.float32))
        image = ops.convert_to_tensor(image, dtype=dtypes.float32)
        label = ops.convert_to_tensor(label, dtype=dtypes.float32)

        return image, label

    def Inputs(filenames, batch_size, shuffle=True):
        filenames = ops.convert_to_tensor(filenames, dtype=dtypes.string)
        filename_queue = tf.train.string_input_producer(filenames, shuffle=shuffle)
        image, label = Reader(filename_queue)
        image = tf.cast(image, tf.float32)
        label = tf.cast(label, tf.float32)

        num_preprocess_threads = 4
        if shuffle:
            image_batch, label_batch = tf.train.shuffle_batch([image, label], batch_size=batch_size, num_threads=num_preprocess_threads, capacity=5*batch_size, min_after_dequeue=2*batch_size)
        else:
            image_batch, label_batch = tf.train.batch([image, label], batch_size=batch_size, num_threads=num_preprocess_threads, capacity=5*batch_size, min_after_dequeue=2*batch_size)
        return image_batch, label_batch

Does anyone know how to convert a string tensor to a python string easily? Thanks.

UPDATED 1: when using the filename.dequeue(), the error information is:

    TypeError: expected str, bytes or os.PathLike object, not Tensor
like image 679
mining Avatar asked Oct 21 '25 16:10

mining


1 Answers

To convert a string TensorFlow tensor to Python string, run bytes.decode(string_tensor.numpy()).

like image 103
Filip Bártek Avatar answered Oct 24 '25 09:10

Filip Bártek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!