Context
I have a CNN that classifies with 98% accuracy. It's training time is ~2 minutes. I want to reduce that time by performing PCA on the training set before I train the CNN on them.
Problem
I'm doing this because I hope this will reduce the training time from 2 minutes to maybe a minute or even less. Here's the problem: I don't know how to run PCA on my 30,000 training images and then pass those images over to the CNN.
- I've run PCA on a few hundred sample images from my training set, but I don't know how to run it on the entire set.
- In addition, even after I run PCA on all my training images, how do I "connect" that output to the CNN's input? In other words, how do I feed the reconstructed, lower-dimensional images from the PCA into the CNN?
I've searched SO and the web vigorously for examples or similar questions, but to no avail. If anyone can help, that would be great.
Below is how a few sample images from my dataset looks like after PCA.
MWE (Google Colab Notebook):
pip install tensorflow
pip install numpy
pip install matplotlib
"""# Import Libraries"""
# Import Libraries
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
from tensorflow.keras import layers
from tensorflow.keras.utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
"""# Load Dataset"""
import pathlib
dataset_url = "*/TrainingSet.tar.gz"
data_dir = tf.keras.utils.get_file(origin = dataset_url,
fname = "TrainingSet",
untar = True)
data_dir = pathlib.Path(data_dir)
"""# Display # Images to check"""
print(list(data_dir.glob('*/*.png')))
image_count = len(list(data_dir.glob('*/*.png')))
print(image_count)
"""# Display sample image"""
pip install sklearn
import numpy as np
import os
import PIL
import PIL.Image
import tensorflow as tf
import tensorflow_datasets as tfds
from sklearn.decomposition import PCA
graphs = list(data_dir.glob('*/*.png'))
PIL.Image.open(str(graphs[6]))
"""# Define Image Dimensions & Batch Size"""
batch_size = 32
img_height = 36
img_width = 36
"""# Create Training & Validation Sets (80%, 20%)"""
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
"""# Define 3 Classes"""
class_names = ['Cubic Sinusoidal', 'Linear Sinusoidal', 'Quadratic Sinusoidal']
print(class_names)
"""# Supervised Learning (9 Samples from the Training Set)"""
!pip install skimage
from skimage import data
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
subGraphs = []
plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
subGraphs.append(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])
plt.axis("off")
subGraphs = np.array(subGraphs)
print(subGraphs.shape)
grayscale = rgb2gray(subGraphs[1])
print(grayscale.shape)
X=grayscale
pca_oliv = PCA(n_components = 36)
X_proj = pca_oliv.fit_transform(X)
print(np.cumsum(pca_oliv.explained_variance_ratio_))
plt.plot(np.cumsum(pca_oliv.explained_variance_ratio_))
plt.imshow(np.reshape(pca_oliv.components_, (36,36)), cmap=plt.cm.bone, interpolation='nearest')
X_inv_proj = pca_oliv.inverse_transform(X_proj)
X_proj_img = np.reshape(X_inv_proj,(1,36,36))
plt.imshow(X_proj_img[0], cmap=plt.cm.bone, interpolation='nearest')
