使用tfrecord建立自己的数据集
注意事项:
1.关于输入图像格式的问题
使用io.imread()的时,根据输入图像确定as_grey的参数值。 转化为字符串之后(image.tostring) ,最后输出看下image_raw的长度。因为不同的图像编码格式,存储方式不同。
我读入的灰度图jpeg格式,类型是int64,image_raw的大小是图像的大小的8倍 。 但如果是RGB图像,则统一类型是uint8。确定了类型,在之后的解码 (decode_raw)中,需要将type设置和存储方式同样的类型。
根据image_raw的长度和原图像大小,推算一下使用的类型,常用的是uint8,int32,int64.
2.转化成tfrecords的时间有点长,需要等待。
import os
import tensorflow as tf
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import cv2
def get_data (file_path):
data = []
label = []
for dirs in os.listdir(file_path):
temp_path = os.path.join(file_path,dirs)
i =0
for dirss in os.listdir(temp_path):
data.append(os.path.join(temp_path,dirss))
num_img = len(os.listdir(temp_path))
label = np.append(label,num_img*[1])
temp = np.array([data,label])
temp = temp.transpose()
np.random.shuffle(temp)
image_list = list(temp[:,0])
label_list = list(temp[:,1])
label_list = [int(float(i)) for i in label_list]
return image_list,label_list
# 转化成字符串
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 convert_tfrecord(images,labels,save_filename):
writer = tf.python_io.TFRecordWriter(save_filename)
print("Transform start....")
num_examples= len(labels)
if np.shape(images)[0]!=num_examples:
raise ValueError('Images size %d does not match label size %d.' % (images.shape[0], num_examples))
for index in np.arange(0,num_examples):
try:
image = io.imread(images[index],as_grey=False)
#image = tf.image.decode_jpeg(images[index])
#print(image.shape)
image_raw = image.tostring()
#print(len(image_raw))
example = tf.train.Example(features = tf.train.Features(feature={
'label' :_int64_feature(int(labels[index])),
'image_raw':_bytes_feature(image_raw)
}))
writer.write(example.SerializeToString())
except IOError as e:
print('Could not read:',images[index])
print('error :%s Skip it !\n'%e)
writer.close()
print("success!") def read_and_decode(tfrecords_file,batch_size):
reader = tf.TFRecordReader()
filename_queue = tf.train.string_input_producer([tfrecords_file])
_,serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'label': tf.FixedLenFeature([],tf.int64),
'image_raw': tf.FixedLenFeature([], tf.string)
}
)
#print(features['image_raw'])
capacity = 1000+3*batch_size
image = tf.decode_raw(features['image_raw'],tf.uint8)
label = tf.cast(features['label'],tf.int32)
#image = tf.image.resize_images(image,[300, 200, 1])
image = tf.reshape(image,[200,300,3])
image_batch,label_batch = tf.train.batch([image,label],
batch_size=batch_size,
capacity=capacity)
image_batch = tf.image.resize_image_with_crop_or_pad(image_batch,100,100)
image_batch = tf.cast(image_batch, tf.float32) * (1. / 255)
return image_batch,label_batch
def plot_images(images, labels):
'''plot one batch size
'''
for i in np.arange(0, 2):
plt.subplot(3, 3, i + 1)
plt.axis('off')
# plt.title((labels[i] - 1), fontsize = 14)
plt.subplots_adjust(top=1)
print(labels[i])
print(images.shape)
# print(images[i].shape)
plt.imshow(images[i][:,:,:])
plt.show()
def train():
image,label = get_data('E:\syn_data')
convert_tfrecord(image,label,'1.tfrecords')
x_batch, y_batch = read_and_decode('1.tfrecords', batch_size=2)
with tf.Session() as sess:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
try:
i=0
while not coord.should_stop() and i<3:
# just plot one batch size
image, label = sess.run([x_batch, y_batch])
plot_images(image, label)
i+=1
except tf.errors.OutOfRangeError:
print('done!')
finally:
coord.request_stop()
coord.join(threads) #train()
使用tfrecord建立自己的数据集的更多相关文章
- tensorflow目标检测API之建立自己的数据集
1 收集数据 为了方便,我找了11张月儿的照片做数据集,如图1,当然这在实际应用过程中是远远不够的 2 labelImg软件的安装 使用labelImg软件(下载地址:https://github.c ...
- SSAS多维数据集以及维度的建立
首先打开vs建立一个Analysis Services项目,然后点击数据源文件右键[新建数据源],根据数据源向导建立自己的数据源,如图1: 点击[确定],选择刚才的数据连接,点击[下一步]进入模拟信息 ...
- ubuntu之路——day6(今天对数据集的建立有了更深的体会)
两个重点: 一.举个例子,如果建立一个图像识别的数据集,你的训练集和你的训练验证集是从网上爬下来的(也就是说这些图片的大小.像素.后期制作都可能很精美),你真正的测试集是用户的手机上传(不同的手机.环 ...
- 第二十二节,TensorFlow中的图片分类模型库slim的使用、数据集处理
Google在TensorFlow1.0,之后推出了一个叫slim的库,TF-slim是TensorFlow的一个新的轻量级的高级API接口.这个模块是在16年新推出的,其主要目的是来做所谓的“代码瘦 ...
- FineReport中如何制作树数据集来实现组织树报表
1. 问题描述 FineReport,组织树报表中由id与父id来实现组织树报表,若层级数较多时,对每个单元格设置过滤条件和形态会比较繁琐,因此FineReport提供了一种特殊的数据集——树数据集, ...
- TensorFlow数据集(一)——数据集的基本使用方法
参考书 <TensorFlow:实战Google深度学习框架>(第2版) 例子:从一个张量创建一个数据集,遍历这个数据集,并对每个输入输出y = x^2 的值. #!/usr/bin/en ...
- TensorFlow学习笔记——LeNet-5(训练自己的数据集)
在之前的TensorFlow学习笔记——图像识别与卷积神经网络(链接:请点击我)中了解了一下经典的卷积神经网络模型LeNet模型.那其实之前学习了别人的代码实现了LeNet网络对MNIST数据集的训练 ...
- ArcGIS Engine开发之地图基本操作(3)
地图数据的加载 一.加载Shapefile数据 Shapefile文件是目前主流的一种空间数据的文件存储方式,也是不同GIS软件进行数据格式转换常用的中间格式.加载Shapefile数据的方式有两种: ...
- 限制Boltzmann机(Restricted Boltzmann Machine)
起源:Boltzmann神经网络 Boltzmann神经网络的结构是由Hopfield递归神经网络改良过来的,Hopfield中引入了统计物理学的能量函数的概念. 即,cost函数由统计物理学的能量函 ...
随机推荐
- 软键盘 显示隐藏 测量高度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 使用winsw将springboot打包的jar注册系统本地服务
1.下载winsw 下载地址:https://github.com/kohsuke/winsw/releases 我这里下载的是2.3.0版. 下载sample-minimal.xml和WinSW.N ...
- C# DataGridView 动态添加列和行
https://blog.csdn.net/alisa525/article/details/7350471 dataGridView1.ReadOnly = true ; //禁用编辑功能 ...
- MLP神经网络实例--手写识别
1.导入MNIST数据集 直接使用fetch_mldata会报错,错误信息是python3.7把fetch_mldata方法移除了,所以需要单独下载数据集从这个网站上下载数据集: https://gi ...
- 节日营销!这样搞-App运营日常
节日送礼需求日益增长,当儿女们有了购买需求的时候,商家如何突出重围,成为孝子们的首选?如何做好节日营销?几个经验分享一下: 1.抓住节日特色 结合节日风格特色,营造节日气氛,如母亲节这种节日,主要体现 ...
- Html5知识精粹纪录
1. HTML5文档的正文结构及新元素 正文结构: <header> <nav> <section> <aside> <footer> 深入 ...
- Extjs 兼容IE8常见问题及解决方法
1. 在IE8中整个页面都打不开,一般情况是: 页面组件中最后一个属性出现了逗号 没有多余的逗号,就很有可能是组件中没有设置renderTo:Ext.getBody(); 2. 页面按钮颜色失效 自定 ...
- HANA数据库无法停止
Symptom: 使用命令HDB stop多次,每次报错为timeout ----------------------- sdpadm@PRDDB01:/usr/sap/SDP/HDB66/prder ...
- mysql日期模糊查找的方法
Mysql模糊查询有以下三种方法: 1.Convert转成日期时间型,在用Like查询.select * from table1 where convert(date,DATETIME) like ' ...
- c# 定制Equals()