溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

TensorFLow如何實(shí)現(xiàn)不同大小圖片的TFrecords存取

發(fā)布時(shí)間:2021-08-21 14:41:11 來(lái)源:億速云 閱讀:163 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了TensorFLow如何實(shí)現(xiàn)不同大小圖片的TFrecords存取,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

全部存入一個(gè)TFrecords文件,然后讀取并顯示第一張。

示例:

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf


IMAGE_PATH = 'test/'
tfrecord_file = IMAGE_PATH + 'test.tfrecord'
writer = tf.python_io.TFRecordWriter(tfrecord_file)


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 get_image_binary(filename):
  """ You can read in the image using tensorflow too, but it's a drag
    since you have to create graphs. It's much easier using Pillow and NumPy
  """
  image = Image.open(filename)
  image = np.asarray(image, np.uint8)
  shape = np.array(image.shape, np.int32)
  return shape, image.tobytes() # convert image to raw data bytes in the array.

def write_to_tfrecord(label, shape, binary_image, tfrecord_file):
  """ This example is to write a sample to TFRecord file. If you want to write
  more samples, just use a loop.
  """
  # write label, shape, and image content to the TFRecord file
  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())


def write_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)



def main():
  # assume the image has the label Chihuahua, which corresponds to class number 1
  label = [1,2]
  image_files = [IMAGE_PATH + 'a.jpg', IMAGE_PATH + 'b.jpg']

  for i in range(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) 


  with tf.Session() as sess:
    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()

全部存入一個(gè)TFrecords文件,然后按照batch_size讀取,注意需要將圖片變成一樣大才能按照batch_size讀取。

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf


IMAGE_PATH = 'test/'
tfrecord_file = IMAGE_PATH + 'test.tfrecord'
writer = tf.python_io.TFRecordWriter(tfrecord_file)


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 get_image_binary(filename):
  """ You can read in the image using tensorflow too, but it's a drag
    since you have to create graphs. It's much easier using Pillow and NumPy
  """
  image = Image.open(filename)
  image = np.asarray(image, np.uint8)
  shape = np.array(image.shape, np.int32)
  return shape, image.tobytes() # convert image to raw data bytes in the array.

def write_to_tfrecord(label, shape, binary_image, tfrecord_file):
  """ This example is to write a sample to TFRecord file. If you want to write
  more samples, just use a loop.
  """
  # write label, shape, and image content to the TFRecord file
  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())


def write_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)



def main():
  # assume the image has the label Chihuahua, which corresponds to class number 1
  label = [1,2]
  image_files = [IMAGE_PATH + 'a.jpg', IMAGE_PATH + 'b.jpg']

  for i in range(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) 


  with tf.Session() as sess:
    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和圖片的互轉(zhuǎn):http://blog.csdn.net/zywvvd/article/details/72810360
    im.show()

if __name__ == '__main__':
  main()

輸出是

(2, 224, 224, 3)
[[1]
 [2]]

第一張圖片的三種顯示(略)

封裝成函數(shù):

# -*- coding: utf-8 -*-
"""
Created on Fri Sep 8 14:38:15 2017

@author: wayne


"""


'''
本文參考了以下代碼,在多個(gè)不同大小圖片存取方面做了重新開(kāi)發(fā):
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

后續(xù):
-存入多個(gè)TFrecords文件的例子見(jiàn)
http://blog.csdn.net/xierhacker/article/details/72357651
-如何作shuffle和數(shù)據(jù)增強(qiáng)
string_input_producer (需要理解tf的數(shù)據(jù)流,標(biāo)簽隊(duì)列的工作方式等等)
http://blog.csdn.net/liuchonge/article/details/73649251
'''

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf


IMAGE_PATH = 'test/'
tfrecord_file = IMAGE_PATH + 'test.tfrecord'
writer = tf.python_io.TFRecordWriter(tfrecord_file)


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 get_image_binary(filename):
  """ You can read in the image using tensorflow too, but it's a drag
    since you have to create graphs. It's much easier using Pillow and NumPy
  """
  image = Image.open(filename)
  image = np.asarray(image, np.uint8)
  shape = np.array(image.shape, np.int32)
  return shape, image.tobytes() # convert image to raw data bytes in the array.

def write_to_tfrecord(label, shape, binary_image, tfrecord_file):
  """ This example is to write a sample to TFRecord file. If you want to write
  more samples, just use a loop.
  """
  # write label, shape, and image content to the TFRecord file
  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())


def write_tfrecord(label, image_file, tfrecord_file):
  shape, binary_image = get_image_binary(image_file)
  write_to_tfrecord(label, shape, binary_image, tfrecord_file)


def read_and_decode(tfrecords_file, batch_size): 
  '''''read and decode tfrecord file, generate (image, label) batches 
  Args: 
    tfrecords_file: the directory of tfrecord file 
    batch_size: number of images in each batch 
  Returns: 
    image: 4D tensor - [batch_size, width, height, channel] 
    label: 1D tensor - [batch_size] 
  ''' 
  # make an input queue from the tfrecord file 

  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])

  ########################################################## 
  # you can put data augmentation here  
#  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) 
  return image_batch, tf.reshape(label_batch, [batch_size]) 

def read_tfrecord2(tfrecord_file, batch_size):
  train_batch, train_label_batch = read_and_decode(tfrecord_file, batch_size)

  with tf.Session() as sess:
    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)
  return train_batch, train_label_batch


def main():
  # assume the image has the label Chihuahua, which corresponds to class number 1
  label = [1,2]
  image_files = [IMAGE_PATH + 'a.jpg', IMAGE_PATH + 'b.jpg']

  for i in range(2):
    write_tfrecord(label[i], image_files[i], tfrecord_file)
  writer.close()

  batch_size = 2
  # read_tfrecord(tfrecord_file) # 讀取一個(gè)圖
  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_batch2 = train_batch[0,:,:,:]
  print(train_batch.shape)
  print(train_batch2.dtype)
  im = Image.fromarray(np.uint8(train_batch2)) #參考numpy和圖片的互轉(zhuǎn):http://blog.csdn.net/zywvvd/article/details/72810360
  im.show()

if __name__ == '__main__':
  main()

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“TensorFLow如何實(shí)現(xiàn)不同大小圖片的TFrecords存取”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI