'How to do PCA on multiple images by flattening the tensors?

I have an array of tensors for a single image. I want to flatten the vectors and perform PCA on the same.

The below is the code to extract the tensors on a single image :

bottle_neck_model_tensors = bottle_neck_model([im_resize])[0]
bottle_neck_model_tensors

I want to do the same on multiple images and also flatten the array. later perform PCA on the array.

Can anyone help me with this?

Code For multiple images

import glob
import cv2 as cv

path = glob.glob(".../SampleImages*.jpg")
    
cv_img = []
for img in path:
   n = cv.imread(img)
   #print(n.shape)
   im_resize = cv2.resize(n , (1024 ,1024))
   im_resize = np.expand_dims(im_resize, axis = 0)
   cv_img.append(im_resize)
   bottle_neck_model_tensors = bottle_neck_model([im_resize])[0]

I want to improvise this code and flatten the vectors. I am reshaping because the model I created from the extracted layers is of shape ( None, 128, 128, 512) and the input image to the model is 1024, 1024, 3.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source