How to return batches of augmented images in an image preprocessor? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) 2019 Moderator Election Q&A - Questionnaire 2019 Community Moderator Election ResultsData augmentation: rotating images and zero valuesHow can I augment my image data?How to properly rotate image and labels for semantic segmentation data augmentation in Tensorflow?convert 16-Bit to 8-Bit imagesHow to filter out babies from image datasetBasis vectors for categorical imagesIs it good in general to subtract background from a sequence of images for learning?how many channels does ultrasound images have?Crop all written letters from image to form a websiteIs image sharpening a good idea for data augmentation?

Protagonist's race is hidden - should I reveal it?

How is an IPA symbol that lacks a name (e.g. ɲ) called?

Is it OK if I do not take the receipt in Germany?

Can I take recommendation from someone I met at a conference?

Like totally amazing interchangeable sister outfit accessory swapping or whatever

Married in secret, can marital status in passport be changed at a later date?

Kepler's 3rd law: ratios don't fit data

Why doesn't the university give past final exams' answers?

How can I wire a 9-position switch so that each position turns on one more LED than the one before?

Pointing to problems without suggesting solutions

Is my guitar’s action too high?

Weaponising the Grasp-at-a-Distance spell

/bin/ls sorts differently than just ls

Are Flameskulls resistant to magical piercing damage?

What is the ongoing value of the Kanban board to the developers as opposed to management

Proving inequality for positive definite matrix

What came first? Venom as the movie or as the song?

Will I be more secure with my own router behind my ISP's router?

Compiling and throwing simple dynamic exceptions at runtime for JVM

Why is one lightbulb in a string illuminated?

FME Console for testing

How to create a command for the "strange m" symbol in latex?

Why does my GNOME settings mention "Moto C Plus"?

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'



How to return batches of augmented images in an image preprocessor?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
2019 Moderator Election Q&A - Questionnaire
2019 Community Moderator Election ResultsData augmentation: rotating images and zero valuesHow can I augment my image data?How to properly rotate image and labels for semantic segmentation data augmentation in Tensorflow?convert 16-Bit to 8-Bit imagesHow to filter out babies from image datasetBasis vectors for categorical imagesIs it good in general to subtract background from a sequence of images for learning?how many channels does ultrasound images have?Crop all written letters from image to form a websiteIs image sharpening a good idea for data augmentation?










0












$begingroup$


I've written the following class that generates augmented images one by one. However, I'd like to be able to generate batches of 8 images each (or any number really). The way I'm hoping to do it is:



  1. Create batches of resized images (store them in a tensor, perhaps?)

  2. Iterate through batches (tensors?) and apply augmentation (the augmenter would return the augmented images)

  3. Add the augmented images to a batch (new tensor) of their own and yield this new batch

Is this a valid approach and if so, how could I go about doing this? And if not, how else might it be done?



class ImagePreprocessor(object):

def __init__(self, path_to_input_images):

