tensorflow文件读取操作

#coding=utf-8import tensorflow as tf

import numpy as np

defreadMyFileFormat(fileNameQueue):

reader = tf.TextLineReader()  

key, value = reader.read(fileNameQueue)  

record_defaults = [[1], [1], [1]]  

col1, col2, col3 = tf.decode_csv(value, record_defaults = record_defaults)  

features = tf.pack([col1, col2])  

label = col3  

return features, label
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

其中,record_defaults = [[1], [1], [1]] ,是用于指定矩阵格式以及数据类型的,CSV文件中的矩阵,是NXM的,则此处为1XM,[1]中的1 用于指定数据类型,比如矩阵中如果有小数,则为float,[1]应该变为[1.0]。
col1, col2, col3 = tf.decode_csv(value, record_defaults = record_defaults) , 矩阵中有几列,这里就要写几个参数,比如5列,就要写到col5,不管你到底用多少。否则报错。

tf.unstack与tf.stack

原型:

unstack(
value,
num=None,
axis=0,
name='unstack' )

官方解释:https://tensorflow.google.cn/api_docs/python/tf/unstack
解释:这是一个对矩阵进行分解的函数,以下为关键参数解释:
value:代表需要分解的矩阵变量(其实就是一个多维数组,一般为二维);
axis:指明对矩阵的哪个维度进行分解。
要理解tf.unstack函数,我们不妨先来看看tf.stack函数。Tf.stack刚好是与tf.unstack函数相反,前者是对矩阵进行拼接,后者则对矩阵进行分解。
转载出处:
https://www.jianshu.com/p/25706575f8d4
数组的行列要分开,矩阵要画成np.array

import numpy as np

x = np.array([[1,2,5],[2,3,5],[3,4,5],[2,3,6]])

输出数组的行和列数

print x.shape  # (4, 3)

只输出行数

print x.shape[0] # 4

只输出列数

print x.shape[1] # 3

图像通道转换[n c h w] 转 [n h w c]

Tensorflow定义的tensor的shape为[n,h,w,c],而我们直接读取文件格式是[n,c,h,w],为了转成[n,h,w,c]形势,可以采用三种方法:

img = np.transpose(img, (0, 2, 3, 1))
 
img = img.reshape(img.shape[0], img.shape[2], img.shape[3], img.shape[1])
 
img_new = zeros((img.shape[0], img.shape[2], img.shape[3], img.shape[1]), dtype = np.float32)
for c in range(0, img.shape[1]):
    for i in range(0, img.shape[2]):
        for j in range(0, img.shape[3]):
            img_new[:,c,i,j] = img_new[:,i,j,c]

https://blog.csdn.net/wweiainn/article/details/80231792

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值