欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python生成tensorflow輸入輸出的圖像格式的方法

 更新時(shí)間:2018年02月12日 09:14:43   作者:何雷  
本篇文章主要介紹了python生成tensorflow輸入輸出的圖像格式的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

TensorFLow能夠識(shí)別的圖像文件,可以通過(guò)numpy,使用tf.Variable或者tf.placeholder加載進(jìn)tensorflow;也可以通過(guò)自帶函數(shù)(tf.read)讀取,當(dāng)圖像文件過(guò)多時(shí),一般使用pipeline通過(guò)隊(duì)列的方法進(jìn)行讀取。下面我們介紹兩種生成tensorflow的圖像格式的方法,供給tensorflow的graph的輸入與輸出。

import cv2 
import numpy as np 
import h5py 
 
height = 460 
width = 345 
 
with h5py.File('make3d_dataset_f460.mat','r') as f: 
  images = f['images'][:] 
   
image_num = len(images) 
 
data = np.zeros((image_num, height, width, 3), np.uint8) 
data = images.transpose((0,3,2,1)) 

先生成圖像文件的路徑:ls *.jpg> list.txt

import cv2 
import numpy as np 
 
image_path = './' 
list_file = 'list.txt' 
height = 48 
width = 48 
 
image_name_list = [] # read image 
with open(image_path + list_file) as fid: 
  image_name_list = [x.strip() for x in fid.readlines()] 
image_num = len(image_name_list) 
 
data = np.zeros((image_num, height, width, 3), np.uint8) 
 
for idx in range(image_num): 
  img = cv2.imread(image_name_list[idx]) 
  img = cv2.resize(img, (height, width)) 
  data[idx, :, :, :] = img 

2 Tensorflow自帶函數(shù)讀取

def get_image(image_path): 
  """Reads the jpg image from image_path. 
  Returns the image as a tf.float32 tensor 
  Args: 
    image_path: tf.string tensor 
  Reuturn: 
    the decoded jpeg image casted to float32 
  """ 
  return tf.image.convert_image_dtype( 
    tf.image.decode_jpeg( 
      tf.read_file(image_path), channels=3), 
    dtype=tf.uint8) 

pipeline讀取方法

# Example on how to use the tensorflow input pipelines. The explanation can be found here ischlag.github.io. 
import tensorflow as tf 
import random 
from tensorflow.python.framework import ops 
from tensorflow.python.framework import dtypes 
 
dataset_path   = "/path/to/your/dataset/mnist/" 
test_labels_file = "test-labels.csv" 
train_labels_file = "train-labels.csv" 
 
test_set_size = 5 
 
IMAGE_HEIGHT = 28 
IMAGE_WIDTH  = 28 
NUM_CHANNELS = 3 
BATCH_SIZE  = 5 
 
def encode_label(label): 
 return int(label) 
 
def read_label_file(file): 
 f = open(file, "r") 
 filepaths = [] 
 labels = [] 
 for line in f: 
  filepath, label = line.split(",") 
  filepaths.append(filepath) 
  labels.append(encode_label(label)) 
 return filepaths, labels 
 
# reading labels and file path 
train_filepaths, train_labels = read_label_file(dataset_path + train_labels_file) 
test_filepaths, test_labels = read_label_file(dataset_path + test_labels_file) 
 
# transform relative path into full path 
train_filepaths = [ dataset_path + fp for fp in train_filepaths] 
test_filepaths = [ dataset_path + fp for fp in test_filepaths] 
 
# for this example we will create or own test partition 
all_filepaths = train_filepaths + test_filepaths 
all_labels = train_labels + test_labels 
 
all_filepaths = all_filepaths[:20] 
all_labels = all_labels[:20] 
 
# convert string into tensors 
all_images = ops.convert_to_tensor(all_filepaths, dtype=dtypes.string) 
all_labels = ops.convert_to_tensor(all_labels, dtype=dtypes.int32) 
 
# create a partition vector 
partitions = [0] * len(all_filepaths) 
partitions[:test_set_size] = [1] * test_set_size 
random.shuffle(partitions) 
 
# partition our data into a test and train set according to our partition vector 
train_images, test_images = tf.dynamic_partition(all_images, partitions, 2) 
train_labels, test_labels = tf.dynamic_partition(all_labels, partitions, 2) 
 
# create input queues 
train_input_queue = tf.train.slice_input_producer( 
                  [train_images, train_labels], 
                  shuffle=False) 
