最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

TensorFLow 不同大小圖片的TFrecords存取實例

 更新時間:2020年01月20日 11:43:00   作者:Wayne2019  
今天小編就為大家分享一篇TensorFLow 不同大小圖片的TFrecords存取實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

全部存入一個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()

全部存入一個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


"""


'''
本文參考了以下代碼,在多個不同大小圖片存取方面做了重新開發(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ù):
-存入多個TFrecords文件的例子見
http://blog.csdn.net/xierhacker/article/details/72357651
-如何作shuffle和數(shù)據(jù)增強
string_input_producer (需要理解tf的數(shù)據(jù)流,標(biāo)簽隊列的工作方式等等)
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) # 讀取一個圖
  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和圖片的互轉(zhuǎn):http://blog.csdn.net/zywvvd/article/details/72810360
  im.show()

if __name__ == '__main__':
  main()

以上這篇TensorFLow 不同大小圖片的TFrecords存取實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Django和Flask框架優(yōu)缺點對比

    Django和Flask框架優(yōu)缺點對比

    這篇文章主要介紹了Django和Flask框架相關(guān)對比,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • Python isinstance函數(shù)介紹

    Python isinstance函數(shù)介紹

    這篇文章主要介紹了Python isinstance函數(shù)介紹,本文用實例講解了判斷變量是否是某個指定類型,需要的朋友可以參考下
    2015-04-04
  • 通過 Python 和 OpenCV 實現(xiàn)目標(biāo)數(shù)量監(jiān)控

    通過 Python 和 OpenCV 實現(xiàn)目標(biāo)數(shù)量監(jiān)控

    這篇文章主要介紹了如何通過 Python 和 OpenCV 實現(xiàn)目標(biāo)數(shù)量監(jiān)控,本文通過實例代碼圖文的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • Python 機器學(xué)習(xí)庫 NumPy入門教程

    Python 機器學(xué)習(xí)庫 NumPy入門教程

    在我們使用Python語言進行機器學(xué)習(xí)編程的時候,這是一個非常常用的基礎(chǔ)庫。本文針對Python 機器學(xué)習(xí)庫 NumPy入門教程,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-04-04
  • python分析inkscape路徑數(shù)據(jù)方案簡單介紹

    python分析inkscape路徑數(shù)據(jù)方案簡單介紹

    這篇文章主要介紹了python分析inkscape路徑數(shù)據(jù)方案簡單介紹,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-09-09
  • Python的print用法示例

    Python的print用法示例

    這篇文章主要介紹了Python的print用法示例,需要的朋友可以參考下
    2014-02-02
  • Django自定義分頁效果

    Django自定義分頁效果

    這篇文章主要為大家詳細(xì)介紹了Django自定義分頁效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • python線程中同步鎖詳解

    python線程中同步鎖詳解

    這篇文章主要為大家詳細(xì)介紹了python線程中同步鎖的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • python獲取對象信息的實例詳解

    python獲取對象信息的實例詳解

    在本篇文章和里小編給大家整理的是一篇關(guān)于python獲取對象信息的實例詳解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-07-07
  • TensorFlow神經(jīng)網(wǎng)絡(luò)構(gòu)造線性回歸模型示例教程

    TensorFlow神經(jīng)網(wǎng)絡(luò)構(gòu)造線性回歸模型示例教程

    這篇文章主要為大家介紹了TensorFlow構(gòu)造線性回歸模型示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-11-11

最新評論

柘城县| 建昌县| 印江| 甘洛县| 大足县| 迁西县| 汶川县| 岚皋县| 军事| 且末县| 和田市| 景德镇市| 鄯善县| 中方县| 民丰县| 高清| 哈巴河县| 平谷区| 金沙县| 常山县| 饶平县| 衡阳县| 牟定县| 龙胜| 寻乌县| 湾仔区| 新宁县| 郸城县| 廉江市| 定襄县| 察哈| 定远县| 景宁| 平潭县| 沧州市| 河津市| 兴义市| 辽宁省| 武城县| 沧州市| 浮梁县|