0

I'm doing some image detection. The detection algorithm has a line of code that's causing an error.

The line of code:

im_rois = im_rois.astype(np.float, copy=False)

The error is:

Error: ValueError: setting an array element with a sequence.

I printed the contents of im_rois. They are:

[[ array([[  56.04,   57.32,  317.16,  285.16],
       [ 125.16,   17.64,  332.52,  285.16],
       [   1.  ,    1.  ,  427.24,  639.72],
       ..., 
       [ 201.96,    1.  ,  248.04,   50.92],
       [ 286.44,    1.  ,  350.44,   47.08],
       [ 304.36,  244.2 ,  338.92,  349.16]])]]

Curious if anyone can point me in the direction.

6
  • 1
    You don't seems to have a array but a list of list of array!! You can try im_rois = im_rois[0][0]. Commented Jul 26, 2015 at 15:14
  • 2
    I think it's best to ask the author of im_rois.astype. Either to provide more meaningful error, or debug their code. Commented Jul 26, 2015 at 15:15
  • 1
    Are you sure you haven't passed in an invalid argument to a function in the code you are using? The name im_rois suggests a sequence of "regions of interest", which (maybe) should be an array with shape (N, 4), where N is the number of RoIs. It looks like your im_rois is nested too deep. Commented Jul 26, 2015 at 15:27
  • Thanks, yes its quite possible I've got some invalid inputs. The roi inputs are generated by another algorithm called selective search using matlab. The matlab file is loaded and converted into a roi array. I'm trying to figure out if I should go back and fix my matlab based code or persist with the matlab output I have. Commented Jul 26, 2015 at 15:33
  • 1
    Is this matlab file a .mat? Do you load it with scipy loadmat? Matlab matrices load as numpy arrays, but matlab cells load as python lists (but may contains arrays). Commented Jul 27, 2015 at 16:53

1 Answer 1

3

I can reproduce your error with this sequence:

In [1317]: im=[[np.array([[1,2,3]])]]  # list containing array    

im.astype(float) would produce a AttributeError: 'list' object has no attribute 'astype' because it is a list, not an array.

But I can embed it in an object array:

In [1318]: x=np.empty((1,),dtype=object)
In [1319]: x[0]=im    
In [1320]: x
Out[1320]: array([[[array([[1, 2, 3]])]]], dtype=object)

The print looks like yours; note the commas in the inner list.

In [1321]: print(x)
[[[array([[1, 2, 3]])]]]

If it was a ndim array, the print wouldn't have the commas.

In [1325]: print(np.array([[[1,2,3]]]))
[[[1 2 3]]]

Now when I try a astype I get your value error.

In [1322]: x.astype(np.float)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1322-1bc194707569> in <module>()
----> 1 x.astype(np.float)

ValueError: setting an array element with a sequence.

But without knowing how im_rois is created I can't suggest ways of avoiding this. If im_rois were

array([[  56.04,   57.32,  317.16,  285.16],
       [ 125.16,   17.64,  332.52,  285.16],
       [   1.  ,    1.  ,  427.24,  639.72],
       ...]])

it wouldn't give this error.

I'd check to make you sure you are feeding this package the right kinds of input, whether it be a list, an array or a scalar.

vstack is able to remove this intermediate 'object' layer, allowing astype.

In [1343]: np.vstack(x)
Out[1343]: array([[[[1, 2, 3]]]])

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.