自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

【深度學(xué)習(xí)系列】CNN模型的可視化

企業(yè)動(dòng)態(tài)
前面幾篇文章講到了卷積神經(jīng)網(wǎng)絡(luò)CNN,但是對(duì)于它在每一層提取到的特征以及訓(xùn)練的過程可能還是不太明白,所以這節(jié)主要通過模型的可視化來(lái)神經(jīng)網(wǎng)絡(luò)在每一層中是如何訓(xùn)練的。

前面幾篇文章講到了卷積神經(jīng)網(wǎng)絡(luò)CNN,但是對(duì)于它在每一層提取到的特征以及訓(xùn)練的過程可能還是不太明白,所以這節(jié)主要通過模型的可視化來(lái)神經(jīng)網(wǎng)絡(luò)在每一層中是如何訓(xùn)練的。我們知道,神經(jīng)網(wǎng)絡(luò)本身包含了一系列特征提取器,理想的feature map應(yīng)該是稀疏的以及包含典型的局部信息。通過模型可視化能有一些直觀的認(rèn)識(shí)并幫助我們調(diào)試模型,比如:feature map與原圖很接近,說(shuō)明它沒有學(xué)到什么特征;或者它幾乎是一個(gè)純色的圖,說(shuō)明它太過稀疏,可能是我們feature map數(shù)太多了(feature_map數(shù)太多也反映了卷積核太?。?。可視化有很多種,比如:feature map可視化、權(quán)重可視化等等,我以feature map可視化為例。

 


 模型可視化

  因?yàn)槲覜]有搜到用paddlepaddle在imagenet 1000分類的數(shù)據(jù)集上預(yù)訓(xùn)練好的googLeNet inception v3,所以用了keras做實(shí)驗(yàn),以下圖作為輸入:

  • 輸入圖片 
    • 北汽紳寶D50:

[[223823]]

  • feature map可視化 

  取網(wǎng)絡(luò)的前15層,每層取前3個(gè)feature map。

  北汽紳寶D50 feature map:

  

  從左往右看,可以看到整個(gè)特征提取的過程,有的分離背景、有的提取輪廓,有的提取色差,但也能發(fā)現(xiàn)10、11層中間兩個(gè)feature map是純色的,可能這一層feature map數(shù)有點(diǎn)多了,另外北汽紳寶D50的光暈對(duì)feature map中光暈的影響也能比較明顯看到。

  • Hypercolumns 
    通常我們把神經(jīng)網(wǎng)絡(luò)***一個(gè)fc全連接層作為整個(gè)圖片的特征表示,但是這一表示可能過于粗糙(從上面的feature map可視化也能看出來(lái)),沒法精確描述局部空間上的特征,而網(wǎng)絡(luò)的***層空間特征又太過精確,缺乏語(yǔ)義信息(比如后面的色差、輪廓等),于是論文《Hypercolumns for Object Segmentation and Fine-grained Localization》提出一種新的特征表示方法:Hypercolumns——將一個(gè)像素的 hypercolumn 定義為所有 cnn 單元對(duì)應(yīng)該像素位置的激活輸出值組成的向量),比較好的tradeoff了前面兩個(gè)問題,直觀地看如圖:

 

  把北汽紳寶D50 第1、4、7層的feature map以及第1, 4, 7, 10, 11, 14, 17層的feature map分別做平均,可視化如下:

  


 

代碼實(shí)踐

 
  1 # -*- coding: utf-8 -*-
  2 from keras.applications import InceptionV3
  3 from keras.applications.inception_v3 import preprocess_input
  4 from keras.preprocessing import image
  5 from keras.models import Model
  6 from keras.applications.imagenet_utils import decode_predictions
  7 import numpy as np
  8 import cv2
  9 from cv2 import *
 10 import matplotlib.pyplot as plt
 11 import scipy as sp
 12 from scipy.misc import toimage
 13 
 14 def test_opencv():
 15     # 加載攝像頭
 16     cam = VideoCapture(0)  # 0 -> 攝像頭序號(hào),如果有兩個(gè)三個(gè)四個(gè)攝像頭,要調(diào)用哪一個(gè)數(shù)字往上加嘛
 17     # 抓拍 5 張小圖片
 18     for x in range(0, 5):
 19         s, img = cam.read()
 20         if s:
 21             imwrite("o-" + str(x) + ".jpg", img)
 22 
 23 def load_original(img_path):
 24     # 把原始圖片壓縮為 299*299大小
 25     im_original = cv2.resize(cv2.imread(img_path), (299, 299))
 26     im_converted = cv2.cvtColor(im_original, cv2.COLOR_BGR2RGB)
 27     plt.figure(0)
 28     plt.subplot(211)
 29     plt.imshow(im_converted)
 30     return im_original
 31 
 32 def load_fine_tune_googlenet_v3(img):
 33     # 加載fine-tuning googlenet v3模型,并做預(yù)測(cè)
 34     model = InceptionV3(include_top=True, weights='imagenet')
 35     model.summary()
 36     x = image.img_to_array(img)
 37     x = np.expand_dims(x, axis=0)
 38     x = preprocess_input(x)
 39     preds = model.predict(x)
 40     print('Predicted:', decode_predictions(preds))
 41     plt.subplot(212)
 42     plt.plot(preds.ravel())
 43     plt.show()
 44     return model, x
 45 
 46 def extract_features(ins, layer_id, filters, layer_num):
 47     '''
 48     提取指定模型指定層指定數(shù)目的feature map并輸出到一幅圖上.
 49     :param ins: 模型實(shí)例
 50     :param layer_id: 提取指定層特征
 51     :param filters: 每層提取的feature map數(shù)
 52     :param layer_num: 一共提取多少層feature map
 53     :return: None
 54     '''
 55     if len(ins) != 2:
 56         print('parameter error:(model, instance)')
 57         return None
 58     model = ins[0]
 59     x = ins[1]
 60     if type(layer_id) == type(1):
 61         model_extractfeatures = Model(input=model.input, output=model.get_layer(index=layer_id).output)
 62     else:
 63         model_extractfeatures = Model(input=model.input, output=model.get_layer(name=layer_id).output)
 64     fc2_features = model_extractfeatures.predict(x)
 65     if filters > len(fc2_features[0][0][0]):
 66         print('layer number error.', len(fc2_features[0][0][0]),',',filters)
 67         return None
 68     for i in range(filters):
 69         plt.subplots_adjust(left=0, right=1, bottom=0, top=1)
 70         plt.subplot(filters, layer_num, layer_id + 1 + i * layer_num)
 71         plt.axis("off")
 72         if i < len(fc2_features[0][0][0]):
 73             plt.imshow(fc2_features[0, :, :, i])
 74 
 75 # 層數(shù)、模型、卷積核數(shù)
 76 def extract_features_batch(layer_num, model, filters):
 77     '''
 78     批量提取特征
 79     :param layer_num: 層數(shù)
 80     :param model: 模型
 81     :param filters: feature map數(shù)
 82     :return: None
 83     '''
 84     plt.figure(figsize=(filters, layer_num))
 85     plt.subplot(filters, layer_num, 1)
 86     for i in range(layer_num):
 87         extract_features(model, i, filters, layer_num)
 88     plt.savefig('sample.jpg')
 89     plt.show()
 90 
 91 def extract_features_with_layers(layers_extract):
 92     '''
 93     提取hypercolumn并可視化.
 94     :param layers_extract: 指定層列表
 95     :return: None
 96     '''
 97     hc = extract_hypercolumn(x[0], layers_extract, x[1])
 98     ave = np.average(hc.transpose(1, 2, 0), axis=2)
 99     plt.imshow(ave)
