2017-11-27 1 views
1

私はtfrecordsファイルを生成するために次のコードを使用しています。上記のコードで異なるイメージサイズを使用するtf.decode_rawとtf.reshape

def generate_tfrecords(data_path, labels, name): 
     """Converts a dataset to tfrecords.""" 
     filename = os.path.join(args.tfrecords_path, name + '.tfrecords') 
     writer = tf.python_io.TFRecordWriter(filename) 
for index, data in enumerate(data_path): 
     with tf.gfile.GFile(data, 'rb') as fid: 
     encoded_jpg = fid.read() 
     print(len(encoded_jpg)) # 17904 
     encoded_jpg_io = io.BytesIO(encoded_jpg) 
     image = pil.open(encoded_jpg_io) 
     image = np.asarray(image) 
     print(image.shape) # 112*112*3 
     example = tf.train.Example(features=tf.train.Features(feature={ 
     'height': _int64_feature(int(image.shape[0])), 
     'width': _int64_feature(int(image.shape[1])), 
     'depth': _int64_feature(int(3)), 
     'label': _int64_feature(int(labels[index])), 
     'image_raw': _bytes_feature(encoded_jpg)})) 
     writer.write(example.SerializeToString()) 
    writer.close() 

encoded_jpg17904を有し、画像が一致しない形状112*112*3を有しています。

I'amは、次のコードを使用tfrecordsを解析する場合:私は上記のコードを使用している場合

def _parse_function(example_proto): 
    features = {'height': tf.FixedLenFeature((), tf.int64, default_value=0), 
      'width': tf.FixedLenFeature((), tf.int64, default_value=0), 
      'depth': tf.FixedLenFeature((), tf.int64, default_value=0), 
      'label': tf.FixedLenFeature((), tf.int64, default_value=0), 
      'image_raw': tf.FixedLenFeature((), tf.string, default_value="")} 
    parsed_features = tf.parse_single_example(example_proto, features) 
    height = tf.cast(parsed_features["height"], tf.int32) # 112 
    width = tf.cast(parsed_features["width"], tf.int32) # 112 
    depth = tf.cast(parsed_features["depth"], tf.int32) #3 
    label = parsed_features['label'] 
    img = tf.decode_raw(parsed_features['image_raw'], tf.uint8, little_endian=True) 
    img = tf.reshape(img, [height, width, depth]) 
    return img, label 

を、私は次のエラーを得た:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 17904 values, but the requested shape has 37632 
[[Node: Reshape = Reshape[T=DT_UINT8, Tshape=DT_INT32](DecodeRaw, Reshape/shape)]] 
[[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,?,?,?], [?]], output_types=[DT_UINT8, DT_INT64], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator)]] 

どのように私はこの問題を解決することができます。画像タイプはpng37632=112*112*3です。ありがとう!

+0

画像の種類はPNGですか。コードには 'jpg'があり、' encoded_jpg'は生のイメージです。エラーメッセージは、生のイメージの1つが 'height'と' depth'フィールドにエンコードされた値と異なるサイズを持っていることを示唆しています...あなたのデータセットに異なるサイズのイメージがある可能性はありますか? – mrry

答えて

2

decode_rawの代わりにdecode_jpegを使用してください。

+0

あなたは1日(数日)を保存しました! –

関連する問題