title: Caffe: Balancing classes inside batch with ImageData layer
body: <p>I am training a CNN in Caffe, whose output is either one of two classes (a binary problem).</p>
  
  <p>I am using a ImageData layer as input layer, passing two <em>.txt</em> with the training and validation set of images. These files are balanced, <em>i.e.</em>, the number of examples are the same for both classes.  In this layer, I am also using the "shuffle" parameter.</p>
  
  <p>Regarding this, I have two doubts:</p>
  
  <p><strong>1. How the batch is sampled/selected from the .txt files?</strong> </p>
  
  <p>Is it constructed by getting the first <em>N</em> examples (let's say N is the batch size) of the file, shuffling them and feeding them to the network? In this sense, the batch itself may not be balanced. Does this affect the training/fine-tuning?</p>
  
  <p>Another way would be to randomly sample <em>N/2</em> examples from one class and <em>N/2</em> from the other, but I don't think Caffe does this.</p>
  
  <p><strong>2. The order of the examples in the .txt files matter to how the batch is constructed?</strong> </p>
  
  <p>Would it be a good idea to build the .txt file in a way the batch would be balanced (for example, every odd line is of one class and every even is of another)?</p>
  
  <p>Thanks for your help!</p>