Convert TensorFlow string to python string












10















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'xffxd8xffxe0x00x10JFIFx00x01x01x00x00x01x00x01x00x00xffxdbx00Cx00x08x06x06x07x06x05x08x07x07x07ttx08'


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)









share|improve this question

























  • If you'd like to work with the string in Python, you need to execute the TensorFlow graph first.

    – Allen Lavoie
    Feb 9 '17 at 20:53






  • 1





    As you can see I have executed the graph inside a session.

    – Ujjwal
    Feb 9 '17 at 20:59











  • Your second approach is fine (decode_raw), you just need to evaluate the Tensor first. Although I have a feeling the reason you're not getting the result you want in the first approach is that this is binary data rather than a sensible filename.

    – Allen Lavoie
    Feb 9 '17 at 21:05











  • The tensor has been evaluated using eval() . After that when I use decode_raw, I get the error as stated in the question. As to the validity of the data, it is valid since the tf.train.string_input_producer() has been fed using a list of python strings ( which are valid filenames).

    – Ujjwal
    Feb 9 '17 at 21:36






  • 1





    Hi, @Ujjwal, have you ever solved this problem? I'm looking for the solution. Thanks.

    – mining
    Jun 13 '17 at 22:12
















10















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'xffxd8xffxe0x00x10JFIFx00x01x01x00x00x01x00x01x00x00xffxdbx00Cx00x08x06x06x07x06x05x08x07x07x07ttx08'


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)









share|improve this question

























  • If you'd like to work with the string in Python, you need to execute the TensorFlow graph first.

    – Allen Lavoie
    Feb 9 '17 at 20:53






  • 1





    As you can see I have executed the graph inside a session.

    – Ujjwal
    Feb 9 '17 at 20:59











  • Your second approach is fine (decode_raw), you just need to evaluate the Tensor first. Although I have a feeling the reason you're not getting the result you want in the first approach is that this is binary data rather than a sensible filename.

    – Allen Lavoie
    Feb 9 '17 at 21:05











  • The tensor has been evaluated using eval() . After that when I use decode_raw, I get the error as stated in the question. As to the validity of the data, it is valid since the tf.train.string_input_producer() has been fed using a list of python strings ( which are valid filenames).

    – Ujjwal
    Feb 9 '17 at 21:36






  • 1





    Hi, @Ujjwal, have you ever solved this problem? I'm looking for the solution. Thanks.

    – mining
    Jun 13 '17 at 22:12














10












10








10


2






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'xffxd8xffxe0x00x10JFIFx00x01x01x00x00x01x00x01x00x00xffxdbx00Cx00x08x06x06x07x06x05x08x07x07x07ttx08'


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)









share|improve this question
















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'xffxd8xffxe0x00x10JFIFx00x01x01x00x00x01x00x01x00x00xffxdbx00Cx00x08x06x06x07x06x05x08x07x07x07ttx08'


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)






python tensorflow






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 19:09









Giovan Cruz

134114




134114










asked Feb 9 '17 at 19:07









UjjwalUjjwal

889626




889626













  • If you'd like to work with the string in Python, you need to execute the TensorFlow graph first.

    – Allen Lavoie
    Feb 9 '17 at 20:53






  • 1





    As you can see I have executed the graph inside a session.

    – Ujjwal
    Feb 9 '17 at 20:59











  • Your second approach is fine (decode_raw), you just need to evaluate the Tensor first. Although I have a feeling the reason you're not getting the result you want in the first approach is that this is binary data rather than a sensible filename.

    – Allen Lavoie
    Feb 9 '17 at 21:05











  • The tensor has been evaluated using eval() . After that when I use decode_raw, I get the error as stated in the question. As to the validity of the data, it is valid since the tf.train.string_input_producer() has been fed using a list of python strings ( which are valid filenames).

    – Ujjwal
    Feb 9 '17 at 21:36






  • 1





    Hi, @Ujjwal, have you ever solved this problem? I'm looking for the solution. Thanks.

    – mining
    Jun 13 '17 at 22:12



















  • If you'd like to work with the string in Python, you need to execute the TensorFlow graph first.

    – Allen Lavoie
    Feb 9 '17 at 20:53






  • 1





    As you can see I have executed the graph inside a session.

    – Ujjwal
    Feb 9 '17 at 20:59











  • Your second approach is fine (decode_raw), you just need to evaluate the Tensor first. Although I have a feeling the reason you're not getting the result you want in the first approach is that this is binary data rather than a sensible filename.

    – Allen Lavoie
    Feb 9 '17 at 21:05











  • The tensor has been evaluated using eval() . After that when I use decode_raw, I get the error as stated in the question. As to the validity of the data, it is valid since the tf.train.string_input_producer() has been fed using a list of python strings ( which are valid filenames).

    – Ujjwal
    Feb 9 '17 at 21:36






  • 1





    Hi, @Ujjwal, have you ever solved this problem? I'm looking for the solution. Thanks.

    – mining
    Jun 13 '17 at 22:12

















