TensorFlow: extract data with a given feature, from NSynth Dataset










0















I have a data set of TFRecord files of serialized TensorFlow Example protocol buffers with one Example proto per note, downloaded from https://magenta.tensorflow.org/datasets/nsynth. I am using the test set, which is approximately 1 Gb, in case someone wants to download it, to check the code below. Each Example contains many features: pitch, instrument ...



The code that reads in this data is:



import tensorflow as tf
import numpy as np

sess = tf.InteractiveSession()

# Reading input data
dataset = tf.data.TFRecordDataset('../data/nsynth-test.tfrecord')

# Convert features into tensors
features =
"pitch": tf.FixedLenFeature([1], dtype=tf.int64),
"audio": tf.FixedLenFeature([64000], dtype=tf.float32),
"instrument_family": tf.FixedLenFeature([1], dtype=tf.int64)

parse_function = lambda example_proto: tf.parse_single_example(example_proto,features)
dataset = dataset.map(parse_function)

# Consuming TFRecord data.
dataset = dataset.shuffle(buffer_size=10000)
dataset = dataset.batch(batch_size=3)
dataset = dataset.repeat()
iterator = dataset.make_one_shot_iterator()
batch = iterator.get_next()
sess.run(batch)


Now, the pitch ranges from 21 to 108. But I want to consider data of a given pitch only, e.g. pitch = 51. How do I extract this "pitch=51" subset from the whole dataset? Or alternatively, what do I do to make my iterator go through this subset only?










