Custom dataset is not fit to svm model

20 views Asked by At

To check performance of SVM, I created custom datasets with images and labels without using any descriptor. The performance was

SVM Accuracy: 0.5525

SVM Precision: 0.5530303030303031

SVM Recall: 0.5525

SVM F1 Score: 0.5513784461152882

I know it means it didn't work well. So I googled about it and found that I need to use descriptor. So I used SIFT and made datasets using the code below.

def create_sift_dataset(folder_path):
    images = []
    labels = []
    sift = cv2.xfeatures2d.SIFT_create()

    for class_name in os.listdir(folder_path):
        class_path = os.path.join(folder_path, class_name)
        if os.path.isdir(class_path):
            for image_name in os.listdir(class_path):
                image_path = os.path.join(class_path, image_name)
                image = cv2.imread(image_path)
                image = cv2.resize(image, (128, 128))
                gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                kp, des = sift.detectAndCompute(gray, None)
                if des is not None:
                    images.append(des)
                    labels.append(class_name)
    
    return np.array(images), np.array(labels)

but when I tried to run the code below, it shows an error message at the fit stage(TypeError: only size-1 arrays can be converted to Python scalars).

# Create the SIFT dataset
train_sift_images, train_sift_labels = create_sift_dataset(train_folder_path)
test_sift_images, test_sift_labels = create_sift_dataset(test_folder_path)

# Reshape the SIFT images
train_sift_images = train_sift_images.reshape(train_sift_images.shape[0], -1)
test_sift_images = test_sift_images.reshape(test_sift_images.shape[0], -1)

# Train and evaluate SVM
svm_model = SVC()
svm_model.fit(train_sift_images, train_sift_labels)

How can I fix this error?

0

There are 0 answers