带标签的 Keras 图像相似性模型问题

Keras image similarity model trouble with labels

我正在研究深度图像相似度模型,我想获得一些帮助。

我一直收到此错误,但不知道该如何处理或如何修复。

Input arrays should have the same number of samples as target arrays. Found 100 input samples and 3 target samples.

我将图像分成三个文件,然后读入。然后我有三个数组(锚点、正和负)。我拥有的标签都是相同的 y =[1,1,0] 即 [a,p,n] 这是正确的方法吗?

我正在关注这个blog/codehttps://medium.com/@akarshzingade/image-similarity-using-deep-ranking-c1bd83855978

模型和损失函数是一样的,唯一不同的是我加载了什么数据,如何加载,以及我如何训练模型。

# Alist of images for anchor similar and negative
# Read in all the image paths
anchor = np.load("list/anchor_list.npy")
pos = np.load("list/positvie_list.npy")
neg = np.load("list/negative_list.npy")

def load_img(path):
    img = image.load_img(path, target_size=(224, 224))
    img = image.img_to_array(img)
    img = np.array(img)
    return img

a = []
p = []
n = []
# Read in sampple of the images
for i in range(0, 100):
    a.append(load_img(os.path.join(data_dir, anchor[i])))
    p.append(load_img(os.path.join(data_dir, pos[i])))
    n.append(load_img(os.path.join(data_dir, neg[i])))

a = np.array(a)
p = np.array(p)
n = np.array(n)
y = [1, 1, 0]

deep_rank_model.fit([a, p, n], y,
                batch_size=batch_size,
                epochs=10,
                verbose = 1)

如错误中所述,输入数组 [a,p,n] 的大小为 (100x3),但输出数组 y 的大小为 (1x3)。因此模型无法将输入数组与其对应的输出配对。

根据你的解释,我了解到a -> 1,p -> 1,n -> 0,每个类别有100个样本。所以你只需要将输出数组乘以 100。试试这个:

deep_rank_model.fit([a, p, n], [y]*100,
            batch_size=batch_size,
            epochs=10,
            verbose = 1)

希望这有效!