PNG TO TFRecord: error : has type numpy.ndarray, but expected one of: bytes












0















I'm doing a .png file to tfrecord.



def _bytes_feature(value):
"""Wrapper for inserting bytes features into Example proto."""
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


convert to example file:



def _convert_to_example(filename, image_buffer, label, text, height, width):
example = tf.train.Example(features=tf.train.Features(feature={
'image/height': _int64_feature(height),
'image/width': _int64_feature(width),
........
'image/encoded': _bytes_feature(image_buffer)})) #Error
return example


decode_png file, not sure this part



def decode_png(image_data):

img_str = image_data.tostring()
reconstructed_img_1d = np.fromstring(img_str, dtype=np.uint8)
reconstructed_img = reconstructed_img_1d.reshape(image_data.shape)

return reconstructed_img


process_image file:



def _process_image(filename):

# Read the image file.
with open(filename, 'r') as f:
image_data = io.imread(f)

# Decode the RGB PNG.
image = decode_png(image_data)

# Check that image converted to RGB
assert len(image.shape) == 3
height = image.shape[0]
width = image.shape[1]
assert image.shape[2] == 3

return image_data, height, width


Main, process image files batch:



for i in files_in_shard:
filename = filenames[i]
label = labels[i]
text = texts[i]

image_buffer, height, width = _process_image(filename)

example = _convert_to_example(filename, image_buffer, label,
text, height, width)
writer.write(example.SerializeToString())
shard_counter += 1
counter += 1


