TensorFlow 中可以通過三種方式讀取資料:
一、通過feed_dict傳遞資料;
input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) output = tf.multiply(input1, input2) with tf.Session() as sess: feed_dict={input1: [[7.,2.]], input2: [[2.],[3.]]} print(sess.run(output,feed_dict ))
二、從檔案中讀取資料;
import os import tensorflow as tf filename = ['A.jpg', 'B.jpg', 'C.jpg'] # string_input_producer會產生一個檔案名佇列 filename_queue = tf.train.string_input_producer(filename, shuffle=False, num_epochs=5) # reader從檔案名佇列中讀資料,對應的方法是reader.read reader = tf.WholeFileReader() key, value = reader.read(filename_queue) init=tf.local_variables_initializer() # tf.train.string_input_producer定義了一個epoch變數,要對它進行初始化 with tf.Session() as sess: sess.run(init) # 使用start_queue_runners之后,才會開始填充佇列 tf.train.start_queue_runners(sess=sess) i = 0 while True: i += 1 # 獲取圖片資料并保存 image_data =https://www.cnblogs.com/Fengqiao/p/ sess.run(value) with open('read/test_%d.jpg' % i, 'wb') as f: f.write(image_data) # 程式最后會拋出一個OutOfRangeError,這是epoch跑完,佇列關閉的標志
運行上面的代碼需要做兩點準備:
1.在python的作業目錄下保存3張圖片,分布命名為:'A.jpg', 'B.jpg', 'C.jpg'
2.在此目錄下建立read檔案夾
三、使用預加載的資料;

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/197785.html
標籤:Python
