Skip to content

TypeError: slice indices must be integers or None or have an __index__ method #480

Closed
@Esugi8

Description

@Esugi8

For training ZF Faster-rcnn with PASCAL VOC 2007 dataset,
I input the command as shown in the tutorial
./experiments/scripts/faster_rcnn_end2end.sh 0 ZF pascal_voc

Then I got an error below

Traceback (most recent call last):
File "./tools/train_net.py", line 112, in
max_iters=args.max_iters)
File "~ /py-faster-rcnn/tools/../lib/fast_rcnn/train.py", line 160, in train_net
model_paths = sw.train_model(max_iters)
File "~ /py-faster-rcnn/tools/../lib/fast_rcnn/train.py", line 101, in train_model
self.solver.step(1)
File "~ /py-faster-rcnn/tools/../lib/rpn/proposal_target_layer.py", line 66, in forward
rois_per_image, self._num_classes)
File "~ /py-faster-rcnn/tools/../lib/rpn/proposal_target_layer.py", line 191, in _sample_rois
_get_bbox_regression_labels(bbox_target_data, num_classes)
File "~ /py-faster-rcnn/tools/../lib/rpn/proposal_target_layer.py", line 127, in _get_bbox_regression_labels
bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
TypeError: slice indices must be integers or None or have an index method

I just followed the tutorial and did not edit any file.
Could you please let me know how to solve this issue?

--using software versions--
python2.7, Cuda-7.0, cuDNN4.0,

Activity

bjornamr

bjornamr commented on Feb 7, 2017

@bjornamr

Got the same error.

SietseHuisman

SietseHuisman commented on Feb 7, 2017

@SietseHuisman

Same here. Checked train.prototxt, checked all annotations in my data..

Edit:

Seems like the latest version of numpy (1.12.0) does not support floats as indexes. From release notes:

Indexing with floats raises IndexError, e.g., a[0, 0.0].

Downgrading to numpy 1.11.0 worked for me.

alexdominguez09

alexdominguez09 commented on Feb 9, 2017

@alexdominguez09

I had exactly the same error message. Solved by uninstalling numpy 1.12.0 and installing numpy 1.11.2.

Worked perfect for me.

Esugi8

Esugi8 commented on Feb 14, 2017

@Esugi8
Author

I really appreciate you all giving good advices.
I uninstalled numpy1.12.0, then installed numpy 1.11.1 and re-installed openCV2.4.13, matplotlib1.5.1.
As a result, all issues have been solved!!

leejaymin

leejaymin commented on Feb 23, 2017

@leejaymin

Thanks.
1.10 numpy, that's ok

tejus-gupta

tejus-gupta commented on Jun 2, 2017

@tejus-gupta

