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]))
}))