2017-08-14 7 views
0

イメージ分類のためにテンソルフローで入力パイプラインを作成しようとしているので、イメージと対応するラベルのバッチを作成したい。 Tensorflow文書は、我々は、入力のバッチを作るためにtf.train.batchを使用することができますことを示唆している:テンソルフロー入力パイプラインが複数の値を返す

train_batch, train_label_batch = tf.train.batch(
[train_image, train_image_label], 
batch_size=batch_size, 
num_threads=1, 
capacity=10*batch_size, 
enqueue_many=False, 
shapes=[[224,224,3], [len(labels),]], 
allow_smaller_final_batch=True 
) 

は、しかし、私は私はこのようなグラフに送り込む場合、それが問題になるだろうと考えています:

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=train_label_batch, logits=Model(train_batch))) 

コスト関数の操作でイメージとそれに対応するラベルがデキューされるか、それとも別々に返されるのでしょうか?そのため、間違った画像とラベルでトレーニングが行われます。

答えて

1

イメージとラベルの順序を保持するために考慮する必要があるいくつかのことがあります。

画像とラベルを与える関数が必要な場合を考えてみましょう。ここで

def _get_test_images(_train=False): 


""" 
Gets the test images and labels as a batch 

Inputs: 
====== 
_train  : Boolean if images are from training set 
random_crop  : Boolean if random cropping is allowed 
random_flip   : Boolean if random horizontal flip is allowed 
distortion  : Boolean if distortions are allowed 

Outputs: 
======== 
images_batch : Batch of images containing BATCH_SIZE images at a time 
label_batch  : Batch of labels corresponding to the images in images_batch 
idx   : Batch of indexes of images 
""" 

#get images and labels 
_,_img_names,_img_class,index= _get_list(_train = _train) 

#total number of distinct images used for train will be equal to the images 
#fed in tf.train.slice_input_producer as _img_names 

img_path,label,idx = tf.train.slice_input_producer([_img_names,_img_class,index],shuffle=False) 

img_path,label,idx = tf.convert_to_tensor(img_path),tf.convert_to_tensor(label),tf.convert_to_tensor(idx) 
img_path = tf.cast(img_path,dtype=tf.string) 

#read file 
image_file = tf.read_file(img_path) 

#decode jpeg/png/bmp 
#tf.image.decode_image won't give shape out. So it will give error while resizing 
image = tf.image.decode_jpeg(image_file) 

#image preprocessing 
image = tf.image.resize_images(image, [IMG_DIM,IMG_DIM]) 

float_image = tf.cast(image,dtype=tf.float32) 

#subtracting mean and divide by standard deviation 
float_image = tf.image.per_image_standardization(float_image) 

#set the shape 
float_image.set_shape(IMG_SIZE) 
labels_original = tf.cast(label,dtype=tf.int32) 
img_index = tf.cast(idx,dtype=tf.int32) 

#parameters for shuffle 
batch_size = BATCH_SIZE 
min_fraction_of_examples_in_queue = 0.3 
num_preprocess_threads = 1 
num_examples_per_epoch = MAX_TEST_EXAMPLE 
min_queue_examples = int(num_examples_per_epoch * 
         min_fraction_of_examples_in_queue) 

images_batch, label_batch,idx = tf.train.batch(
     [float_image,label,img_index], 
     batch_size=batch_size, 
     num_threads=num_preprocess_threads, 
     capacity=min_queue_examples + 3 * batch_size) 

# Display the training images in the visualizer. 
tf.summary.image('images', images_batch) 

return images_batch, label_batch,idx 

tf.train.slice_input_producer([_img_names,_img_class,index],shuffle=False)はあなたがshuffle=Trueを置けば、それは連携して、すべての3つの配列をシャッフルする場所を見て面白いものです。

第2の事は、num_preprocess_threadsです。デキュー操作に単一スレッドを使用している限り、バッチは確定的な方法で出力されます。しかし、複数のスレッドがランダムに配列をシャッフルします。たとえば画像0001.jpgの場合、Trueラベルが1の場合は2または4が得られます。デキューするとテンソル形式になります。 tf.nn.softmax_cross_entropy_with_logitsはそのようなテンソルには問題がありません。

関連する問題