windows编译tensorflow tensorflow单机多卡程序的框架 tensorflow的操作 tensorflow的变量初始化和scope 人体姿态检测 segmentation标注工具 tensorflow模型恢复与inference的模型简化 利用多线程读取数据加快网络训练 tensorflow使用LSTM pytorch examples 利用tensorboard调参 深度学习中的loss函数汇总 纯C++代码实现的faster rcnn tensorflow使用记录 windows下配置caffe_ssd use ubuntu caffe as libs use windows caffe like opencv windows caffe implement caffe model convert to keras model flappyBird DQN Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Neural Networks Fast-style-transfer tensorflow安装 tensorflow DQN Fully Convolutional Models for Semantic Segmentation Transposed Convolution, Fractionally Strided Convolution or Deconvolution 基于tensorflow的分布式部署 用python实现mlp bp算法 用tensorflow和tflearn搭建经典网络结构 Data Augmentation Tensorflow examples Training Faster RCNN with Online Hard Example Mining 使用Tensorflow做Prisma图像风格迁移 RNN(循环神经网络)推导 深度学习中的稀疏编码思想 利用caffe与lmdb读写图像数据 分析voc2007检测数据 用python写caffe网络配置 ssd开发 将KITTI的数据格式转换为VOC Pascal的xml格式 Faster RCNN 源码分析 在Caffe中建立Python layer 在Caffe中建立C++ layer 为什么CNN反向传播计算梯度时需要将权重旋转180度 Caffe使用教程(下) Caffe使用教程(上) CNN反向传播 Softmax回归 Caffe Ubuntu下环境配置

tensorflow使用记录

2017年05月19日

减均值后图像复原

image = tf.placeholder(tf.float32, shape=[None, IMAGE_SIZE, IMAGE_SIZE, 3], name="input_image")
channel_mean = tf.constant(np.array([123.68,116.779,103.938], dtype=np.float32))
image_before_process = tf.add(image,channel_mean)

在构造图时候查看tensor的shape

tensor.shape
#returns tensor's static shape, while the graph is being built.

tensor.shape.as_list() 
#returns the static shape as a integer list.

tensor.shape[i].value 
#returns the static shape's i-th dimension size as an integer.

tf.shape(t) 
#returns t's run-time shape as a tensor.

#An example:
x = tf.placeholder(tf.float32, shape=[None, 8]) # x shape is non-deterministic while building the graph.
print(x.shape) # Outputs static shape (?, 8).
shape_t = tf.shape(x)
with tf.Session() as sess:
    print(sess.run(shape_t, feed_dict={x: np.random.random(size=[4, 8])})) # Outputs run-time shape (4, 8).

tf.app.run()pudb调试时候遇到参数错误,运行时候正常

使用tf.app.run(main=main)

tensorflow查看可用设备

from tensorflow.python.client import device_lib as _device_lib
print _device_lib.list_local_devices()

从checkpoint中读取tensor

import os
from tensorflow.python import pywrap_tensorflow

checkpoint_path = os.path.join(model_dir, "model.ckpt")
# Read data from checkpoint file
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
# Print tensor name and values
for key in var_to_shape_map:
    print("tensor_name: ", key)
    print(reader.get_tensor(key))

或者

import tensorflow as tf
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
ckpt = tf.train.get_checkpoint_state('./model_dir/')                          # 通过检查点文件锁定最新的模型
saver = tf.train.import_meta_graph(ckpt.model_checkpoint_path +'.meta')   # 载入图结构,保存在.meta文件中
var = [v for v in tf.trainable_variables()]

with tf.Session() as sess:
    saver.restore(sess,ckpt.model_checkpoint_path)
    logging.info("load parameter done")
    parameter = []
    for i in range(len(var)):
        parameter.append(sess.run(var[i]))
        logging.info(var[i].name)

关于opencv读图片和tf.image.decode_image区别

需要在numpy和cv数据的IO以及tf解码时候注意,不然训练模型时和测试模型数据通道顺序不一样会导致模型预测出错

import tensorflow as tf
import cv2

image = cv2.imread('test.jpg')   #BGR order
image_string = tf.read_file('test.jpg')                                                                                                                   
image_decoded = tf.image.decode_image(image_string)
with tf.Session() as sess:                                                                                                                                                        
    image_d = sess.run(image_decoded)      #RGB order                                                                                                                                       

blog comments powered by Disqus