成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

【深度學習系列】CNN模型的可視化

企業動態
前面幾篇文章講到了卷積神經網絡CNN,但是對于它在每一層提取到的特征以及訓練的過程可能還是不太明白,所以這節主要通過模型的可視化來神經網絡在每一層中是如何訓練的。

前面幾篇文章講到了卷積神經網絡CNN,但是對于它在每一層提取到的特征以及訓練的過程可能還是不太明白,所以這節主要通過模型的可視化來神經網絡在每一層中是如何訓練的。我們知道,神經網絡本身包含了一系列特征提取器,理想的feature map應該是稀疏的以及包含典型的局部信息。通過模型可視化能有一些直觀的認識并幫助我們調試模型,比如:feature map與原圖很接近,說明它沒有學到什么特征;或者它幾乎是一個純色的圖,說明它太過稀疏,可能是我們feature map數太多了(feature_map數太多也反映了卷積核太?。???梢暬泻芏喾N,比如:feature map可視化、權重可視化等等,我以feature map可視化為例。

 


 模型可視化

  因為我沒有搜到用paddlepaddle在imagenet 1000分類的數據集上預訓練好的googLeNet inception v3,所以用了keras做實驗,以下圖作為輸入:

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

[[223823]]

  • feature map可視化 

  取網絡的前15層,每層取前3個feature map。

  北汽紳寶D50 feature map:

  

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

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

 

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

  


 

代碼實踐

 
  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 -> 攝像頭序號,如果有兩個三個四個攝像頭,要調用哪一個數字往上加嘛
 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模型,并做預測
 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     提取指定模型指定層指定數目的feature map并輸出到一幅圖上.
 49     :param ins: 模型實例
 50     :param layer_id: 提取指定層特征
 51     :param filters: 每層提取的feature map數
 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 # 層數、模型、卷積核數
 76 def extract_features_batch(layer_num, model, filters):
 77     '''
 78     批量提取特征
 79     :param layer_num: 層數
 80     :param model: 模型
 81     :param filters: feature map數
 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])

 


 

總結

  還有一些網站做的關于CNN的可視化做的非常不錯,譬如這個網站:http://shixialiu.com/publications/cnnvis/demo/,大家可以在訓練的時候采取不同的卷積核尺寸和個數對照來看訓練的中間過程。最近PaddlePaddle也開源了可視化工具VisaulDL,下篇文章我們講講paddlepaddle的visualDL和tesorflow的tensorboard。

責任編輯:張燕妮 來源: www.cnblogs.com
相關推薦

2018-03-26 20:07:25

深度學習

2022-02-21 00:05:25

深度學習可視化工具

2018-04-03 14:42:46

Python神經網絡深度學習

2020-03-11 14:39:26

數據可視化地圖可視化地理信息

2014-04-23 09:21:38

大數據

2017-10-14 13:54:26

數據可視化數據信息可視化

2020-05-08 13:44:26

Spark架構RDD

2009-04-21 14:26:41

可視化監控IT管理摩卡

2022-08-26 09:15:58

Python可視化plotly

2017-09-01 10:11:04

深度學習可視化工具

2022-08-18 11:36:16

可視化JavaScript事件循環

2017-01-12 17:28:59

數據分析數據可視化可視化

2015-08-20 10:06:36

可視化

2010-06-09 15:09:57

IP網絡

2022-03-01 10:29:44

Kubernetes容器

2017-02-23 09:42:53

大數據數據可視化技術誤區

2022-07-08 15:00:04

農業噴灌項目鴻蒙

2022-06-06 21:46:32

Kubernetes網絡

2020-09-18 16:37:59

數據可視化技術Python

2018-05-31 08:25:13

誤區工具可視化
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久久亚洲精品视频 | 久久久久久久久中文字幕 | 亚洲一区 中文字幕 | 欧美成年网站 | 日韩午夜影院 | 欧美日韩在线观看视频网站 | 精品欧美一区免费观看α√ | 欧美午夜精品理论片a级按摩 | 亚洲国产aⅴ成人精品无吗 欧美激情欧美激情在线五月 | 91不卡| 激情久久网 | 久久久久亚洲精品国产 | 成人性生交大片 | 久久91av | 亚洲精品电影网在线观看 | 在线成人精品视频 | 国产成人免费网站 | 国产精品一区二区电影 | av片免费 | 日韩精品在线播放 | 久久伊人影院 | 国产一级片免费看 | 国产色网 | 成人在线免费网站 | 欧美日韩亚洲国产综合 | 91精品国产综合久久久久久 | 天天爽夜夜操 | 美女国产| 黄色一级大片在线免费看产 | 国产高清在线精品一区二区三区 | 91精品国产综合久久精品 | 中文字幕亚洲一区二区三区 | 91视频在线看 | 亚洲第一av | 91精品久久久久久久久久入口 | av毛片 | 国产黄色av网站 | 一区二区免费视频 | 亚洲精品视频观看 | 色视频网站在线观看 | 国产精品激情在线 |