使用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函数由统计物理学的能量函 ...
随机推荐
- Docker 方式部署的应用的版本更新
前言 公司使用 Docker-Compose 的方式部署 Jenkins/Gitlab/Sonar/Confluence/Apollo/Harbor/ELK/MySQL 等一系列开发工具/数据库. 而 ...
- Arm-Linux 移植 alsa
ref : https://www.cnblogs.com/yutingliuyl/p/6718875.html https://blog.csdn.net/yuanxinfei920/article ...
- Linux入职基础-1.2_U盘安装RedHat5具体步骤
从U盘安装RedHat5具体步骤 从U盘安装RedHat Linux的具体步骤: 准备工作: RHEL_5.6_i386_DVD.ISO文件 具体步骤: 1.解压并用ultraiso软件打开rhel- ...
- .Net DLL类库引用时没有注释信息
自己编写的类库提供给别人引用时,别人获取不到DLL内部的方法.变量的注释信息,无法了解内部情况和使用方法. 原因:没有随DLL类库一同输出注释文档 解决方案: 在VS界面中选中提供给别人的类库项目 在 ...
- nodejs中使用mongodb
/** * 使用mongodb存储数据 * 1 首先安装mongodb nodejs插件 npm install mongodb --save-dev * 2 安装express (非必须) * * ...
- python之爬取小说
继上一篇爬取小说一念之间的第一章,这里将进一步展示如何爬取整篇小说 # -*- coding: utf- -*- import urllib.request import bs4 import re ...
- POJ1861(Network)-Kruskal
题目在这 Sample Input 4 6 1 2 1 1 3 1 1 4 2 2 3 1 3 4 1 2 4 1 Sample Output 1 4 1 2 1 3 2 3 3 4 题目意思:4个点 ...
- Windows cmd操作文件夹
ir // 列出目录下所有文件夹 rd dirname // 删除dirname文件夹(空文件夹) rd /s/q dirname // 删除dirname文件夹(非空)
- 【转载】Asp.Net中应用程序的事件响应次序
Asp.Net应用程序事件响应次序是指Application事件的相应次序,涉及到的事件包括Application_Start事件.BeginRequest事件.AuthenticateRequest ...
- JavaScript,遍历,for
(for循环,for...in ,for...of ,forEach)(:for in总是得到数组,字符串的下标,而for of和forEach一样,是直接得到值) (forEach() 方法用于调用 ...