筛选数据集以仅从特定类中获取图像

2024-04-18 10:50:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为n-shot学习准备omniglot数据集。 因此,我需要5个样本从10个类(字母表)

要复制的代码

import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np

builder = tfds.builder("omniglot")
# assert builder.info.splits['train'].num_examples == 60000
builder.download_and_prepare()
# Load data from disk as tf.data.Datasets
datasets = builder.as_dataset()
dataset, test_dataset = datasets['train'], datasets['test']


def resize(example):
    image = example['image']
    image = tf.image.resize(image, [28, 28])
    image = tf.image.rgb_to_grayscale(image, )
    image = image / 255
    one_hot_label = np.zeros((51, 10))
    return image, one_hot_label, example['alphabet']


def stack(image, label, alphabet):
    return (image, label), label[-1]

def filter_func(image, label, alphabet):
    # get just images from alphabet in array, not just 2
    arr = np.array(2,3,4,5)
    result = tf.reshape(tf.equal(alphabet, 2 ), [])
    return result

# correct size
dataset = dataset.map(resize)
# now filter the dataset for the batch
dataset = dataset.filter(filter_func)
# infinite stream of batches (classes*samples + 1)
dataset = dataset.repeat().shuffle(1024).batch(51)
# stack the images together
dataset = dataset.map(stack)
dataset = dataset.shuffle(buffer_size=1000)
dataset = dataset.batch(32)

for i, (image, label) in enumerate(tfds.as_numpy(dataset)):
    print(i, image[0].shape)

现在我想用filter函数过滤数据集中的图像。 tf.相等让我按一个类来过滤,我想要像数组中的张量这样的东西。在

你有没有看到用过滤器函数来做这个的方法? 还是这条路错了,还有更简单的路?在

我想创建一批51个图像和相应的标签,它们来自相同的N=10个类。从每个类中,我需要K=5个不同的图像和一个额外的(我需要分类)。每一批N*K+1(51)幅图像应来自10个新的随机类。在

事先非常感谢。在


Tags: 图像imageimporttfdefasnpbuilder
1条回答
网友
1楼 · 发布于 2024-04-18 10:50:50

^{}支持广播,并允许将标量与rank > 0的张量进行比较。在

要仅保留特定标签,请使用以下谓词:

dataset = datasets['train']

def predicate(x, allowed_labels=tf.constant([0., 1., 2.])):
    label = x['label']
    isallowed = tf.equal(allowed_labels, tf.cast(label, tf.float32))
    reduced = tf.reduce_sum(tf.cast(isallowed, tf.float32))
    return tf.greater(reduced, tf.constant(0.))

dataset = dataset.filter(predicate).batch(20)

for i, x in enumerate(tfds.as_numpy(dataset)):
    print(x['label'])
# [1 0 0 1 2 1 1 2 1 0 0 1 2 0 1 0 2 2 0 1]
# [1 0 2 2 0 2 1 2 1 2 2 2 0 2 0 2 1 2 1 1]
# [2 1 2 1 0 1 1 0 1 2 2 0 2 0 1 0 0 0 0 0]

allowed_labels指定要保留的标签。所有不在此张量中的标签都将被过滤掉。在

相关问题 更多 >