简体   繁体   中英

PCA in CNN deep learning model using Keras

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?

I have tried the following code but when I run pca.fit() code, the code still running for hours and the RAM become full.

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

you can use truncated SVD instead. Another method that you can use is, IncrementalPCA PCA.

from sklearn.decomposition import TruncatedSVD
from sklearn.decomposition import IncrementalPCA

def func_PCA(input_data):
    input_data = np.array(input_data)
    pca = IncrementalPCA(n_components=50, batch_size=50)
    pca.fit(input_data)
    pca_input_data = pca.transform(input_data)
    eigenvalues = pca.explained_variance_
    eigenvectors = pca.components_
    return pca_input_data, eigenvalues, eigenvectors

def svd_func(input_data):
    svd = TruncatedSVD(n_components=50)
    svd.fit(input_data)
    pca_input_data = svd.transform(input_data)
    eigenvalues = svd.explained_variance_
    eigenvectors = svd.components_
    return pca_input_data, eigenvalues, eigenvectors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM