1、数据集的构建
1、生成 tfrecords 文件
1) 事前确定数据集的路径,数据集的分类 classes(字典类型)。(函数之前)
2) 定义函数,创建一个写入器: tf.python_io.TFRecordWriter(file_name)
3) 开始定位到每一个图片。首先定位到每一类文件夹,这一步同时明确了
每一类的 label。 for index, class_path in enumerate(classes)
4) 然后在每一类文件下定位到每一张图片:for name os.listdir(class_path)
5) 定位到每一张图之后,打开 Image.open(name_path),重新标定大小
Image.resize((size,size)),转化成 raw 类型 Image.robytes(img)
6) 按照 example 协议转化:example = tf.train.Example():
7) 写入 tfrecords 文件:writer.write(example.SerializeToString())
8) 关闭 writer.close()
第 6)步中的函数具体为:
Tfrecord 文件中,在写入的时候格式 byte 格式。
上图中,
example = tf.train.Example(
features=tf.train.Features(feature={
"label":tf.train.Feature(int64_list = tf.train.Int64List(value = [index])),
'img_raw':tf.train.Feature(bytes_list = tf.train.BytesList(value = [img_raw]))
}))
2、读取和 decode 数据集
1) 函数中创建文件队列:file_queue = tf.train.string_input_pruductor(filename)
2) 创建阅读器: reader = tf.TFRecordReader()
3) 阅 读 器 从 队 列 总 一 个 一 个 的 读 出 _, example : example =
Commented [W 用 1]: 返回值是索引值和对应的数值。
索引值代表 label,对应的值就是文件夹的名称
Commented [W 用 2]: 函数能返回文件夹下所有文件的
名称。注意是名称
Commented [W 用 3]: 因为 tf.train.Example 包含着
Featrues 字段。这个 features 是字段的关键字
Commented [W 用 4]: 这是包含的 Features 字段。
Commented [W 用 5]: 字段其实是一个 feature 字典,
这是字典的关键字
Commented [W 用 6]: label 和 img_raw 是字典中两个
键 这是不变的
Commented [W 用 7]: 这个和后边的,是数据类型和写
入赋值
Commented [W 用 8]: 指的是创建好的 tfrecords 文件