日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

keras系列︱Application中五款已训练模型、VGG16框架(Sequential式、Model式)解读(二)...

發(fā)布時(shí)間:2023/12/6 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 keras系列︱Application中五款已训练模型、VGG16框架(Sequential式、Model式)解读(二)... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

引自:http://blog.csdn.net/sinat_26917383/article/details/72859145

中文文檔:http://keras-cn.readthedocs.io/en/latest/?
官方文檔:https://keras.io/?
文檔主要是以keras2.0。?
.


.

Keras系列:

1、keras系列︱Sequential與Model模型、keras基本結(jié)構(gòu)功能(一)?
2、keras系列︱Application中五款已訓(xùn)練模型、VGG16框架(Sequential式、Model式)解讀(二)?
3、keras系列︱圖像多分類訓(xùn)練與利用bottleneck features進(jìn)行微調(diào)(三)?
4、keras系列︱人臉表情分類與識(shí)別:opencv人臉檢測+Keras情緒分類(四)?
5、keras系列︱遷移學(xué)習(xí):利用InceptionV3進(jìn)行fine-tuning及預(yù)測、完整案例(五)


一、Application的五款已訓(xùn)練模型 + H5py簡述

Kera的應(yīng)用模塊Application提供了帶有預(yù)訓(xùn)練權(quán)重的Keras模型,這些模型可以用來進(jìn)行預(yù)測、特征提取和finetune。?
后續(xù)還有對以下幾個(gè)模型的參數(shù)介紹:

  • Xception
  • VGG16
  • VGG19
  • ResNet50
  • InceptionV3

所有的這些模型(除了Xception)都兼容Theano和Tensorflow,并會(huì)自動(dòng)基于~/.keras/keras.json的Keras的圖像維度進(jìn)行自動(dòng)設(shè)置。例如,如果你設(shè)置data_format=”channel_last”,則加載的模型將按照TensorFlow的維度順序來構(gòu)造,即“Width-Height-Depth”的順序。

模型的官方下載路徑:https://github.com/fchollet/deep-learning-models/releases

其中:?
.

1、thtf的區(qū)別

==================

Keras提供了兩套后端,Theano和Tensorflow,?
th和tf的大部分功能都被backend統(tǒng)一包裝起來了,但二者還是存在不小的沖突,有時(shí)候你需要特別注意Keras是運(yùn)行在哪種后端之上,它們的主要沖突有:

dim_ordering,也就是維度順序。比方說一張224*224的彩色圖片,theano的維度順序是(3,224,224),即通道維在前。而tf的維度順序是(224,224,3),即通道維在后。?
卷積層權(quán)重的shape:從無到有訓(xùn)練一個(gè)網(wǎng)絡(luò),不會(huì)有任何問題。但是如果你想把一個(gè)th訓(xùn)練出來的卷積層權(quán)重載入風(fēng)格為tf的卷積層……說多了都是淚。我一直覺得這個(gè)是個(gè)bug,數(shù)據(jù)的dim_ordering有問題就罷了,為啥卷積層權(quán)重的shape還需要變換咧?我遲早要提個(gè)PR把這個(gè)bug修掉!?
然后是卷積層kernel的翻轉(zhuǎn)不翻轉(zhuǎn)問題,這個(gè)我們說過很多次了,就不再多提。?
數(shù)據(jù)格式的區(qū)別,channels_last”對應(yīng)原本的“tf”,“channels_first”對應(yīng)原本的“th”。?
以128x128的RGB圖像為例,“channels_first”應(yīng)將數(shù)據(jù)組織為(3,128,128),而“channels_last”應(yīng)將數(shù)據(jù)組織為(128,128,3)。?
譬如:?
vgg16_weights_th_dim_ordering_th_kernels_notop.h5?
vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5?
.

2、notop模型是指什么?

==============

是否包含最后的3個(gè)全連接層(whether to include the 3 fully-connected layers at the top of the network)。用來做fine-tuning專用,專門開源了這類模型。?
.

3、H5py簡述

========

keras的已訓(xùn)練模型是H5PY格式的,不是caffe的.caffemodel?
h5py.File類似Python的詞典對象,因此我們可以查看所有的鍵值:?
讀入

file=h5py.File('.../notop.h5','r') f.attrs['nb_layers'],代表f的屬性,其中有一個(gè)屬性為'nb_layers'

?

>>> f.keys() [u'block1_conv1', u'block1_conv2', u'block1_pool', u'block2_conv1', u'block2_conv2', u'block2_pool', u'block3_conv1', u'block3_conv2', u'block3_conv3', u'block3_pool', u'block4_conv1', u'block4_conv2', u'block4_conv3', u'block4_pool', u'block5_conv1', u'block5_conv2', u'block5_conv3', u'block5_pool']

?

可以看到f中各個(gè)層內(nèi)有些什么。

for name in f:print(name)# 類似f.keys()

.

4、官方案例——利用ResNet50網(wǎng)絡(luò)進(jìn)行ImageNet分類

================================

rom keras.applications.resnet50 import ResNet50 from keras.preprocessing import image from keras.applications.resnet50 import preprocess_input, decode_predictions import numpy as npmodel = ResNet50(weights='imagenet') img_path = 'elephant.jpg' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) print('Predicted:', decode_predictions(preds, top=3)[0]) # Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]

還有的案例可見Keras官方文檔

利用VGG16提取特征、從VGG19的任意中間層中抽取特征、在定制的輸入tensor上構(gòu)建InceptionV3

.

5、調(diào)用參數(shù)解釋

========

