TransWikia.com

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

Data Science Asked on November 19, 2021

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)

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!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP