Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: 'numpy.float64' object cannot be interpreted as an index #481

Closed
snsie opened this issue Feb 7, 2017 · 26 comments
Closed

TypeError: 'numpy.float64' object cannot be interpreted as an index #481

snsie opened this issue Feb 7, 2017 · 26 comments

Comments

@snsie
Copy link

snsie commented Feb 7, 2017

I am hitting an error when following the tutorial linked below, which uses the INRIA Person dataset as an example.

https://github.com/deboc/py-faster-rcnn/tree/master/help

Everything seems to be working, but after two hours I keep hitting the following error at Process 3:

Solving...
Process Process-3:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "./tools/train_faster_rcnn_alt_opt.py", line 236, in train_fast_rcnn
max_iters=max_iters
File "/home/scott/code/py-faster-rcnn/tools/../lib/fast_rcnn/train.py", line 185, in train_net
model_paths = sw.train_model(max_iters)
File "/home/scott/code/py-faster-rcnn/tools/../lib/fast_rcnn/train.py", line 112, in train_model
self.solver.step(1)
File "/home/scott/code/py-faster-rcnn/tools/../lib/roi_data_layer/layer.py", line 155, in forward
blobs = self._get_next_minibatch()
File "/home/scott/code/py-faster-rcnn/tools/../lib/roi_data_layer/layer.py", line 68, in _get_next_minibatch
return get_minibatch(minibatch_db, self._num_classes)
File "/home/scott/code/py-faster-rcnn/tools/../lib/roi_data_layer/minibatch.py", line 64, in get_minibatch
num_classes)
File "/home/scott/code/py-faster-rcnn/tools/../lib/roi_data_layer/minibatch.py", line 110, in _sample_rois
fg_inds, size=fg_rois_per_this_image, replace=False
File "mtrand.pyx", line 1176, in mtrand.RandomState.choice (numpy/random/mtrand/mtrand.c:18822)
TypeError: 'numpy.float64' object cannot be interpreted as an index

Does anyone know what might be causing this problem?

@Microos
Copy link

Microos commented Feb 9, 2017

Same problem! An int type is expected.
Solution: Try to install numpy 1.11.0 sudo pip install -U numpy==1.11.0.
It is caused by unsupported float index in 1.12.0 even if the case likex[1.0: 3.0] should be considered as valid.

@tamiebug
Copy link

tamiebug commented Feb 9, 2017

Yes, the ability to use floating point indices was deprecated for a bit; It was finally removed in 1.12. You can also fix the problem by modifying lib/roi_data_layer/minibatch.py

On line 26, there's a call to np.round
fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)
This, unfortunately, creates a float, which causes the issues. Chaning this line to
fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int)
will fix this without having to use an older version of numpy, since fg_rois_per_image is where the floats were creeping in and causing trouble.

I believe there are only 3 other places where floats are being used to index, so you'd have to add
.astype(np.int) to the end of

lib/datasets/ds_utils.py line 12 : hashes = np.round(boxes * scale).dot(v)
lib/fast_rcnn/test.py line 129 : hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v)
lib/rpn/proposal_target_layer.py line 60 : fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)

I guess if you're really bored you can sniff out any remaining problematic float indexing attempts, or if you're not then maybe yes, you should install numpy 1.11.0 as suggested above!

@MC-devel-staudt
Copy link

There seems to be a similar error caused by line 173 in lib/roi_data_layer/minibatch.py.
cls = clss[ind]
This is then used to slice bbox_targets[], however it is not an int and throws an error after hours of training. Change it to,
cls = int(clss[ind])
You will save yourself a headache.

@snsie
Copy link
Author

snsie commented Feb 17, 2017

Thanks that fixed it!

@FredaZhang338
Copy link

@PentaHiggs Thanks a lot!! your solution works well, my questions were fixed.

@sulth
Copy link

sulth commented Sep 26, 2017

