CS231n 学习笔记(1)——神经网络 part1 :图像分类与数据驱动方法
生活随笔
收集整理的這篇文章主要介紹了
CS231n 学习笔记(1)——神经网络 part1 :图像分类与数据驱动方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
*此系列為斯坦福李飛飛團隊的系列公開課“cs231n convolutional neural network for visual recognition ”的學(xué)習(xí)筆記。本文主要是對module 1 的part1 Image classification 的翻譯與學(xué)習(xí)。
圖像分類是指對于輸入的圖像,從一系列的標(biāo)簽中找出正確的與之對應(yīng)的標(biāo)簽,圖像分類是許多復(fù)雜的計算機視覺算法的基礎(chǔ)。
對于計算機而言,它看到的圖像可以看成是一個三維數(shù)組,其中灰度圖像的寬度和高度各占一維,還有一維是RGB的組合。
幾乎所有的圖像分類算法都會面臨以下幾方面問題:1. 觀察角度會變化。2. 物體大小會變化,觀察范圍會變化。3. 變形。4. 背景干擾。5. 類內(nèi)變化。例如,椅子的形狀可以有很多種。
數(shù)據(jù)驅(qū)動方法
我們并不是去寫一個算法來識別圖像中的貓,而是想對待孩童一樣,我們把每一個類型中大量的圖片提供給計算機,然后寫一個學(xué)習(xí)算法,讓計算機自己學(xué)會分類。這種方法是基于數(shù)據(jù)驅(qū)動的。需要訓(xùn)練集和標(biāo)簽數(shù)據(jù)集。圖像分類過程
圖像分類是為每一個數(shù)組分配一個標(biāo)簽。這個模式化過程如下: -輸入--訓(xùn)練集,有許多有標(biāo)簽的圖像構(gòu)成。 -學(xué)習(xí)--用訓(xùn)練集訓(xùn)練分類器,進行數(shù)學(xué)建模。 -評價--用未標(biāo)簽的新數(shù)據(jù)評價分類器的性能。正確率越高,學(xué)習(xí)的效果越好。最近鄰域分類法
此法并不是卷積網(wǎng)絡(luò)的方法,圖像分類的實踐中也很少用到,但是這個方法有助于幫我們理解圖像分類。 最近領(lǐng)域法將測試圖片和訓(xùn)練集中的每一幅圖求距離,兩幅圖像之間的距離是指這兩幅圖像中的每一個對于像素的差距之和。使用下面這個公式來求取: 在求得測試集圖像與訓(xùn)練集的沒衣服圖像的距離之后,找到距離最近的圖像,將測試集圖像歸為此類。輸入訓(xùn)練集及測試集
cs231n通過函數(shù)`Xtr, Ytr, Xte, Yte = load_CIFAR10('data/cifar10/')`導(dǎo)入 CIFAR-10 數(shù)據(jù)集, CIFAR-10 中有50000個訓(xùn)練樣本,每個樣本大小是32 x 32 x 3,每個樣本有一個標(biāo)簽。此外,CIFAR-10 中還有10000個測試樣本。我在網(wǎng)絡(luò)上沒有搜到`load_CIFAR10()`函數(shù)的源代碼,因此需要自己動手來寫一個數(shù)據(jù)導(dǎo)入程序。 import numpy as npdef unpickle(file):import cPicklefo = open(file,'rb')dict = cPickle.load(fo)fo.close()return dictdef load_CIFAR10(file): #get the training datadataTrain = []labelTrain = []for i in range(1,6):dic = unpickle(file+"\\data_batch_"+str(i))for item in dic["data"]:dataTrain.append(item)for item in dic["labels"]:labelTrain.append(item)#get test datadataTest = []labelTest = []dic = unpickle(file+"\\test_batch")for item in dic["data"]:dataTest.append(item)for item in dic["labels"]:labelTest.append(item)return (dataTrain,labelTrain,dataTest,labelTest)Xtr, Ytr, Xte, Yte = load_CIFAR10('C:\\Users\\Administrator\\Documents\\python Scripts\\cifar-10-batches-py') #print "Xte:%d" %(len(dataTest)) #print "Yte:%d" %(len(labelTest)) dataTr = np.asarray(Xtr) dataTs = np.asarray(Xte) labelTr = np.asarray(Ytr) labelTs = np.asarray(Yte) print dataTr.shape print dataTs.shape print labelTr.shape print labelTs.shape 獲取CIFAR-10 數(shù)據(jù)集的主要過程是,先將CIFAR-10 數(shù)據(jù)集的訓(xùn)練數(shù)據(jù)寫入一個字典變量中,再將字典變量中的data和label分別拿到兩個數(shù)組中去。用同樣的方法獲取測試數(shù)據(jù),最后返回四個數(shù)組。 在程序最初版本里,路徑中都是單斜杠,可是總是編譯不通過,始終報錯提示:invalid mode ('rb') or filename: 'C:\\Users\\Administrator\\Documents\\Python Scripts\\cifar-10-batches-py\test_batch' 將路徑中所有的單斜杠都變成雙斜杠后,編譯就能通過了。 導(dǎo)入數(shù)據(jù)后,通過最簡單的鄰域聚類的方法,對圖像進行分類 datatr, labeltr, datate, labelte = load_CIFAR10('C:\\Users\\Administrator\\ywj\\data\\cifar-10-python\\cifar-10-batches-py')#print "Xte:%d" %(len(dataTest)) #print "Yte:%d" %(len(labelTest)) Xtr = np.asarray(datatr) Xte = np.asarray(datate) Ytr = np.asarray(labeltr) Yte = np.asarray(labelte) Xtr_rows = Xtr.reshape(Xtr.shape[0], 32 * 32 * 3) # Xtr_rows becomes 50000 x 3072 Xte_rows = Xte.reshape(Xte.shape[0], 32 * 32 * 3) # Xte_rows becomes 10000 x 3072 print Xtr.shape print Xte.shape print Ytr.shape print Yte.shape print type(Xtr)class NearestNeighbor(object):def __init__(self):passdef train(self, X, y):""" X is N x D where each row is an example. Y is 1-dimension of size N """# the nearest neighbor classifier simply remembers all the training dataself.Xtr = Xself.ytr = ydef predict(self, X):""" X is N x D where each row is an example we wish to predict label for """num_test = X.shape[0]# lets make sure that the output type matches the input typeYpred = np.zeros(num_test, dtype = self.ytr.dtype)# loop over all test rowsfor i in xrange(num_test):# find the nearest training image to the i'th test image# using the L1 distance (sum of absolute value differences)distances = np.sum(np.abs(self.Xtr - X[i,:]), axis = 1)min_index = np.argmin(distances) # get the index with smallest distanceYpred[i] = self.ytr[min_index] # predict the label of the nearest examplereturn Yprednn = NearestNeighbor() # create a Nearest Neighbor classifier class nn.train(Xtr_rows, Ytr) # train the classifier on the training images and labels Yte_predict = nn.predict(Xte_rows) # predict labels on the test images # and now print the classification accuracy, which is the average number # of examples that are correctly predicted (i.e. label matches) print 'accuracy: %f' % ( np.mean(Yte_predict == Yte) )KNN
用鄰域分析來對CIFAR-10數(shù)據(jù)集進行圖像分類的算法復(fù)雜度比較高,準(zhǔn)確率也不高(斯坦福的講義中為38.6%),我們可以用KNN(k-Nearest Neighbor Classifier)對圖像分類的算法進行改進。在KNN中,我們找到k個與測試集距離最近的圖像,通過投票,確定測試集圖像的類別。之前的算法可以看作是k=1的特例。驗證集超參數(shù)優(yōu)化
KNN方法需要給出參數(shù)K,如何選取最佳的參數(shù)K?通常采用的方法是嘗試所有不同的可能的值,找出中間的最優(yōu)解。但值得一提的是,不建議采用測試集來篩選超參數(shù),這樣會造成過擬合。建議大家只在機器學(xué)習(xí)的模型建立后使用且僅使用一次測試集。 我們可以將訓(xùn)練集分成兩部分,一部分仍舊作為訓(xùn)練集,只是規(guī)模有所減小,另一部分作為驗證集。以 CIFAR-10 為例,我們將原先50000張圖片的訓(xùn)練集分為49000張圖片的訓(xùn)練集和1000張圖片的驗證集,驗證集用來調(diào)整超參數(shù)。Split your training set into training set and a validation set. Use validation set to tune all hyperparameters. At the end run a single time on the test set and report performance.(將訓(xùn)練集劃分為訓(xùn)練集和驗證集,用驗證集調(diào)整所有的超參數(shù),最后,測試集只用一遍,用于測試模型)交叉驗證
有時,訓(xùn)練樣本比較小,無法分割為訓(xùn)練集和驗證集,此時可采用交叉驗證的方式來調(diào)整超參數(shù)。仍舊是以 CIFAR-10 為例,我們可以將訓(xùn)練集的50000張圖片劃分成5個子集,用其中4個子集作為訓(xùn)練集,剩余那個作為驗證集??偣灿形宸N劃分的方式。在每一種方式下,對模型進行評估。最后,在不同的組合里取平均。鄰域分類器的優(yōu)缺點:
訓(xùn)練簡單,測試復(fù)雜。 神經(jīng)網(wǎng)絡(luò)與之相反,神經(jīng)網(wǎng)絡(luò)是訓(xùn)練復(fù)雜,測試簡單。 鄰域分類器的計算復(fù)雜度很大,如何簡化計算復(fù)雜度是一個研究熱點。現(xiàn)有的降低NN計算復(fù)雜度的算法有:ANN, kdtree,k-means等。 對于低維數(shù)據(jù)而言,KNN是有時是一個不錯的選擇。但很少用于圖像分類。通過逐個像素來比較圖像,再進行分類的思路并不可取。總結(jié)
以上是生活随笔為你收集整理的CS231n 学习笔记(1)——神经网络 part1 :图像分类与数据驱动方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7怎么显示盘符 Win7如何查看硬
- 下一篇: CS231n 学习笔记(2)——神经网络