【问题标题】:Keras image similarity model trouble with labels带有标签的 Keras 图像相似性模型问题
【发布时间】:2019-06-28 02:43:03
【问题描述】:

我正在研究一个深度图像相似性模型,我希望得到一些帮助。

我不断收到此错误,不知道该如何处理或修复它。

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] 这是正确的方法吗?

我正在关注这个博客/代码https://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)

【问题讨论】:

    标签: python tensorflow keras training-data


    【解决方案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)
    

    希望这行得通!

    【讨论】:

    • 谢谢你 我没有意识到它不会只为每一个标签贴上标签。我很感激
    猜你喜欢
    • 1970-01-01
    • 2019-05-29
    • 2013-01-24
    • 2011-09-19
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    相关资源
    最近更新 更多