Iam having the same error.i tried to resolve error with numpy version as :
a)version 1.13—>index not an int
b)downgraded it to 1.11 —>unable to import multi array.How to resolveit now.

@sulth
Copy link

sulth commented Sep 28, 2017

@PentaHiggs & @DavidSPumpkins Thanks!! It worked i think .The training is not complete though.Thanks.

@tzrtzr000
Copy link

Hey @sulth
I have the same problem (ImportError: numpy.core.multiarray failed to import) after downgrading to 1.11. How did you figure it out? Thanks!

@MC-devel-staudt
Copy link

MC-devel-staudt commented Oct 3, 2017

@tzrtzr000 I would try upgrading to numpy 1.13 and solving the "index not an int" problem, as described earlier in this thread.

@yanxp
Copy link

yanxp commented Oct 17, 2017

@tzrtzr000 @sulth Have you solved the problem?

@sulth
Copy link

sulth commented Oct 17, 2017

yes .I followed @PentaHiggs & @DavidSPumpkins .It really helped .

@tzrtzr000
Copy link

tzrtzr000 commented Oct 17, 2017

@yanxp Yes. I adopted @PentaHiggs' solution : )

@Xinying666
Copy link

@tzrtzr000 I have the same problem (ImportError: numpy.core.multiarray failed to import) after downgrading to 1.11. How did you figure it out? Thanks!

@tzrtzr000
Copy link

@Xinying666 try solution provided by @PentaHiggs above

@Xinying666
Copy link

@tzrtzr000 I tried,but no work for me

@huangbo1221
Copy link

If you have the same error, you'd better check this sentence: fg_inds = npr.choice(
fg_inds, size=fg_rois_per_this_image, replace=False) .It's in the py-faster-rcnn/lib/roi_data_layer/minibatch.py. Maybe the type of fg_rois_per_this_image is numpy.float64 and will cause the same error.

@oguzhantogay
Copy link

oguzhantogay commented Jan 27, 2018

I cannot locate roi_data_layer/minibatch.py under Lib. Is there any other solutions that I can try for this problem? I am using python from anaconda 3.6.

File "C:\Python3.6\lib\site-packages\numpy\core\fromnumeric.py", line 2317, in amax return amax(axis=axis, out=out, **kwargs)

File "C:\Python3.6\lib\site-packages\numpy\core\_methods.py", line 26, in _amax return umr_maximum(a, axis, None, out, keepdims)
TypeError: 'numpy.float64' object cannot be interpreted as an integer

@ghost
Copy link

ghost commented Feb 9, 2018

Compiled changes necessary for proper integer slicing.
Change the following lines to resolve floating number indexing issue:

lib/roi_data_layer/minibatch.py line 26: 
fg_rois_per_image = int(np.round(cfg.TRAIN.FG_FRACTION * rois_per_image))
lib/datasets/ds_utils.py line 12 : 
hashes = int(np.round(boxes * scale).dot(v))
lib/fast_rcnn/test.py line 129 : 
hashes = int(np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v))
lib/rpn/proposal_target_layer.py line 60 : 
fg_rois_per_image = int(np.round(cfg.TRAIN.FG_FRACTION * rois_per_image))
lib/roi_data_layer/minibatch.py line 173: 
cls = int(clss[ind])
lib/rpn/proposal_target_layer.py line 124: 
cls = int(clss[ind])

@thepaulm
Copy link

thepaulm commented Dec 9, 2018

Yes, the ability to use floating point indices was deprecated for a bit; It was finally removed in 1.12. You can also fix the problem by modifying lib/roi_data_layer/minibatch.py

On line 26, there's a call to np.round
fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)
This, unfortunately, creates a float, which causes the issues. Chaning this line to
fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int)
will fix this without having to use an older version of numpy, since fg_rois_per_image is where the floats were creeping in and causing trouble.

I believe there are only 3 other places where floats are being used to index, so you'd have to add
.astype(np.int) to the end of

