2017-09-14 9 views
0

Githubのテンソルフローから自分のデータを訓練する方法に関するチュートリアルに従います。https://github.com/tensorflow/models/tree/master/inception#how-to-construct-a-new-dataset-for-retraining。 データを分割し(トレーニングと検証)、作成したラベルを提案し、bazel-binを使用してTFrecordを作成しました。すべてが機能し、今では自分のデータをTFrecordsとして持っています。Tensorflowを使用して、自分のデータを使用して画像分類器をトレーニングします。

今私は最初からinception-v3モデルを使用して画像分類器を訓練したいと思いますが、私はスクリプトinception_train.pyを使うべきですが、わかりません。そうですか? https://github.com/tensorflow/models/blob/master/inception/inception/inception_train.py

もしそうなら、私には2つの質問があります: 1)TFrecordsを使ってどのように訓練できますか?あなたが私に例を示すことができれば、素晴らしいことになるでしょう。 2)CPUで実行できますか、またはGPUでのみ可能ですか?

ありがとうございました。

答えて

0

あなた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上に作成されます。

この説明は役立ちます。

関連する問題