AI智能
改变未来

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.al

报错的代码:

if np.min(dis_list) <= threshold:#预测图片与底库id的特征足够接近min_val = np.min(dis_list)print(min_val)#pred_id = np.argmin(dis_list)pred_id = dis_list.index(min_val)print(\'pred_id\', pred_id)

找了很久,dis_list类型为列表,np.min(dis_list) 是查找列表的最小值,dis_list.index(min_val) 是返回列表最小值对应的索引,实在不知道哪里有问题,debug后发现,dis_list 的维度并不是我以为的[1,1000],里面的元素也是很多列表,所以用min(dis_list)会报错。后来维度更正后错误得到解决。
原来算距离:

for i in range(len(embedding_left)):diff = np.subtract(embedding_left[i], embedding_right[i])dis = np.sum(np.square(diff), 0)dis = np.sqrt(dis)dis_list.append(dis) # 两张图片特征的距离

更正后:

for index, em in enumerate(embedding_1k):#index就是对应图片特征的iddiff = np.subtract(embedding, em)dis = np.sum(np.square(diff))dis = np.sqrt(dis)dis_list.append(dis)
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.al