简体   繁体   中英

One class SVM, got all -1

I am doing a binary classification that only returns "yes" or "no" for the image . As I only got an image of one class , so I wanna classify between "Target" and " Outlier".

For example, I am classifying the fireman. 在此处输入图片说明

I am using Scikit Learn svm.OneClassSVM() . However, after training the model, I got "-1" every time, even for predicting the training data.

Here is my code:

X_train = []
for subdir, dirs, files in os.walk("training"):
    for imagePath in files:
        print ("path = ", imagePath)
        img = Image.open(os.path.join(subdir, imagePath))
        img = img.resize(sample_size, PIL.Image.ANTIALIAS)
        img = np.array(img)
        img = img[:,:,0]
        img = img.reshape(1, img.shape[0]* img.shape[1])
        X_train.append(img[0])

clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1)
clf.fit(X_train)

And then I predict the result of "training data"

print clf.predict (X_train)

However, I still get all "-1". Can anyone tell me what's wrong?

you can set the threshold level by calculating the score sample. For example

clf = OneClassSVM()        
y_scores = clf.score_samples(test)

You need to check by yourself which threshold is better. You can check that by running the model on some data of abnormal class and check below which threshold are you getting your answer. The lesser the score sample the more outlier is that.

hyper parameter tuning most likely. gamma should probably be a lot lower.

what's the final shape of your training examples "img[0]"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM