注意事项:

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建立自己的数据集的更多相关文章

  1. tensorflow目标检测API之建立自己的数据集

    1 收集数据 为了方便,我找了11张月儿的照片做数据集,如图1,当然这在实际应用过程中是远远不够的 2 labelImg软件的安装 使用labelImg软件(下载地址:https://github.c ...

  2. SSAS多维数据集以及维度的建立

    首先打开vs建立一个Analysis Services项目,然后点击数据源文件右键[新建数据源],根据数据源向导建立自己的数据源,如图1: 点击[确定],选择刚才的数据连接,点击[下一步]进入模拟信息 ...

  3. ubuntu之路——day6(今天对数据集的建立有了更深的体会)

    两个重点: 一.举个例子,如果建立一个图像识别的数据集,你的训练集和你的训练验证集是从网上爬下来的(也就是说这些图片的大小.像素.后期制作都可能很精美),你真正的测试集是用户的手机上传(不同的手机.环 ...

  4. 第二十二节,TensorFlow中的图片分类模型库slim的使用、数据集处理

    Google在TensorFlow1.0,之后推出了一个叫slim的库,TF-slim是TensorFlow的一个新的轻量级的高级API接口.这个模块是在16年新推出的,其主要目的是来做所谓的“代码瘦 ...

  5. FineReport中如何制作树数据集来实现组织树报表

    1. 问题描述 FineReport,组织树报表中由id与父id来实现组织树报表,若层级数较多时,对每个单元格设置过滤条件和形态会比较繁琐,因此FineReport提供了一种特殊的数据集——树数据集, ...

  6. TensorFlow数据集(一)——数据集的基本使用方法

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 例子:从一个张量创建一个数据集,遍历这个数据集,并对每个输入输出y = x^2 的值. #!/usr/bin/en ...

  7. TensorFlow学习笔记——LeNet-5(训练自己的数据集)

    在之前的TensorFlow学习笔记——图像识别与卷积神经网络(链接:请点击我)中了解了一下经典的卷积神经网络模型LeNet模型.那其实之前学习了别人的代码实现了LeNet网络对MNIST数据集的训练 ...

  8. ArcGIS Engine开发之地图基本操作(3)

    地图数据的加载 一.加载Shapefile数据 Shapefile文件是目前主流的一种空间数据的文件存储方式,也是不同GIS软件进行数据格式转换常用的中间格式.加载Shapefile数据的方式有两种: ...

  9. 限制Boltzmann机(Restricted Boltzmann Machine)

    起源:Boltzmann神经网络 Boltzmann神经网络的结构是由Hopfield递归神经网络改良过来的,Hopfield中引入了统计物理学的能量函数的概念. 即,cost函数由统计物理学的能量函 ...

随机推荐

  1. 怎样让ssh连接保持连接, 而不会因为没有操作而中断

    因为安全方面的考虑, ssh服务默认在一段时间内不操作会断开连接, 解决方法修改ssh的配置文件, 让ssh每隔一段时间就自动进行一次连接, 以达到保持连接的目的. 首先找到ssh配置文件的位置: f ...

  2. 线程二(Monitor)

    Monitor 类的命名空间是 System.Threading,它的用法和 lock 本质是一样的. 使用 Monitor 类锁定资源的代码如下. Monitor.Enter(object); tr ...

  3. csv注入复现代码

    以下代码生成的csv文件,使用Microsoft Execl能成功弹出计算器,虽然打开时有安全提示,但是大多数src还是会接收该类漏洞 -------------------------------- ...

  4. ORACLE获取年初年末,月初月末,季度初季度末

    转自:https://www.cnblogs.com/leqhome/p/5319984.html --年初,年末select trunc(sysdate,'yyyy') from dual;sele ...

  5. nginx 之 https 证书配置

    HTTPS原理和作用 为什么需要HTTPS 原因:HTTP不安全 传输数据被中间人盗用.信息泄露 数据内容劫持.篡改 HTTPS协议的实现 对传输内容进行加密以及身份验证 对称加密:加密秘钥和解密秘钥 ...

  6. impala 表迁移方式 partquet数据文件移动方法

    1.原表查询:select count(*) from edm.ucard_wxd0123 where stat_dt = '2024-01-09' and id_no = '110101199003 ...

  7. c# 串行化事件

  8. linux 的GUNB修复问题

    1.意外断电,kali linux 虚拟机没有正常关机的时候 , 突然断电之后重启电脑之后,kali linux 直接黑屏了无法进入系统.如下面的界面 光标一直在闪烁. 这里可以使用 快捷键 同时按住 ...

  9. Gitlab-CI +Docker + Docker-Compose构建可持续集成java项目的镜像

    先注册好Runner: https://www.cnblogs.com/zsifan/p/11714788.html 以及搭建Java和Maven环境: https://www.cnblogs.com ...

  10. osi七层网络模型(一)

    在网络中OSI分为七层网络模型,怎么理解七层模型呢?简单理解我们的手机都有手机壳.屏幕.摄像头.电池.芯片等很多原件,每个原件都是由不同的 厂家生产,最终组装成了一部功能完整的手机,同样,在网络中,也 ...