title: Keras.NET How to Use KerasIterator
body: <p>I want to do the same as F. Chollet's <a href="https://github.com/fchollet/deep-learning-with-python-notebooks/blob/master/5.3-using-a-pretrained-convnet.ipynb" rel="nofollow noreferrer">notebook</a> but in C#.</p>
  <p>However, I can't find a way to iterate over my KerasIterator object:</p>
  <pre class="lang-py prettyprint-override"><code>def extract_features(directory, sample_count):
      features = np.zeros(shape=(sample_count, 4, 4, 512))
      labels = np.zeros(shape=(sample_count))
  
      generator = datagen.flow_from_directory(
          directory,
          target_size=(150, 150),
          batch_size=20,
          class_mode='binary'
      )
  
      i = 0
      for inputs_batch, labels_batch in generator:
          features_batch = conv_base.predict(inputs_batch)
          features[i * 20 : (i + 1) * 20] = features_batch
          labels[i * 20 : (i + 1) * 20] = labels_batch
  
          i += 1
          if i * 20 &gt;= sample_count:
              break
  
      return features, labels
  </code></pre>
  <p>My current C# function is below.</p>
  <p>Details:</p>
  <ul>
  <li>foreach loop gives me a 'Compiler Error CS1579' on KerasIterator</li>
  <li>for loop is not possible because a KerasIterator doesn't have a Length (or Size, etc.)</li>
  </ul>
  <pre class="lang-cs prettyprint-override"><code>private List&lt;NDarray&gt; ExtractFeatures(ImageDataGenerator datagen, String directory, int sample_count)
  {
      // create the return NDarrays
      NDarray features = np.zeros(shape: (sample_count, 4, 4, 512));
      NDarray labels = np.zeros(shape: (sample_count, 4, 4, 512));
  
      KerasIterator generator = datagen.FlowFromDirectory(
          directory,
          target_size: (150, 150).ToTuple(),
          batch_size: 20,
          class_mode: &quot;binary&quot;
      );
  
      int i = 0;
      /* how do I iterate on 'generator' here, in a similar way than in Python? */
  
      // return the List element
      return new List&lt;NDarray&gt; { features, labels };
  }
  </code></pre>