share|improve this question


























    0















    I have a data set of TFRecord files of serialized TensorFlow Example protocol buffers with one Example proto per note, downloaded from https://magenta.tensorflow.org/datasets/nsynth. I am using the test set, which is approximately 1 Gb, in case someone wants to download it, to check the code below. Each Example contains many features: pitch, instrument ...



    The code that reads in this data is:



    import tensorflow as tf
    import numpy as np

    sess = tf.InteractiveSession()

    # Reading input data
    dataset = tf.data.TFRecordDataset('../data/nsynth-test.tfrecord')

    # Convert features into tensors
    features =
    "pitch": tf.FixedLenFeature([1], dtype=tf.int64),
    "audio": tf.FixedLenFeature([64000], dtype=tf.float32),
    "instrument_family": tf.FixedLenFeature([1], dtype=tf.int64)

    parse_function = lambda example_proto: tf.parse_single_example(example_proto,features)
    dataset = dataset.map(parse_function)

    # Consuming TFRecord data.
    dataset = dataset.shuffle(buffer_size=10000)
    dataset = dataset.batch(batch_size=3)
    dataset = dataset.repeat()
    iterator = dataset.make_one_shot_iterator()
    batch = iterator.get_next()
    sess.run(batch)


    Now, the pitch ranges from 21 to 108. But I want to consider data of a given pitch only, e.g. pitch = 51. How do I extract this "pitch=51" subset from the whole dataset? Or alternatively, what do I do to make my iterator go through this subset only?










    share|improve this question
























      0












      0








      0








      I have a data set of TFRecord files of serialized TensorFlow Example protocol buffers with one Example proto per note, downloaded from https://magenta.tensorflow.org/datasets/nsynth. I am using the test set, which is approximately 1 Gb, in case someone wants to download it, to check the code below. Each Example contains many features: pitch, instrument ...



      The code that reads in this data is:



      import tensorflow as tf
      import numpy as np

      sess = tf.InteractiveSession()

      # Reading input data
      dataset = tf.data.TFRecordDataset('../data/nsynth-test.tfrecord')

      # Convert features into tensors
      features =
      "pitch": tf.FixedLenFeature([1], dtype=tf.int64),
      "audio": tf.FixedLenFeature([64000], dtype=tf.float32),
      "instrument_family": tf.FixedLenFeature([1], dtype=tf.int64)

      parse_function = lambda example_proto: tf.parse_single_example(example_proto,features)
      dataset = dataset.map(parse_function)

      # Consuming TFRecord data.
      dataset = dataset.shuffle(buffer_size=10000)
      dataset = dataset.batch(batch_size=3)
      dataset = dataset.repeat()
      iterator = dataset.make_one_shot_iterator()
      batch = iterator.get_next()
      sess.run(batch)


      Now, the pitch ranges from 21 to 108. But I want to consider data of a given pitch only, e.g. pitch = 51. How do I extract this "pitch=51" subset from the whole dataset? Or alternatively, what do I do to make my iterator go through this subset only?










      share|improve this question














      I have a data set of TFRecord files of serialized TensorFlow Example protocol buffers with one Example proto per note, downloaded from https://magenta.tensorflow.org/datasets/nsynth. I am using the test set, which is approximately 1 Gb, in case someone wants to download it, to check the code below. Each Example contains many features: pitch, instrument ...



      The code that reads in this data is:



      import tensorflow as tf
      import numpy as np

      sess = tf.InteractiveSession()

      # Reading input data
      dataset = tf.data.TFRecordDataset('../data/nsynth-test.tfrecord')

      # Convert features into tensors
      features =
      "pitch": tf.FixedLenFeature([1], dtype=tf.int64),
      "audio": tf.FixedLenFeature([64000], dtype=tf.float32),
      "instrument_family": tf.FixedLenFeature([1], dtype=tf.int64)

      parse_function = lambda example_proto: tf.parse_single_example(example_proto,features)
      dataset = dataset.map(parse_function)

      # Consuming TFRecord data.
      dataset = dataset.shuffle(buffer_size=10000)
      dataset = dataset.batch(batch_size=3)
      dataset = dataset.repeat()
      iterator = dataset.make_one_shot_iterator()
      batch = iterator.get_next()
      sess.run(batch)


      Now, the pitch ranges from 21 to 108. But I want to consider data of a given pitch only, e.g. pitch = 51. How do I extract this "pitch=51" subset from the whole dataset? Or alternatively, what do I do to make my iterator go through this subset only?







      python-3.x tensorflow magenta






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 16:58









      MenciaMencia

      2371316




      2371316






















          1 Answer
          1






          active

          oldest

          votes


















          1














          What you have looks pretty good, all you're missing is a filter function.



          For example if you only wanted to extract pitch=51, you should add after your map function



          dataset = dataset.filter(lambda example: tf.equal(example["pitch"][0], 51))





          share|improve this answer























          • Thank you so much, this is exactly what I needed!

            – Mencia
            Nov 28 '18 at 11:41










          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%2f53305259%2ftensorflow-extract-data-with-a-given-feature-from-nsynth-dataset%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









          1














          What you have looks pretty good, all you're missing is a filter function.



          For example if you only wanted to extract pitch=51, you should add after your map function



          dataset = dataset.filter(lambda example: tf.equal(example["pitch"][0], 51))





          share|improve this answer























          • Thank you so much, this is exactly what I needed!

            – Mencia
            Nov 28 '18 at 11:41















          1














          What you have looks pretty good, all you're missing is a filter function.



          For example if you only wanted to extract pitch=51, you should add after your map function



          dataset = dataset.filter(lambda example: tf.equal(example["pitch"][0], 51))





          share|improve this answer























          • Thank you so much, this is exactly what I needed!

            – Mencia
            Nov 28 '18 at 11:41













          1












          1








          1







          What you have looks pretty good, all you're missing is a filter function.



          For example if you only wanted to extract pitch=51, you should add after your map function



          dataset = dataset.filter(lambda example: tf.equal(example["pitch"][0], 51))





          share|improve this answer













          What you have looks pretty good, all you're missing is a filter function.



          For example if you only wanted to extract pitch=51, you should add after your map function



          dataset = dataset.filter(lambda example: tf.equal(example["pitch"][0], 51))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '18 at 18:36









          jengeljengel

          44938




          44938












          • Thank you so much, this is exactly what I needed!

            – Mencia
            Nov 28 '18 at 11:41

















          • Thank you so much, this is exactly what I needed!

            – Mencia
            Nov 28 '18 at 11:41
















          Thank you so much, this is exactly what I needed!

          – Mencia
          Nov 28 '18 at 11:41





          Thank you so much, this is exactly what I needed!

          – Mencia
          Nov 28 '18 at 11:41



















          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%2f53305259%2ftensorflow-extract-data-with-a-given-feature-from-nsynth-dataset%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto