2013-04-30 11 views
11

私は一連の写真を取得し、それらを分類する必要があるという問題があります。イメージをクラスに分類する

実際には、私はこれらの画像について全く知っていません。だから私は見つけることができるように多くのディスクリプタを使用し、それらに私に使用されているディスクリプタのみを識別するためのPCAを実行することを計画しています。

多くのデータポイントで教師なし学習をすることができます。しかし、画像同士がつながっている可能性があります。画像Xから画像X + 1への展開があるかもしれないことを意味しますが、私はちょうどこれが各画像の情報で整理されることを願っています。

私の質問は以下のとおりです。Pythonのを使用しているとき

  1. がどのように私はこのベストを尽くしていますか? (私はスピードが問題ではない最初の概念の証明をしたい)。どのライブラリを使うべきですか?
  2. すでに画像の例がありますか?そのような種類の分類はありますか?ディスクリプタの束を使ってPCA経由で調理する例?正直言って、この部分は私にとっては恐ろしいことです。私はPythonはすでに私のためにこれのような何かを行う必要がありますが。

編集: 私は、私は現在、このためにアウトしようとしているきちんとしたキットを発見した:http://scikit-image.org/そこにはいくつかの記述子があるように思われます。自動的な特徴抽出を行い、対象分類に対する記述力に応じて特徴をランク付けする方法はありますか? PCAは自動的にランク付けすることができます。

編集2: データの保存のためのフレームワークがもう少し洗練されています。私はFatシステムをデータベースとして使用します。私は、クラスの組み合わせのインスタンスごとに1つのフォルダを持っています。したがって、画像がクラス1と2に属している場合、それらの画像を含むフォルダimg12が存在します。この方法で、私は各クラスのデータ量をより適切に制御できます。

編集3: 私は何をしたいのか何かを行うpythonのlibary(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() 
+0

これまでに何を試しましたか?何人かの努力相手を示してください。 –

+0

私はこれまでのところ達成したものを編集します。 – tarrasch

答えて

8

ピクチャをピクセルのベクトルに変換し、そのベクトルに対して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 
+0

これを行う方法の例を追加できますか? – tarrasch

+0

例を追加しました。寸法削減のために、 –

+0

がアップリストされている。 – tarrasch

3

私は私が直接あなたの質問に答えていないよ知っている:ここ

は私がsklearnを使用した例です。しかし、画像は大きく異なります:リモートセンシング、オブジェクト、シーン、fMRI、生体メディカル、フェイスなど...カテゴリを少し絞り込んでお知らせください。

あなたはどのような記述子を計算していますか?私が使用するコード(コンピュータビジョンコミュニティ)は、PythonではなくMATLABにありますが、同様のコードが利用可能であると確信しています(pycvモジュール& http://www.pythonware.com/products/pil/)。 MITの人々から最新の状態コードをプリコンパイルしたこの記述子ベンチマークを試してみてください:http://people.csail.mit.edu/jxiao/SUN/ GIST、HOG、SIFTを見てみましょう。それらはあなたが分析したいものに応じてかなり標準的です:シーン、オブジェクトまたはポイント。

+0

には、これらの記述子をすべて同時に使用する方法がありますか? PCAは、その後、貢献していない人々を除外することができます。あなたはPythonコードの例を作ることができますか? – tarrasch

+0

あなたのアプローチの問題点は、あなたには短期間でコンピュータビジョンの文献に頼るのではなく、純粋に「プログラマーのアプローチ」で解決しようとしていることです。 私はいくつかの記述子を混ぜて、巨大な特徴ベクトルをハイブライドし、各ベクトルを正規化することができると信じていますが、あなたのアプローチは非常に "荒々​​しく"見えます。あなたは、私が前に言及したカテゴリーのどのタイプのイメージを使用する予定かを定義していません。 – Arturo

+0

です。私はコンピュータビジョンのものに頼らずに乗り降りしようとしています。私はアルゴリズムが画像の中で重要なものを自分自身で把握したい。これはデータに完全に依存するはずです。 – tarrasch

0

まず、インポートライブラリや画像

from sklearn import datasets  
%matplotlib inline 
import sklearn as sk 
import numpy as np 
import matplotlib.pyplot as plt 
digits = datasets.load_digits() 
X_digits = digits.data 
y_digits = digits.target 
ind4 = np.where(y_digits==4) 
ind5= np.where(y_digits==5) 
plt.imshow(X_digits[1778].reshape((8,8)),cmap=plt.cm.gray_r) 
0

を抽出し、この機能を使用する:

XX = np.arange(64)

DEF feature_11(XX) :

yy=xx.reshape(8,8) 
feature_1 = sum(yy[0:2,:]) 
feature11 = sum(feature_1) 
print (feature11) 
return feature11 

feature_11(X_digits [1778])

次いでLDA使用:sklearn.discriminant_analysisインポートLinearDiscriminantAnalysis

CLF = LinearDiscriminantAnalysis()

ind_all = np.arange(0、lenの(y_digitsから

を))

np.random.shuffle(ind_all)

ind_training = ind_all [0:INT(0.8 * lenは(ind_all))]

ind_test = ind_all [INT(0.8 * lenは(ind_all)):]

clf.fit(X_digitsは[ind_training]、[ind_training] y_digits)

y_predicted = clf.predict(X_digits [ind_test 】)

plt.subplot(211)

plt.stem(y_predicted)

plt.subplot(212)

plt.stem(y_digits [ind_test]、 'R')

plt.stem(y_digits [ind_test] - y_predicted、 'R')

和(y_predicted == y_digits [ind_test])/ LEN (y_predicted)

+0

あなたの答えにいくつかの説明を加えてください。コードを表示するだけでは混乱する可能性があります。 –

関連する問題