title: Fitting array to datagen then passing as parameter to Keras Classifier
body: <p>I have a convolutional neural network that i am using to classify cats and dogs, using keras classifier. I had to use custom cross validation, due to how my data is organized, where i have n groups of different breeds of cats and dogs and each breed has 200 images, 600 images per pet class. Now, what i am trying to do is create augmented data (on the fly/in place data augmentation) and concatenate them to my original array of groups. However i get the following error when i try to iterate through datagen:</p>
  <pre><code>TypeError: __init__() got an unexpected keyword argument 'y'
  </code></pre>
  <p>For this attempt:</p>
  <pre><code>for i in range(0, 2):
      datagen = ImageDataGenerator(
      groups[i],
      y=labels[i],
      batch_size=32,
      save_to_dir=None,
      save_prefix=&quot;&quot;,
      save_format=&quot;png&quot;,
      rotation_range=20,
      zoom_range=0.2
      )
  </code></pre>
  <p>I am using custom cross validation to check for the best weights and passing it as parameter to KerasClassifier function like this:</p>
  <pre><code>X = np.concatenate([group_1, group_2, group_3], axis=0)[..., np.newaxis]
  y = np.concatenate([y_g_1, y_g_2, y_g_3], axis=0)
      def n_fold_cv():
          lengths = [0] + list(accumulate(map(len, [y_g_1, y_g_2, y_g_3])))
          i = 1
          while i &lt;= 3:
              min_length = lengths[i - 1]
              max_length = lengths[i]
              idx = np.arange(min_length, max_length, dtype=int)
              yield idx, idx
              i += 1
  
  
      keras_clf = KerasClassifier(build_fn=build_model, epochs=100, batch_size=8, verbose=0)
      accuracies = cross_val_score(estimator=keras_clf, scoring=&quot;accuracy&quot;, X=X, y=y, cv=n_fold_cv())
      print(accuracies)
  </code></pre>
  <p>Where i have group 1, group 2 and group 3. I split them into k-folds with my custom k-fold function, before the concatenation.</p>
  <p>I would like to set each group for augmentation once, before concatenating and doing cross validation. I haven't really grasped how to fit data augmentation to Keras Classifier.</p>
  <p><a href="https://www.kaggle.com/puelon/notebooka945977ceb" rel="nofollow noreferrer">Public kaggle notebook with dataset</a>:</p>
  <p>Just code, with <a href="http://www.mediafire.com/file/y6o9mvpr9fti1zv/file" rel="nofollow noreferrer">dataset</a>:</p>
  <pre><code>from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
  from sklearn.model_selection import cross_val_score
  import numpy as np
  from tensorflow.keras.layers import *
  from tensorflow.keras import Sequential
  from itertools import accumulate
  import tensorflow as tf
  from keras import backend as K
  from keras.preprocessing import image
  from keras.layers import Conv2D, MaxPooling2D
  from keras.layers import Activation, Dropout, Flatten, Dense
  from keras.callbacks import ModelCheckpoint
  import os
  import numpy as np
  import cv2
  from PIL import Image as PImage
  from os import listdir
  from keras.preprocessing.image import ImageDataGenerator
  
  img_width, img_height = 128, 160
  
  def load_dataset(path):
      imagesList = listdir(path)
      loadedImages = []
      for root, dirs, files in os.walk(path):
          for i, name in enumerate(files):
              image_path = os.path.join(root, name)
              img = PImage.open(image_path)
              arr = np.array(img)
              loadedImages.append(arr)
      return loadedImages 
  
  def n_fold_cv():
      lengths = [0] + list(accumulate(map(len, [y_g_1, y_g_2, y_g_3])))
      i = 1
      while i &lt;= 3:
          min_length = lengths[i - 1]
          max_length = lengths[i]
          idx = np.arange(min_length, max_length, dtype=int)
          yield idx, idx
          i += 1
                
  def generateLabel(sober, drunk):
      label = []
      for i in range(0, 1):
          for idx in range(1):
              label.extend( [idx]*sober)
              for idx in range(1):
                  label.extend([idx+1]*drunk)
      label = np.array(label)
      return label
  
  y_g_1 = generateLabel(200, 200)
  y_g_2 = generateLabel(200, 200)
  y_g_3 = generateLabel(200, 200)
  
  
  group_1 = np.asarray(load_dataset('../input/cats-and-dogs-dataset/Pets 1'))
  group_2 = np.asarray(load_dataset('../input/cats-and-dogs-dataset/Pets 2'))
  group_3 = np.asarray(load_dataset('../input/cats-and-dogs-dataset/Pets 3'))
  
  groups = np.stack((grupo_1, grupo_2, grupo_3),axis=0)
  labels = np.stack((y_g_2, y_g_2, y_g_3), axis=0)
  
  
  
  for i in range(0, 2):
      datagen = ImageDataGenerator(
      groups[i],
      y=labels[i],
      batch_size=32,
      save_to_dir=None,
      save_prefix=&quot;&quot;,
      save_format=&quot;png&quot;,
      rotation_range=20,
      zoom_range=0.2
      )
      
      
      
  X = np.concatenate([group_1, group_2, group_3], axis=0)[..., np.newaxis]
  y = np.concatenate([y_g_1, y_g_2, y_g_3], axis=0)
  
  if K.image_data_format() == 'channels_first':
     input_shape = (3, img_width, img_height)
  else:
     input_shape = (img_width, img_height, 3)
  
  def build_model():
      model = Sequential()
      model.add(Conv2D(32, (3, 3), input_shape=input_shape))
      model.add(Activation('relu'))
      model.add(MaxPooling2D(pool_size=(2, 2)))
      model.add(Dropout(0.25))
  
      model.add(Dense(1))
      model.add(Activation('sigmoid'))
      model.summary()
      model.compile(loss='binary_crossentropy',
                    optimizer='rmsprop',
                    metrics=['mse','accuracy'])
  
  
      return model
  
  
  keras_clf = KerasClassifier(build_fn=build_model, epochs=100, batch_size=8, verbose=0)
  
  
  accuracies = cross_val_score(estimator=keras_clf, scoring=&quot;accuracy&quot;, X=X, y=y, cv=n_fold_cv())
  print(accuracies)
  </code></pre>