我使用 python 和手表图像(例如:watch_1、watch_2、watch_3)。我的目标是随机拍一张手表的照片,然后在我的数据库中找到与其最相似的手表。显然,区分手表的一个主要特征是它们的形状(方形、矩形、圆形、椭圆形),但也有其他特征。
目前,我只是在手表的 rgb 图像上运行 PCA 和 KNN,以找到其中最相似的图像。我的源代码如下:
import cv2
import numpy as np
import os
from glob import glob
from sklearn.decomposition import PCA
from sklearn import neighbors
from sklearn import preprocessing
data = []
# Read images from file
for filename in glob('Watches/*.jpg'):
img = cv2.imread(filename)
height, width = img.shape[:2]
img = np.array(img)
# Check that all my images are of the same resolution
if height == 529 and width == 940:
# Reshape each image so that it is stored in one line
img = np.concatenate(img, axis=0)
img = np.concatenate(img, axis=0)
data.append(img)
# Normalise data
data = np.array(data)
Norm = preprocessing.Normalizer()
Norm.fit(data)
data = Norm.transform(data)
# PCA model
pca = PCA(0.95)
pca.fit(data)
data = pca.transform(data)
# K-Nearest neighbours
knn = neighbors.NearestNeighbors(n_neighbors=4, algorithm='ball_tree', metric='minkowski').fit(data)
distances, indices = knn.kneighbors(data)
print(indices)
但是,当我尝试为超过 1500 个 rgb 图像运行此脚本时,我会MemoryError
在 PCA 方法处理数据的位置得到一个。
这对于具有 24GB RAM 和 3.6GHz Intel Core CPU 且没有任何独立 GPU 的 PC 是否正常?
我该如何克服呢?
我应该使用另一种方法,如增量 PCA(或深度学习算法)还是干脆购买离散 GPU?