3

The code below is used for the training process of isolation forest in order to create a .pkl file (You can see the link here scikit-learn.org/stable/modules/generated/…). After generating the .pkl file, I am to load it from ubuntu to raspbian OS. However, I encountered this error "ValueError: Did not recognise loaded array layout". Can anyone help me with this? Complete error:

Traceback (most recent call last):
File "oneclass_test.py", line 24, in
 clf_one,stdSlr,voc,k = joblib.load('oneclass.pkl')
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 575, in
 load obj = _unpickle(fobj, filename, mmap_mode)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 507, in
 _unpickle obj = unpickler.load()
File "/usr/lib/python2.7/pickle.py", line 858, in
 load dispatchkey
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/numpy_pickle.py", line 327, in
 load_build Unpickler.load_build(self)
File "/usr/lib/python2.7/pickle.py", line 1217, in
 load_build setstate(state)
File "sklearn/tree/_tree.pyx", line 650, in
 sklearn.tree._tree.Tree.setstate (sklearn/tree/_tree.c:8406)
ValueError: Did not recognise loaded array layout

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
3
  • Sorry this is my first time to post. The code above is used for the training process of isolation forest in order to create a .pkl file (You can see the link here scikit-learn.org/stable/modules/generated/…). I am loading the .pkl file from ubuntu to raspbian OS. After running the code I encountered the error given above. Commented Dec 13, 2016 at 13:33
  • Do not comment comments asking for additional information or clarification: edit the question. Do you know the protocol version used in the .pkl file from ubuntu? Your question is tagged python-2.7, which wouldn't unpickle version 3 (don't know about the error message). Commented Dec 13, 2016 at 14:10
  • @greybeard i already edited the question Commented Dec 14, 2016 at 16:31

1 Answer 1

2

try matching the versions of the libraries used in both raspbian and ubuntu

Sign up to request clarification or add additional context in comments.

1 Comment

I matched the version of sklearn on my raspberry pi (running Raspbian Jessie) and desktop system (running Ubuntu 16.04). sklearn version on both the machine is '0.18.1'. Even I matched the version of numpy, scipy, pandas also. But still I am getting same error: "Did not recognize loaded array layout" when I am trying to load model.pkl file, trained on Ubuntu desktop system to Raspberry Pi (Raspbian Jessie). Please let me know about what else I can try...Thanks...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.