Tensorflow學習(5)引數和特徵的提取
阿新 • • 發佈:2019-02-01
在tf中,參與訓練的引數可用 tf.trainable_variables()提取出來,如:
params=tf.trainable_variables()
print("Trainable variables:-------")
#迴圈列出引數
for idx, v in enumerate(params):
print("param {:3}:{:15} {}".format(idx, str(v.get_shape()),v.name))
這裡只能看引數的shape和name,並沒有具體的值。
如果要檢視具體的值,必須先初始化。即
sess=tf.Session()
sess.run (tf.global_variables_initializer())
同理,我們也可以提取圖片經過訓練後的值。圖片經過卷積後變成了特徵,必須先把圖片feed進去。
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 3 12:07:59 2017
@author: Administrator
"""
import tensorflow as tf
from skimage import io,transform
import numpy as np
#-----------------構建網路----------------------
#佔位符
x=tf.placeholder(tf.float32,shape=[None,100,100,3],name='x')
y_=tf.placeholder(tf.int32,shape=[None,],name='y_')
#第一個卷積層(100——>50)
conv1=tf.layers.conv2d(
inputs=x,
filters=32,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu,
kernel_initializer=tf.truncated_normal_initializer(stddev=0.01 ))
pool1=tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
#第二個卷積層(50->25)
conv2=tf.layers.conv2d(
inputs=pool1,
filters=64,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu,
kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
pool2=tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
#第三個卷積層(25->12)
conv3=tf.layers.conv2d(
inputs=pool2,
filters=128,
kernel_size=[3, 3],
padding="same",
activation=tf.nn.relu,
kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
pool3=tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2], strides=2)
#第四個卷積層(12->6)
conv4=tf.layers.conv2d(
inputs=pool3,
filters=128,
kernel_size=[3, 3],
padding="same",
activation=tf.nn.relu,
kernel_initializer=tf.truncated_normal_initializer(stddev=0.01))
pool4=tf.layers.max_pooling2d(inputs=conv4, pool_size=[2, 2], strides=2)
re1 = tf.reshape(pool4, [-1, 6 * 6 * 128])
#全連線層
dense1 = tf.layers.dense(inputs=re1,
units=1024,
activation=tf.nn.relu,
kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
kernel_regularizer=tf.nn.l2_loss)
dense2= tf.layers.dense(inputs=dense1,
units=512,
activation=tf.nn.relu,
kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
kernel_regularizer=tf.nn.l2_loss)
logits= tf.layers.dense(inputs=dense2,
units=5,
activation=None,
kernel_initializer=tf.truncated_normal_initializer(stddev=0.01),
kernel_regularizer=tf.nn.l2_loss)
#---------------------------網路結束---------------------------
#%%
#取出所有參與訓練的引數
params=tf.trainable_variables()
print("Trainable variables:------------------------")
#迴圈列出引數
for idx, v in enumerate(params):
print(" param {:3}: {:15} {}".format(idx, str(v.get_shape()), v.name))
#%%
#讀取圖片
img=io.imread('d:/cat.jpg')
#resize成100*100
img=transform.resize(img,(100,100))
#三維變四維(100,100,3)-->(1,100,100,3)
img=img[np.newaxis,:,:,:]
img=np.asarray(img,np.float32)
sess=tf.Session()
sess.run(tf.global_variables_initializer())
#提取最後一個全連線層的引數 W和b
W=sess.run(params[26])
b=sess.run(params[27])
#提取第二個全連線層的輸出值作為特徵
fea=sess.run(dense2,feed_dict={x:img})
最後一條語句就是提取某層的資料輸出作為特徵。
注意:這個程式並沒有經過訓練,因此提取出的引數只是初始化的引數。