@SietseHuisman has identified the error correctly(numpy v1.12.0 doesn't support float indices)

An easier solution is to add the following lines to lib/proposal_target_layer.py
After line 126,

start=int(start)
end=int(end)

After line 166,

fg_rois_per_this_image=int(fg_rois_per_this_image)
Po-Hsuan-Huang

Po-Hsuan-Huang commented on Jun 7, 2017

@Po-Hsuan-Huang

I can confirm @tejus-gupta 's method worked on numpy 1.12.1.
I was running
./tools/train_net.py --gpu 0 --solver models/pvanet/example_finetune/solver.prototxt --weights models/pvanet/pva9.1/PVA9.1_ImgNet_COCO_VOC0712.caffemodel --iters 100000 --cfg models/pvanet/cfgs/train.yml --imdb voc_2007_trainval

zhcf

zhcf commented on Aug 21, 2017

@zhcf

Meet the same problem, fix it using tejus-gupta's suggestion, it worked.

sdews

sdews commented on Sep 13, 2017

@sdews

I tried to downgrade numpy to version 1.11.2. But there was a new error
ImportError: numpy.core.multiarray failed to import.
I tried to solve this new error and failed. Finally, I upgrade numpy to the latest version 1.13.1 . And with @tejus-gupta 's method, I solve the error
TypeError: slice indices must be integers or None or have an index method.

Xinying666

Xinying666 commented on Nov 14, 2017

@Xinying666

the same problem
I tried to downgrade numpy to version 1.11.2. But there was a new error
ImportError: numpy.core.multiarray failed to import.
I tried to solve this new error and failed. Finally, I upgrade numpy to the latest version 1.13.1 . however,I haven't solved the problem:
TypeError: slice indices must be integers or None or have an index method.

ShanQincheng

ShanQincheng commented on Jan 6, 2018

@ShanQincheng

the same as @sdews ,but I there is one thing different.

My proposal_target_layer.py directory position is at lib/rcn/proposal_target_layer.py

fengyuentau

fengyuentau commented on Jan 15, 2018

@fengyuentau

@tejus-gupta 's method worked. Huge thanks.
But I think it's better to refer the exact line code rather than line number, since the line number mentioned does not match the one I have.

In code block,

    for ind in inds:
        cls = clss[ind]
        start = 4 * cls
        end = start + 4
        bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
        bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS

add the following two lines after end = start + 4,

        start = int(start)
        end = int(end)

Add line fg_rois_per_this_image = int(fg_rois_per_this_image) after code block,

    # Guard against the case when an image has fewer than fg_rois_per_image
    # foreground RoIs
    fg_rois_per_this_image = min(fg_rois_per_image, fg_inds.size)

And in my case, lib/proposal_target_layer.py should be lib/rpn/proposal_target_layer.py.

BTW, I am using numpy 1.14.0 and only one version of numpy is installed. By downgrading numpy version to 1.11.2, I met the same problem as @sdews. I googled this problem but found no working solutions. Most of them are suggesting uninstalling other versions of numpy. Maybe editing the path could make python pick up the right version of numpy, but I don't know how to do this. Anyone who figures this out please let me know. Thanks.

UPDATE:
For those who don't want to edit any code, downgrading numpy version to 1.11.0 will not help unless you recompile everything. I finally succeeded in using numpy-1.11.0 without editing any code just by cloning another copy of py-faster-rcnn and following the official installation guide again. I tried only make clean && make superclean under $FRCN_ROOT/caffe-fast-rcnn, but that did not help solve the problem. Also I tried to rebuild the Cython modules by command make under $FRCN_ROOT/lib, but it told me that everything is up-to-date which did not solve the problem too. Since step of building Cython modules did not offer any cleaning command like make clean, maybe deleting the whole current directory $FRCN_ROOT/lib and replacing it with its original one will help, which I have not yet tried. However, recompiling everything in another new copy will always work after downgrading numpy.

tacsam1212

tacsam1212 commented on Jun 18, 2018

@tacsam1212

@Fung-yuantao I am new to Python and new to Github so straightaway, I apologize for the lack of the formatting standard. I am having this same issue as above, and am using numpy 1.14.0. However I can't find where the code is I need to add that you mentioned above. Basically, I can't find lib/rpn/proposal_target_layer.py. Could you explain how to find it?

Here is my error message:

TypeError Traceback (most recent call last)
in ()
----> 1 spectrogram = violin.make_spectrogram(seg_length=1024)
2 spectrogram.plot(high=5000)

~\Anaconda3\lib\site-packages\thinkdsp.py in make_spectrogram(self, seg_length, win_flag)
941
942 while j < len(self.ys):
--> 943 segment = self.slice(i, j)
944 if win_flag:
945 segment.window(window)

~\Anaconda3\lib\site-packages\thinkdsp.py in slice(self, i, j)
895 j: second slice index
896 """
--> 897 ys = self.ys[i:j].copy()
898 ts = self.ts[i:j].copy()
899 return Wave(ys, ts, self.framerate)

TypeError: slice indices must be integers or None or have an index method

fengyuentau

fengyuentau commented on Jun 18, 2018

@fengyuentau

@tacsam1212 Assuming that you are using the up-to-date py-faster-rcnn, the file is located in py-faster-rcnn/lib/rpn/ as the following picture. Or you can use find command to search for it on Linux.
image

But I think what your are dealing with is another project , which is not py-faster-rcnn, and for sure you cannot find that file. Errors you got are similar though. You should really read the last part in your error message. Guess j is other type like float, but the type of index in numpy 1.14.0 must be integer. Maybe you should turn type of j to be integer before you are using it as index.

tacsam1212

tacsam1212 commented on Jun 20, 2018

@tacsam1212

@Fung-yuantao I don't see how/where to change the type. Here is the code from where it is saying the error is originating from:

def make_spectrogram(self, seg_length, win_flag=True):
    """Computes the spectrogram of the wave.

    seg_length: number of samples in each segment
    win_flag: boolean, whether to apply hamming window to each segment

    returns: Spectrogram
    """
    if win_flag:
        window = np.hamming(seg_length)
    i, j = 0, seg_length
    step = seg_length // 2

    # map from time to Spectrum
    spec_map = {}

    while j < len(self.ys):
        segment = self.slice(i, j)
        if win_flag:
            segment.window(window)

        # the nominal time for this segment is the midpoint
        t = (segment.start + segment.end) / 2
        spec_map[t] = segment.make_spectrum()

        i += step
        j += step
        
    return Spectrogram(spec_map, seg_length)

And the second: Lines 891 - 900

def slice(self, i, j):
    """Makes a slice from a Wave.

    i: first slice index
    j: second slice index
    """
    ys = self.ys[i:j].copy()
    ts = self.ts[i:j].copy()
    
    return Wave(ys, ts, self.framerate)

How do you change the type of index?

Thanks in advance

fengyuentau

fengyuentau commented on Jun 22, 2018

@fengyuentau

@tacsam1212 In slice, change type of j like the following:

def slice(self, i, j):
    """Makes a slice from a Wave.

    i: first slice index
    j: second slice index
    """
    j = (int) j # Change type of j to be integer
    ys = self.ys[i:j].copy()
    ts = self.ts[i:j].copy()
    
    return Wave(ys, ts, self.framerate)
rh01

rh01 commented on Aug 1, 2019

@rh01

@SietseHuisman has identified the error correctly(numpy v1.12.0 doesn't support float indices)

An easier solution is to add the following lines to lib/proposal_target_layer.py
After line 126,

start=int(start)
end=int(end)

After line 166,

fg_rois_per_this_image=int(fg_rois_per_this_image)

@tejus-gupta 's method worked on numpy 1.16.

JorgeYama

JorgeYama commented on Feb 3, 2023

@JorgeYama

@Fung-yuantao
I found the way to fix it without chaging the version of numpy
First you must know the directory of the file with the error:
In my case: C:\Users\yamaj\AppData\Local\Programs\Python\Python310\Lib\site-packages\thinkdsp.py
Inside the file .py you must change the following

def slice(self, i, j):
    """Makes a slice from a Wave.

    i: first slice index
    j: second slice index
    """
    j = int(j) # Change type of j to be integer
    i = int(i)

    ys = self.ys[i:j].copy()
    ts = self.ts[i:j].copy()
    
    return Wave(ys, ts, self.framerate)

and also you must add the same in the first part

def make_spectrogram(self, seg_length, win_flag=True):
    """Computes the spectrogram of the wave.

    seg_length: number of samples in each segment
    win_flag: boolean, whether to apply hamming window to each segment

    returns: Spectrogram
    """
    if win_flag:
        window = np.hamming(seg_length)
    i, j = 0, seg_length
    step = seg_length // 2

    # map from time to Spectrum
    spec_map = {}

    while j < len(self.ys):
        j = int(j)
        i = int(i)
        segment = self.slice(i, j)
        if win_flag:
            segment.window(window)

        # the nominal time for this segment is the midpoint
        t = (segment.start + segment.end) / 2
        spec_map[t] = segment.make_spectrum()

        i += step
        j += step
        
    return Spectrogram(spec_map, seg_length)

And then, it worked perfectly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @leejaymin@bjornamr@rh01@SietseHuisman@fengyuentau

        Issue actions

          TypeError: slice indices must be integers or None or have an __index__ method · Issue #480 · rbgirshick/py-faster-rcnn