上次作業(yè)最后加粗了,要用features來分類,原來后一個作業(yè)便是。
之前四個小作業(yè)都是用像素作為圖像的描述,進(jìn)行分類,效果最好的是兩層神經(jīng)網(wǎng)絡(luò),達(dá)到50%了。現(xiàn)在將圖像用features來描述。 用作業(yè)里的提示來說就是
For each image we will compute a Histogram of Oriented Gradients (HOG) as well as a color histogram using the hue channel in HSV color space. We form our final feature vector for each image by concatenating the HOG and color histogram feature vectors. Roughly speaking, HOG should capture the texture of the image while ignoring color information, and the color histogram rePResents the color of the input image while ignoring texture. As a result, we expect that using both together ought to work better than using either alone. Verifying this assumption would be a good thing to try for the bonus section. The hog_feature and color_histogram_hsv functions both Operate on a single image and return a feature vector for that image. The extract_features function takes a set of images and a list of feature functions and evaluates each feature function on each image, storing the results in a matrix where each column is the concatenation of all feature vectors for a single image.
用HOG和HSV特征來描述圖像,HOG會重視紋理結(jié)構(gòu),忽視了顏色,而HSV重視顏色而忽視了紋理結(jié)構(gòu),于是將兩者一起用來描述圖像。下面分別用HOG、 HSV 、HOG+HSV來做測試。
HOG特征是指方向梯度直方圖(Histogram of Oriented Gradient, HOG),是一種在計算機(jī)視覺和圖像處理中用來進(jìn)行物體檢測的特征描述子。具體可以Google。在這里,作業(yè)中給出了計算一張圖片HOG特征的方法。
def hog_feature(im): """Compute Histogram of Gradient (HOG) feature for an image Modified from skimage.feature.hog http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog Reference: Histograms of Oriented Gradients for Human Detection Navneet Dalal and Bill Triggs, CVPR 2005 Parameters: im : an input grayscale or rgb image Returns: feat: Histogram of Gradient (HOG) feature """ # convert rgb to grayscale if needed if im.ndim == 3: image = rgb2gray(im) else: image = np.at_least_2d(im) sx, sy = image.shape # image size orientations = 9 # number of gradient bins cx, cy = (8, 8) # pixels per cell gx = np.zeros(image.shape) gy = np.zeros(image.shape) gx[:, :-1] = np.diff(image, n=1, axis=1) # compute gradient on x-direction gy[:-1, :] = np.diff(image, n=1, axis=0) # compute gradient on y-direction grad_mag = np.sqrt(gx ** 2 + gy ** 2) # gradient magnitude grad_ori = np.arctan2(gy, (gx + 1e-15)) * (180 / np.pi) + 90 # gradient orientation n_cellsx = int(np.floor(sx / cx)) # number of cells in x n_cellsy = int(np.floor(sy / cy)) # number of cells in y # compute orientations integral images orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations)) for i in range(orientations): # create new integral image for this orientation # isolate orientations in this range temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori, 0) temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0) # select magnitudes for those orientations cond2 = temp_ori > 0 temp_mag = np.where(cond2, grad_mag, 0) orientation_histogram[:,:,i] = uniform_filter(temp_mag, size=(cx, cy))[cx/2::cx, cy/2::cy].T return orientation_histogram.ravel()注意:為了方便,將最終返回的特征展成一維,方便與其他特征append。這里返回的特征為144維。 將測試代碼中feature_fns設(shè)為一個hog_feature
feature_fns = [hog_feature]最終用2層神經(jīng)網(wǎng)絡(luò)得到驗證集準(zhǔn)確率為56%
lr 5.000000e-01 reg 1.000000e-03 val accuracy: 0.560000 best validation accuracy achieved during cross-validation: 0.560000
測試集為56.4%
net = best_net test_acc = (net.predict(X_test_feats) == y_test).mean() print test_acc 0.564
其中用SVM做分類時,分類錯誤的圖像可以看一下
卡車和汽車輪廓差不多,會誤識別,
HSV顏色模型中顏色的參數(shù)分別是:色調(diào)(H),飽和度(S),明度(V)。作業(yè)里也給了計算特征的代碼。
def color_histogram_hsv(im, nbin=10, xmin=0, xmax=255, normalized=True): """ Compute color histogram for an image using hue. Inputs: - im: H x W x C array of pixel data for an RGB image. - nbin: Number of histogram bins. (default: 10) - xmin: Minimum pixel value (default: 0) - xmax: Maximum pixel value (default: 255) - normalized: Whether to normalize the histogram (default: True) Returns: 1D vector of length nbin giving the color histogram over the hue of the input image. """ ndim = im.ndim bins = np.linspace(xmin, xmax, nbin+1) hsv = matplotlib.colors.rgb_to_hsv(im/xmax) * xmax imhist, bin_edges = np.histogram(hsv[:,:,0], bins=bins, density=normalized) imhist = imhist * np.diff(bin_edges) # return histogram return imhist輸出的imhist為10維,也就是在0~255中有10個bins,imhist歸一化了,imhist.sum() = 1.0 將測試代碼中feature_fns設(shè)為一個lambda img: color_histogram_hsv(img, nbin=num_color_bins)
num_color_bins = 10 # Number of bins in the color histogramfeature_fns = [lambda img: color_histogram_hsv(img, nbin=num_color_bins)]最終用2層神經(jīng)網(wǎng)絡(luò)得到驗證集準(zhǔn)確率為30.2%
lr 5.000000e-01 reg 1.000000e-03 val accuracy: 0.302000 best validation accuracy achieved during cross-validation: 0.302000
測試集為26.9%
net = best_nettest_acc = (net.predict(X_test_feats) == y_test).mean()print test_acc0.269相比與HOG特征,HSV得到的準(zhǔn)確率降了很多,一種理解是卡車和汽車并不是用顏色來區(qū)別的,所以紋理更勝一籌。
將測試代碼中feature_fns設(shè)為兩個
num_color_bins = 10 # Number of bins in the color histogramfeature_fns = [hog_feature, lambda img: color_histogram_hsv(img, nbin=num_color_bins)]最終用2層神經(jīng)網(wǎng)絡(luò)得到驗證集準(zhǔn)確率為58.9%,可以看到如果超參數(shù)沒有選好,準(zhǔn)確率會很低
lr 1.000000e-02 reg 1.000000e-03 val accuracy: 0.091000 lr 1.000000e-02 reg 5.000000e-03 val accuracy: 0.144000 lr 1.000000e-02 reg 1.000000e-02 val accuracy: 0.134000 lr 1.000000e-02 reg 1.000000e-01 val accuracy: 0.161000 lr 1.000000e-02 reg 5.000000e-01 val accuracy: 0.079000 lr 1.000000e-02 reg 1.000000e+00 val accuracy: 0.078000 lr 1.000000e-01 reg 1.000000e-03 val accuracy: 0.521000 lr 1.000000e-01 reg 5.000000e-03 val accuracy: 0.510000 lr 1.000000e-01 reg 1.000000e-02 val accuracy: 0.507000 lr 1.000000e-01 reg 1.000000e-01 val accuracy: 0.431000 lr 1.000000e-01 reg 5.000000e-01 val accuracy: 0.079000 lr 1.000000e-01 reg 1.000000e+00 val accuracy: 0.087000 lr 5.000000e-01 reg 1.000000e-03 val accuracy: 0.589000 lr 5.000000e-01 reg 5.000000e-03 val accuracy: 0.567000 lr 5.000000e-01 reg 1.000000e-02 val accuracy: 0.568000 lr 5.000000e-01 reg 1.000000e-01 val accuracy: 0.404000 lr 5.000000e-01 reg 5.000000e-01 val accuracy: 0.079000 lr 5.000000e-01 reg 1.000000e+00 val accuracy: 0.107000 lr 1.000000e+00 reg 1.000000e-03 val accuracy: 0.586000 lr 1.000000e+00 reg 5.000000e-03 val accuracy: 0.571000 lr 1.000000e+00 reg 1.000000e-02 val accuracy: 0.529000 lr 1.000000e+00 reg 1.000000e-01 val accuracy: 0.420000 lr 1.000000e+00 reg 5.000000e-01 val accuracy: 0.154000 lr 1.000000e+00 reg 1.000000e+00 val accuracy: 0.102000 lr 5.000000e+00 reg 1.000000e-03 val accuracy: 0.087000 lr 5.000000e+00 reg 5.000000e-03 val accuracy: 0.078000 lr 5.000000e+00 reg 1.000000e-02 val accuracy: 0.079000 lr 5.000000e+00 reg 1.000000e-01 val accuracy: 0.078000 lr 5.000000e+00 reg 5.000000e-01 val accuracy: 0.087000 lr 5.000000e+00 reg 1.000000e+00 val accuracy: 0.087000 best validation accuracy achieved during cross-validation: 0.589000
測試集為57.3%,比原先只用的56.4%提升了一點(diǎn)。O(∩_∩)O哈哈~
net = best_nettest_acc = (net.predict(X_test_feats) == y_test).mean()print test_acc0.573其他特征作為分類依據(jù),先加粗,防忘
https://zhuanlan.zhihu.com/p/21441838?refer=intelligentunit
新聞熱點(diǎn)
疑難解答