100     plt.show()
101 
102 def extract_hypercolumn(model, layer_indexes, instance):
103     '''
104     提取指定模型指定層的hypercolumn向量
105     :param model: 模型
106     :param layer_indexes: 層id
107     :param instance: 模型
108     :return:
109     '''
110     feature_maps = []
111     for i in layer_indexes:
112         feature_maps.append(Model(input=model.input, output=model.get_layer(index=i).output).predict(instance))
113     hypercolumns = []
114     for convmap in feature_maps:
115         for i in convmap[0][0][0]:
116             upscaled = sp.misc.imresize(convmap[0, :, :, i], size=(299, 299), mode="F", interp='bilinear')
117             hypercolumns.append(upscaled)
118     return np.asarray(hypercolumns)
119 
120 if __name__ == '__main__':
121     img_path = '~/auto1.jpg'
122     img = load_original(img_path)
123     x = load_fine_tune_googlenet_v3(img)
124     extract_features_batch(15, x, 3)
125     extract_features_with_layers([1, 4, 7])
126     extract_features_with_layers([1, 4, 7, 10, 11, 14, 17])

 


 

總結(jié)

  還有一些網(wǎng)站做的關(guān)于CNN的可視化做的非常不錯(cuò),譬如這個(gè)網(wǎng)站:http://shixialiu.com/publications/cnnvis/demo/,大家可以在訓(xùn)練的時(shí)候采取不同的卷積核尺寸和個(gè)數(shù)對(duì)照來(lái)看訓(xùn)練的中間過程。最近PaddlePaddle也開源了可視化工具VisaulDL,下篇文章我們講講paddlepaddle的visualDL和tesorflow的tensorboard。

責(zé)任編輯:張燕妮 來(lái)源: www.cnblogs.com
相關(guān)推薦

2018-03-26 20:07:25

深度學(xué)習(xí)

2022-02-21 00:05:25

深度學(xué)習(xí)可視化工具

2018-04-03 14:42:46

Python神經(jīng)網(wǎng)絡(luò)深度學(xué)習(xí)

2020-03-11 14:39:26

數(shù)據(jù)可視化地圖可視化地理信息

2014-04-23 09:21:38

大數(shù)據(jù)

2017-10-14 13:54:26

數(shù)據(jù)可視化數(shù)據(jù)信息可視化

2020-05-08 13:44:26

Spark架構(gòu)RDD

2022-08-26 09:15:58

Python可視化plotly

2009-04-21 14:26:41

可視化監(jiān)控IT管理摩卡

2017-09-01 10:11:04

深度學(xué)習(xí)可視化工具

2017-01-12 17:28:59

數(shù)據(jù)分析數(shù)據(jù)可視化可視化

2022-08-18 11:36:16

可視化JavaScript事件循環(huán)

2015-08-20 10:06:36

可視化

2022-03-01 10:29:44

Kubernetes容器

2010-06-09 15:09:57

IP網(wǎng)絡(luò)

2017-02-23 09:42:53

大數(shù)據(jù)數(shù)據(jù)可視化技術(shù)誤區(qū)

2022-07-08 15:00:04

農(nóng)業(yè)噴灌項(xiàng)目鴻蒙

2022-06-06 21:46:32

Kubernetes網(wǎng)絡(luò)

2020-09-18 16:37:59

數(shù)據(jù)可視化技術(shù)Python

2018-05-31 08:25:13

誤區(qū)工具可視化
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)