But, there is a error that TypeError: array([[[223, 198, 219],
[215, 185, 209],
[207, 174, 201],
...,
[230 has type numpy.ndarray, but expected one of: bytes



How should I deal with this? Any help would be great.
Thank you










share|improve this question



























    0















    I'm doing a .png file to tfrecord.



    def _bytes_feature(value):
    """Wrapper for inserting bytes features into Example proto."""
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


    convert to example file:



    def _convert_to_example(filename, image_buffer, label, text, height, width):
    example = tf.train.Example(features=tf.train.Features(feature={
    'image/height': _int64_feature(height),
    'image/width': _int64_feature(width),
    ........
    'image/encoded': _bytes_feature(image_buffer)})) #Error
    return example


    decode_png file, not sure this part



    def decode_png(image_data):

    img_str = image_data.tostring()
    reconstructed_img_1d = np.fromstring(img_str, dtype=np.uint8)
    reconstructed_img = reconstructed_img_1d.reshape(image_data.shape)

    return reconstructed_img


    process_image file:



    def _process_image(filename):

    # Read the image file.
    with open(filename, 'r') as f:
    image_data = io.imread(f)

    # Decode the RGB PNG.
    image = decode_png(image_data)

    # Check that image converted to RGB
    assert len(image.shape) == 3
    height = image.shape[0]
    width = image.shape[1]
    assert image.shape[2] == 3

    return image_data, height, width


    Main, process image files batch:



    for i in files_in_shard:
    filename = filenames[i]
    label = labels[i]
    text = texts[i]

    image_buffer, height, width = _process_image(filename)

    example = _convert_to_example(filename, image_buffer, label,
    text, height, width)
    writer.write(example.SerializeToString())
    shard_counter += 1
    counter += 1


    But, there is a error that TypeError: array([[[223, 198, 219],
    [215, 185, 209],
    [207, 174, 201],
    ...,
    [230 has type numpy.ndarray, but expected one of: bytes



    How should I deal with this? Any help would be great.
    Thank you










    share|improve this question

























      0












      0








      0








      I'm doing a .png file to tfrecord.



      def _bytes_feature(value):
      """Wrapper for inserting bytes features into Example proto."""
      return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


      convert to example file:



      def _convert_to_example(filename, image_buffer, label, text, height, width):
      example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': _int64_feature(height),
      'image/width': _int64_feature(width),
      ........
      'image/encoded': _bytes_feature(image_buffer)})) #Error
      return example


      decode_png file, not sure this part



      def decode_png(image_data):

      img_str = image_data.tostring()
      reconstructed_img_1d = np.fromstring(img_str, dtype=np.uint8)
      reconstructed_img = reconstructed_img_1d.reshape(image_data.shape)

      return reconstructed_img


      process_image file:



      def _process_image(filename):

      # Read the image file.
      with open(filename, 'r') as f:
      image_data = io.imread(f)

      # Decode the RGB PNG.
      image = decode_png(image_data)

      # Check that image converted to RGB
      assert len(image.shape) == 3
      height = image.shape[0]
      width = image.shape[1]
      assert image.shape[2] == 3

      return image_data, height, width


      Main, process image files batch:



      for i in files_in_shard:
      filename = filenames[i]
      label = labels[i]
      text = texts[i]

      image_buffer, height, width = _process_image(filename)

      example = _convert_to_example(filename, image_buffer, label,
      text, height, width)
      writer.write(example.SerializeToString())
      shard_counter += 1
      counter += 1


      But, there is a error that TypeError: array([[[223, 198, 219],
      [215, 185, 209],
      [207, 174, 201],
      ...,
      [230 has type numpy.ndarray, but expected one of: bytes



      How should I deal with this? Any help would be great.
      Thank you










      share|improve this question














      I'm doing a .png file to tfrecord.



      def _bytes_feature(value):
      """Wrapper for inserting bytes features into Example proto."""
      return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


      convert to example file:



      def _convert_to_example(filename, image_buffer, label, text, height, width):
      example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': _int64_feature(height),
      'image/width': _int64_feature(width),
      ........
      'image/encoded': _bytes_feature(image_buffer)})) #Error
      return example


      decode_png file, not sure this part



      def decode_png(image_data):

      img_str = image_data.tostring()
      reconstructed_img_1d = np.fromstring(img_str, dtype=np.uint8)
      reconstructed_img = reconstructed_img_1d.reshape(image_data.shape)

      return reconstructed_img


      process_image file:



      def _process_image(filename):

      # Read the image file.
      with open(filename, 'r') as f:
      image_data = io.imread(f)

      # Decode the RGB PNG.
      image = decode_png(image_data)

      # Check that image converted to RGB
      assert len(image.shape) == 3
      height = image.shape[0]
      width = image.shape[1]
      assert image.shape[2] == 3

      return image_data, height, width


      Main, process image files batch:



      for i in files_in_shard:
      filename = filenames[i]
      label = labels[i]
      text = texts[i]

      image_buffer, height, width = _process_image(filename)

      example = _convert_to_example(filename, image_buffer, label,
      text, height, width)
      writer.write(example.SerializeToString())
      shard_counter += 1
      counter += 1


      But, there is a error that TypeError: array([[[223, 198, 219],
      [215, 185, 209],
      [207, 174, 201],
      ...,
      [230 has type numpy.ndarray, but expected one of: bytes



      How should I deal with this? Any help would be great.
      Thank you







      python tensorflow png tfrecord






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 20:36









      ZhuoZhuo

      2516




      2516
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Ok I got it... logic pro



          def decode_png(image_data):

          img_str = image_data.tostring()
          reconstructed_img_1d = np.fromstring(img_str, dtype=np.uint8)
          reconstructed_img = reconstructed_img_1d.reshape(image_data.shape)

          return img_str


          def _process_image(filename):
          # Read the image file.
          with open(filename, 'r') as f:
          image_data = io.imread(f)

          # Decode the RGB PNG.
          img_str = image_data.tostring()

          height = image_data.shape[0]
          width = image_data.shape[1]

          return img_str, height, width





          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%2f53382268%2fpng-to-tfrecord-error-has-type-numpy-ndarray-but-expected-one-of-bytes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Ok I got it... logic pro



            def decode_png(image_data):

            img_str = image_data.tostring()
            reconstructed_img_1d = np.fromstring(img_str, dtype=np.uint8)
            reconstructed_img = reconstructed_img_1d.reshape(image_data.shape)

            return img_str


            def _process_image(filename):
            # Read the image file.
            with open(filename, 'r') as f:
            image_data = io.imread(f)

            # Decode the RGB PNG.
            img_str = image_data.tostring()

            height = image_data.shape[0]
            width = image_data.shape[1]

            return img_str, height, width





            share|improve this answer




























              0














              Ok I got it... logic pro



              def decode_png(image_data):

              img_str = image_data.tostring()
              reconstructed_img_1d = np.fromstring(img_str, dtype=np.uint8)
              reconstructed_img = reconstructed_img_1d.reshape(image_data.shape)

              return img_str


              def _process_image(filename):
              # Read the image file.
              with open(filename, 'r') as f:
              image_data = io.imread(f)

              # Decode the RGB PNG.
              img_str = image_data.tostring()

              height = image_data.shape[0]
              width = image_data.shape[1]

              return img_str, height, width





              share|improve this answer


























                0












                0








                0







                Ok I got it... logic pro



                def decode_png(image_data):

                img_str = image_data.tostring()
                reconstructed_img_1d = np.fromstring(img_str, dtype=np.uint8)
                reconstructed_img = reconstructed_img_1d.reshape(image_data.shape)

                return img_str


                def _process_image(filename):
                # Read the image file.
                with open(filename, 'r') as f:
                image_data = io.imread(f)

                # Decode the RGB PNG.
                img_str = image_data.tostring()

                height = image_data.shape[0]
                width = image_data.shape[1]

                return img_str, height, width





                share|improve this answer













                Ok I got it... logic pro



                def decode_png(image_data):

                img_str = image_data.tostring()
                reconstructed_img_1d = np.fromstring(img_str, dtype=np.uint8)
                reconstructed_img = reconstructed_img_1d.reshape(image_data.shape)

                return img_str


                def _process_image(filename):
                # Read the image file.
                with open(filename, 'r') as f:
                image_data = io.imread(f)

                # Decode the RGB PNG.
                img_str = image_data.tostring()

                height = image_data.shape[0]
                width = image_data.shape[1]

                return img_str, height, width






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 '18 at 20:59









                ZhuoZhuo

                2516




                2516
































                    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%2f53382268%2fpng-to-tfrecord-error-has-type-numpy-ndarray-but-expected-one-of-bytes%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







                    這個網誌中的熱門文章

                    Academy of Television Arts & Sciences

                    L'Équipe

                    1995 France bombings