我想为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个新的随机类。在
事先非常感谢。在
^{} 支持广播,并允许将标量与
rank > 0
的张量进行比较。在要仅保留特定标签,请使用以下谓词:
allowed_labels
指定要保留的标签。所有不在此张量中的标签都将被过滤掉。在相关问题 更多 >
编程相关推荐