[英]why is my Keras Siamese network gives issue on sample size
所以基本上我從這里復制了整個keras示例的Siamese網絡https://keras.io/examples/mnist_siamese/
但是我改變了一些主要是創建對功能的東西。
def create_pairs(datapath, directories):
pairs = []
labels = []
for file in os.listdir(datapath):
file_path = datapath + file
for pic in os.listdir(file_path):
# Get positive pair
file_choice = None
while file_choice is None:
file_choice = get_random_pic(file_path, pic)
#print(file_choice)
p1_path = file_path +"/"+ pic
p2_path = file_path +"/"+ file_choice
#print("File: {} Pic1: {} Rand:{}".format(file,p1_path,p2_path))
# Get negative pair from a random directory
rand_dir = None
while rand_dir is None:
rand_dir = get_random_dir(directories, file_path)
#print("CP:{} RD:{}".format(file_path, rand_dir))
rand_pic = random.choice(os.listdir(rand_dir))
# Negative example
p3_path = rand_dir +"/"+ rand_pic
#print("P1", p1_path)
#print("P2",p2_path)
#print("P3",p3_path)
# Read in all the file using cv2
a = cv2.imread(p1_path)
p = cv2.imread(p2_path)
n = cv2.imread(p3_path)
pos_pair = [a, p]
neg_pair = [a,n]
# Now create paris
#pairs += [[x[z1], x[z2]]]
pairs.append(pos_pair)
labels.append(1)
pairs.append(neg_pair)
labels.append(0)
return np.array(pairs), np.array(labels)
這是我從文件夾中讀取圖像的方法,該文件夾按人或類別分割。
我創建了像這樣的數據集X, labels = create_pairs(datapath, directories)
這給了我一個這樣的火車和測試裝置
Train sample : data:1000 label:1000
Test sample : data:920 label:920
(1000, 2, 160, 60, 3)
(920, 2, 160, 60, 3)
我對基礎網絡的輸入形狀是(2,160,60,3)這是整體輸入形狀
print("Train Shape: {} label:{}".format(x_train.shape, y_train.shape))
print("Test Shape: {} label:{}".format(x_test.shape, y_test.shape))
print(input_a.shape)
print(input_shape)
Train Shape: (1000, 2, 160, 60, 3) label:(1000,)
Test Shape: (920, 2, 160, 60, 3) label:(920,)
(?, 2, 160, 60, 3)
(2, 160, 60, 3)
這是我的模型擬合方法
model.fit([x_train[:0],x_train[:1]], y_train,
batch_size=128,
epochs=epochs,
validation_data=([x_test[:1], x_test[:1]], y_test))
從示例中它應該工作正常,因為它看起來與示例 - msint數據相同。
但它給了我這個錯誤:
所有輸入數組(
x
)應具有相同數量的樣本。 得到陣列形狀:[(0, 2, 160, 60, 3), (1, 2, 160, 60, 3)]
**********************新職位在此后加入*********************** ********所以我得到了它的工作,但我不知道它是否正在學習正確的東西。
history = model.fit([x_train, x_train], y_train,
batch_size=128,
epochs=epochs,
verbose = 1,
validation_data=([x_test, x_test], y_test))
這就是我這樣做的方式,但它看起來不像是如何設置的。 這是正確的做法嗎?
我找到了解決問題的方法。 所以新模型擬合類似於constt所說的:
history = model.fit([a_train, c_train], y_train,
batch_size=128,
epochs=epochs,
verbose = 1,
validation_data=([a_test, c_test], y_test))
我做出的重大改變是創造了一對。 我基本上把兩個數組做成了Anchor數組和比較數組。 這是return語句的最終代碼。
anchor.append(a)
anchor.append(a)
comp.append(p)
comp.append(n)
labels.append(1)
labels.append(0)
#pos_pair = [a, p]
#neg_pair = [a,n]
# Now create paris
#pairs += [[x[z1], x[z2]]]
#pairs += [[a,p]]
#pairs += [[a,n]]
#labels += [1,0]
#pairs.append(pos_pair)
#labels.append(1)
#pairs.append(neg_pair)
#labels.append(0)
return np.array(anchor), np.array(comp), np.array(labels)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.