0

这个问题类似于这个 SO question。我有以下代码,它就像一个魅力,用于为连体网络配对:

def make_pairs(images, labels):
    # initialize two empty lists to hold the (image, image) pairs and
    # labels to indicate if a pair is positive or negative
    pairImages = []
    pairLabels = []
    # calculate the total number of classes present in the dataset
    # and then build a list of indexes for each class label that
    # provides the indexes for all examples with a given label
    numClasses = len(np.unique(labels))
    idx = [np.where(labels == i)[0] for i in range(0, numClasses)]
    # loop over all images
    for idxA in range(len(images)):
        # grab the current image and label belonging to the current
        # iteration
        currentImage = images[idxA]
        label = labels[idxA]
        # randomly pick an image that belongs to the *same* class
        # label
        idxB = np.random.choice(idx[label])
        posImage = images[idxB]
        # prepare a positive pair and update the images and labels
        # lists, respectively
        pairImages.append([currentImage, posImage])
        pairLabels.append([1])
        # grab the indices for each of the class labels *not* equal to
        # the current label and randomly pick an image corresponding
        # to a label *not* equal to the current label
        negIdx = np.where(labels != label)[0]
        negImage = images[np.random.choice(negIdx)]
        # prepare a negative pair of images and update our lists
        pairImages.append([currentImage, negImage])
        pairLabels.append([0])
    # return a 2-tuple of our image pairs and labels
    return (np.array(pairImages), np.array(pairLabels))

好的,此代码通过为 MNIST 数据集中的每个图像选择对来工作。它通过随机选择同一类(标签)的另一张图像和不同类(标签)的另一个补丁来制作另一对来为该图像构建一对。通过运行代码,返回的两个矩阵的最终形状为:

# load MNIST dataset and scale the pixel values to the range of [0, 1]
print("[INFO] loading MNIST dataset...")
(trainX, trainY), (testX, testY) = mnist.load_data()

# build the positive and negative image pairs
print("[INFO] preparing positive and negative pairs...")
(pairTrain, labelTrain) = make_pairs(trainX, trainY)
(pairTest, labelTest) = make_pairs(testX, testY)

>> print(pairTrain.shape)
(120000, 2, 28, 28)
>> print(labelTrain.shape)
(120000, 1)

我想make_pair通过匹配同一类标签正对的图像来扩展上述功能。

而对于负对,我想将每个图像(例如 Img_A)与来自其他类的所有图像进行匹配。

以下是修改后功能的预期示例

labels = array([3,2,1,4], dtype=int32)

Images = array([A,B,C,D,E,A,C,B,C,A,B,C] dtype=int32)

上面的函数输出正对 =[[A,A],[B,B],[C,C],[D,D],[E,E]标签为 1. 和负对[[A,B],[B,A],[C,D],[D,A],[E,D]标签为 0 。

我想像这样输出所有可能的负对[[A,B],[A,C],[A,D],[A,E],[B,A],[B,C],[B,D],[B,E],[C,A],[C,B],[C,D],[C,E],[D,A],[D,B],[D,C],[D,E],[E,A],[E,B],[E,C],[E,D]

我的数据集

我想对另一个数据集做一些不同的事情。

>>> images2.shape
(420, 28, 28, 3)

我有另一个数组,我们称之为labels2,它为所有420个图像有41个标签,每个标签有10个图像,如下所示:

>>> labels2.shape
(420,)

>>> len(np.unique(labels2))
42

>>> (labels2==0).sum()
10
>>> (labels2==1).sum()
10

make_pairs函数的预期输出是

>>> pairs_train = make_pairs(train, label)
>>>  x_train_1 = pairs_train[:, 0]
>>>  x_train_2 = pairs_train[:, 1]

>>> positive_pairs = x_train_1.shape
>>>  positive_pairs 
>>> 420
>>> negative_pairs = x_train_2.shape 
>>>  negative_pairs  #420*41
>>> 17220

我将函数修改为以下函数,但是当我尝试使用生成的数据训练模型时系统崩溃。

def make_pairs(images, labels):
    # initialize two empty lists to hold the (image, image) pairs and
    # labels to indicate if a pair is positive or negative
    pairImages = []
    pairLabels = []
    neg_list = []
    # calculate the total number of classes present in the dataset
    # and then build a list of indexes for each class label that
    # provides the indexes for all examples with a given label
    numClasses = len(np.unique(labels))
    idx = [np.where(labels == i)[0] for i in range(0, numClasses)]
    # loop over all images
    for idxA in range(len(images)):
        # grab the current image and label belonging to the current
        # iteration
        currentImage = images[idxA]
        label = labels[idxA]
        # randomly pick an image that belongs to the *same* class
        # label
        idxB = np.random.choice(idx[label])
        posImage = images[idxB]
        # prepare a positive pair and update the images and labels
        # lists, respectively
        pairImages.append([currentImage, posImage])
        pairLabels.append([1])
        # grab the indices for each of the class labels *not* equal to
        # the current label and randomly pick an image corresponding
        # to a label *not* equal to the current label
        negIdx = np.where(np.unique(labels, axis=0) != label)[0]
        for i in range(len(negIdx)):
            negImage = images[negIdx[i]]
            pairImages.append([currentImage, negImage]) 
            pairLabels.append([0])
    return (np.array(pairImages), np.array(pairLabels))
4

0 回答 0