生成TFRecord文件完整代码实例
import os
import json def get_annotation_dict(input_folder_path, word2number_dict):
label_dict = {}
father_file_list = os.listdir(input_folder_path)
for father_file in father_file_list:
full_father_file = os.path.join(input_folder_path, father_file)
son_file_list = os.listdir(full_father_file)
for image_name in son_file_list:
label_dict[os.path.join(full_father_file, image_name)] = word2number_dict[father_file]
return label_dict def save_json(label_dict, json_path):
with open(json_path, 'w') as json_path:
json.dump(label_dict, json_path)
print("label json file has been generated successfully!")
- generate_annotation_json.py
总共有七种分类图片,类别的名称就是每个文件夹名称
{
"/images/hangs/862e67a8-5bd9-41f1-8c6d-876a3cb270df.JPG": 6,
"/images/tags/adc264af-a76b-4477-9573-ac6c435decab.JPG": 3,
"/images/tags/fd231f5a-b42c-43ba-9e9d-4abfbaf38853.JPG": 3,
"/images/hangs/2e47d877-1954-40d6-bfa2-1b8e3952ebf9.jpg": 6,
"/images/tileds/a07beddc-4b39-4865-8ee2-017e6c257e92.png": 5,
"/images/models/642015c8-f29d-4930-b1a9-564f858c40e5.png": 4
}
- generate_tfrecord.py
import os
import tensorflow as tf
import io
from PIL import Image
from generate_annotation_json import get_annotation_dict flags = tf.app.flags
flags.DEFINE_string('images_dir',
'/data2/raycloud/jingxiong_datasets/six_classes/images',
'Path to image(directory)')
flags.DEFINE_string('annotation_path',
'/data1/humaoc_file/classify/data/annotations/annotations.json',
'Path to annotation')
flags.DEFINE_string('record_path',
'/data1/humaoc_file/classify/data/train_tfrecord/train.record',
'Path to TFRecord')
FLAGS = flags.FLAGS def int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) def bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) def process_image_channels(image):
process_flag = False
# process the 4 channels .png
if image.mode == 'RGBA':
r, g, b, a = image.split()
image = Image.merge("RGB", (r,g,b))
process_flag = True
# process the channel image
elif image.mode != 'RGB':
image = image.convert("RGB")
process_flag = True
return image, process_flag def process_image_reshape(image, resize):
width, height = image.size
if resize is not None:
if width > height:
width = int(width * resize / height)
height = resize
else:
width = resize
height = int(height * resize / width)
image = image.resize((width, height), Image.ANTIALIAS)
return image def create_tf_example(image_path, label, resize=None):
#以二进制格式打开图片
with tf.gfile.GFile(image_path, 'rb') as fid:
encode_jpg = fid.read()
encode_jpg_io = io.BytesIO(encode_jpg)
image = Image.open(encode_jpg_io)
# process png pic with four channels,将图片转为RGB
image, process_flag = process_image_channels(image)
# reshape image
image = process_image_reshape(image, resize)
if process_flag == True or resize is not None:
bytes_io = io.BytesIO()
image.save(bytes_io, format='JPEG')
encoded_jpg = bytes_io.getvalue()
width, height = image.size
tf_example = tf.train.Example(
features=tf.train.Features(
feature={
'image/encoded': bytes_feature(encode_jpg),
'image/format': bytes_feature(b'jpg'),
'image/class/label': int64_feature(label),
'image/height': int64_feature(height),
'image/width': int64_feature(width)
}
))
return tf_example def generate_tfrecord(annotation_dict, record_path, resize=None):
num_tf_example = 0
#writer就是我们TFrecord生成器
writer = tf.python_io.TFRecordWriter(record_path)
for image_path, label in annotation_dict.items():
#tf.gfile.GFile获取文本操作句柄,类似于python提供的文本操作open()函数
#filename是要打开的文件名,mode是以何种方式去读写,将会返回一个文本操作句柄。
if not tf.gfile.GFile(image_path):
print("{} does not exist".format(image_path))
tf_example = create_tf_example(image_path, label, resize)
#tf_example.SerializeToString()是将Example中的map压缩为二进制文件
writer.write(tf_example.SerializeToString())
num_tf_example += 1
if num_tf_example % 100 == 0:
print("Create %d TF_Example" % num_tf_example)
writer.close()
print("{} tf_examples has been created successfully, which are saved in {}".format(num_tf_example, record_path)) def main(_):
word2number_dict = {
"combinations": 0,
"details": 1,
"sizes": 2,
"tags": 3,
"models": 4,
"tileds": 5,
"hangs": 6
}
# 图片路径
images_dir = FLAGS.images_dir
#annotation_path = FLAGS.annotation_path
#生成TFRecord文件的路径
record_path = FLAGS.record_path
annotation_dict = get_annotation_dict(images_dir, word2number_dict)
generate_tfrecord(annotation_dict, record_path) if __name__ == '__main__':
tf.app.run()
总结:1.制作数据(图片路径和标签)
2.利用tf.python_io.TFRecordWriter创建一个writer,就是我们TFrecord生成器
3.遍历数据集,以二进制形式打开图片
4.利用tf.train.Example将图片,图片格式,标签和长宽进行保存
5然后利用writer.write(tf_example.SerializeToString())将tf.train.Example存储的数据格式写入TFRecord即可
参考链接:https://www.jianshu.com/p/b480e5fcb638
生成TFRecord文件完整代码实例的更多相关文章
- Java生成MD5加密字符串代码实例
这篇文章主要介绍了Java生成MD5加密字符串代码实例,本文对MD5的作用作了一些介绍,然后给出了Java下生成MD5加密字符串的代码示例,需要的朋友可以参考下 (1)一般使用的数据库中都会保存用 ...
- Extjs的GridPanel分页前后台完整代码实例
第一次写文章啊,有些冲动.最近在公司学习Extjs,做了一个分页的小实例和大家分享. 1.首先编写paging-grid.js文件,这是我在网上参考的例子改写的,大同小异. Ext.onReady(f ...
- python_文件操作代码实例
"""提示:代码中的内容均被注释,请参考,切勿照搬""" 1 #文件的打开和关闭 ''' 文件对象 = open('文件名','使用方式') ...
- C#生成漂亮验证码完整代码类
using System;using System.Web;using System.Drawing;using System.Security.Cryptography; namespace Dot ...
- django 生成csv文件重要代码
import csv from django.http import HttpResponse # Number of unruly passengers each year 1995 - 2005. ...
- 运行pyqt4生成py文件增加代码
if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) Form = QtGui.QWid ...
- TFRecord文件的读写
前言在跑通了官网的mnist和cifar10数据之后,笔者尝试着制作自己的数据集,并保存,读入,显示. TensorFlow可以支持cifar10的数据格式, 也提供了标准的TFRecord 格式,而 ...
- Keil提示premature end of file错误 无法生成HEX文件
今天舍友在使用Keil UV4的时候遇到一个问题:Keil提示premature end of file,无法生成hex文件. 代码是没有错误的.那么问题就出在设置上面了. 百度了一圈,发现很少人解答 ...
- java自动生成entity文件
网上关于自动生成entity文件的代码很多,看了很多代码后,在先辈们的基础上再完善一些功能(指定多个表,全部表). 为了使用方便所以把两个类写在一个java文件中,所以大家可以直接拿这个java文件, ...
随机推荐
- Activity学习(一):生命周期
一. 认识Activity Activity是Android的四大组件之一,那么它是什么呢?如果简单的理解,可以把它当成应用的一个显示的屏幕. Activity类处于android.app包中,继承体 ...
- Python数据可视化matplotlib和seaborn
Python在数据科学中的地位,不仅仅是因为numpy, scipy, pandas, scikit-learn这些高效易用.接口统一的科学计算包,其强大的数据可视化工具也是重要组成部分.在Pytho ...
- java基本数据类型和包装类相互转换
把基本数据类型 → 包装类: 通过对应包装类的构造方法实现 除了Character外,其他包装类都可以传入一个字符串参数构建包装类对象. 包装类 → 基本数据类型 包装类的实例方法xxxValue() ...
- echarts 图表自适应外部盒子大小
项目中用到了echarts,由于页面是自适应的,还得兼容移动, 因此图表还需要根据盒子的大小来变化. 自适应窗口及盒子大小 页面中有一个[放大.缩小]功能,及全屏展示和预览图表 窗口自适应 let m ...
- P1101 走迷宫一
题目描述 大魔王抓住了爱丽丝,将她丢进了一口枯井中,并堵住了井口. 爱丽丝在井底发现了一张地图,他发现他现在身处一个迷宫当中,从地图中可以发现,迷宫是一个N*M的矩形,爱丽丝身处迷宫的左上角,唯一的出 ...
- Lavarel之环境配置 .env
.env 文件位于项目根目录下,作为全局环境配置文件. 1. 配置参数 // 运行环境名称 APP_ENV=local // 调试模式,开发阶段启用,上线状态禁用. APP_DEBUG=true // ...
- RabbitMQ-工作原理
使用场景 在我们秒杀抢购商品的时候,系统会提醒我们稍等排队中,而不是像几年前一样页面卡死或报错给用户. 像这种排队结算就用到了消息队列机制,放入通道里面一个一个结算处理,而不是某个时间断突然涌入大批量 ...
- 正则&转义字符&特殊字符
正则 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. 由于正则表达式主要应用对 ...
- 用mingw静态编译Qt4.8.2和Qt5.1.1(需要修改不少源码)
因为一些乱七八糟的原因,我需要用mingw静态编译Qt4.8.2和Qt5.1.1.经历了一天的折腾之后,自觉编译一下Qt还是件颇为麻烦的事情,故将过程略作总结,以备不时之需. 首先,在编译之前,我需要 ...
- git之github下载篇(ssh需要配置密钥)
1.使用git命令行下载 在想要下载的文件夹打开命令行 git clone ssh 成功如图所示 2.使用小乌龟图形界面克隆 在文件夹右键鼠标 如果复制有链接,会自动填入.点击确定 成功后如图