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

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

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

TensorFLow能夠識別的圖像文件,可以通過numpy,使用tf.Variable或者tf.placeholder加載進tensorflow;也可以通過自帶函數(shù)(tf.read)讀取,當圖像文件過多時,一般使用pipeline通過隊列的方法進行讀取。下面我們介紹兩種生成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() 

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • python多進程實現(xiàn)進程間通信實例

    python多進程實現(xiàn)進程間通信實例

    這篇文章主要介紹了python多進程實現(xiàn)進程間通信實例,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Python模擬實現(xiàn)全功能貸款計算器

    Python模擬實現(xiàn)全功能貸款計算器

    在個人理財中,貸款計算器是一款非常實用的工具,本文將教你如何使用Python編寫一個全功能的貸款計算器,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-12-12
  • 修改 CentOS 6.x 上默認Python的方法

    修改 CentOS 6.x 上默認Python的方法

    這篇文章主要介紹了修改 CentOS 6.x 上默認Python的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • Django ORM 查詢表中某列字段值的方法

    Django ORM 查詢表中某列字段值的方法

    這篇文章主要介紹了Django ORM 查詢表中某列字段值的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Python爬蟲防封ip的一些技巧

    Python爬蟲防封ip的一些技巧

    這篇文章主要介紹了Python爬蟲防封ip的一些技巧,對平時學習爬蟲有所幫助,感興趣的朋友可以了解下
    2020-08-08
  • Matplotlib?3D?繪制小紅花原理

    Matplotlib?3D?繪制小紅花原理

    這篇文章主要介紹了Matplotlib?3D?繪制小紅花原理,小編上一篇文章一家介紹了繪制小紅化,本篇博文主要介紹一下3D小紅花的繪制原理,看過上篇博文的朋友可以參考一下
    2022-02-02
  • pygame 鍵盤事件的實踐

    pygame 鍵盤事件的實踐

    本文主要介紹了pygame 鍵盤事件,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Python3 中作為一等對象的函數(shù)解析

    Python3 中作為一等對象的函數(shù)解析

    這篇文章主要介紹了Python3 中作為一等對象的函數(shù),本文通過實例代碼講解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Python設計模式之工廠方法模式實例詳解

    Python設計模式之工廠方法模式實例詳解

    這篇文章主要介紹了Python設計模式之工廠方法模式,結合實例形式較為詳細的分析了工廠方法模式的概念、原理、用法及相關操作技巧,需要的朋友可以參考下
    2019-01-01
  • Python直接賦值、淺拷貝與深度拷貝實例分析

    Python直接賦值、淺拷貝與深度拷貝實例分析

    這篇文章主要介紹了Python直接賦值、淺拷貝與深度拷貝,結合實例形式分析了Python直接賦值、淺拷貝與深度拷貝的概念、原理、用法及相關操作注意事項,需要的朋友可以參考下
    2019-06-06

最新評論

宜都市| 大石桥市| 全州县| 兰坪| 兴安县| 铁力市| 农安县| 西乌珠穆沁旗| 柘荣县| 兴宁市| 正蓝旗| 韩城市| 潮安县| 龙岩市| 永顺县| 南投县| 曲周县| 延安市| 郧西县| 横山县| 华池县| 闵行区| 沈丘县| 莫力| 商南县| 阿瓦提县| 永安市| 泌阳县| 龙井市| 邹城市| 钦州市| 乌兰察布市| 盐山县| 安龙县| 兴隆县| 峨眉山市| 新民市| 逊克县| 苏尼特左旗| 邵阳市| 延津县|