test_input_queue = tf.train.slice_input_producer( 
                  [test_images, test_labels], 
                  shuffle=False) 
 
# process path and string tensor into an image and a label 
file_content = tf.read_file(train_input_queue[0]) 
train_image = tf.image.decode_jpeg(file_content, channels=NUM_CHANNELS) 
train_label = train_input_queue[1] 
 
file_content = tf.read_file(test_input_queue[0]) 
test_image = tf.image.decode_jpeg(file_content, channels=NUM_CHANNELS) 
test_label = test_input_queue[1] 
 
# define tensor shape 
train_image.set_shape([IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CHANNELS]) 
test_image.set_shape([IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CHANNELS]) 
 
 
# collect batches of images before processing 
train_image_batch, train_label_batch = tf.train.batch( 
                  [train_image, train_label], 
                  batch_size=BATCH_SIZE 
                  #,num_threads=1 
                  ) 
test_image_batch, test_label_batch = tf.train.batch( 
                  [test_image, test_label], 
                  batch_size=BATCH_SIZE 
                  #,num_threads=1 
                  ) 
 
print "input pipeline ready" 
 
with tf.Session() as sess: 
  
 # initialize the variables 
 sess.run(tf.initialize_all_variables()) 
  
 # initialize the queue threads to start to shovel data 
 coord = tf.train.Coordinator() 
 threads = tf.train.start_queue_runners(coord=coord) 
 
 print "from the train set:" 
 for i in range(20): 
  print sess.run(train_label_batch) 
 
 print "from the test set:" 
 for i in range(10): 
  print sess.run(test_label_batch) 
 
 # stop our queue threads and properly close the session 
 coord.request_stop() 
 coord.join(threads) 
 sess.close() 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Yolov5訓(xùn)練意外中斷后如何接續(xù)訓(xùn)練詳解

    Yolov5訓(xùn)練意外中斷后如何接續(xù)訓(xùn)練詳解

    目標(biāo)檢測(cè)是計(jì)算機(jī)視覺(jué)上的一個(gè)重要任務(wù),下面這篇文章主要給大家介紹了關(guān)于Yolov5訓(xùn)練意外中斷后如何接續(xù)訓(xùn)練的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • Pytorch框架構(gòu)建ResNet模型的實(shí)現(xiàn)示例

    Pytorch框架構(gòu)建ResNet模型的實(shí)現(xiàn)示例

    本文主要介紹了Pytorch框架構(gòu)建ResNet模型的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • 使用python向MongoDB插入時(shí)間字段的操作

    使用python向MongoDB插入時(shí)間字段的操作

    這篇文章主要介紹了使用python向MongoDB插入時(shí)間字段的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • python通過(guò)pil為png圖片填充上背景顏色的方法

    python通過(guò)pil為png圖片填充上背景顏色的方法

    這篇文章主要介紹了python通過(guò)pil為png圖片填充上背景顏色的方法,實(shí)例分析了Python使用pil模塊操作png圖片的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • python實(shí)現(xiàn)簡(jiǎn)單猜單詞游戲

    python實(shí)現(xiàn)簡(jiǎn)單猜單詞游戲

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單猜單詞游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Python裝飾器有哪些絕妙的用法

    Python裝飾器有哪些絕妙的用法

    本文主要介紹了Python裝飾器有哪些絕妙的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 利用Python代碼實(shí)現(xiàn)數(shù)據(jù)可視化的5種方法詳解

    利用Python代碼實(shí)現(xiàn)數(shù)據(jù)可視化的5種方法詳解

    在數(shù)據(jù)科學(xué)中,有多種工具可以進(jìn)行可視化。下面這篇文章主要給大家介紹了關(guān)于利用Python代碼實(shí)現(xiàn)數(shù)據(jù)可視化的5種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2018-03-03
  • python密碼學(xué)各種加密模塊教程

    python密碼學(xué)各種加密模塊教程

    這篇文章主要為大家介紹了python密碼學(xué)各種加密模塊教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 用python實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    用python實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了用python實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Ubuntu下使用python讀取doc和docx文檔的內(nèi)容方法

    Ubuntu下使用python讀取doc和docx文檔的內(nèi)容方法

    今天小編就為大家分享一篇Ubuntu下使用python讀取doc和docx文檔的內(nèi)容方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05

最新評(píng)論