我发现很多人通过在numpy数组中添加astype(int(来解决这个问题,我也尝试过,但没有成功。这是我的代码
print('----loading data----')
t0 = time.time()
f1 = 'D:/building/big data/not test'
def get_imlist(path):
return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')]
imagePath1 = get_imlist(f1)
test_N = len(imagePath1)
test_data = np.ones((test_N,256,256,3),dtype='float64')
def loadIMG(imagePath , number, Array):
while number > 0:
img = cv2.imread(imagePath[number-1])
img = cv2.resize(img,(256,256),interpolation=cv2.INTER_AREA)
img_ndarray=np.asarray(img,dtype=int)
Array[number-1] = img_ndarray
number = number - 1
loadIMG(imagePath1, test_N, test_data)
test_data = test_data/255
test_data.astype(int)
print('---finish loading----')
t1 = time.time()
print('time:'+ str(round((t1-t0),4))+ ' sec')
print('----start predicting----')
t2 = time.time()
#load model
autoencoder = load_model('segnet2_outputgray_v3.h5')
for i in test_data:
result = autoencoder.predict(test_data[i])
a=result[0]
ret, th1 = cv2.threshold(a, np.mean(result), 255 , 0)
th1 = cv2.cvtColor(th1,cv2.COLOR_GRAY2RGB)
imgstack = np.hstack((test_data, th1))
cv2.imwrite('D:/building/big data/predict/'+str(i)+'.jpg', imgstack)
i = i + 1
t3 = time.time()
print('----finish predicting----')
并且错误发生在这一行
result = autoencoder.predict(test_data[i])
和IndexError
IndexError: arrays used as indices must be of integer (or boolean) type
当我把numpy ones数组创建为float64时,我已经专门定义了dtype。即使我把dytpe改为int,它也不起作用。
错误为;
用作索引的数组必须是整数(或布尔值(类型的
然后你说;
当我为float64创建numpy ones数组时,我已经专门定义了dtype,所以我不知道发生了什么
你不知道发生了什么???
正如您自己所说,您明确地将dtype定义为float64,这就是您的问题!。
float64顾名思义是"float64";浮动";而不是";int";。Numpy需要一个整数或整数数组作为索引,并且不能使用float64。