lib/datasets/ds_utils.py line 12 : hashes = np.round(boxes * scale).dot(v)
lib/fast_rcnn/test.py line 129 : hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v)
lib/rpn/proposal_target_layer.py line 60 : fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)

I guess if you're really bored you can sniff out any remaining problematic float indexing attempts, or if you're not then maybe yes, you should install numpy 1.11.0 as suggested above!

Thanks @PentaHiggs!
Why are we not submitting these back as patches? I just went through this same debugging cycle :(

@jundeli
Copy link

jundeli commented Jul 18, 2019

i tried the suggested solutions of downgrading numpy and adding .astype(np.int). But they cannot solve my same error ''numpy.float64' object cannot be interpreted as an integer'. Anyone encounters the same case with me?

@bosonbeard
Copy link

A have the same error

  File "/home/bosonbeard/anaconda3/envs/TOD15/lib/python3.7/site-packages/numpy/core/function_base.py", line 117, in linspace
    num = operator.index(num)

TypeError: 'numpy.float64' object cannot be interpreted as an integer

@MC-devel-staudt
Copy link

@bosonbeard You are trying to use a float, namely a numpy.float, as an index. But that index needs to an int. So you need to cast the num from a float to an int.
int(num)
Assuming all other things are correct, this should work.

@KoraVarisha
Copy link

Hi all,
I'm a python beginner. I am doing my thesis in a climate science project and I have been provided with the script already. While trying to run this I get the error:

'numpy.float64' object cannot be interpreted as an index

from the following code:

FluxDispMatSmall = np.zeros([5,5])
FluxDispMatSmall[3,2] = 1
FluxDispMat = np.tile(FluxDispMatSmall,[np.floor(len(latitude)/5+1),np.floor(len(longitude)/5)+1])
FluxDispMat = FluxDispMat[:len(latitude),:len(longitude)]

I'm using anaconda 1.9.12 on a Mac. with Python 2.7
I have been reading about this error, and seems like a way to solve it, is to go back to numpy version 1.11 ? my current numpy version is 1.16
is there other way to fix this problem?

thanks so much.

@snsie
Copy link
Author

snsie commented Mar 22, 2020

I don't think the issue would be fixed by changing the numpy version. It seems like a common data type issue that can be easily fixed with type casting. Python is picky about data types and will complain if you use floats to indicate an array index.

The array expects an integer so (index=5.0; array[index]) wouldn't work but (index=5; array[index]) would.

To fix this error with type casting, just wrap the variable with int() to cast it as an integer. (i.e. index=5.0; array[int(index)]) and (index=int(5.0); array[index]) would both work. The second version would permanently change the index variable type to int.

@KoraVarisha
Copy link

Snsie

THANK YOU !! .. I got it to work with your help. :)
Thanks so much for taking the time to reply. Much appreciated.

Have a great day.

@Himanshu66
Copy link

Yes, the ability to use floating point indices was deprecated for a bit; It was finally removed in 1.12. You can also fix the problem by modifying lib/roi_data_layer/minibatch.py

On line 26, there's a call to np.round
fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)
This, unfortunately, creates a float, which causes the issues. Chaning this line to
fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int)
will fix this without having to use an older version of numpy, since fg_rois_per_image is where the floats were creeping in and causing trouble.

I believe there are only 3 other places where floats are being used to index, so you'd have to add
.astype(np.int) to the end of

lib/datasets/ds_utils.py line 12 : hashes = np.round(boxes * scale).dot(v)
lib/fast_rcnn/test.py line 129 : hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v)
lib/rpn/proposal_target_layer.py line 60 : fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)

I guess if you're really bored you can sniff out any remaining problematic float indexing attempts, or if you're not then maybe yes, you should install numpy 1.11.0 as suggested above!

Hey, I hope this will work but i am not able to locate minibatch.py file in my project, can anybody help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests