InsideDarkWeb.com

How to use PCA in CNN for image recognition using Keras?

I created a CNN model for image classification and I want to use Principal Component Analysis (PCA) but when I run pca.fit() code, the code still running for hours and the RAM become full. So, I want to know how to use PCA in CNN for image recognition using Keras?

My 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)

Data Science Asked on November 19, 2021

1 Answers

One Answer

Standard implementations of PCA calculates statistics across the entire dataset in order to find the projection that has the greatest variance. The entire dataset needs to be loaded into memory for that calculation.

You are using ImageDataGenerator to generate synthetic variations of the data, greatly increasing the size of the training set that has to be loaded into memory.

Here a couple of options to run PCA:

  • Reduce the number of variations in ImageDataGenerator
  • Use approximate PCA
  • Get a larger machine or distribute across a cluster

Answered by Brian Spiering on November 19, 2021

Add your own answers!

Related Questions

Random forest multivariate forecast in Python

0  Asked on December 5, 2020 by user3792245

   

ValueError: bad input shape

0  Asked on December 5, 2020 by rajan-lagah

     

How should I sample from a mixture distribution?

1  Asked on December 4, 2020 by josh-purtell

       

Gradient descent formula implementation in python

1  Asked on December 4, 2020 by manas-tripathi

   

How do I build an image dataset for CNN?

3  Asked on December 4, 2020 by 55thswiss

         

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.