我是ML和图像分类的新手。我正在关注this tutorial
我无法计算混淆矩阵或准确性得分...
有人可以帮我吗?
我已经重塑并尝试将预测放入数组中,但是nothong可以工作...
testLabelsGlobal
的形状为(16,)
,文件夹Test中有114张图像。
# split the training and testing data
(trainDataGlobal, testDataGlobal, trainLabelsGlobal,testLabelsGlobal) = train_test_split(np.array(global_features),
np.array(global_labels),
test_size=test_size,
random_state=seed)
# 10-fold cross validation
for name, model in models:
kfold = KFold(n_splits=10, random_state=seed)
cv_results = cross_val_score(model, trainDataGlobal, trainLabelsGlobal, cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
print('testdataglobal ' , testDataGlobal.shape)
print('testlabelglobal ' , testLabelsGlobal.shape)
#testLabelsGlobal.reshape((16, 1))
#print('testlabelglobal reshape ' , testLabelsGlobal.shape)
clf = KNeighborsClassifier(n_neighbors=3)
# fit the training data to the model
clf.fit(trainDataGlobal, trainLabelsGlobal)
predictions =[]
# loop through the test images
resultat=[]
for i, file in enumerate(glob.glob(test_path)):
image = cv2.imread(file)
fv_hu_moments = fd_hu_moments(image)
fv_haralick = fd_haralick(image)
fv_histogram = fd_histogram(image)
global_feature = np.hstack([fv_hu_moments, fv_haralick,fv_histogram])
print('GLOABAL FEATURE TEST ' , global_feature)
# scale features in the range (0-1)
#scaler = MinMaxScaler(feature_range=(-1, 1))
#rescaled_feature = scaler.fit_transform(global_feature)
# predict label of test image
prediction = clf.predict(global_feature.reshape(1,-1))[0]
print('prediction' , prediction)
prediction= np.array(prediction)
accuracy = accuracy_score(testLabelsGlobal, np.argmax(prediction))
#print(manually_calculate_tp_tn_fp_fn(testLabelsGlobal, prediction))
#print(accuracy_score(testDataGlobal, prediction))
#.append(prediction)
#resultat.append(metrics.accuracy_score(testLabelsGlobal, prediction))
#cm = confusion_matrix(testLabelsGlobal, prediction)
# show predicted label on image
cv2.putText(image, train_labels[prediction], (20,30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,255), 3)
# display the output image
#plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
#plt.axis('off')
plt.savefig("resultat"+str(i) + '.jpg')
#plt.show()