【问题标题】:Python error after loading .pkl file "ValueError: Did not recognise loaded array layout"加载 .pkl 文件后出现 Python 错误“ValueError:无法识别加载的数组布局”
【发布时间】:2023-04-05 01:09:01
【问题描述】:

以下代码用于隔离森林的训练过程,以创建 .pkl 文件(您可以在此处查看链接 scikit-learn.org/stable/modules/generated/...)。生成 .pkl 文件后,我要将其从 ubuntu 加载到 raspbian OS。但是,我遇到了这个错误“ValueError:无法识别加载的数组布局”。谁能帮我这个?
完全错误:

回溯(最近一次通话最后一次):
文件“oneclass_test.py”,第 24 行,在
clf_one,stdSlr,voc,k = joblib.load('oneclass.pkl')
文件“/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py”,第 575 行,在
加载 obj = _unpickle(fobj, 文件名, mmap_mode)
文件“/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py”,第 507 行,在
_unpickle obj = unpickler.load()
文件“/usr/lib/python2.7/pickle.py”,第 858 行,在
加载调度键
文件“/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py”,第 327 行,在
load_build Unpickler.load_build(self)
文件“/usr/lib/python2.7/pickle.py”,第 1217 行,在
load_build 设置状态(状态)
文件“sklearn/tree/_tree.pyx”,第 650 行,在
sklearn.tree._tree.Tree.setstate (sklearn/tree/_tree.c:8406)
ValueError: 无法识别加载的数组布局

oneclass_train.py:

#!/usr/local/bin/python2.7

import argparse as ap
# Importing library that supports user friendly commandline interfaces
import cv2
# Importing the opencv library
import imutils
# Importing the library that supports basic image processing functions
import numpy as np
# Importing the array operations library for python
import os
# Importing the library which supports standard systems commands
from scipy.cluster.vq import *
# Importing the library which classifies set of observations into clusters
from sklearn.externals import joblib
from sklearn.svm import OneClassSVM
from sklearn.neighbors import KNeighborsClassifier

clf_one,stdSlr, voc,k = joblib.load("oneclass.pkl")

# Get the path of the testing set
parser = ap.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-t", "--testingSet", help="Path to testing Set")
group.add_argument("-i", "--image", help="Path to image")
parser.add_argument('-v',"--visualize", action='store_true')
args = vars(parser.parse_args())

# Get the path of the testing image(s) and store them in a list
image_paths = []
if args["testingSet"]:
    test_path = args["testingSet"]
    try:
        testing_names = os.listdir(test_path)
    except OSError:
        print "No such directory {}\nCheck if the file      exists".format(test_path)
        exit()
    for testing_name in testing_names:
        dir = os.path.join(test_path, testing_name)
        class_path = imutils.imlist(dir)
        image_paths+=class_path
else:
    image_paths = [args["image"]]

# Create feature extraction and keypoint detector objects
fea_det = cv2.xfeatures2d.SIFT_create()
des_ext = cv2.xfeatures2d.SIFT_create()

# List where all the descriptors are stored
des_list = []
for image_path in image_paths:
    im = cv2.imread(image_path)
    r = 960.0 / im.shape[1]
    dim = (960, int(im.shape[0]*r))
    im = cv2.resize(im, dim, interpolation = cv2.INTER_AREA)
    if im == None:
        print "No such file {}\nCheck if the file exists".format(image_path)
        exit()
    img=im
    img2=im
    s = 75
    mask = np.zeros(img.shape[:2],np.uint8)
    bgdModel = np.zeros((1,65),np.float64)
    fgdModel = np.zeros((1,65),np.float64)
    rect = (s,s,im.shape[1]-(2*s),im.shape[0]-(2*s)) cv2.grabCut(img,mask,rect,bgdModel,fgdModel,1,cv2.GC_INIT_WITH_RECT)
    mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
    im = img*mask2[:,:,np.newaxis]
    cv2.imwrite(image_path + "_Segment.jpg" ,im)
    print im.shape
    cv2.namedWindow("segmentation", cv2.WINDOW_NORMAL)
    pt = (0, 3 * im.shape[0] // 4)
    cv2.putText(im, "segmentation", pt ,cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 3, [0, 255, 0], 5)
    cv2.imshow("segmentation", im)
    cv2.waitKey(2000)
    kpts = fea_det.detect(im)  # Computing the key points of test image
    kpts, des = des_ext.compute(im, kpts)  # Computing the descriptors of the test image
    des_list.append((image_path, des))   # Appending the descriptors to a single list

# Stack all the descriptors vertically in a numpy array
descriptors = des_list[0][1]
for image_path, descriptor in des_list[0:]:
    descriptors = np.vstack((descriptors, descriptor))   # Stacking the descriptors in to a numpy array

# Computing the histogram of features
test_features = np.zeros((len(image_paths), k), "float32")
for i in xrange(len(image_paths)):
    words, distance = vq(des_list[i][1],voc)
    for w in words:
        test_features[i][w] += 1  # Calculating the histogram of features

# Perform Tf-Idf vectorization
nbr_occurences = np.sum( (test_features > 0) * 1, axis = 0)  # Getting the number of occurrences of each word
idf = np.array(np.log((1.0*len(image_paths)+1) / (1.0*nbr_occurences + 1)), 'float32')
# Assigning weight to one that is occurring more frequently

test_features = stdSlr.transform(test_features)

predictions = []
confidences = []

predictions = []
pred = clf_one.predict(test_features)
print clf_one.predict(test_features)
for i in pred:
    if i == 1:
            predictions += ["PPB"]
        if i == -1:
            predictions += ["NOT PPB"]

a=0
# Visualize the results, if "visualize" flag set to true by the user
if args["visualize"]:
    for image_path, prediction in zip(image_paths, predictions):
        image = cv2.imread(image_path)
        cv2.namedWindow(str(image_path), cv2.WINDOW_NORMAL)
        pt = (0, 3 * image.shape[0] // 4)
        cv2.putText(image, prediction , pt ,cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 5, [0, 255, 0], 5)
        cv2.imshow(str(image_path), image)
        cv2.imwrite(image_path + "_oneclass_Result.jpg" ,image)
        cv2.waitKey(3000)
        cv2.destroyAllWindows()
        a= a + 1

【问题讨论】:

  • 对不起,这是我第一次发帖。上面的代码用于隔离森林的训练过程,以便创建一个 .pkl 文件(您可以在此处查看链接scikit-learn.org/stable/modules/generated/…)。我正在将 .pkl 文件从 ubuntu 加载到 raspbian OS。运行代码后,我遇到了上面给出的错误。
  • 不要评论要求更多信息或澄清的 cmets:编辑问题。你知道the .pkl file from ubuntu中使用的协议版本吗?你的问题被标记为python-2.7,它不会解开第3版(不知道错误信息)。
  • @greybeard 我已经编辑了问题

标签:
python
python-2.7
svm