2017-01-13 3 views
1

こんにちは私はイメージ入力パイプを構築しようとしています。私の前処理トレーニングデータは、私はコードの次の行で作成しましたtfrecordsファイルに格納されている..tfrecordsバイナリファイル(タイプmissmatch)を読み書きする

def _bytes_feature(value): 
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) 

def _int64_feature(value): 
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) 

img_raw = img.tostring()          # typeof(img) = np.Array with shape (50, 80) dtype float64 
img_label_text_raw = str.encode(img_lable) 
example = tf.train.Example(features=tf.train.Features(feature={ 
    'height': _int64_feature(height),       #heigth (integer) 
    'width': _int64_feature(width),        #width (integer) 
    'depth': _int64_feature(depth),        #num of rgb channels (integer) 
    'image_data': _bytes_feature(img_raw),      #raw image data (byte string) 
    'label_text': _bytes_feature(img_label_text_raw),   #raw image_lable_text (byte string) 
    'lable': _int64_feature(lable_txt_to_int[img_lable])}))  #label index (integer) 

writer.write(example.SerializeToString()) 

を私はテンソルを再構築するために、バイナリデータを読み込もうそれから:

def read_and_decode(filename_queue): 
    reader = tf.TFRecordReader() 
    _, serialized_example = reader.read(filename_queue) 

    features = tf.parse_single_example(
     serialized_example, 
     # Defaults are not specified since both keys are required. 
     features={ 
      'label': tf.FixedLenFeature([], tf.int64), 
      'height': tf.FixedLenFeature([], tf.int64), 
      'width': tf.FixedLenFeature([], tf.int64), 
      'depth': tf.FixedLenFeature([], tf.int64), 
      'image_data': tf.FixedLenFeature([], tf.string) 
     }) 

    label = features['label'] 
    height = tf.cast(features['height'], tf.int64) 
    width = tf.cast(features['width'], tf.int64) 
    depth = tf.cast(features['depth'], tf.int64) 

    image_shape = tf.pack([height, width, depth]) 
    image = tf.decode_raw(features['image_data'], tf.float64) 
    image = tf.reshape(image, image_shape) 


    images, labels = tf.train.shuffle_batch([image, label],  batch_size=2, 
               capacity=30, 
               num_threads=1, 
               min_after_dequeue=10) 
    return images, labels 

悲しいことに、これは動作しません。このエラーメッセージが表示されます:

ValueError: Tensor conversion requested dtype string for Tensor with dtype int64: 'Tensor("ParseSingleExample/Squeeze_label:0", shape=(), dtype=int64)' ...

TypeError: Input 'bytes' of 'DecodeRaw' Op has type int64 that does not match expected type of string.

いくつか私にこれを修正する方法のヒントを教えてもらえますか?

ありがとうございます!

UPDATE

@mmryはどうもありがとうございました "read_and_decode" の完全なコードリスト。今度はバッチをシャッフルする際にコードが壊れます。 :

ValueError: All shapes must be fully defined: [TensorShape([Dimension(None), Dimension(None), Dimension(None)]), TensorShape([])]

どのような提案ですか?

+0

こんにちは、あなたのtfrecordsで 'label_text'をシリアル化しました。次に、このデータをデコーダでどのように解析しますか?アドバイスをお願いしますか?ありがとう。 record_iteratorでstring_recordため – mining

+0

record_iterator = tf.python_io.tf_record_iterator(パス= ) : 例えば= tf.train.Example() example.ParseFromString(string_record) label_txt =(example.features.feature [ 'LABEL_TEXT '] .bytes_list .value [0] .decode( "utf-8")) – monchi

+0

上記の例では、すべてのデータ要素を繰り返して、lable文字列をデコードします。 – monchi

答えて

3

この行にtf.decode_raw()オペアンプを使用する必要はありません。

label = tf.decode_raw(features['label'], tf.int64) 

代わりに、あなたが書くことができる必要があります:

label = features['label'] 

tf.decode_raw() opが唯一tf.stringテンソル、および変換を受け入れますいくつかのテンソルデータのバイナリ表現(可変長文字列として)を型付き表現(特定の型の要素のベクトル)に変換します。しかし、'label'フィーチャをtf.int64タイプと定義しているため、tf.int64として使用する場合はそのフィーチャを変換する必要はありません。

+1

ここで問題となるのは、それぞれの画像が異なるサイズ(「高さ」、「幅」、および「深さ」の機能に応じて)である可能性があるということです。 'tf.train.shuffle_batch()'を使うには、画像の大きさを変えて、すべて同じサイズにする必要があります。 ['tf.image.resize_image_with_crop_or_pad()'](https://www.tensorflow.org/api_docs/python/image/cropping#resize_image_with_crop_or_pad)のような画像処理演算子を使用します。もし入力のすべての画像が同じ大きさであれば、そのサイズを 'tf.reshape()'と 'tf.train 'に定数(例えば' [299,29,3] ')で渡すことができます。 shuffle_batch() 'が動作します。 – mrry

+0

そうでした!すべての画像が同じサイズを持つので、固定数が渡されます!私はもともと、訓練セットにサイズを含めて、私が必要な場合にはその画像を再構成しました – monchi

関連する問題