4
$\begingroup$

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)
$\endgroup$
1
  • $\begingroup$ Would it even make sense to put the PCs in an array and run the CNN filters on them? $\endgroup$ – Dave Jun 22 '20 at 1:04
0
$\begingroup$

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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.