I am working on a deep image similarity model and I would like to get some help on it.
I keep getting this error and don't know what to exactly do with it or how to fix it.
Input arrays should have the same number of samples as target arrays. Found 100 input samples and 3 target samples.
I have the images broken up into three files which I then read in. I then have three arrays (anchor, positive, and negative). The label I have will all be the same y =[1,1,0] i.e. [a,p,n] Is this the right approach for this?
I am following this blog/code https://medium.com/@akarshzingade/image-similarity-using-deep-ranking-c1bd83855978
The model and loss function are the same, the only difference I change is what data I load and how to load it and how I train the model.
# 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)
As stated in the error, the input array [a,p,n] is of size(100x3) but your output array y is of size (1x3). So the model is not able to pair the input array to its corresponding output.
From your explanation, I understand that a -> 1, p -> 1, and n -> 0, and you have 100 samples in each category. So you just need to multiply the output array by 100. Try this:
deep_rank_model.fit([a, p, n], [y]*100,
batch_size=batch_size,
epochs=10,
verbose = 1)
Hope this works!