tensorflow實現(xiàn)從.ckpt文件中讀取任意變量
思路有些混亂,希望大家能理解我的意思。
看了faster rcnn的tensorflow代碼,關于fix_variables的作用我不是很明白,所以寫了以下代碼,讀取了預訓練模型vgg16得fc6和fc7的參數(shù),以及faster rcnn中heat_to_tail中的fc6和fc7,將它們做了對比,發(fā)現(xiàn)結果不一樣,說明vgg16的fc6和fc7只是初始化了faster rcnn中heat_to_tail中的fc6和fc7,之后后者被訓練。
具體讀取任意變量的代碼如下:
import tensorflow as tf
import numpy as np
from tensorflow.python import pywrap_tensorflow
file_name = '/home/dl/projectBo/tf-faster-rcnn/data/imagenet_weights/vgg16.ckpt' #.ckpt的路徑
name_variable_to_restore = 'vgg_16/fc7/weights' #要讀取權重的變量名
reader = pywrap_tensorflow.NewCheckpointReader(file_name)
var_to_shape_map = reader.get_variable_to_shape_map()
print('shape', var_to_shape_map[name_variable_to_restore]) #輸出這個變量的尺寸
fc7_conv = tf.get_variable("fc7", var_to_shape_map[name_variable_to_restore], trainable=False) # 定義接收權重的變量名
restorer_fc = tf.train.Saver({name_variable_to_restore: fc7_conv }) #定義恢復變量的對象
sess = tf.Session()
sess.run(tf.variables_initializer([fc7_conv], name='init')) #必須初始化
restorer_fc.restore(sess, file_name) #恢復變量
print(sess.run(fc7_conv)) #輸出結果
用以上的代碼分別讀取兩個網絡的fc6 和 fc7 ,對應參數(shù)尺寸和權值都不同,但參數(shù)量相同。
再看lib/nets/vgg16.py中的:
(注意注釋)
def fix_variables(self, sess, pretrained_model):
print('Fix VGG16 layers..')
with tf.variable_scope('Fix_VGG16') as scope:
with tf.device("/cpu:0"):
# fix the vgg16 issue from conv weights to fc weights
# fix RGB to BGR
fc6_conv = tf.get_variable("fc6_conv", [7, 7, 512, 4096], trainable=False)
fc7_conv = tf.get_variable("fc7_conv", [1, 1, 4096, 4096], trainable=False)
conv1_rgb = tf.get_variable("conv1_rgb", [3, 3, 3, 64], trainable=False) #定義接收權重的變量,不可被訓練
restorer_fc = tf.train.Saver({self._scope + "/fc6/weights": fc6_conv,
self._scope + "/fc7/weights": fc7_conv,
self._scope + "/conv1/conv1_1/weights": conv1_rgb}) #定義恢復變量的對象
restorer_fc.restore(sess, pretrained_model) #恢復這些變量
sess.run(tf.assign(self._variables_to_fix[self._scope + '/fc6/weights:0'], tf.reshape(fc6_conv,
self._variables_to_fix[self._scope + '/fc6/weights:0'].get_shape())))
sess.run(tf.assign(self._variables_to_fix[self._scope + '/fc7/weights:0'], tf.reshape(fc7_conv,
self._variables_to_fix[self._scope + '/fc7/weights:0'].get_shape())))
sess.run(tf.assign(self._variables_to_fix[self._scope + '/conv1/conv1_1/weights:0'],
tf.reverse(conv1_rgb, [2]))) #將vgg16中的fc6、fc7中的權重reshape賦給faster-rcnn中的fc6、fc7
我的理解:faster rcnn的網絡繼承了分類網絡的特征提取權重和分類器的權重,讓網絡從一個比較好的起點開始被訓練,有利于訓練結果的快速收斂。
補充知識:TensorFlow:加載部分ckpt文件變量&不同命名空間中加載模型
TensorFlow中,在加載和保存模型時,一般會直接使用tf.train.Saver.restore()和tf.train.Saver.save()
然而,當需要選擇性加載模型參數(shù)時,則需要利用pywrap_tensorflow讀取模型,分析模型內的變量關系。
例子:Faster-RCNN中,模型加載vgg16.ckpt,需要利用pywrap_tensorflow讀取ckpt文件中的參數(shù)
from tensorflow.python import pywrap_tensorflow
model=VGG16()#此處構建vgg16模型
variables = tf.global_variables()#獲取模型中所有變量
file_name='vgg16.ckpt'#vgg16網絡模型
reader = pywrap_tensorflow.NewCheckpointReader(file_name)
var_to_shape_map = reader.get_variable_to_shape_map()#獲取ckpt模型中的變量名
print(var_to_shape_map)
sess=tf.Session()
my_scope='my/'#外加的空間名
variables_to_restore={}#構建字典:需要的變量和對應的模型變量的映射
for v in variables:
if my_scope in v.name and v.name.split(':')[0].split(my_scope)[1] in var_to_shape_map:
print('Variables restored: %s' % v.name)
variables_to_restore[v.name.split(':0')[0][len(my_scope):]]=v
elif v.name.split(':')[0] in var_to_shape_map:
print('Variables restored: %s' % v.name)
variables_to_restore[v.name]=v
restorer=tf.train.Saver(variables_to_restore)#將需要加載的變量作為參數(shù)輸入
restorer.restore(sess, file_name)
實際中,F(xiàn)aster RCNN中所構建的vgg16網絡的fc6和fc7權重shape如下:
<tf.Variable 'my/vgg_16/fc6/weights:0' shape=(25088, 4096) dtype=float32_ref>,
<tf.Variable 'my/vgg_16/fc7/weights:0' shape=(4096, 4096) dtype=float32_ref>,
vgg16.ckpt的fc6,fc7權重shape如下:
'vgg_16/fc6/weights': [7, 7, 512, 4096],
'vgg_16/fc7/weights': [1, 1, 4096, 4096],
因此,有如下操作:
fc6_conv = tf.get_variable("fc6_conv", [7, 7, 512, 4096], trainable=False)
fc7_conv = tf.get_variable("fc7_conv", [1, 1, 4096, 4096], trainable=False)
restorer_fc = tf.train.Saver({"vgg_16/fc6/weights": fc6_conv,
"vgg_16/fc7/weights": fc7_conv,
})
restorer_fc.restore(sess, pretrained_model)
sess.run(tf.assign(self._variables_to_fix['my/vgg_16/fc6/weights:0'], tf.reshape(fc6_conv,self._variables_to_fix['my/vgg_16/fc6/weights:0'].get_shape())))
sess.run(tf.assign(self._variables_to_fix['my/vgg_16/fc7/weights:0'], tf.reshape(fc7_conv,self._variables_to_fix['my/vgg_16/fc7/weights:0'].get_shape())))
以上這篇tensorflow實現(xiàn)從.ckpt文件中讀取任意變量就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
flask框架自定義過濾器示例【markdown文件讀取和展示功能】
這篇文章主要介紹了flask框架自定義過濾器,結合實例形式分析了flask基于自定義過濾器實現(xiàn)markdown文件讀取和展示功能相關操作技巧,需要的朋友可以參考下2019-11-11
Pytorch數(shù)據(jù)類型Tensor張量操作的實現(xiàn)
本文主要介紹了Pytorch數(shù)據(jù)類型Tensor張量操作的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07
Windows10下Tensorflow2.0 安裝及環(huán)境配置教程(圖文)
這篇文章主要介紹了Windows10下Tensorflow2.0 安裝及環(huán)境配置教程(圖文),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
pandas如何使用列表和字典創(chuàng)建?Series
這篇文章主要介紹了pandas如何使用列表和字典創(chuàng)建?Series,pandas 是基于NumPy的一種工具,該工具是為解決數(shù)據(jù)分析任務而創(chuàng)建的,下文我們就來看看文章是怎樣介紹pandas,需要的朋友也可以參考一下2021-12-12
Python使用Selenium+BeautifulSoup爬取淘寶搜索頁
這篇文章主要為大家詳細介紹了Python使用Selenium+BeautifulSoup爬取淘寶搜索頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02
Python使用xpath對解析內容進行數(shù)據(jù)提取
XPath 使用路徑表達式來選取HTML/ XML 文檔中的節(jié)點或節(jié)點集,節(jié)點是通過沿著路徑 (path) 或者步 (steps) 來選取的,本文將給大家介紹Python使用xpath對解析內容進行數(shù)據(jù)提取的方法,需要的朋友可以參考下2024-05-05

