あなたtfrecordsから画像やラベルを読み取るために、次のサンプルコードを試してみてください、
import os
import glob
import tensorflow as tf
from matplotlib import pyplot as plt
def read_and_decode_file(filename_queue):
# Create an instance of tf record reader
reader = tf.TFRecordReader()
# Read the generated filename queue
_, serialized_reader = reader.read(filename_queue)
# extract the features you require from the tfrecord using their corresponding key
# In my example, all images were written with 'image' key
features = tf.parse_single_example(
serialized_reader, features={
'image': tf.FixedLenFeature([], tf.string),
'labels': tf.FixedLenFeature([], tf.int16)
})
# Extract the set of images as shown below
img = features['image']
img_out = tf.image.resize_image_with_crop_or_pad(img, target_height=128, target_width=128)
# Similarly extract the labels, be careful with the type
label = features['labels']
return img_out, label
if __name__ == "__main__":
tf.reset_default_graph()
# Path to your tfrecords
path_to_tf_records = os.getcwd() + '/*.tfrecords'
# Collect all tfrecords present in the records folder using glob
list_of_tfrecords = sorted(glob.glob(path_to_tf_records))
# Generate a tensorflow readable filename queue by supplying it with
# a list of tfrecords, optionally it is recommended to shuffle your data
# before feeding into the network
filename_queue = tf.train.string_input_producer(list_of_tfrecords, shuffle=False)
# Supply the tensorflow generated filename queue to the custom function above
image, label = read_and_decode_file(filename_queue)
# Create a new tf session to read the data
sess = tf.Session()
tf.train.start_queue_runners(sess=sess)
# Arbitrary number of iterations
for i in range(50):
img =sess.run(image)
# Show image
plt.imshow(img)
さて、あなたはまた、あなたがこの機能を実行する複数のCPUのスレッドを生成するのに役立つtf.train.shuffle_batchと呼ばれる機能を持っており、ユーザーが指定したバッチサイズに基づいてイメージとラベルを返します。同時に動作するように、データとトレーニングのパイプラインを同時に作成する必要があります。
あなたの2番目の質問に答えるには、はい、CPUだけを使用してモデルを訓練することができますが、遅くなり、まともな結果を得るには数時間から数日かかることがあります。 with tf.device('/gpu:{0}'):
デコレータを削除してから、開始モデルとテンソルフローを作成すると、そのモデルがCPU上に作成されます。
この説明は役立ちます。