title: Few Shot Learning / Siamese Network - 3-channel input images
body: <p>I'm trying to few shot learning on a prepared dataset with different few classes and 40 training sampels (40-shot learning). To load my data I used the following code:</p>
  <pre><code>def list_files(startpath):
      X = []
      images = []
      full_path = []
      for root, dirs, files in os.walk(startpath):
          level = root.replace(startpath, '').count(os.sep)
          indent = ' ' * 4 * (level)
          print('{}{}/'.format(indent, os.path.basename(root)))
          subindent = ' ' * 4 * (level + 1)
          l_alpha.append(dirs)
          for f in files:
          #    print('{}{}'.format(subindent, f))
          #    l_char.append(f)
              full_path.append(str(dirs)+'\\'+str(root)+'\\'+str(f))
              for pixel in f:
                  #img_data = cv2.imread(str(root)+'\\'+str(f))
              #    # store loaded image
              #    loaded_images.append([img_data])
              ##X.append(np.stack(loaded_images))
              #X = np.concatenate( loaded_images]]
          #full_path = [full_path[i][2:] for i in range(len(full_path))]
              #print(full_path[i][3:])
                  images = [np.array(Image.open(v[3:])) for v in full_path]
          images = [images]
      return root, dirs, files, full_path, images
  </code></pre>
  <p>This works out perfectly as I'm getting a shape for my output images of shape <code>(3267, 100, 100, 3)</code>. My problem lies within the next fraction of code where collect a batch of data each time:</p>
  <pre><code>def get_batch(batch_size,s=&quot;train&quot;):
      n_examples= 40
      &quot;&quot;&quot;Create batch of n pairs, half same class, half different class&quot;&quot;&quot;
      if s == 'train':
          X = Xtrain
          X= X.reshape(-1,100,100,3)
          #X= X.reshape(-1,20,105,105)
          categories = train_classes
      else:
          X = Xval
          X= X.reshape(-1,100,100,3)
          categories = val_classes
      #n_classes, n_examples, w, h, chan = X.shape
      tot_examples, w, h, chan = X.shape
      
      n_classes = 51#tot_examples / len(full_path) *100
      
      # randomly sample several classes to use in the batch
      #categories = rng.choice(n_classes,size=(batch_size,),replace=False)
      categories = rng.choice(int(n_classes),size=(batch_size,),replace=True)
      
      # initialize 2 empty arrays for the input image batch
      #pairs=[np.zeros((batch_size, h, w,1)) for i in range(2)]
      pairs=[np.zeros((batch_size, h, w, chan)) for i in range(2)]
      
      # initialize vector for the targets
      targets=np.zeros((batch_size,))
      
      # make one half of it '1's, so 2nd half of batch has same class
      targets[batch_size//2:] = 1
      for i in range(batch_size):
          category = categories[i]
          print(category)
          idx_1 = rng.randint(0, n_examples)#
          pairs[0][i,:,:,:] = X[category, idx_1].reshape(w, h, chan)
          idx_2 = rng.randint(0, n_examples)
          
          # pick images of same class for 1st half, different for 2nd
          if i &gt;= batch_size // 2:
              category_2 = category  
          else: 
              # add a random number to the category modulo n classes to ensure 2nd image has a different category
              category_2 = (category + rng.randint(1,n_classes)) % n_classes
          
          pairs[1][i,:,:,:] = X[category_2,idx_2].reshape(w, h,1)
      
      return pairs, targets
  </code></pre>
  <p>The traceback I'm getting is:</p>
  <pre><code>---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-129-a37505d52b27&gt; in &lt;module&gt;
  ----&gt; 1 (inputs,targets) = get_batch(batch_size)
  
  &lt;ipython-input-128-e55ce72910a7&gt; in get_batch(batch_size, s)
       33         print(category)
       34         idx_1 = rng.randint(0, n_examples)#
  ---&gt; 35         pairs[0][i,:,:,:] = X[category, idx_1].reshape(w, h, chan)
       36         idx_2 = rng.randint(0, n_examples)
       37 
  
  ValueError: cannot reshape array of size 300 into shape (100,100,3)
  </code></pre>
  <p>I understand the error but I don't see the problem. As the number of examples correspond to 40 as speciefied. Can someone help me where the problem of my code is lying? I made some remarks which might help understanding the code better. Thank you in advance.</p>