以下幾類,因?yàn)檎{(diào)用好像都是從網(wǎng)站下載權(quán)重,所以可以自己修改一下源碼,讓其可以讀取本地H5文件。

Xception模型

ImageNet上,該模型取得了驗(yàn)證集top1 0.790和top5 0.945的正確率;?
,該模型目前僅能以TensorFlow為后端使用,由于它依賴于”SeparableConvolution”層,目前該模型只支持channels_last的維度順序(width, height, channels)

默認(rèn)輸入圖片大小為299x299

keras.applications.xception.Xception(include_top=True, weights='imagenet',input_tensor=None, input_shape=None,pooling=None, classes=1000)

?

VGG16模型

VGG16模型,權(quán)重由ImageNet訓(xùn)練而來

該模型再Theano和TensorFlow后端均可使用,并接受channels_first和channels_last兩種輸入維度順序

模型的默認(rèn)輸入尺寸時(shí)224x224

keras.applications.vgg16.VGG16(include_top=True, weights='imagenet',input_tensor=None, input_shape=None,pooling=None, classes=1000)

VGG19模型

VGG19模型,權(quán)重由ImageNet訓(xùn)練而來

該模型在Theano和TensorFlow后端均可使用,并接受channels_first和channels_last兩種輸入維度順序

模型的默認(rèn)輸入尺寸時(shí)224x224

keras.applications.vgg19.VGG19(include_top=True, weights='imagenet',input_tensor=None, input_shape=None,pooling=None, classes=1000)

?

ResNet50模型

50層殘差網(wǎng)絡(luò)模型,權(quán)重訓(xùn)練自ImageNet

該模型在Theano和TensorFlow后端均可使用,并接受channels_first和channels_last兩種輸入維度順序

模型的默認(rèn)輸入尺寸時(shí)224x224

keras.applications.resnet50.ResNet50(include_top=True, weights='imagenet',input_tensor=None, input_shape=None,pooling=None, classes=1000)

InceptionV3模型

InceptionV3網(wǎng)絡(luò),權(quán)重訓(xùn)練自ImageNet

該模型在Theano和TensorFlow后端均可使用,并接受channels_first和channels_last兩種輸入維度順序

模型的默認(rèn)輸入尺寸時(shí)299x299

keras.applications.inception_v3.InceptionV3(include_top=True,weights='imagenet',input_tensor=None,input_shape=None,pooling=None, classes=1000)

.


二、 keras-applications-VGG16解讀——函數(shù)式

.py文件來源于:https://github.com/fchollet/deep-learning-models/blob/master/vgg16.py?
VGG16默認(rèn)的輸入數(shù)據(jù)格式應(yīng)該是:channels_last

# -*- coding: utf-8 -*- '''VGG16 model for Keras. # Reference: - [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556) ''' from __future__ import print_functionimport numpy as np import warnings from keras.models import Model from keras.layers import Flatten from keras.layers import Dense from keras.layers import Input from keras.layers import Conv2D from keras.layers import MaxPooling2D from keras.layers import GlobalMaxPooling2D from keras.layers import GlobalAveragePooling2D from keras.preprocessing import image from keras.utils import layer_utils from keras.utils.data_utils import get_file from keras import backend as K from keras.applications.imagenet_utils import decode_predictions # decode_predictions 輸出5個(gè)最高概率:(類名, 語義概念, 預(yù)測概率) decode_predictions(y_pred) from keras.applications.imagenet_utils import preprocess_input # 預(yù)處理 圖像編碼服從規(guī)定,譬如,RGB,GBR這一類的,preprocess_input(x) from keras.applications.imagenet_utils import _obtain_input_shape # 確定適當(dāng)?shù)妮斎胄螤?#xff0c;相當(dāng)于opencv中的read.img,將圖像變?yōu)閿?shù)組 from keras.engine.topology import get_source_inputs WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5' WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5' def VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): # 檢查weight與分類設(shè)置是否正確 if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # 設(shè)置圖像尺寸,類似caffe中的transform # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=48, # 模型所能接受的最小長寬 data_format=K.image_data_format(), # 數(shù)據(jù)的使用格式 include_top=include_top) #是否通過一個(gè)Flatten層再連接到分類器 # 數(shù)據(jù)簡單處理,resize if input_tensor is None: img_input = Input(shape=input_shape) # 這里的Input是keras的格式,可以用于轉(zhuǎn)換 else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # 如果是tensor的數(shù)據(jù)格式,需要兩步走: # 先判斷是否是keras指定的數(shù)據(jù)類型,is_keras_tensor # 然后get_source_inputs(input_tensor) # 編寫網(wǎng)絡(luò)結(jié)構(gòu),prototxt # Block 1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) if include_top: # Classification block x = Flatten(name='flatten')(x) x = Dense(4096, activation='relu', name='fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # 調(diào)整數(shù)據(jù) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) # get_source_inputs 返回計(jì)算需要的數(shù)據(jù)列表,List of input tensors. # 如果是tensor的數(shù)據(jù)格式,需要兩步走: # 先判斷是否是keras指定的數(shù)據(jù)類型,is_keras_tensor # 然后get_source_inputs(input_tensor) else: inputs = img_input # 創(chuàng)建模型 # Create model. model = Model(inputs, x, name='vgg16') # 加載權(quán)重 # load weights if weights == 'imagenet': if include_top: weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='block5_pool') shape = maxpool.output_shape[

總結(jié)

以上是生活随笔為你收集整理的keras系列︱Application中五款已训练模型、VGG16框架(Sequential式、Model式)解读(二)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。