简体   繁体   English

如何获得经过训练的CNN模型的某一层的输出[Tensorflow]

[英]How to get the output of certain layer of trained CNN model [Tensorflow]

I have a CNN model for image classification which I have trained over my dataset. 我有一个用于image classificationCNN模型,该模型已针对我的数据集进行了训练。 The model goes something like this 该模型是这样的

Convolution 
Relu
pooling

Convolution 
Relu
Convolution 
Relu
pooling

flat

fully connected (FC1)
Relu
fully connected (FC2)
softmax

After training, I want to get the feature vectors for an image that I input to the pre-trained model ie I want to get the output of FC1 layer. 训练后,我想获得输入到预训练模型中的图像的特征向量,即我想获得FC1层的输出。 Is there any way we can get it, I browsed the web but couldn't find anything useful any suggestions would be of great help guys. 有什么方法可以获取它,我浏览了网络,但找不到任何有用的建议,对您有很大帮助。

Training script 训练脚本

# input
x = tf.placeholder(tf.float32, shape=[None, img_size_h, img_size_w, num_channels], name='x')
# lables
y_true     = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')
y_true_cls = tf.argmax(y_true, axis=1)

y_pred = build_model(x)     # Builds model architecture
y_pred_cls = tf.argmax(y_pred, axis=1)

cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=y_pred, labels=y_true)
cost = tf.reduce_mean(cross_entropy)

optimizer = tf.train.MomentumOptimizer(learn_rate, 0.9, use_locking=False, use_nesterov=True).minimize(cost)

accuracy  = tf.reduce_mean(tf.cast(tf.equal(y_pred_cls, y_true_cls), tf.float32))

sess = tf.Session()

sess.run(tf.global_variables_initializer())

tf_saver = tf.train.Saver()

train(num_iteration)    # Trains the network and saves the model

sess.close()

Testing script 测试脚本

sess = tf.Session()

tf_saver = tf.train.import_meta_graph('model/model.meta')
tf_saver.restore(sess, tf.train.latest_checkpoint('model'))

x = tf.get_default_graph().get_tensor_by_name('x:0')

y_true = tf.get_default_graph().get_tensor_by_name('y_true:0')
y_true_cls = tf.argmax(y_true, axis=1)

y_pred = tf.get_default_graph().get_tensor_by_name('y_pred:0')      # refers to FC2 in the model
y_pred_cls = tf.argmax(y_pred, axis=1)

correct_prediction = tf.equal(y_pred_cls, y_true_cls)
accuracy           = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

images, labels = read_data()     # read data for testing

feed_dict_test  = {x: images, y_true: labels}

test_acc = sess.run(accuracy, feed_dict=feed_dict_test)

sess.close()

You can just perform sess.run on the right tensor to get the values. 您可以只在正确的张量上执行sess.run来获取值。 First you need the tensor. 首先,您需要张量。 You can give it a name inside build_model by adding a name argument (which you can do for any tensor), eg: 您可以在build_model内通过添加名称参数(可以对任何张量执行此操作)为其命名,例如:

FC1 = tf.add(tf.multiply(Flat, W1), b1, name="FullyConnected1")

Later, you can get the tensor for the fully connected layer and evaluate it: 稍后,您可以获取完全连接层的张量并对其进行评估:

with tf.Session() as sess:
    FC1 = tf.get_default_graph().get_tensor_by_name('FullyConnected1:0')
    FC1_values = sess.run(FC1, feed_dict={x: input_img_arr})        

(This is assuming there is no other layer called FullyConnected1 in the graph) (这是假设图中没有其他称为FullyConnected1的层)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Keras 中更改预训练 CNN model 中层的 output? - How to change output of a layer in a pre-trained CNN model in Keras? 如何从Tensorflow中的CNN获取全连接层的output? - How to get the output of the fully connected layer from CNN in Tensorflow? 如何在外部非Tensorflow环境中测试经过训练的CNN模型? - How to test a trained CNN model in an external non-Tensorflow environment? 如何使用TensorFlow Java API移除预训练模型的输出层? - How can I remove the output layer of pre-trained model with TensorFlow java api? Tensorflow:如何从预先训练的CNN的特定层提取图像特征? - Tensorflow: How can I extract image features from a specific layer of a pre-trained CNN? 如何在训练到全连接层之前将潜在向量作为 cnn 模型的输出? - How to get the latent vector as an output from a cnn model before training to the fully connected layer? 在训练TensorFlow模型(,?不是Keras模型)时,如何获取模型中间层(op)的输入输出? - During training the TensorFlow model(!!Not the Keras model), How to get the input and output of the intermediate layer(op) of the model? 如何使用经过训练的CNN张量流模型测试来自另一个.py文件的输入图像 - How can I use a trained CNN tensorflow model to test an input image from another .py file 如何用我们自己的输入层替换预训练的输入层 tensorflow model...? - How to replace the input layer of a pre-trained tensorflow model with our own input layer...? TensorFlow保存和恢复训练有素的CNN模型不起作用 - TensorFlow save and restore the trained well CNN model was not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM