关说不练假把式。手上正好有车牌字符的数据集,想把他们写成TFRecord格式,然后读进来,构建一个简单的cnn训练看看。然后发现准确率只有0.0x。随机猜也比这要好点吧。只能一步步检查整个过程。暂时想到问题可能出现的地方:

  • 数据编码解码错误
  • 网络构建问题
  • 学习步长问题
  • 数据量太小
  • label设置

不确定是不是这些问题先检查下,tensorboard能给我们很多信息。今天先检查了图片解码编码问题。在读取数据的时候为image增加一个summary,这样就能在tensorboard上看到图片了。

img=tf.summary.image('input',x,batch_size)

tensorboard的使用之前有说过。出现的结果说我没有图片记录,大约是这个意思。所以我的解码编码程序还是有问题。图片编码成tfreord无非就是把图片数据按照tfrecord的格式填进去。tfrecord是example的集合,example的格式:

message Example {#message类似于类
Features features = 1;
};

而 Features 是字典集合,(key,value)。

下面直接上tfrecord编码解码代码,改好过的。

 import tensorflow as tf
import numpy as np
import glob
import os
from PIL import Image
def _int64_feature(value):
if not isinstance(value,list):
value=[value]
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
def _byte_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def encode_to_tfrecords(data_path,name,rows=24,cols=16):#从图片路径读取图片编码成tfrecord
folders=os.listdir(data_path)#这里都和我图片的位置有关
folders.sort()
numclass=len(folders)
i=0
npic=0
writer=tf.python_io.TFRecordWriter(name)
for floder in folders:
path=data_path+"/"+floder
img_names=glob.glob(os.path.join(path,"*.bmp"))
for img_name in img_names:
img_path=img_name
img=Image.open(img_path).convert('P')
img=img.resize((cols,rows))
img_raw=img.tobytes()
labels=[0]*34#我用的是softmax,要和预测值的维度一致
labels[i]=1
example=tf.train.Example(features=tf.train.Features(feature={#填充example
'image_raw':_byte_feature(img_raw),
'label':_int64_feature(labels)}))
writer.write(example.SerializeToString())#把example加入到writer里,最后写到磁盘。
npic=npic+1
i=i+1
writer.close()
print npic def decode_from_tfrecord(filequeuelist,rows=24,cols=16):
reader=tf.TFRecordReader()#文件读取
_,example=reader.read(filequeuelist)
features=tf.parse_single_example(example,features={'image_raw':#解码
tf.FixedLenFeature([],tf.string),
'label':tf.FixedLenFeature([34,1],tf.int64)})
image=tf.decode_raw(features['image_raw'],tf.uint8)
image.set_shape(rows*cols)
image=tf.cast(image,tf.float32)*(1./255)-0.5
label=tf.cast(features['label'],tf.int32)
return image,label def get_batch(filename_queue,batch_size):
with tf.name_scope('get_batch'):
[image,label]=decode_from_tfrecord(filename_queue)
images,labels=tf.train.shuffle_batch([image,label],batch_size=batch_size,num_threads=2,
capacity=100+3*batch_size,min_after_dequeue=100)
return images,labels def generate_filenamequeue(filequeuelist):
filename_queue=tf.train.string_input_producer(filequeuelist,num_epochs=5)
return filename_queue def test(filename,batch_size):
filename_queue=generate_filenamequeue(filename)
[images,labels]=get_batch(filename_queue,batch_size)
init_op = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
sess=tf.InteractiveSession()
sess.run(init_op)
coord=tf.train.Coordinator()
threads=tf.train.start_queue_runners(sess=sess,coord=coord)
i=0
try:
while not coord.should_stop():
image,label=sess.run([images,labels])
i=i+1
if i%1000==0:
for j in range(batch_size):#之前tfrecord编码的时候,数据范围变成[-0.5,0.5],现在相当于逆操作,把数据变成图片像素值
image[j]=(image[j]+0.5)*255
ar=np.asarray(image[j],np.uint8)
#image[j]=tf.cast(image[j],tf.uint8)
print ar.shape
img=Image.frombytes("P",(16,24),ar.tostring())#函数参数中宽度高度要注意。构建24×16的图片
img.save("/home/wen/MNIST_data/reverse_%d.bmp"%(i+j),"BMP")#保存部分图片查看
'''if(i>710):
print("step %d"%(i))
print image
print label'''
except tf.errors.OutOfRangeError:
print('Done training -- epoch limit reached')
finally:
# When done, ask the threads to stop.
coord.request_stop() # Wait for threads to finish.
coord.join(threads)
sess.close()

读取文件的动态图,来自官网教程

在文件夹中:

在tensorboard里:

