在tensorflow中有效阅读自己的图像
我浏览了所有的tensorflow教程,其中所有数据集都是在RAM中加载的,因为它们很小。但是,我自己的数据(约30GB的图像)无法加载到内存中,因此我正在寻找读取图像以便进一步处理的有效方法。谁能给我举个例子说明我该怎么做 另外,我有两个文件在tensorflow中有效阅读自己的图像,tensorflow,deep-learning,Tensorflow,Deep Learning,我浏览了所有的tensorflow教程,其中所有数据集都是在RAM中加载的,因为它们很小。但是,我自己的数据(约30GB的图像)无法加载到内存中,因此我正在寻找读取图像以便进一步处理的有效方法。谁能给我举个例子说明我该怎么做 另外,我有两个文件train\u images和validation\u images,其中包含: 关于udacity的教程中介绍了随机方法,您可以在一次更改中使用相同的方法,而不是将所有图像保存在单个pickle文件中,而是将它们保存在正在使用的批大小的块中。通过这种方式
train\u images
和validation\u images
,其中包含:
关于udacity的教程中介绍了随机方法,您可以在一次更改中使用相同的方法,而不是将所有图像保存在单个pickle文件中,而是将它们保存在正在使用的批大小的块中。通过这种方式,一次只能加载一批中使用的数据。有关udacity的教程中介绍了随机方法,您可以在一次更改中使用相同的方法,而不是将所有图像保存在单个pickle文件中,而是将它们保存在正在使用的批大小的块中。这样,一次只能加载一批中使用的数据。推荐的方法是将其放入分片protobuf文件中,其中编码的jpeg和标签是tf的功能。示例
。在repository中,展示了如何从目录结构创建这样一个图像/标签对数据库,您需要根据您的情况对其进行一些调整(这很简单)。然后,对于训练时间,您可以查看它显示如何从tf.Example
proto到图像/标签张量的位置(从示例记录中提取解码的jpg和标签,解码jpg,调整大小,根据需要应用增强,然后排队)。建议的方法是将其放入分片protobuf文件中,其中编码的jpeg和标签是tf的功能。示例。在repository中,展示了如何从目录结构创建这样一个图像/标签对数据库,您需要根据您的情况对其进行一些调整(这很简单)。然后,对于训练时间,您可以查看它显示如何从tf.Example
proto转换为图像/标签张量的位置(从示例记录中提取解码的jpg和标签,解码jpg,调整大小,根据需要应用增强,然后排队)。这就是您要寻找的:
确切的代码片段如下所示:
def read_labeled_image_list(image_list_file):
"""Reads a .txt file containing pathes and labeles
Args:
image_list_file: a .txt file with one /path/to/image per line
label: optionally, if set label will be pasted after each line
Returns:
List with all filenames in file image_list_file
"""
f = open(image_list_file, 'r')
filenames = []
labels = []
for line in f:
filename, label = line[:-1].split(' ')
filenames.append(filename)
labels.append(int(label))
return filenames, labels
def read_images_from_disk(input_queue):
"""Consumes a single filename and label as a ' '-delimited string.
Args:
filename_and_label_tensor: A scalar string tensor.
Returns:
Two tensors: the decoded image, and the string label.
"""
label = input_queue[1]
file_contents = tf.read_file(input_queue[0])
example = tf.image.decode_png(file_contents, channels=3)
return example, label
# Reads pfathes of images together with their labels
image_list, label_list = read_labeled_image_list(filename)
images = ops.convert_to_tensor(image_list, dtype=dtypes.string)
labels = ops.convert_to_tensor(label_list, dtype=dtypes.int32)
# Makes an input queue
input_queue = tf.train.slice_input_producer([images, labels],
num_epochs=num_epochs,
shuffle=True)
image, label = read_images_from_disk(input_queue, num_labels=num_labels)
# Optional Preprocessing or Data Augmentation
# tf.image implements most of the standard image augmentation
image = preprocess_image(image)
label = preprocess_label(label)
# Optional Image and Label Batching
image_batch, label_batch = tf.train.batch([image, label],
batch_size=batch_size)
这就是你要找的: 确切的代码片段如下所示:
def read_labeled_image_list(image_list_file):
"""Reads a .txt file containing pathes and labeles
Args:
image_list_file: a .txt file with one /path/to/image per line
label: optionally, if set label will be pasted after each line
Returns:
List with all filenames in file image_list_file
"""
f = open(image_list_file, 'r')
filenames = []
labels = []
for line in f:
filename, label = line[:-1].split(' ')
filenames.append(filename)
labels.append(int(label))
return filenames, labels
def read_images_from_disk(input_queue):
"""Consumes a single filename and label as a ' '-delimited string.
Args:
filename_and_label_tensor: A scalar string tensor.
Returns:
Two tensors: the decoded image, and the string label.
"""
label = input_queue[1]
file_contents = tf.read_file(input_queue[0])
example = tf.image.decode_png(file_contents, channels=3)
return example, label
# Reads pfathes of images together with their labels
image_list, label_list = read_labeled_image_list(filename)
images = ops.convert_to_tensor(image_list, dtype=dtypes.string)
labels = ops.convert_to_tensor(label_list, dtype=dtypes.int32)
# Makes an input queue
input_queue = tf.train.slice_input_producer([images, labels],
num_epochs=num_epochs,
shuffle=True)
image, label = read_images_from_disk(input_queue, num_labels=num_labels)
# Optional Preprocessing or Data Augmentation
# tf.image implements most of the standard image augmentation
image = preprocess_image(image)
label = preprocess_label(label)
# Optional Image and Label Batching
image_batch, label_batch = tf.train.batch([image, label],
batch_size=batch_size)
从此处使用方法1或2:从此处使用方法1或2: