如何在Tensorflow中为Siamese网络构建输入管道?

时间:2017-07-18 04:53:09

标签: tensorflow

目前,我正在尝试使用Tensorflow实现本文中的实验:用于单次图像识别的连体神经网络

图像集是Omniglot,其中每个图像都可以作为[105,105,1]数组加载。

由于Siamese网络的输入是一对具有相同或不同类别的图像,我需要按如下方式对数据集进行预处理。

我将Omniglot数据集转换为[n,20,105,105,1] numpy数组,其中n表示类的数量,其中每个类有20个大小为[105,105,1]的图像示例。

然后我实现一个函数来返回一对图像:

def get_example(dataset):
"""
get one pair of images 
:param dataset: the set, eg. training set
:return: when label is 1, return a concatenated array of two imgs from same character
         when label is 0, return a concatenated array of two imgs from different characters
"""
# randint(0, x) generates 1 random numbers from 0 ~ x
set_upper = len(dataset)
set_lower = 0

# sample(range(0, 20), 2) generates 2 random numbers from 0 ~ 19
char_upper = 20
char_lower = 0

label = randint(0, 1)

if label:
    # randomly select one character from the set
    char = randint(set_lower, set_upper-1)
    rand_char = dataset[char]

    # randomly select two different images from the character
    a = b = 0
    while a == b:
        a, b = sample(range(char_lower, char_upper), 2)
    img_a = rand_char[a]
    img_b = rand_char[b]

else:
    # randomly select two characters from the set
    c1, c2 = sample(range(set_lower, set_upper), 2)
    rand_char1 = dataset[c1]
    rand_char2 = dataset[c2]

    # randomly select two images from two characters
    a, b = sample(range(char_lower, char_upper), 2)
    img_a = rand_char1[a]
    img_b = rand_char2[b]

img_input = np.concatenate((img_a, img_b), axis=0)
img_input = img_input[..., newaxis]
return img_input, label

所以这是我的问题,如何将图像分组,以及如何在Tensorflow中将它们输入模型?

1 个答案:

答案 0 :(得分:-1)

您应该能够按照https://www.tensorflow.org/guide/datasets#consuming_numpy_arrays中所述创建数据集,并使用诸如tf.data.Datasetshuffle之类的标准batch操作来实现目标。