Help to make a little data augmentation in deep-text-recognition-benchmark

username: Miguel_Campos
cooked: <p>Hello from spain comunity.</p>
<p>I am working with this repository<br>
<a href="https://github.com/clovaai/deep-text-recognition-benchmark" rel="noopener nofollow ugc">deep-text-recognition-benchmark</a></p>
<p>I am using the <strong>TRAIN.PY</strong> file (to train) and i would like make a little data augmentation (random rotation between -45 and 45 degree) in the fly.<br>
In the lane 31 you can see this</p>
<blockquote>
<p>train_dataset = Batch_Balanced_Dataset(opt)</p>
</blockquote>
<p>And line 145</p>
<blockquote>
<pre><code>while(True):
   # train part
    image_tensors, labels = train_dataset.get_batch()
    image = image_tensors.to(device)
</code></pre>
</blockquote>
<p>In Dataset.py, the method get_batch in Batch_Balanced_Dataset is something like this</p>
<blockquote>
<pre><code>def get_batch(self):
    balanced_batch_images = []
    balanced_batch_texts = []

    for i, data_loader_iter in enumerate(self.dataloader_iter_list):
        try:
            image, text = data_loader_iter.next()
            balanced_batch_images.append(image)
            balanced_batch_texts += text
        except StopIteration:
            self.dataloader_iter_list[i] = iter(self.data_loader_list[i])
            image, text = self.dataloader_iter_list[i].next()
            balanced_batch_images.append(image)
            balanced_batch_texts += text
        except ValueError:
            pass

    balanced_batch_images = torch.cat(balanced_batch_images, 0)

    return balanced_batch_images, balanced_batch_texts
</code></pre>
</blockquote>
<p>This are link to the code</p>
<p>train.py</p><aside class="onebox githubblob" data-onebox-src="https://github.com/clovaai/deep-text-recognition-benchmark/blob/master/train.py#L147">
  <header class="source">

      <a href="https://github.com/clovaai/deep-text-recognition-benchmark/blob/master/train.py#L147" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/clovaai/deep-text-recognition-benchmark/blob/master/train.py#L147" target="_blank" rel="noopener nofollow ugc">clovaai/deep-text-recognition-benchmark/blob/master/train.py#L147</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="137" style="counter-reset: li-counter 136 ;">
          <li>    except:</li>
          <li>        pass</li>
          <li></li>
          <li>start_time = time.time()</li>
          <li>best_accuracy = -1</li>
          <li>best_norm_ED = -1</li>
          <li>iteration = start_iter</li>
          <li></li>
          <li>while(True):</li>
          <li>    # train part</li>
          <li class="selected">    image_tensors, labels = train_dataset.get_batch()</li>
          <li>    image = image_tensors.to(device)</li>
          <li>    text, length = converter.encode(labels, batch_max_length=opt.batch_max_length)</li>
          <li>    batch_size = image.size(0)</li>
          <li></li>
          <li>    if 'CTC' in opt.Prediction:</li>
          <li>        preds = model(image, text)</li>
          <li>        preds_size = torch.IntTensor([preds.size(1)] * batch_size)</li>
          <li>        if opt.baiduCTC:</li>
          <li>            preds = preds.permute(1, 0, 2)  # to use CTCLoss format</li>
          <li>            cost = criterion(preds, text, preds_size, length) / batch_size</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
dataset.py</p><aside class="onebox githubblob" data-onebox-src="https://github.com/clovaai/deep-text-recognition-benchmark/blob/68a80fe97943a111ff1efaf52a63ad8f0f1c0e5d/dataset.py#L81">
  <header class="source">

      <a href="https://github.com/clovaai/deep-text-recognition-benchmark/blob/68a80fe97943a111ff1efaf52a63ad8f0f1c0e5d/dataset.py#L81" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/clovaai/deep-text-recognition-benchmark/blob/68a80fe97943a111ff1efaf52a63ad8f0f1c0e5d/dataset.py#L81" target="_blank" rel="noopener nofollow ugc">clovaai/deep-text-recognition-benchmark/blob/68a80fe97943a111ff1efaf52a63ad8f0f1c0e5d/dataset.py#L81</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="71" style="counter-reset: li-counter 70 ;">
          <li>    Total_batch_size_log = f'{dashed_line}\n'</li>
          <li>    batch_size_sum = '+'.join(batch_size_list)</li>
          <li>    Total_batch_size_log += f'Total_batch_size: {batch_size_sum} = {Total_batch_size}\n'</li>
          <li>    Total_batch_size_log += f'{dashed_line}'</li>
          <li>    opt.batch_size = Total_batch_size</li>
          <li></li>
          <li>    print(Total_batch_size_log)</li>
          <li>    log.write(Total_batch_size_log + '\n')</li>
          <li>    log.close()</li>
          <li></li>
          <li class="selected">def get_batch(self):</li>
          <li>    balanced_batch_images = []</li>
          <li>    balanced_batch_texts = []</li>
          <li></li>
          <li>    for i, data_loader_iter in enumerate(self.dataloader_iter_list):</li>
          <li>        try:</li>
          <li>            image, text = data_loader_iter.next()</li>
          <li>            balanced_batch_images.append(image)</li>
          <li>            balanced_batch_texts += text</li>
          <li>        except StopIteration:</li>
          <li>            self.dataloader_iter_list[i] = iter(self.data_loader_list[i])</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Could you help me to make that random rotation in the fly? I am not familiarized with dataloader in pytorch yet</p>

username: ptrblck
cooked: <p>The usual way would be to pass a transformation to your <code>Dataset</code> and apply it in the <code>__getitem__</code>.<br>
Based on the linked code, it seems that you are using different abstractions, where <code>get_batch</code> is calling into a <code>DataLoader</code>. You could either try to apply the transformation directly in the <code>get_batch</code> method or add it to the <code>LmdbDataset</code> and apply it in the <a href="https://github.com/clovaai/deep-text-recognition-benchmark/blob/68a80fe97943a111ff1efaf52a63ad8f0f1c0e5d/dataset.py#L181-L216"><code>__getitem__</code></a>. The <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html">data loading tutorial</a> explains the common use case in more detail.</p>