title: PCA in CNN deep learning model using Keras
body: <p>I built a CNN model for image classification using Keras and I want to use principal component analysis (PCA) with the model. How to use PCA in CNN for image recognition using Keras? </p>
  
  <p>I have tried the following code but when I run <code>pca.fit()</code> code, the code still running for hours and the RAM become full. </p>
  
  <pre><code>#Data files
  train_iris_data = 'Iris_Database_01/Training'
  valid_iris_data = 'Iris_Database_01/Validation'
  test_iris_data = 'Iris_Database_01/Testing'
  
  #Image data generator
  train_iris_datagen = ImageDataGenerator(
  rotation_range=10,
  shear_range=0.2,
  zoom_range=0.1,
  width_shift_range=0.1,
  height_shift_range=0.1
  )
  
  test_iris_datagen = ImageDataGenerator()
  
  #Image batches
  image_size = (224, 224)
  batch = 32
  
  # Training
  train_iris_generator = train_iris_datagen.flow_from_directory(
  train_iris_data,
  target_size=image_size,
  batch_size=batch,
  class_mode='categorical')
  
  # Validation
  validation_iris_generator = test_iris_datagen.flow_from_directory(
  valid_iris_data, 
  target_size=image_size, 
  batch_size=batch, 
  class_mode='categorical',
  shuffle = False)
  
  # Testing
  test_iris_generator = test_iris_datagen.flow_from_directory(
  test_iris_data,
  target_size=image_size, 
  batch_size=1, 
  class_mode='categorical',
  shuffle = False)
  
  pca = PCA(n_components=2)
  pca.fit(train_iris_generator)
  
  #pca = PCA(n_components=0.8)
  #pca.fit(train_iris_generator)
  </code></pre>