卷積神經(jīng)網(wǎng)絡(luò)層可視化實(shí)戰(zhàn)--Apple的學(xué)習(xí)筆記

學(xué)習(xí)了基本理論后,進(jìn)行keras實(shí)戰(zhàn)練習(xí)手寫數(shù)字識別,主要目的是了解Keras相關(guān)API的使用,并且對深度學(xué)習(xí)再深入理解下。深度學(xué)習(xí)的主要特點(diǎn)是特征的自動提取。我都不需要設(shè)置權(quán)重的初始化值。它會自行修正。

為了再理解下卷積核對特征圖片的影響,所以實(shí)現(xiàn)了一個工程,期望打印出過程中的特征圖片。打印出來后,貌似沒看出什么東東。后來了解了卷積神經(jīng)網(wǎng)絡(luò)的缺點(diǎn)就是物理含義不明確(也就說,我們并不知道沒個卷積層到底提取到的是什么特征,而且神經(jīng)網(wǎng)絡(luò)本身就是一種難以解釋的“黑箱模型”)

python源碼及數(shù)據(jù)集都在github上
https://github.com/AppleCai/Keras_cnn_mytest.git

網(wǎng)絡(luò)模型如下:


model_my.png

打印的參數(shù)如下:


Layer (type) Output Shape Param #


Dense_1_my (Conv2D) (None, 32, 28, 28) 832


pool_1_my (MaxPooling2D) (None, 32, 14, 14) 0


Dense_2_my (Conv2D) (None, 64, 14, 14) 51264


max_pooling2d_1 (MaxPooling2 (None, 64, 7, 7) 0


flatten_1 (Flatten) (None, 3136) 0


dense_1 (Dense) (None, 1024) 3212288


activation_1 (Activation) (None, 1024) 0


dense10 (Dense) (None, 10) 10250


softmax (Activation) (None, 10) 0


參數(shù)如何算?

Dense_1_my參數(shù)個數(shù)計算55321+32=832
Dense_2_my參數(shù)個數(shù)計算5
56432+64=51264

其中5是卷積核的size。

嘗試提取了2層如下效果。

python代碼

    import os
    import numpy as np
    import cv2
    np.random.seed(1337)  # for reproducibility
    from keras.models import Sequential,Model,load_model
    from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten,Input,Conv2D
    from keras.optimizers import Adam
    import random
    
    #工程1,訓(xùn)練出模型
    i,j=0,0
    img_rows, img_cols = 28, 28
    data = np.zeros([8000, 1,img_rows, img_cols])
    label = np.zeros([8000,10])
    sum = 0
    imgs = os.listdir("D:\pytorchpro\pro\mnist_digits_images")
    print(len(os.listdir("D:\pytorchpro\pro\mnist_digits_images\\"+imgs[0])))
    num = len(imgs)
    for i in range(num):
        path="D:\pytorchpro\pro\mnist_digits_images\\"+imgs[i]
        pic=os.listdir(path)
        for j, val in enumerate(pic):
            data[j+sum, :, :,:] = cv2.resize(cv2.cvtColor(cv2.imread(path +"\\" +val), cv2.COLOR_BGR2GRAY), (img_rows, img_cols))/255
            label[j+sum,i:i+1] = i
            if (np.mod(j+sum, 100) == 0):
                print('第', j+sum, '個訓(xùn)練圖片正在裝載')
        sum += j+1 #每個i循環(huán)還要再加1的原因是,list的循環(huán)是從j=0開始的,所以要補(bǔ)加1個。
    
    # print(data.shape)
    #打散
    index = [i for i in range(len(data))]
    random.shuffle(index)
    data = data[index]
    label = label[index]
    #分配數(shù)量
    train_data = data[:7000]
    train_labels = label[:7000]
    validation_data = data[7000:]
    validation_labels = label[7000:]
    # print(train_data.shape)
    # print(train_labels.shape)
    # print(validation_data.shape)
    # print(validation_labels.shape)
    
    #創(chuàng)建模型
    model = Sequential()
    # Conv layer 2 output shape (32, 28, 28)
    model.add(Conv2D(
        batch_input_shape=(None, 1, 28, 28),
        filters=32,
        kernel_size=5,
        strides=1,
        padding='same',     # Padding method
        data_format='channels_first',
        activation='relu',
        name="Dense_1_my"
    ))
    # Pooling layer 1 (max pooling) output shape (32, 14, 14)
    model.add(MaxPooling2D(
        pool_size=2,
        strides=2,
        padding='same',    # Padding method
        data_format='channels_first',
        name="pool_1_my",
    ))
    # Conv layer 2 output shape (64, 14, 14)
    model.add(Convolution2D(64, 5, strides=1, padding='same', data_format='channels_first',activation='relu',name="Dense_2_my"))
    # Pooling layer 2 (max pooling) output shape (64, 7, 7)
    model.add(MaxPooling2D(2, 2, 'same', data_format='channels_first'))
    # Fully connected layer 1 input shape (64 * 7 * 7) = (3136), output shape (1024)
    model.add(Flatten())
    model.add(Dense(1024))
    model.add(Activation('relu'))
    # Fully connected layer 2 to shape (10) for 10 classes
    model.add(Dense(10,name="dense10"))
    model.add(Activation('softmax',name="softmax"))
    # Another way to define your optimizer
    adam = Adam(lr=1e-4)
    # We add metrics to get more results you want to see
    model.compile(optimizer=adam,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    print('Training ------------')
    # Another way to train the model
    model.fit(train_data, train_labels, epochs=5, batch_size=64,)
    print('\nTesting ------------')
    # Evaluate the model with the metrics we defined earlier
    loss, accuracy = model.evaluate(validation_data, validation_labels)
    print('\ntest loss: ', loss)
    print('\ntest accuracy: ', accuracy)
    model.summary()
    from keras.utils.vis_utils import plot_model
    plot_model(model, to_file='model_my.png',show_shapes=True)
    model.save('my_model.h5')
    del model
    
    #工程2,查看部分特征圖
    image = cv2.cvtColor(cv2.imread('2.bmp', 1), cv2.COLOR_BGR2GRAY)
    print(image.shape)
    myimage = np.zeros([1, 1,28, 28])
    myimage[0,0,:,:] = cv2.resize(image, (28, 28))/255
    #可以修改想要導(dǎo)入的模塊
    model = load_model('my_model.h5')
    dense1_layer_model = Model(inputs=model.input,outputs=model.get_layer('Dense_1_my').output)
    out = dense1_layer_model.predict(myimage)
    print (type(out.shape))
    num = out.shape[1]
    print(num)
    image_conv=[]
    out = out.reshape(32,28,28)
    for i in range(num):
        image_conv.append(out[i,:,:].reshape(28,28))
    imgs = np.hstack(image_conv)
    cv2.imshow("Dense_1_my", imgs)

    pool1_layer_model = Model(inputs=model.input, outputs=model.get_layer('Dense_2_my').output)
    out = pool1_layer_model.predict(myimage)
    print(out.shape)
    image_conv2=[]
    out = out.reshape(64,14,14)
    for i in range(64):
        image_conv2.append(out[i,:,:].reshape(14,14))
    imgs2 = np.hstack(image_conv2)
    cv2.imshow("Dense_2_my", imgs2)
    cv2.waitKey(0)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容