CS231n 学习笔记(1)——神经网络 part1 :图像分类与数据驱动方法
生活随笔
收集整理的這篇文章主要介紹了
CS231n 学习笔记(1)——神经网络 part1 :图像分类与数据驱动方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
*此系列為斯坦福李飛飛團隊的系列公開課“cs231n convolutional neural network for visual recognition ”的學習筆記。本文主要是對module 1 的part1 Image classification 的翻譯與學習。
圖像分類是指對于輸入的圖像,從一系列的標簽中找出正確的與之對應的標簽,圖像分類是許多復雜的計算機視覺算法的基礎。
對于計算機而言,它看到的圖像可以看成是一個三維數組,其中灰度圖像的寬度和高度各占一維,還有一維是RGB的組合。
幾乎所有的圖像分類算法都會面臨以下幾方面問題:1. 觀察角度會變化。2. 物體大小會變化,觀察范圍會變化。3. 變形。4. 背景干擾。5. 類內變化。例如,椅子的形狀可以有很多種。
數據驅動方法
我們并不是去寫一個算法來識別圖像中的貓,而是想對待孩童一樣,我們把每一個類型中大量的圖片提供給計算機,然后寫一個學習算法,讓計算機自己學會分類。這種方法是基于數據驅動的。需要訓練集和標簽數據集。圖像分類過程
圖像分類是為每一個數組分配一個標簽。這個模式化過程如下: -輸入--訓練集,有許多有標簽的圖像構成。 -學習--用訓練集訓練分類器,進行數學建模。 -評價--用未標簽的新數據評價分類器的性能。正確率越高,學習的效果越好。最近鄰域分類法
此法并不是卷積網絡的方法,圖像分類的實踐中也很少用到,但是這個方法有助于幫我們理解圖像分類。 最近領域法將測試圖片和訓練集中的每一幅圖求距離,兩幅圖像之間的距離是指這兩幅圖像中的每一個對于像素的差距之和。使用下面這個公式來求取: 在求得測試集圖像與訓練集的沒衣服圖像的距離之后,找到距離最近的圖像,將測試集圖像歸為此類。輸入訓練集及測試集
cs231n通過函數`Xtr, Ytr, Xte, Yte = load_CIFAR10('data/cifar10/')`導入 CIFAR-10 數據集, CIFAR-10 中有50000個訓練樣本,每個樣本大小是32 x 32 x 3,每個樣本有一個標簽。此外,CIFAR-10 中還有10000個測試樣本。我在網絡上沒有搜到`load_CIFAR10()`函數的源代碼,因此需要自己動手來寫一個數據導入程序。 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 數據集的主要過程是,先將CIFAR-10 數據集的訓練數據寫入一個字典變量中,再將字典變量中的data和label分別拿到兩個數組中去。用同樣的方法獲取測試數據,最后返回四個數組。 在程序最初版本里,路徑中都是單斜杠,可是總是編譯不通過,始終報錯提示:invalid mode ('rb') or filename: 'C:\\Users\\Administrator\\Documents\\Python Scripts\\cifar-10-batches-py\test_batch' 將路徑中所有的單斜杠都變成雙斜杠后,編譯就能通過了。 導入數據后,通過最簡單的鄰域聚類的方法,對圖像進行分類 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數據集進行圖像分類的算法復雜度比較高,準確率也不高(斯坦福的講義中為38.6%),我們可以用KNN(k-Nearest Neighbor Classifier)對圖像分類的算法進行改進。在KNN中,我們找到k個與測試集距離最近的圖像,通過投票,確定測試集圖像的類別。之前的算法可以看作是k=1的特例。驗證集超參數優化
KNN方法需要給出參數K,如何選取最佳的參數K?通常采用的方法是嘗試所有不同的可能的值,找出中間的最優解。但值得一提的是,不建議采用測試集來篩選超參數,這樣會造成過擬合。建議大家只在機器學習的模型建立后使用且僅使用一次測試集。 我們可以將訓練集分成兩部分,一部分仍舊作為訓練集,只是規模有所減小,另一部分作為驗證集。以 CIFAR-10 為例,我們將原先50000張圖片的訓練集分為49000張圖片的訓練集和1000張圖片的驗證集,驗證集用來調整超參數。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.(將訓練集劃分為訓練集和驗證集,用驗證集調整所有的超參數,最后,測試集只用一遍,用于測試模型)交叉驗證
有時,訓練樣本比較小,無法分割為訓練集和驗證集,此時可采用交叉驗證的方式來調整超參數。仍舊是以 CIFAR-10 為例,我們可以將訓練集的50000張圖片劃分成5個子集,用其中4個子集作為訓練集,剩余那個作為驗證集。總共有五種劃分的方式。在每一種方式下,對模型進行評估。最后,在不同的組合里取平均。鄰域分類器的優缺點:
訓練簡單,測試復雜。 神經網絡與之相反,神經網絡是訓練復雜,測試簡單。 鄰域分類器的計算復雜度很大,如何簡化計算復雜度是一個研究熱點。現有的降低NN計算復雜度的算法有:ANN, kdtree,k-means等。 對于低維數據而言,KNN是有時是一個不錯的選擇。但很少用于圖像分類。通過逐個像素來比較圖像,再進行分類的思路并不可取。總結
以上是生活随笔為你收集整理的CS231n 学习笔记(1)——神经网络 part1 :图像分类与数据驱动方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7怎么显示盘符 Win7如何查看硬
- 下一篇: CS231n 学习笔记(2)——神经网络