终于看见熟悉的图片了,现在数据编码解码没问题,效果依然0.0x。。。。

错误改掉一个少一个,接着检查其他部分。

debug过程中,一直在google,stackoverflow,官网documents,度娘略微鸡肋。

***********************************************************************************************************************************************************************************************

猜测是同类样本集中b并且同类数据多,用batch_shuffle装进来的都还是同类

image和TFRecord互相转换的更多相关文章

  1. day21 TFRecord格式转换MNIST并显示

    首先简要介绍了下TFRecord格式以及内部实现protobuf协议,然后基于TFRecord格式,对MNIST数据集转换成TFRecord格式,写入本地磁盘文件,再从磁盘文件读取,通过pyplot模 ...

  2. 学习笔记TF016:CNN实现、数据集、TFRecord、加载图像、模型、训练、调试

    AlexNet(Alex Krizhevsky,ILSVRC2012冠军)适合做图像分类.层自左向右.自上向下读取,关联层分为一组,高度.宽度减小,深度增加.深度增加减少网络计算量. 训练模型数据集 ...

  3. 深度学习原理与框架-Tfrecord数据集的制作 1.tf.train.Examples(数据转换为二进制) 3.tf.image.encode_jpeg(解码图片加码成jpeg) 4.tf.train.Coordinator(构建多线程通道) 5.threading.Thread(建立单线程) 6.tf.python_io.TFR(TFR读入器)

    1. 配套使用: tf.train.Examples将数据转换为二进制,提升IO效率和方便管理 对于int类型 : tf.train.Examples(features=tf.train.Featur ...

  4. Tensorflow中使用tfrecord方式读取数据-深度学习-周振洋

    本博客默认读者对神经网络与Tensorflow有一定了解,对其中的一些术语不再做具体解释.并且本博客主要以图片数据为例进行介绍,如有错误,敬请斧正. 使用Tensorflow训练神经网络时,我们可以用 ...

  5. TensorFlow高效读取数据的方法——TFRecord的学习

    关于TensorFlow读取数据,官网给出了三种方法: 供给数据(Feeding):在TensorFlow程序运行的每一步,让python代码来供给数据. 从文件读取数据:在TensorFlow图的起 ...

  6. 更加清晰的TFRecord格式数据生成及读取

    TFRecords 格式数据文件处理流程 TFRecords 文件包含了 tf.train.Example 协议缓冲区(protocol buffer),协议缓冲区包含了特征 Features.Ten ...

  7. TFRecord 的使用

    什么是 TFRecord PS:这段内容摘自 http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/reading_data.html 一 ...

  8. TFRecord读写简介+Demo 基于Ubuntu18.04+Tensorflow1.12 无WARNING

    简介 TFRecord是TensorFlow官方推荐使用的数据格式化存储工具. 它规范了数据的读写方式. 只要生成一次TFRecord,之后的数据读取和加工处理的效率都会得到提高. 将图片转换成TFR ...

  9. javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈

    Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...

随机推荐

  1. POJ1984 Navigation Nightmare —— 种类并查集

    题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K T ...

  2. BZOJ_1067_[SCOI2007]降雨量_ST表

    BZOJ_1067_[SCOI2007]降雨量_ST表 Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意 Y<Z< ...

  3. RobotFrameWork--selenium2模拟chrome的user agent

    ${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver ${opt ...

  4. hdu1026(bfs+优先队列+打印路径)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. vue 组件 全局注册和局部注册

    全局注册,注册的组件需要在初始化根实例之前注册了组件: 局部注册,通过使用组件实例选项注册,可以使组件仅在另一个组件或者实例的作用域中可用: 全局组件 js Vue.component('tab-ti ...

  6. 《10 minutes to pandas》(转)

    原文出处:http://pandas.pydata.org/pandas-docs/stable/10min.html 10 Minutes to pandas This is a short int ...

  7. 解决Linux主机上的 远程MySQL客户端无法连接的问题

    无法连接到 MySQL 数据库可能的原因有: 1. PHP 无法连接 MySQL 可能是 PHP 配置不正确,没加上连接 MySQL 的功能. 2. MySQL 软件包升级,但没有升级数据库,或安装 ...

  8. oauth X-Frame-Options 跳转授权页面时,302重定向禁用iframe

    因为oauth/authorize响应头包含X-Frame-Options: DENY解决方案:openresty nginx 移除该属性,经测试生效 more_clear_headers X-Fra ...

  9. python 高阶函数二 map()和reduce()

    一.map()函数 map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. >>> fro ...

  10. bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛【Floyd】

    floyd传递关系,一个牛能确定排名的条件是能和所有牛确定关系 #include<iostream> #include<cstdio> using namespace std; ...