何百万もの 32x32 画像? CIFAR とまったく同じように聞こえます。TensorFlow Modelsを確認してください。CIFAR10をダウンロードして TFRecords に変換するスクリプトが含まれています: download_and_convert_data.py。データが CIFAR でない場合でも、とにかくコードをチェックしてください。おそらく役に立ちます。
CIFAR10 をロードするコードは次のようになります。
with tf.Graph().as_default():
image_placeholder = tf.placeholder(dtype=tf.uint8)
encoded_image = tf.image.encode_png(image_placeholder)
with tf.Session('') as sess:
for j in range(num_images):
[...] # load image and label from disk
image = [...]
label = [...]
png_string = sess.run(encoded_image,
feed_dict={image_placeholder: image})
example = dataset_utils.image_to_tfexample(
png_string, 'png', _IMAGE_SIZE, _IMAGE_SIZE, label)
tfrecord_writer.write(example.SerializeToString())
[...]
関数は次のimage_to_tfexample()
ようになります。
def image_to_tfexample(image_data, image_format, height, width, class_id):
return tf.train.Example(features=tf.train.Features(feature={
'image/encoded': bytes_feature(image_data),
'image/format': bytes_feature(image_format),
'image/class/label': int64_feature(class_id),
'image/height': int64_feature(height),
'image/width': int64_feature(width),
}))
関数は次のようになりますint_64_feature()
(bytes_feature()
関数は似ています)。
def int64_feature(values):
if not isinstance(values, (tuple, list)):
values = [values]
return tf.train.Feature(int64_list=tf.train.Int64List(value=values))
編集
さらにいくつかの詳細:
は次のTFRecordWriter
ように作成されます (これにより、ファイルも作成されます)。
with tf.python_io.TFRecordWriter(training_filename) as tfrecord_writer:
[...] # use the tfrecord_writer
のドキュメントにtf.image.encode_png()
は、画像には shape が必要であると[height, width, channels]
記載されています。channels = 1
グレースケールの場合channels = 2
はグレースケール + アルファ、RGB カラーの場合は 3、channels = 4
RGB カラー + アルファ (RGBA) の場合は 3 です。