HH vs SY 2021-06-03 21:30 采纳率: 100%
浏览 56
已采纳

请问一下这个错误应该怎么修改呢?

import numpy as np
from sklearn.svm import SVC
from sklearn.externals import joblib
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt


def load_hog_data(hog_txt):
    """ load hog features.
    Args:
        hog_txt: a txt file used to save hog features.
            one line data is formated as "img_path \t cls_num \t hog_feature_vector"
    Return:
        img_names: a list of image names.
        labels: numpy array labels (1-dim).
        hog_feature: numpy array hog features.
            formated as [[hog1], [hog2], ...]
    """
    img_names = []
    labels = []
    hog_features = []
    with open(hog_txt, "r") as f:
        data = f.readlines()
        for row_data in data:
            row_data = row_data.rstrip()
            img_path, label, hog_str = row_data.split("\t")
            img_name = img_path.split("\\")[-1]
            hog_feature = hog_str.split(" ")
            hog_feature = [float(hog) for hog in hog_feature]
            # print "hog feature length = ", len(hog_feature)
            img_names.append(img_name)
            labels.append(int(label))
            hog_features.append(hog_feature)
    return img_names, np.array(labels), np.array(hog_features)


def svm_train(hog_features, labels, save_path="\svm_model.pkl"):
    """ SVM train
    Args:
        hog_feature: numpy array hog features.
            formated as [[hog1], [hog2], ...]
        labels: numpy array labels (1-dim).
        save_path: model save path.
    Return:
        none.
    """
    clf = SVC(C=10, tol=1e-3, probability=True)
    clf.fit(hog_features, labels)
    joblib.dump(clf, save_path)
    print("finished.")


def svm_test(svm_model, hog_feature, labels):
    """SVM test
    Args:
        hog_feature: numpy array hog features.
            formated as [[hog1], [hog2], ...]
        labels: numpy array labels (1-dim).
    Return:
        accuracy: test accuracy.
    """
    clf = joblib.load(svm_model)
    accuracy = clf.score(hog_feature, labels)
    return accuracy


def plot_roc_auc(clf, x_test):
    y_test = clf.predict(x_test)
    y_score = clf.decision_function(x_test)
    fpr, tpr, threshold = roc_curve(y_test, y_score)
    roc_auc = auc(fpr, tpr)
    plt.figure()
    plt.plot(fpr, tpr)
    plt.plot([0, 1], [1, 0])
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic example')


if __name__ == "__main__":
    hog_train_txt = "D:\computer_vision\proposals_train_hog.txt"
    hog_test_txt = "D:\computer_vision\proposals_test_hog.txt"
    model_path = "\svm_model.pkl"

    train = False

    if train is True:
        img_names, labels, hog_train_features = load_hog_data(hog_train_txt)
        svm_train(hog_train_features, labels, model_path)
    else:
        img_names, labels, hog_test_features = load_hog_data(hog_test_txt)
        test_accuracy = svm_test(model_path, hog_test_features, labels)
        print
        "test accuracy = ", test_accuracy

        # clf = joblib.load(model_path)
        # plot_roc_auc(clf, hog_test_features)

报错:

Traceback (most recent call last):
  File "C:/Users/samyeung/PycharmProjects/pythonProject/svm_train.py", line 94, in <module>
    test_accuracy = svm_test(model_path, hog_test_features, labels)
  File "C:/Users/samyeung/PycharmProjects/pythonProject/svm_train.py", line 62, in svm_test
    clf = joblib.load(svm_model)
  File "C:\Users\samyeung\PycharmProjects\pythonProject\venv\lib\site-packages\sklearn\externals\joblib\numpy_pickle.py", line 590, in load
    with open(filename, 'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory: '\\svm_model.pkl'

Process finished with exit code 1


 

  • 写回答

5条回答 默认 最新

  • CSDN专家-Time 2021-06-03 21:31
    关注

    No such file or directory: '\\svm_model.pkl'

    检查一下你的model是否存在呢

    训练集不存在!!!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • CSDN专家-Time 2021-06-03 21:44
    关注

    https://tensorflow.google.cn/tutorials?hl=zh_cn

    自己可以先跟着图像识别、文字情绪识别走一圈。

    评论
  • CSDN专家-Time 2021-06-03 21:48
    关注

    我可以提供参考文档。。

    https://www.scikitlearn.com.cn/

    评论
  • 有问必答小助手 2021-06-04 09:59
    关注

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    评论
  • qq_45987358 2023-04-17 19:21
    关注

    你好,请问你的问题解决了吗,我遇到了和你一样的问题

    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 arm虚拟机无法和物理机互通
  • ¥15 如何在此代码上增加一个统计学生生源的功能?(语言-c语言)
  • ¥15 Android导航条遮盖异常
  • ¥15 计算机网络技术基础问题
  • ¥15 设置mac系统只能访问指定网站
  • ¥15 西门子博途 s7 1200控制三台步进电机
  • ¥15 基于非参数的方向距离函数求污染物影子价格(有偿)
  • ¥15 vue+element 生成table
  • ¥15 实验 4 FIFO 算法和 LRU 算法-C 程序实现
  • ¥15 有偿拼接大疆精灵4RGB影像