加载.pkl文件后出现Python错误" ValueError:无法识别加载的数组布局"

时间:2016-12-13 11:35:48

标签: python python-2.7 svm

以下代码用于隔离林的培训过程,以便创建.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行,中
  load obj = _unpickle(fobj,filename,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行,中
  load dispatchkey
  文件" /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 setstate(state)
  文件" 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

1 个答案:

答案 0 :(得分:1)

尝试匹配raspbian和ubuntu中使用的库版本