原文地址(https://github.com/wmpscc/TensorflowBaseDemo )

本篇文章将介绍使用tensorflow的训练模型的基本流程,包括制作读取TFRecord,训练和保存模型,读取模型。
语言:Python3
库:tensorflow、cv2、numpy、matplotlib
数据集:Chars74K dataset 的数字部分
网络:CNN
所有代码已经上传至github:https://github.com/wmpscc/TensorflowBaseDemo

TFRecord
TensorFlow提供了一种统一的格式来存储数据,这个格式就是TFRecord.

message Example {
Features features = 1;
};

message Features{
map featrue = 1;
};

message Feature{
oneof kind{
BytesList bytes_list = 1;
FloatList float_list = 2;
Int64List int64_list = 3;
}
};
从代码中我们可以看到, tf.train.Example 包含了一个字典,它的键是一个字符串,值为Feature,Feature可以取值为字符串(BytesList)、浮点数列表(FloatList)、整型数列表(Int64List)。

写入一个TFRecord一般分为三步:
读取需要转化的数据
将数据转化为Example Protocol Buffer,并写入这个数据结构
通过将数据转化为字符串后,通过TFRecordWriter写出
方法一
这次我们的数据是分别保存在多个文件夹下的,因此读取数据最直接的方法是遍历目录下所有文件,然后读入写出TFRecord文件。该方法对应文件MakeTFRecord.py,我们来看关键代码

filenameTrain = 'TFRecord/train.tfrecords'
filenameTest = 'TFRecord/test.tfrecords'
writerTrain = tf.python_io.TFRecordWriter(filenameTrain)
writerTest = tf.python_io.TFRecordWriter(filenameTest)
folders = os.listdir(HOME_PATH)
for subFoldersName in folders:
label = transform_label(subFoldersName)
path = os.path.join(HOME_PATH, subFoldersName) # 文件夹路径
subFoldersNameList = os.listdir(path)
i = 0
for imageName in subFoldersNameList:
imagePath = os.path.join(path, imageName)
images = cv2.imread(imagePath)
res = cv2.resize(images, (128, 128), interpolation=cv2.INTER_CUBIC)
image_raw_data = res.tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'label': _int64_feature(label),
'image_raw': _bytes_feature(image_raw_data)
}))
if i

最新文章

  1. JAVA设计模式《二》
  2. C#操作Word的超详细总结
  3. GridView 控件中如何绑定 CheckBoxList
  4. 刷了OpenWrt Attitude Adjustment 12.09,很满意
  5. iOS didReceiveMemoryWarning 的处理
  6. HTML 列表
  7. JavaScript 一些基础练习
  8. Python中编码问题?
  9. Kali for Android
  10. Swift Strings and Characters
  11. XSS学习笔记(五)-XSS防御
  12. java Static的使用
  13. java的数组
  14. 怎么使用zepto.js的tap事件引起的探索
  15. [Swift]LeetCode881. 救生艇 | Boats to Save People
  16. 【百度杯】ctf夺旗大战,16万元奖励池等你拿
  17. redmine screenshot paste(粘贴截图)
  18. json转对象,奇怪的映射
  19. 通过scp拷贝文件时无需交互输入密码
  20. QTCreator 调试:unknown debugger type "No engine"

热门文章

  1. uclibc,eglibc,glibc,Musl-libc之间的区别和联系
  2. 30天学会绘画 (Mark Kistler 著)
  3. 手写SpringMVC实现
  4. repo/repo init-解决同步源码Cannot get http://gerrit.googlesource.com/git-repo/clone.bundle
  5. 通用唯一识别码UUID
  6. Windows 10 RS4 无法完全关闭Hyper-V导致Virtual Box 虚拟机无法启动
  7. PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20151012/php_mbstring.dll' 的解决方法
  8. Unity 缓冲池概念
  9. Unity 三角函数 向量 运算
  10. Immutable 学习