If you'd like to work with the string in Python, you need to execute the TensorFlow graph first.

– Allen Lavoie
Feb 9 '17 at 20:53





If you'd like to work with the string in Python, you need to execute the TensorFlow graph first.

– Allen Lavoie
Feb 9 '17 at 20:53




1




1





As you can see I have executed the graph inside a session.

– Ujjwal
Feb 9 '17 at 20:59





As you can see I have executed the graph inside a session.

– Ujjwal
Feb 9 '17 at 20:59













Your second approach is fine (decode_raw), you just need to evaluate the Tensor first. Although I have a feeling the reason you're not getting the result you want in the first approach is that this is binary data rather than a sensible filename.

– Allen Lavoie
Feb 9 '17 at 21:05





Your second approach is fine (decode_raw), you just need to evaluate the Tensor first. Although I have a feeling the reason you're not getting the result you want in the first approach is that this is binary data rather than a sensible filename.

– Allen Lavoie
Feb 9 '17 at 21:05













The tensor has been evaluated using eval() . After that when I use decode_raw, I get the error as stated in the question. As to the validity of the data, it is valid since the tf.train.string_input_producer() has been fed using a list of python strings ( which are valid filenames).

– Ujjwal
Feb 9 '17 at 21:36





The tensor has been evaluated using eval() . After that when I use decode_raw, I get the error as stated in the question. As to the validity of the data, it is valid since the tf.train.string_input_producer() has been fed using a list of python strings ( which are valid filenames).

– Ujjwal
Feb 9 '17 at 21:36




1




1





Hi, @Ujjwal, have you ever solved this problem? I'm looking for the solution. Thanks.

– mining
Jun 13 '17 at 22:12





Hi, @Ujjwal, have you ever solved this problem? I'm looking for the solution. Thanks.

– mining
Jun 13 '17 at 22:12












3 Answers
3






active

oldest

votes


















0














key, value = reader.read(filename_queue)


In this, the Reader just read the file you give, so value is the content of the file, not the filename, but you can output key, then you get filename






share|improve this answer































    0














    If I understand your issue correctly you can try:



    print(filename.decode())





    share|improve this answer































      0














      Use the as_text function in compat (from tensorflow.python.util) to convert the byte string of tensorflow. I.e.



      filename = compat.as_text(filename)






      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42144915%2fconvert-tensorflow-string-to-python-string%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        key, value = reader.read(filename_queue)


        In this, the Reader just read the file you give, so value is the content of the file, not the filename, but you can output key, then you get filename






        share|improve this answer




























          0














          key, value = reader.read(filename_queue)


          In this, the Reader just read the file you give, so value is the content of the file, not the filename, but you can output key, then you get filename






          share|improve this answer


























            0












            0








            0







            key, value = reader.read(filename_queue)


            In this, the Reader just read the file you give, so value is the content of the file, not the filename, but you can output key, then you get filename






            share|improve this answer













            key, value = reader.read(filename_queue)


            In this, the Reader just read the file you give, so value is the content of the file, not the filename, but you can output key, then you get filename







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 24 '18 at 12:18









            mxlmxl

            366




            366

























                0














                If I understand your issue correctly you can try:



                print(filename.decode())





                share|improve this answer




























                  0














                  If I understand your issue correctly you can try:



                  print(filename.decode())





                  share|improve this answer


























                    0












                    0








                    0







                    If I understand your issue correctly you can try:



                    print(filename.decode())





                    share|improve this answer













                    If I understand your issue correctly you can try:



                    print(filename.decode())






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 '18 at 16:37









                    Ohad MeirOhad Meir

                    350213




                    350213























                        0














                        Use the as_text function in compat (from tensorflow.python.util) to convert the byte string of tensorflow. I.e.



                        filename = compat.as_text(filename)






                        share|improve this answer




























                          0














                          Use the as_text function in compat (from tensorflow.python.util) to convert the byte string of tensorflow. I.e.



                          filename = compat.as_text(filename)






                          share|improve this answer


























                            0












                            0








                            0







                            Use the as_text function in compat (from tensorflow.python.util) to convert the byte string of tensorflow. I.e.



                            filename = compat.as_text(filename)






                            share|improve this answer













                            Use the as_text function in compat (from tensorflow.python.util) to convert the byte string of tensorflow. I.e.



                            filename = compat.as_text(filename)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 5 at 7:58









                            wontleavewontleave

                            536




                            536






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42144915%2fconvert-tensorflow-string-to-python-string%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Guess what letter conforming each word

                                Port of Spain

                                Run scheduled task as local user group (not BUILTIN)