2020人工神經網路第一次作業-參考答案第十部分

卓晴發表於2020-11-27

本文是 2020人工神經網路第一次作業 的參考答案第十部分

 

第十題參考答案-第1小題


1.題目分析

(1) 資料集分析

資料集合包括有兩個目錄:test, train。其中分別包括有95,510張車牌號的圖片。圖片命名的最後一個字元表明了車牌號的顏色。

(2) 資料轉換

使用本文後面作業程式附件中的:1.將車牌圖片目錄轉換成YUV將test、train兩個車牌號的圖片轉換成YUV引數。形成兩個資料NPZ檔案:test_data_npz, train_data_npz。每個檔案包括有"yuv", "color"兩個陣列。

使用本文後面作業程式附件中的2.將訓練資料的顏色轉換成one-hot編碼將資料中的顏色轉換成one-hot的形式。one-hot的三個分量分別表示為(藍、白、黃)。

3.構建網路

構建單隱層神經網路,其結構如下圖所示:

  • 隱層節點:4,傳遞函式:sigmoid
  • 輸入層節點:3
  • 輸出層節點:3,傳遞函式:線性。

▲ 網路結構

▲ 網路結構

具體程式參見本文後面的程式:4.車牌顏色識別BP網路演算法

其中的BP網路演算法參見程式:5.單隱層BP網路演算法

2.求解過程

訓練引數:

  • 學習速率: η = 0.5 \eta = 0.5 η=0.5
  • 迭代次數: N = 5000 N = 5000 N=5000

下圖顯示了訓練誤差和測試樣本變化情況。

▲ 網路訓練誤差收斂曲線

▲ 網路訓練誤差收斂曲線

3.結果分析

由於測試樣本過於少,所以網路的誤差收斂到0.1以下之後,測試誤差便減小到4,並一直維持在這個水平。

對於訓練樣本中出現錯誤的兩個車牌號如下:
▲ 產生錯誤的兩個樣本

▲ 產生錯誤的兩個樣本

000280_白.jpg
000497_藍.jpg

其中一個是白色車牌號,它本身就是非常少的樣本。
另外一個是本身就標識出錯的樣本。

 

第十題參考答案-第2小題


1.題目分析

2.求解過程

3.結果分析

 

第十題參考答案-第3小題


1.題目分析

2.求解過程

3.結果分析

 

※ 作業程式


1.將車牌圖片目錄轉換成YUV

from PIL                    import Image

train_dir = r'D:\Temp\dataset\color\train'
test_dir  = r'D:\Temp\dataset\color\test'

#------------------------------------------------------------
def dl_dir_data(dirstr):
    """Convert the driver license picture to YUV vector
    In: dirstr-directory of the picture file
    Out: yuv_array, color_array
    """

    color_array = []
    yuv_array   = []
    cm = array([[0.299, 0.587, 0.114], [-0.147108, -0.288804,  0.435912], [0.614777, -0.514799,-0.099978]])

    for f in os.listdir(dirstr):
        fname = os.path.join(dirstr, f)
#        printf(f, fname)
        img = Image.open(fname).convert('RGB')
        imgdata = array(img)/255.0

        imgdata = imgdata.dot(cm)
        yuv = mean(imgdata, (0,1))

        yuv_array.append(list(yuv))
        color_array.append(f.split('.')[0][-1])

    return yuv_array, color_array

if __name__ == "__main__":
    yuv,color = dl_dir_data(train_dir)
    tspsave('train_data', yuv=yuv, color=color)
    printf(shape(yuv), shape(color))

2.將訓練資料的顏色轉換成one-hot編碼

#------------------------------------------------------------
yuv0, color0 = tspload('test_data', 'yuv', 'color')
yuv1, color1 = tspload('train_data', 'yuv', 'color')

def color_code(colorname, cd):
    cdnum = len(cd)
    cddim = []
    for c in colorname:
        id = cd.index(c)
        code = zeros(cdnum)
        code[id] = 1
        cddim.append(code)

    return cddim

#------------------------------------------------------------
if __name__ == "__main__":
    colordim = list(set(hstack((color0, color1))))

    color0_c = color_code(color0, colordim)
    color1_c = color_code(color1, colordim)

    printf(color1_c)

    tspsave('testc_data', yuv=list(yuv0), color=color0_c)
    tspsave('trainc_data', yuv=list(yuv1), color=color1_c)

4.車牌顏色識別BP網路演算法

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# HW110_1_BP.PY                    -- by Dr. ZhuoQing 2020-11-19
#
# Note:
#============================================================

from headm import *
from bp1sigmoid                import *

yuv0, color0 = tspload('testc_data', 'yuv', 'color')
yuv1, color1 = tspload('trainc_data', 'yuv', 'color')

x_train = yuv1
y_train = color1.T

#------------------------------------------------------------

#------------------------------------------------------------
# Define the training
DISP_STEP           = 100

#------------------------------------------------------------
pltgif = PlotGIF()

