Tensorflow读取带有标签的图像

Tensorflow读取带有标签的图像,第1张

Tensorflow读取带有标签的图像

使用

slice_input_producer
提供了一种更清洁的解决方案。切片输入生产者允许我们创建一个包含任意多个可分离值的输入队列。这个问题的片段如下所示:

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, labelsdef 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 depred image, and the string label.    """    label = input_queue[1]    file_contents = tf.read_file(input_queue[0])    example = tf.image.depre_png(file_contents, channels=3)    return example, label# Reads pfathes of images together with their labelsimage_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 queueinput_queue = tf.train.slice_input_producer([images, labels],num_epochs=num_epochs,shuffle=True)image, label = read_images_from_disk(input_queue)# Optional Preprocessing or Data Augmentation# tf.image implements most of the standard image augmentationimage = preprocess_image(image)label = preprocess_label(label)# Optional Image and Label Batchingimage_batch, label_batch = tf.train.batch([image, label],         batch_size=batch_size)

又见generic_input_producer从TensorVision全输入管道的例子。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5011062.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存