TensorFLow 不同大小图片的TFrecords存取实例
全部存入一个TFrecords文件,然后读取并显示第一张。
不多写了,直接贴代码。
fromPILimportImage
importnumpyasnp
importmatplotlib.pyplotasplt
importtensorflowastf
IMAGE_PATH='test/'
tfrecord_file=IMAGE_PATH+'test.tfrecord'
writer=tf.python_io.TFRecordWriter(tfrecord_file)
def_int64_feature(value):
returntf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def_bytes_feature(value):
returntf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
defget_image_binary(filename):
"""Youcanreadintheimageusingtensorflowtoo,butit'sadrag
sinceyouhavetocreategraphs.It'smucheasierusingPillowandNumPy
"""
image=Image.open(filename)
image=np.asarray(image,np.uint8)
shape=np.array(image.shape,np.int32)
returnshape,image.tobytes()#convertimagetorawdatabytesinthearray.
defwrite_to_tfrecord(label,shape,binary_image,tfrecord_file):
"""ThisexampleistowriteasampletoTFRecordfile.Ifyouwanttowrite
moresamples,justusealoop.
"""
#writelabel,shape,andimagecontenttotheTFRecordfile
example=tf.train.Example(features=tf.train.Features(feature={
'label':_int64_feature(label),
'h':_int64_feature(shape[0]),
'w':_int64_feature(shape[1]),
'c':_int64_feature(shape[2]),
'image':_bytes_feature(binary_image)
}))
writer.write(example.SerializeToString())
defwrite_tfrecord(label,image_file,tfrecord_file):
shape,binary_image=get_image_binary(image_file)
write_to_tfrecord(label,shape,binary_image,tfrecord_file)
#print(shape)
defmain():
#assumetheimagehasthelabelChihuahua,whichcorrespondstoclassnumber1
label=[1,2]
image_files=[IMAGE_PATH+'a.jpg',IMAGE_PATH+'b.jpg']
foriinrange(2):
write_tfrecord(label[i],image_files[i],tfrecord_file)
writer.close()
batch_size=2
filename_queue=tf.train.string_input_producer([tfrecord_file])
reader=tf.TFRecordReader()
_,serialized_example=reader.read(filename_queue)
img_features=tf.parse_single_example(
serialized_example,
features={
'label':tf.FixedLenFeature([],tf.int64),
'h':tf.FixedLenFeature([],tf.int64),
'w':tf.FixedLenFeature([],tf.int64),
'c':tf.FixedLenFeature([],tf.int64),
'image':tf.FixedLenFeature([],tf.string),
})
h=tf.cast(img_features['h'],tf.int32)
w=tf.cast(img_features['w'],tf.int32)
c=tf.cast(img_features['c'],tf.int32)
image=tf.decode_raw(img_features['image'],tf.uint8)
image=tf.reshape(image,[h,w,c])
label=tf.cast(img_features['label'],tf.int32)
label=tf.reshape(label,[1])
#image=tf.image.resize_images(image,(500,500))
#image,label=tf.train.batch([image,label],batch_size=batch_size)
withtf.Session()assess:
coord=tf.train.Coordinator()
threads=tf.train.start_queue_runners(coord=coord)
image,label=sess.run([image,label])
coord.request_stop()
coord.join(threads)
print(label)
plt.figure()
plt.imshow(image)
plt.show()
if__name__=='__main__':
main()
全部存入一个TFrecords文件,然后按照batch_size读取,注意需要将图片变成一样大才能按照batch_size读取。
fromPILimportImage
importnumpyasnp
importmatplotlib.pyplotasplt
importtensorflowastf
IMAGE_PATH='test/'
tfrecord_file=IMAGE_PATH+'test.tfrecord'
writer=tf.python_io.TFRecordWriter(tfrecord_file)
def_int64_feature(value):
returntf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def_bytes_feature(value):
returntf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
defget_image_binary(filename):
"""Youcanreadintheimageusingtensorflowtoo,butit'sadrag
sinceyouhavetocreategraphs.It'smucheasierusingPillowandNumPy
"""
image=Image.open(filename)
image=np.asarray(image,np.uint8)
shape=np.array(image.shape,np.int32)
returnshape,image.tobytes()#convertimagetorawdatabytesinthearray.
defwrite_to_tfrecord(label,shape,binary_image,tfrecord_file):
"""ThisexampleistowriteasampletoTFRecordfile.Ifyouwanttowrite
moresamples,justusealoop.
"""
#writelabel,shape,andimagecontenttotheTFRecordfile
example=tf.train.Example(features=tf.train.Features(feature={
'label':_int64_feature(label),
'h':_int64_feature(shape[0]),
'w':_int64_feature(shape[1]),
'c':_int64_feature(shape[2]),
'image':_bytes_feature(binary_image)
}))
writer.write(example.SerializeToString())
defwrite_tfrecord(label,image_file,tfrecord_file):
shape,binary_image=get_image_binary(image_file)
write_to_tfrecord(label,shape,binary_image,tfrecord_file)
#print(shape)
defmain():
#assumetheimagehasthelabelChihuahua,whichcorrespondstoclassnumber1
label=[1,2]
image_files=[IMAGE_PATH+'a.jpg',IMAGE_PATH+'b.jpg']
foriinrange(2):
write_tfrecord(label[i],image_files[i],tfrecord_file)
writer.close()
batch_size=2
filename_queue=tf.train.string_input_producer([tfrecord_file])
reader=tf.TFRecordReader()
_,serialized_example=reader.read(filename_queue)
img_features=tf.parse_single_example(
serialized_example,
features={
'label':tf.FixedLenFeature([],tf.int64),
'h':tf.FixedLenFeature([],tf.int64),
'w':tf.FixedLenFeature([],tf.int64),
'c':tf.FixedLenFeature([],tf.int64),
'image':tf.FixedLenFeature([],tf.string),
})
h=tf.cast(img_features['h'],tf.int32)
w=tf.cast(img_features['w'],tf.int32)
c=tf.cast(img_features['c'],tf.int32)
image=tf.decode_raw(img_features['image'],tf.uint8)
image=tf.reshape(image,[h,w,c])
label=tf.cast(img_features['label'],tf.int32)
label=tf.reshape(label,[1])
image=tf.image.resize_images(image,(224,224))
image=tf.reshape(image,[224,224,3])
image,label=tf.train.batch([image,label],batch_size=batch_size)
withtf.Session()assess:
coord=tf.train.Coordinator()
threads=tf.train.start_queue_runners(coord=coord)
image,label=sess.run([image,label])
coord.request_stop()
coord.join(threads)
print(image.shape)
print(label)
plt.figure()
plt.imshow(image[0,:,:,0])
plt.show()
plt.figure()
plt.imshow(image[0,:,:,1])
plt.show()
image1=image[0,:,:,:]
print(image1.shape)
print(image1.dtype)
im=Image.fromarray(np.uint8(image1))#参考numpy和图片的互转:http://blog.csdn.net/zywvvd/article/details/72810360
im.show()
if__name__=='__main__':
main()
输出是
(2,224,224,3) [[1] [2]] 第一张图片的三种显示(略)
封装成函数:
#-*-coding:utf-8-*-
"""
CreatedonFriSep814:38:152017
@author:wayne
"""
'''
本文参考了以下代码,在多个不同大小图片存取方面做了重新开发:
https://github.com/chiphuyen/stanford-tensorflow-tutorials/blob/master/examples/09_tfrecord_example.py
http://blog.csdn.net/hjxu2016/article/details/76165559
https://stackoverflow.com/questions/41921746/tensorflow-varlenfeature-vs-fixedlenfeature
https://github.com/tensorflow/tensorflow/issues/10492
后续:
-存入多个TFrecords文件的例子见
http://blog.csdn.net/xierhacker/article/details/72357651
-如何作shuffle和数据增强
string_input_producer(需要理解tf的数据流,标签队列的工作方式等等)
http://blog.csdn.net/liuchonge/article/details/73649251
'''
fromPILimportImage
importnumpyasnp
importmatplotlib.pyplotasplt
importtensorflowastf
IMAGE_PATH='test/'
tfrecord_file=IMAGE_PATH+'test.tfrecord'
writer=tf.python_io.TFRecordWriter(tfrecord_file)
def_int64_feature(value):
returntf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def_bytes_feature(value):
returntf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
defget_image_binary(filename):
"""Youcanreadintheimageusingtensorflowtoo,butit'sadrag
sinceyouhavetocreategraphs.It'smucheasierusingPillowandNumPy
"""
image=Image.open(filename)
image=np.asarray(image,np.uint8)
shape=np.array(image.shape,np.int32)
returnshape,image.tobytes()#convertimagetorawdatabytesinthearray.
defwrite_to_tfrecord(label,shape,binary_image,tfrecord_file):
"""ThisexampleistowriteasampletoTFRecordfile.Ifyouwanttowrite
moresamples,justusealoop.
"""
#writelabel,shape,andimagecontenttotheTFRecordfile
example=tf.train.Example(features=tf.train.Features(feature={
'label':_int64_feature(label),
'h':_int64_feature(shape[0]),
'w':_int64_feature(shape[1]),
'c':_int64_feature(shape[2]),
'image':_bytes_feature(binary_image)
}))
writer.write(example.SerializeToString())
defwrite_tfrecord(label,image_file,tfrecord_file):
shape,binary_image=get_image_binary(image_file)
write_to_tfrecord(label,shape,binary_image,tfrecord_file)
defread_and_decode(tfrecords_file,batch_size):
'''''readanddecodetfrecordfile,generate(image,label)batches
Args:
tfrecords_file:thedirectoryoftfrecordfile
batch_size:numberofimagesineachbatch
Returns:
image:4Dtensor-[batch_size,width,height,channel]
label:1Dtensor-[batch_size]
'''
#makeaninputqueuefromthetfrecordfile
filename_queue=tf.train.string_input_producer([tfrecord_file])
reader=tf.TFRecordReader()
_,serialized_example=reader.read(filename_queue)
img_features=tf.parse_single_example(
serialized_example,
features={
'label':tf.FixedLenFeature([],tf.int64),
'h':tf.FixedLenFeature([],tf.int64),
'w':tf.FixedLenFeature([],tf.int64),
'c':tf.FixedLenFeature([],tf.int64),
'image':tf.FixedLenFeature([],tf.string),
})
h=tf.cast(img_features['h'],tf.int32)
w=tf.cast(img_features['w'],tf.int32)
c=tf.cast(img_features['c'],tf.int32)
image=tf.decode_raw(img_features['image'],tf.uint8)
image=tf.reshape(image,[h,w,c])
label=tf.cast(img_features['label'],tf.int32)
label=tf.reshape(label,[1])
##########################################################
#youcanputdataaugmentationhere
#distorted_image=tf.random_crop(images,[530,530,img_channel])
#distorted_image=tf.image.random_flip_left_right(distorted_image)
#distorted_image=tf.image.random_brightness(distorted_image,max_delta=63)
#distorted_image=tf.image.random_contrast(distorted_image,lower=0.2,upper=1.8)
#distorted_image=tf.image.resize_images(distorted_image,(imagesize,imagesize))
#float_image=tf.image.per_image_standardization(distorted_image)
image=tf.image.resize_images(image,(224,224))
image=tf.reshape(image,[224,224,3])
#image,label=tf.train.batch([image,label],batch_size=batch_size)
image_batch,label_batch=tf.train.batch([image,label],
batch_size=batch_size,
num_threads=64,
capacity=2000)
returnimage_batch,tf.reshape(label_batch,[batch_size])
defread_tfrecord2(tfrecord_file,batch_size):
train_batch,train_label_batch=read_and_decode(tfrecord_file,batch_size)
withtf.Session()assess:
coord=tf.train.Coordinator()
threads=tf.train.start_queue_runners(coord=coord)
train_batch,train_label_batch=sess.run([train_batch,train_label_batch])
coord.request_stop()
coord.join(threads)
returntrain_batch,train_label_batch
defmain():
#assumetheimagehasthelabelChihuahua,whichcorrespondstoclassnumber1
label=[1,2]
image_files=[IMAGE_PATH+'a.jpg',IMAGE_PATH+'b.jpg']
foriinrange(2):
write_tfrecord(label[i],image_files[i],tfrecord_file)
writer.close()
batch_size=2
#read_tfrecord(tfrecord_file)#读取一个图
train_batch,train_label_batch=read_tfrecord2(tfrecord_file,batch_size)
print(train_batch.shape)
print(train_label_batch)
plt.figure()
plt.imshow(train_batch[0,:,:,0])
plt.show()
plt.figure()
plt.imshow(train_batch[0,:,:,1])
plt.show()
train_batch1=train_batch[0,:,:,:]
print(train_batch.shape)
print(train_batch1.dtype)
im=Image.fromarray(np.uint8(train_batch1))#参考numpy和图片的互转:http://blog.csdn.net/zywvvd/article/details/72810360
im.show()
if__name__=='__main__':
main()
以上这篇TensorFLow不同大小图片的TFrecords存取实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。