#------------------------------------------------------------
def train(X, Y, num_iterations, learning_rate, print_cost=False, Hn=10):
    n_x = X.shape[1]
    n_y = Y.shape[0]
    n_h = Hn

    lr = learning_rate

    parameters = initialize_parameters(n_x, n_h, n_y)
    XX,YY = x_train, y_train #shuffledata(x_train, y_train)

    costdim = []
    errordim = []

    for i in range(0, num_iterations):
        A2, cache = forward_propagate(XX, parameters)
        cost = calculate_cost(A2, YY, parameters)
        grads = backward_propagate(parameters, cache, XX, YY)
        parameters = update_parameters(parameters, grads, lr)

        if print_cost and i % DISP_STEP == 0:
            err = error(yuv0, color0.T)
            printf('Cost after iteration:%i: %f, error:%d'%(i, cost, err))
            costdim.append(cost)
            errordim.append(err)

    return parameters, costdim,errordim

#------------------------------------------------------------
def error(A2, y):
    A2[A2 > 0.5] = 1
    A2[A2 <= 0.5] = 0
    return sum(A2 != y)

#------------------------------------------------------------
parameters,cost,err = train(x_train,y_train, 5000, 0.5, True, 4)
A2, cache = forward_propagate(x_train, parameters)

stepdim = arange(0, len(cost)) * DISP_STEP
plt.subplot(211)
plt.plot(stepdim, cost)
plt.xlabel("Step")
plt.ylabel("Cost")
plt.grid(True)
plt.tight_layout()

plt.subplot(212)
plt.plot(stepdim, err)
plt.xlabel("Step")
plt.ylabel("Error")
plt.grid(True)
plt.tight_layout()

plt.show()

'''
cost_dim = []
Hn_dim = []
Err_dim = []

for i in linspace(0.01, 0.5, 100):
    Hn = i + 1
    parameter,costdim = train(x_train, y_train, 2000, i, True, 10)

    cost_dim.append(costdim)
    Hn_dim.append(i)
    Err_dim.append(costdim[-1])

    tspsave('data', costdim = cost_dim, Hndim=Hn_dim, err=Err_dim)

plt.plot(Hn_dim, Err_dim)
plt.xlabel("Learning Rate")
plt.ylabel("Error")
plt.grid(True)
plt.tight_layout()
plt.show()
'''
#------------------------------------------------------------
#        END OF FILE : HW110_1_BP.PY
#============================================================

5.單隱層BP網路演算法

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# BP1SIGMOID.PY                    -- by Dr. ZhuoQing 2020-11-17
#
# Note:
#============================================================

from headm import *

#------------------------------------------------------------
# Samples data construction

random.seed(int(time.time()))

#------------------------------------------------------------
def shuffledata(X, Y):
    id = list(range(X.shape[0]))
    random.shuffle(id)
    return X[id], (Y.T[id]).T

#------------------------------------------------------------
# Define and initialization NN
def initialize_parameters(n_x, n_h, n_y):
    W1 = random.randn(n_h, n_x) * 0.5          # dot(W1,X.T)
    W2 = random.randn(n_y, n_h) * 0.5          # dot(W2,Z1)
    b1 = zeros((n_h, 1))                       # Column vector
    b2 = zeros((n_y, 1))                       # Column vector

    parameters = {'W1':W1,
                  'b1':b1,
                  'W2':W2,
                  'b2':b2}

    return parameters

#------------------------------------------------------------
# Forward propagattion
# X:row->sample;
# Z2:col->sample
def forward_propagate(X, parameters):
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    Z1 = dot(W1, X.T) + b1                    # X:row-->sample; Z1:col-->sample
    A1 = 1/(1+exp(-Z1))

    Z2 = dot(W2, A1) + b2                     # Z2:col-->sample
    A2 = Z2                                   # Linear output

    cache = {'Z1':Z1,
             'A1':A1,
             'Z2':Z2,
             'A2':A2}
    return Z2, cache

#------------------------------------------------------------
# Calculate the cost
# A2,Y: col->sample
def calculate_cost(A2, Y, parameters):
    err = [x1-x2 for x1,x2 in zip(A2.T, Y.T)]
    cost = [dot(e,e) for e in err]
    return mean(cost)

#------------------------------------------------------------
# Backward propagattion
def backward_propagate(parameters, cache, X, Y):
    m = X.shape[0]                  # Number of the samples

    W1 = parameters['W1']
    W2 = parameters['W2']
    A1 = cache['A1']
    A2 = cache['A2']

    dZ2 = (A2 - Y)
    dW2 = dot(dZ2, A1.T) / m
    db2 = sum(dZ2, axis=1, keepdims=True) / m

    dZ1 = dot(W2.T, dZ2) * (A1 * (1-A1))
    dW1 = dot(dZ1, X) / m
    db1 = sum(dZ1, axis=1, keepdims=True) / m

    grads = {'dW1':dW1,
             'db1':db1,
             'dW2':dW2,
             'db2':db2}

    return grads

#------------------------------------------------------------
# Update the parameters
def update_parameters(parameters, grads, learning_rate):
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    dW1 = grads['dW1']
    db1 = grads['db1']
    dW2 = grads['dW2']
    db2 = grads['db2']

    W1 = W1 - learning_rate * dW1
    W2 = W2 - learning_rate * dW2
    b1 = b1 - learning_rate * db1
    b2 = b2 - learning_rate * db2

    parameters = {'W1':W1,
                  'b1':b1,
                  'W2':W2,
                  'b2':b2}

    return parameters

#------------------------------------------------------------
#        END OF FILE : BP1SIGMOID.PY
#============================================================

相關文章