tensorflow API _ 6 (tf.gfile)
一、gfile模块是什么
tf.gfile模块的主要角色是:
1.提供一个接近Python文件对象的API,以及
2.提供基于TensorFlow C ++ FileSystem API的实现。
C ++ FileSystem API支持多种文件系统实现,包括本地文件,谷歌云存储(以gs://开头)和HDFS(以hdfs:/开头)。 TensorFlow将它们导出为tf.gfile,以便我们可以使用这些实现来保存和加载检查点,编写TensorBoard log以及访问训练数据(以及其他用途)。但是,如果所有文件都是本地文件,则可以使用常规的Python文件API而不会造成任何问题。
二、gfile API介绍
下面将分别介绍每一个gfile API!
2-1)tf.gfile.Copy(oldpath, newpath, overwrite=False)
拷贝源文件并创建目标文件,无返回,其形参说明如下:
oldpath:带路径名字的拷贝源文件;
newpath:带路径名字的拷贝目标文件;
overwrite:目标文件已经存在时是否要覆盖,默认为false,如果目标文件已经存在则会报错
2-2)tf.gfile.MkDir(dirname)
创建一个目录,dirname为目录名字,无返回。
2-3)tf.gfile.Remove(filename)
删除文件,filename即文件名,无返回。
2-4)tf.gfile.DeleteRecursively(dirname)
递归删除所有目录及其文件,dirname即目录名,无返回。
2-5)tf.gfile.Exists(filename)
判断目录或文件是否存在,filename可为目录路径或带文件名的路径,有该目录则返回True,否则False。
2-6)tf.gfile.Glob(filename)
查找匹配pattern的文件并以列表的形式返回,filename可以是一个具体的文件名,也可以是包含通配符的正则表达式。
2-7)tf.gfile.IsDirectory(dirname)
判断所给目录是否存在,如果存在则返回True,否则返回False,dirname是目录名。
2-8)tf.gfile.ListDirectory(dirname)
罗列dirname目录下的所有文件并以列表形式返回,dirname必须是目录名。
2-9)tf.gfile.MakeDirs(dirname)
以递归方式建立父目录及其子目录,如果目录已存在且是可覆盖则会创建成功,否则报错,无返回。
2-10)tf.gfile.Rename(oldname, newname, overwrite=False)
重命名或移动一个文件或目录,无返回,其形参说明如下:
oldname:旧目录或旧文件;
newname:新目录或新文件;
overwrite:默认为false,如果新目录或新文件已经存在则会报错,否则重命名或移动成功。
2-11)tf.gfile.Stat(filename)
返回目录的统计数据,该函数会返回FileStatistics数据结构,以dir(tf.gfile.Stat(filename))获取返回数据的属性如下:
2-12)tf.gfile.Walk(top, in_order=True)
递归获取目录信息生成器,top是目录名,in_order默认为True指示顺序遍历目录,否则将无序遍历,每次生成返回如下格式信息(dirname, [subdirname, subdirname, ...], [filename, filename, ...])。
2-13)tf.gfile.GFile(filename, mode)
获取文本操作句柄,类似于python提供的文本操作open()函数,filename是要打开的文件名,mode是以何种方式去读写,将会返回一个文本操作句柄。
tf.gfile.Open()是该接口的同名,可任意使用其中一个!
2-14)tf.gfile.FastGFile(filename, mode)
该函数与tf.gfile.GFile的差别仅仅在于“无阻塞”,即该函数会无阻赛以较快的方式获取文本操作句柄。
举例两个作用:
pb文件保存:
output_graph_def = convert_variables_to_constants(sess, sess.graph_def, ["output2"])
with tf.gfile.FastGFile('./models/emotion_resnet_graph.pb',mode='wb') as f:
f.write(output_graph_def.SerializeToString())
图像编码解码:
tf.gfile.FastGFile(path,decodestyle)
函数功能:实现对图片的读取。
函数参数:(1)path:图片所在路径 (2)decodestyle:图片的解码方式。(‘r’:UTF-8编码; ‘rb’:非UTF-8编码)
import matplotlib.pyplot as plt
import tensorflow as tf
#tf.gfileGFile()函数:读取图像
image_jpg = tf.gfile.FastGFile('dog.jpg','rb').read()
image_png = tf.gfile.FastGFile('lizard.png','rb').read()
with tf.Session() as sess:
image_jpg = tf.image.decode_jpeg(image_jpg) #图像解码
print(sess.run(image_jpg))#打印解码后的图像(即为一个三维矩阵[w,h,3])
image_jpg = tf.image.convert_image_dtype(image_jpg,dtype=tf.uint8) #改变图像数据类型
image_png = tf.image.decode_png(image_png)
print(sess.run(image_jpg))
image_png = tf.image.convert_image_dtype(image_png,dtype=tf.uint8)
plt.figure(1) #图像显示
plt.imshow(image_jpg.eval())
plt.figure(2)
plt.imshow(image_png.eval())
tensorflow API _ 6 (tf.gfile)的更多相关文章
- tensorflow API _ 2 (tf.app.flags.FLAGS)
tf.app.flags.FLAGS 的使用,主要是在用命令行执行程序时,需要传些参数,代码如下:新建一个名为:app_flags.py 的文件. #coding:utf-8 import tens ...
- tensorflow API _ 3 (tf.train.polynomial_decay)
学习率的三种调整方式:固定的,指数的,多项式的 def _configure_learning_rate(num_samples_per_epoch, global_step): "&quo ...
- tensorflow API _ 4 (Logging with tensorflow)
TensorFlow用五个不同级别的日志信息.为了升序的严重性,他们是调试DEBUG,信息INFO,警告WARN,错误ERROR和致命FATAL的.当你配置日志记录在任何级别,TensorFlow将输 ...
- tensorflow API _ 5 (tensorflow.summary)
tensorflow的可视化是使用summary和tensorboard合作完成的. 基本用法 首先明确一点,summary也是op. 输出网络结构 with tf.Session() as sess ...
- tensorflow API _ 1 (control_flow_ops.cond)
该函数用来控制程序执行流,相当于if-else了import tensorflow as tffrom tensorflow.python.ops import control_flow_ops a ...
- tensorflow API _ 4 (优化器配置)
"""Configures the optimizer used for training. Args: learning_rate: A scalar or `Tens ...
- Tensorflow API 学习(1)-tf.slice()
slice()函数原型为: tf.slice(input_, begin, size, name=None) 函数有4个参数: 1,input_ :图片的矩阵输入格式. 2,begin :开始截取的位 ...
- tf.gfile
一.功能和目的 tf.gfile模块定义在tensorflow/python/platform/gfile.py,但其源代码实现主要位于tensorflow/tensorflow/python/lib ...
- TensorFlow API 汉化
TensorFlow API 汉化 模块:tf 定义于tensorflow/__init__.py. 将所有公共TensorFlow接口引入此模块. 模块 app module:通用入口点脚本. ...
随机推荐
- imposm模块安装
imposm安装 pip install imposm 报错,参考:https://imposm.org/docs/imposm/2.5.0/install.html 并安装依赖: sudo ap-g ...
- 防范sql注入值得注意地方
sql注入是大家基本都清楚,一般来说用参数化就能解决注入的问题,也是最好的解决方式. 有次技术群里问到一个问题,如下图 很显然tableName是外部传递过来的,暂时不考虑具体的业务环境,但如果以se ...
- Vue 动态路由的实现以及 Springsecurity 按钮级别的权限控制
思路: 动态路由实现:在导航守卫中判断用户是否有用户信息,通过调用接口,拿到后台根据用户角色生成的菜单树,格式化菜单树结构信息并递归生成层级路由表并使用Vuex保存,通过 router.addRout ...
- 超级简单POI导出Excel实战
在一般的生产管理系统都会将数据通过页面导出到Excel,这里以Java为例通过第三方开源poi进行对Excel的操作,具体操作如下 1.引入jar包依赖 这里我以maven的方式引入jar包,具体依赖 ...
- 【leetcode】589. N-ary Tree Preorder Traversal
题目: Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3- ...
- Spring Boot 注解大全,真是太全了!
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguratio ...
- Golang ---testing包
golang自带了testing测试包,使用该包可以进行自动化的单元测试,输出结果验证,并且可以测试性能. 建议安装gotests插件自动生成测试代码: go get -u -v github.com ...
- C#中Chart的简单使用(柱状图和折线图)
首先创建一个windows窗体应用程序,在工具箱—>数据中拖拽一个Chart控件,设置ChartArea背景色为黄色,Legend背景色为绿色,三个Series,Name属性分别为1,2,3,添 ...
- Vue 项目 VSCode 调试
调试Vue搭建的前端项目 在项目根目录下的vue.config.js中添加: module.exports = { lintOnSave: false, //关闭eslint语法校验 //填写这部分 ...
- txt文件每行内容与图片文件名字组合,输出txt格式
import os dir_list = os.listdir('C:\\Users\\10107472\\Desktop\\practice\\JPEGImages')i=0f1=open('C:\ ...