2016-10-18 21 views
3

TensorFlowで複数ラベルの分類を行いたいと思います。 私は約95000の画像を持っており、それぞれの画像に対応するラベルベクトルがあります。すべての画像に7つのラベルがあります。これらの7つのラベルは、サイズ7のテンソルとして表されます。各画像の形状は(299,299,3)です。私は今、ファイル.tfrecordsに対応するラベルベクトル/テンソルで画像を書き込むことができますどのように分類用の画像とマルチラベル付きのtfrecordsの作成

私の現在のコード/アプローチ:

def get_decode_and_resize_image(image_id): 
    image_queue = tf.train.string_input_producer(['../../original-data/'+image_id+".jpg"]) 
    image_reader = tf.WholeFileReader() 
    image_key, image_value = image_reader.read(image_queue) 
    image = tf.image.decode_jpeg(image_value,channels=3) 
    resized_image= tf.image.resize_images(image, 299, 299, align_corners=False) 
    return resized_image 



init_op = tf.initialize_all_variables() 
with tf.Session() as sess: 
# Start populating the filename queue. 

sess.run(init_op) 
coord = tf.train.Coordinator() 
threads = tf.train.start_queue_runners(coord=coord) 

# get all labels and image ids 
csv= pd.read_csv('../../filteredLabelsToPhotos.csv') 

#create a writer for writing to the .tfrecords file 
writer = tf.python_io.TFRecordWriter("tfrecords/data.tfrecords") 

for index,row in csv.iterrows(): 

    # the labels 
    image_id = row['photo_id'] 
    lunch = tf.to_float(row["lunch"]) 
    dinner= tf.to_float(row["dinner"]) 
    reservations= tf.to_float(row["TK"]) 
    outdoor = tf.to_float(row["OS"]) 
    waiter = tf.to_float(row["WS"]) 
    classy = tf.to_float(row["c"]) 
    gfk = tf.to_float(row["GFK"]) 

    labels_list = [lunch,dinner,reservations,outdoor,waiter,classy,gfk] 
    labels_tensor = tf.convert_to_tensor(labels_list) 

    #get the corresponding image 
    image_file= get_decode_and_resize_image(image_id=image_id) 

    #here : how do I now create a TFExample and write it to the .tfrecords file 






coord.request_stop() 
coord.join(threads) 

そしてI've後

は.tfrecordsファイルを作成TensorFlowトレーニングコードから読み込んでデータを自動的にバッチすることはできますか?

答えて

0

tf.train.Exampleを作成するには、単にexample = tf.train.Example()を実行します。その後、通常のprotocol buffers python APIを使用して操作できます。

0

アレクサンドルの答えに展開するには、あなたがこのような何か行うことができます。

# Set this up before your for-loop, you'll use this repeatedly 
tfrecords_filename = 'myfile.tfrecords' 
writer = tf.python_io.TFRecordWriter(tfrecords_filename) 

# Then within your for-loop, you can write like so: 
for ...: 

    #here : how do I now create a TFExample and write it to the .tfrecords file 

    example = tf.train.Example(features=tf.train.Features(feature={ 
    'image_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_file])), 
    # the other features, labels you wish to include go here too 
    })) 
    writer.write(example.SerializeToString()) 

# then finally, don't forget to close the writer. 
writer.close() 

をこれは、あなたがすでにimage_file変数にバイト配列に画像を変換している前提としています。

this very helpful postからこれを適用しました。シリアル化イメージ&の詳細については、私の前提がfalseの場合に役立ちます。

関連する問題