if not os.path.exists(path_to_input_images):
raise ValueError('path(s) doesn't exist!')

self.input_root, self.input_folders, self.input_files = next(os.walk(path_to_input_images))

def load_image_from_file(self, filename):
filepath = os.path.join(self.input_root, filename)
img = skio.imread(filepath) # returns image as ndarray
return img

def create_augmenter(self):
# (augmenter details removed for brevity; I know the augmenter works)
return augmenter

def augment(self, img):
augmenter = self.create_augmenter()
augmented_image = augmenter.augment_image(img)
return augmented_image

def generate_data(self):
images = self.input_files

i = 0
shuffle(images)
while True:
# If all the images have been gone through, shuffle and restart
if i >= len(images):
i = 0
shuffle(images)


# Here is where I would like to create batches of images so that
# I can yield entire batches rather than just one image at a time

im = self.load_image_from_file(images[i])
resized_im = cv2.resize(im,(2000,2000))
aug_img = self.augment(resized_im)
i += 1
yield aug_img

```









share|improve this question









$endgroup$
















    0












    $begingroup$


    I've written the following class that generates augmented images one by one. However, I'd like to be able to generate batches of 8 images each (or any number really). The way I'm hoping to do it is:



    1. Create batches of resized images (store them in a tensor, perhaps?)

    2. Iterate through batches (tensors?) and apply augmentation (the augmenter would return the augmented images)

    3. Add the augmented images to a batch (new tensor) of their own and yield this new batch

    Is this a valid approach and if so, how could I go about doing this? And if not, how else might it be done?



    class ImagePreprocessor(object):

    def __init__(self, path_to_input_images):

    if not os.path.exists(path_to_input_images):
    raise ValueError('path(s) doesn't exist!')

    self.input_root, self.input_folders, self.input_files = next(os.walk(path_to_input_images))

    def load_image_from_file(self, filename):
    filepath = os.path.join(self.input_root, filename)
    img = skio.imread(filepath) # returns image as ndarray
    return img

    def create_augmenter(self):
    # (augmenter details removed for brevity; I know the augmenter works)
    return augmenter

    def augment(self, img):
    augmenter = self.create_augmenter()
    augmented_image = augmenter.augment_image(img)
    return augmented_image

    def generate_data(self):
    images = self.input_files

    i = 0
    shuffle(images)
    while True:
    # If all the images have been gone through, shuffle and restart
    if i >= len(images):
    i = 0
    shuffle(images)


    # Here is where I would like to create batches of images so that
    # I can yield entire batches rather than just one image at a time

    im = self.load_image_from_file(images[i])
    resized_im = cv2.resize(im,(2000,2000))
    aug_img = self.augment(resized_im)
    i += 1
    yield aug_img

    ```









    share|improve this question









    $endgroup$














      0












      0








      0





      $begingroup$


      I've written the following class that generates augmented images one by one. However, I'd like to be able to generate batches of 8 images each (or any number really). The way I'm hoping to do it is:



      1. Create batches of resized images (store them in a tensor, perhaps?)

      2. Iterate through batches (tensors?) and apply augmentation (the augmenter would return the augmented images)

      3. Add the augmented images to a batch (new tensor) of their own and yield this new batch

      Is this a valid approach and if so, how could I go about doing this? And if not, how else might it be done?



      class ImagePreprocessor(object):

      def __init__(self, path_to_input_images):

      if not os.path.exists(path_to_input_images):
      raise ValueError('path(s) doesn't exist!')

      self.input_root, self.input_folders, self.input_files = next(os.walk(path_to_input_images))

      def load_image_from_file(self, filename):
      filepath = os.path.join(self.input_root, filename)
      img = skio.imread(filepath) # returns image as ndarray
      return img

      def create_augmenter(self):
      # (augmenter details removed for brevity; I know the augmenter works)
      return augmenter

      def augment(self, img):
      augmenter = self.create_augmenter()
      augmented_image = augmenter.augment_image(img)
      return augmented_image

      def generate_data(self):
      images = self.input_files

      i = 0
      shuffle(images)
      while True:
      # If all the images have been gone through, shuffle and restart
      if i >= len(images):
      i = 0
      shuffle(images)


      # Here is where I would like to create batches of images so that
      # I can yield entire batches rather than just one image at a time

      im = self.load_image_from_file(images[i])
      resized_im = cv2.resize(im,(2000,2000))
      aug_img = self.augment(resized_im)
      i += 1
      yield aug_img

      ```









      share|improve this question









      $endgroup$




      I've written the following class that generates augmented images one by one. However, I'd like to be able to generate batches of 8 images each (or any number really). The way I'm hoping to do it is:



      1. Create batches of resized images (store them in a tensor, perhaps?)

      2. Iterate through batches (tensors?) and apply augmentation (the augmenter would return the augmented images)

      3. Add the augmented images to a batch (new tensor) of their own and yield this new batch

      Is this a valid approach and if so, how could I go about doing this? And if not, how else might it be done?



      class ImagePreprocessor(object):

      def __init__(self, path_to_input_images):

      if not os.path.exists(path_to_input_images):
      raise ValueError('path(s) doesn't exist!')

      self.input_root, self.input_folders, self.input_files = next(os.walk(path_to_input_images))

      def load_image_from_file(self, filename):
      filepath = os.path.join(self.input_root, filename)
      img = skio.imread(filepath) # returns image as ndarray
      return img

      def create_augmenter(self):
      # (augmenter details removed for brevity; I know the augmenter works)
      return augmenter

      def augment(self, img):
      augmenter = self.create_augmenter()
      augmented_image = augmenter.augment_image(img)
      return augmented_image

      def generate_data(self):
      images = self.input_files

      i = 0
      shuffle(images)
      while True:
      # If all the images have been gone through, shuffle and restart
      if i >= len(images):
      i = 0
      shuffle(images)


      # Here is where I would like to create batches of images so that
      # I can yield entire batches rather than just one image at a time

      im = self.load_image_from_file(images[i])
      resized_im = cv2.resize(im,(2000,2000))
      aug_img = self.augment(resized_im)
      i += 1
      yield aug_img

      ```






      data-augmentation image-preprocessing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 5 at 1:11









      cloud_colourscloud_colours

      11




      11




















          0






          active

          oldest

          votes












          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "557"
          ;
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fdatascience.stackexchange.com%2fquestions%2f48644%2fhow-to-return-batches-of-augmented-images-in-an-image-preprocessor%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Data Science Stack Exchange!


          • 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.

          Use MathJax to format equations. MathJax reference.


          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%2fdatascience.stackexchange.com%2fquestions%2f48644%2fhow-to-return-batches-of-augmented-images-in-an-image-preprocessor%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

          Marja Vauras Lähteet | Aiheesta muualla | NavigointivalikkoMarja Vauras Turun yliopiston tutkimusportaalissaInfobox OKSuomalaisen Tiedeakatemian varsinaiset jäsenetKasvatustieteiden tiedekunnan dekaanit ja muu johtoMarja VaurasKoulutusvienti on kestävyys- ja ketteryyslaji (2.5.2017)laajentamallaWorldCat Identities0000 0001 0855 9405n86069603utb201588738523620927

          Which is better: GPT or RelGAN for text generation?2019 Community Moderator ElectionWhat is the difference between TextGAN and LM for text generation?GANs (generative adversarial networks) possible for text as well?Generator loss not decreasing- text to image synthesisChoosing a right algorithm for template-based text generationHow should I format input and output for text generation with LSTMsGumbel Softmax vs Vanilla Softmax for GAN trainingWhich neural network to choose for classification from text/speech?NLP text autoencoder that generates text in poetic meterWhat is the interpretation of the expectation notation in the GAN formulation?What is the difference between TextGAN and LM for text generation?How to prepare the data for text generation task

          Is this part of the description of the Archfey warlock's Misty Escape feature redundant?When is entropic ward considered “used”?How does the reaction timing work for Wrath of the Storm? Can it potentially prevent the damage from the triggering attack?Does the Dark Arts Archlich warlock patrons's Arcane Invisibility activate every time you cast a level 1+ spell?When attacking while invisible, when exactly does invisibility break?Can I cast Hellish Rebuke on my turn?Do I have to “pre-cast” a reaction spell in order for it to be triggered?What happens if a Player Misty Escapes into an Invisible CreatureCan a reaction interrupt multiattack?Does the Fiend-patron warlock's Hurl Through Hell feature dispel effects that require the target to be on the same plane as the caster?What are you allowed to do while using the Warlock's Eldritch Master feature?