我遇到了一组图片的问题,需要对它们进行分类。
问题是,我对这些图像一无所知。所以我计划使用我能找到的尽可能多的描述符,然后对这些进行 PCA 以仅识别对我有用的描述符。
如果有帮助的话,我可以对很多数据点进行监督学习。但是,图片有可能相互连接。这意味着可能会有从 Image X 到 Image X+1 的发展,尽管我有点希望每个图像中的信息都能解决这个问题。
我的问题是:
- 我如何在使用 Python 时做到最好? (我想先做一个概念证明,速度不是问题)。我应该使用哪些库?
- 是否已经有此类图像分类的示例?使用一堆描述符并通过 PCA 处理它们的示例?老实说,这部分对我来说有点可怕。虽然我认为 python 应该已经为我做了这样的事情。
编辑: 我找到了一个我目前正在尝试的简洁工具包:http://scikit-image.org/那里似乎有一些描述符。有没有办法进行自动特征提取,并根据特征对目标分类的描述能力对特征进行排序? PCA 应该能够自动排名。
编辑 2: 我的数据存储框架现在更加完善了。我将使用 Fat 系统作为数据库。我将为类组合的每个实例创建一个文件夹。因此,如果图像属于 1 类和 2 类,则会有一个文件夹 img12 包含这些图像。这样我就可以更好地控制每个类(class)的数据量。
编辑 3: 我找到了一个 python 库(sklearn)的例子,它做了我想做的事情。它是关于识别手写数字的。我正在尝试将我的数据集转换成我可以使用的东西。
这是我使用 sklearn 找到的示例:
import pylab as pl
# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics
# The digits dataset
digits = datasets.load_digits()
# The data that we are interested in is made of 8x8 images of digits,
# let's have a look at the first 3 images, stored in the `images`
# attribute of the dataset. If we were working from image files, we
# could load them using pylab.imread. For these images know which
# digit they represent: it is given in the 'target' of the dataset.
for index, (image, label) in enumerate(zip(digits.images, digits.target)[:4]):
pl.subplot(2, 4, index + 1)
pl.axis('off')
pl.imshow(image, cmap=pl.cm.gray_r, interpolation='nearest')
pl.title('Training: %i' % label)
# To apply an classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))
# Create a classifier: a support vector classifier
classifier = svm.SVC(gamma=0.001)
# We learn the digits on the first half of the digits
classifier.fit(data[:n_samples / 2], digits.target[:n_samples / 2])
# Now predict the value of the digit on the second half:
expected = digits.target[n_samples / 2:]
predicted = classifier.predict(data[n_samples / 2:])
print("Classification report for classifier %s:\n%s\n"
% (classifier, metrics.classification_report(expected, predicted)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))
for index, (image, prediction) in enumerate(
zip(digits.images[n_samples / 2:], predicted)[:4]):
pl.subplot(2, 4, index + 5)
pl.axis('off')
pl.imshow(image, cmap=pl.cm.gray_r, interpolation='nearest')
pl.title('Prediction: %i' % prediction)
pl.show()
最佳答案
您可以将图片转换为像素向量,然后对该向量执行 PCA。这可能比尝试手动查找描述符更容易。你可以在 python 中使用 numPy 和 sciPy。 例如:
import scipy.io
from numpy import *
#every row in the *.mat file is 256*256 numbers representing gray scale values
#for each pixel in an image. i.e. if XTrain.mat has 1000 lines than each line
#will be made up of 256*256 numbers and there would be 1000 images in the file.
#The following loads the image into a sciPy matrix where each row is a vector
#of length 256*256, representing an image. This code will need to be switched
#out if you have a different method of storing images.
Xtrain = scipy.io.loadmat('Xtrain.mat')["Xtrain"]
Ytrain = scipy.io.loadmat('Ytrain.mat')["Ytrain"]
Xtest = scipy.io.loadmat('Xtest.mat')["Xtest"]
Ytest = scipy.io.loadmat('Ytest.mat')["Ytest"]
learn(Xtest,Xtrain,Ytest,Ytrain,5) #this lowers the dimension from 256*256 to 5
def learn(testX,trainX,testY,trainY,n):
pcmat = PCA(trainX,n)
lowdimtrain=mat(trainX)*pcmat #lower the dimension of trainX
lowdimtest=mat(testX)*pcmat #lower the dimension of testX
#run some learning algorithm here using the low dimension matrices for example
trainset = []
knnres = KNN(lowdimtrain, trainY, lowdimtest ,k)
numloss=0
for i in range(len(knnres)):
if knnres[i]!=testY[i]:
numloss+=1
return numloss
def PCA(Xparam, n):
X = mat(Xparam)
Xtranspose = X.transpose()
A=Xtranspose*X
return eigs(A,n)
def eigs(M,k):
[vals,vecs]=LA.eig(M)
return LM2ML(vecs[:k])
def LM2ML(lm):
U=[[]]
temp = []
for i in lm:
for j in range(size(i)):
temp.append(i[0,j])
U.append(temp)
temp = []
U=U[1:]
return U
为了对图像进行分类,您可以使用 k-最近邻。即,您找到 k 个最近的图像,并通过对 k 个最近的图像进行多数投票来标记您的图像。例如:
def KNN(trainset, Ytrainvec, testset, k):
eucdist = scidist.cdist(testset,trainset,'sqeuclidean')
res=[]
for dists in eucdist:
distup = zip(dists, Ytrainvec)
minVals = []
sumLabel=0;
for it in range(k):
minIndex = index_min(dists)
(minVal,minLabel) = distup[minIndex]
del distup[minIndex]
dists=numpy.delete(dists,minIndex,0)
if minLabel == 1:
sumLabel+=1
else:
sumLabel-=1
if(sumLabel>0):
res.append(1)
else:
res.append(0)
return res
关于python - 将一组图像分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16302127/