我有训练数据,这是jpeg图像的目录和包含文件名和相关类别标签的相应文本文件.我正在尝试将此训练数据转换为tfrecords文件,如tensorflow文档中所述.我花了很多时间试图让它工作但是tensorflow中没有示例演示如何使用任何读者读取jpeg文件并使用tfrecordwriter将它们添加到tfrecord
Ham*_* MP 40
我希望这有帮助:
filename_queue = tf.train.string_input_producer(['/Users/HANEL/Desktop/tf.png']) # list of files to read
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
my_img = tf.image.decode_png(value) # use decode_png or decode_jpeg decoder based on your files.
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1): #length of your filename list
image = my_img.eval() #here is your image Tensor :)
print(image.shape)
Image.show(Image.fromarray(np.asarray(image)))
coord.request_stop()
coord.join(threads)
Run Code Online (Sandbox Code Playgroud)
要将所有图像作为张量数组获取,请使用以下代码示例.
更新:
在上一个答案中,我刚刚讲述了如何以TF格式读取图像,但不将其保存在TFRecords中.为此你应该使用:
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
# images and labels array as input
def convert_to(images, labels, name):
num_examples = labels.shape[0]
if images.shape[0] != num_examples:
raise ValueError("Images size %d does not match label size %d." %
(images.shape[0], num_examples))
rows = images.shape[1]
cols = images.shape[2]
depth = images.shape[3]
filename = os.path.join(FLAGS.directory, name + '.tfrecords')
print('Writing', filename)
writer = tf.python_io.TFRecordWriter(filename)
for index in range(num_examples):
image_raw = images[index].tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'height': _int64_feature(rows),
'width': _int64_feature(cols),
'depth': _int64_feature(depth),
'label': _int64_feature(int(labels[index])),
'image_raw': _bytes_feature(image_raw)}))
writer.write(example.SerializeToString())
Run Code Online (Sandbox Code Playgroud)
更多信息在这里
你读了这样的数据:
# Remember to generate a file name queue of you 'train.TFRecord' file path
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
dense_keys=['image_raw', 'label'],
# Defaults are not specified since both keys are required.
dense_types=[tf.string, tf.int64])
# Convert from a scalar string tensor (whose single string has
image = tf.decode_raw(features['image_raw'], tf.uint8)
image = tf.reshape(image, [my_cifar.n_input])
image.set_shape([my_cifar.n_input])
# OPTIONAL: Could reshape into a 28x28 image and apply distortions
# here. Since we are not applying any distortions in this
# example, and the next step expects the image to be flattened
# into a vector, we don't bother.
# Convert from [0, 255] -> [-0.5, 0.5] floats.
image = tf.cast(image, tf.float32)
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
# Convert label from a scalar uint8 tensor to an int32 scalar.
label = tf.cast(features['label'], tf.int32)
return image, label
Run Code Online (Sandbox Code Playgroud)
我也有同样的问题。
所以这是我如何获取自己的jpeg文件的tfrecords文件的方法
编辑:添加溶胶1-更好,更快的方法
来自tensorflow官方github:如何构建用于重新训练的新数据集,直接使用官方python脚本build_image_data.py和bazel是一个更好的主意。
这是说明:
要运行
build_image_data.py,您可以运行以下命令行:Run Code Online (Sandbox Code Playgroud)# location to where to save the TFRecord data. OUTPUT_DIRECTORY=$HOME/my-custom-data/ # build the preprocessing script. bazel build inception/build_image_data # convert the data. bazel-bin/inception/build_image_data \ --train_directory="${TRAIN_DIR}" \ --validation_directory="${VALIDATION_DIR}" \ --output_directory="${OUTPUT_DIRECTORY}" \ --labels_file="${LABELS_FILE}" \ --train_shards=128 \ --validation_shards=24 \ --num_threads=8分片
$OUTPUT_DIRECTORY的位置 在哪里TFRecords。该$LABELS_FILE会是由提供所有标签的列表中的脚本读取的文本文件。
然后,它应该可以解决问题。
ps。由Google制造的bazel将代码转换为makefile。
首先,我参考@capitalistpug的指令并检查shell脚本文件
(由Google提供的shell脚本文件:download_and_preprocess_flowers.sh)
其次,我还找到了NVIDIA的迷你Inception-v3培训教程
(NVIDIA官方采用GPU加速的TENSORFLOW进行了快速培训)
请注意,在Bazel WORKSAPCE环境中需要执行以下步骤
因此Bazel构建文件可以成功运行
第一步,我注释掉了已经下载的imagenet数据集的下载部分
其余部分我不需要download_and_preprocess_flowers.sh
第二步,将目录更改为tensorflow / models / inception
它是Bazel环境,由Bazel在
$ cd tensorflow/models/inception
Run Code Online (Sandbox Code Playgroud)
可选:如果以前未构建过,请在cmd中键入以下代码
$ bazel build inception/download_and_preprocess_flowers
Run Code Online (Sandbox Code Playgroud)
您需要在下图中找出内容
最后一步,输入以下代码:
$ bazel-bin/inception/download_and_preprocess_flowers $Your/own/image/data/path
Run Code Online (Sandbox Code Playgroud)
然后,它将开始调用build_image_data.py并创建tfrecords文件
请注意,图像将作为未压缩的张量保存在 TFRecord 中,可能会将大小增加约 5 倍。这会浪费存储空间,并且由于需要读取的数据量可能会相当慢。
最好将文件名保存在 TFRecord 中,然后按需读取文件。新的DatasetAPI 运行良好,文档中有这个例子:
# Reads an image from a file, decodes it into a dense tensor, and resizes it
# to a fixed shape.
def _parse_function(filename, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_string)
image_resized = tf.image.resize_images(image_decoded, [28, 28])
return image_resized, label
# A vector of filenames.
filenames = tf.constant(["/var/data/image1.jpg", "/var/data/image2.jpg", ...])
# `labels[i]` is the label for the image in `filenames[i].
labels = tf.constant([0, 37, ...])
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
dataset = dataset.map(_parse_function)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50876 次 |
| 最近记录: |