affordance-net
affordance-net copied to clipboard
TypeError: slice indices must be integers or None or have an __index__ method
When training the network with your script I get the following error:
Solving...
Traceback (most recent call last):
File "./tools/train_net.py", line 118, in <module>
max_iters=args.max_iters)
File "/home/rdlm/affordance-net/tools/../lib/fast_rcnn/train.py", line 175, in train_net
model_paths = sw.train_model(max_iters)
File "/home/rdlm/affordance-net/tools/../lib/fast_rcnn/train.py", line 114, in train_model
self.solver.step(1)
File "/home/rdlm/affordance-net/tools/../lib/rpn/proposal_target_layer.py", line 106, in forward
rois_per_image, self._num_classes) #bbox_targets_oris: original gt of rois
File "/home/rdlm/affordance-net/tools/../lib/rpn/proposal_target_layer.py", line 606, in _sample_rois
_get_bbox_regression_labels(bbox_target_data, num_classes)
File "/home/rdlm/affordance-net/tools/../lib/rpn/proposal_target_layer.py", line 532, in _get_bbox_regression_labels
bbox_targets[ind, start:end] = bbox_target_data[ind, 1:] #gan gia tri tai class tuong ung la bbox_target_data, con lai la so 0
TypeError: slice indices must be integers or None or have an __index__ method
These errors are probably caused by the version of Numpy I am using (v 13.3). I managed to solve these errors by modifying the `$AffordanceNet/lib/rpn/proposal_target_layer.py file as follows:
-
vim $AffordanceNet/lib/rpn/proposal_target_layer.py
- replace the for loop in line 532 with:
for ind in inds:
cls = clss[ind]
start = 4 * cls
end = start + 4
start = int(start)
end = int(end)
bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS
- replace the if statement in line 579 with:
if fg_inds.size > 0:
fg_inds = npr.choice(fg_inds, size=int(fg_rois_per_this_image), replace=False)
- replace the if statement in line 590 with:
if bg_inds.size > 0:
bg_inds = npr.choice(bg_inds, size=int(bg_rois_per_this_image), replace=False)
- replace line 597 with:
labels[int(fg_rois_per_this_image):] = 0
I get a new error, however, which I am not able to solve:
Solving...
Traceback (most recent call last):
File "./tools/train_net.py", line 118, in <module>
max_iters=args.max_iters)
File "/home/rdlm/affordance-net/tools/../lib/fast_rcnn/train.py", line 175, in train_net
model_paths = sw.train_model(max_iters)
File "/home/rdlm/affordance-net/tools/../lib/fast_rcnn/train.py", line 114, in train_model
self.solver.step(1)
File "/home/rdlm/affordance-net/tools/../lib/rpn/proposal_target_layer.py", line 305, in forward
roi_mask = -1 * np.ones((h, w), dtype=np.float32)
File "/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py", line 192, in ones
a = empty(shape, dtype, order)
TypeError: 'numpy.float64' object cannot be interpreted as an index
This is a numpy version problem. I guess you're using version > 1.11.2, which doesn't support float index.
A quick fix is just to install numpy 1.11.2: sudo pip install numpy==1.11.2
, then you don't have to change anything in the code.
I have had this problem before. Because I have no root user rights, so I directly change the code in rpn/proposal_target_layer.py", line 305
roi_mask = -1 * np.ones((int(h), int(w)), dtype=np.float32)
There are another places need to be change in the rpn/proposal_target_layer.py
You can fix the float index problem by converting the indexes to integer.
Or without root, you can try pip install numpy==1.11.2 --user
. It will be installed on your local system, and there is no need to change the code :)
@nqanh Thanks a lot! It's a good means to solve this problem. You always can give us good advice!
Thanks guys! I did as @lsj910128 proposed and changed the lib/rpn/proposal_target_layer.py
to force the values to be integers. After modifying this, the training script worked! This is the summary of the changes I made:
- replace the for loop in line 532 with:
for ind in inds:
cls = clss[ind]
start = 4 * cls
end = start + 4
start = int(start)
end = int(end)
bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS
- replace the if statement in line 579 with:
if fg_inds.size > 0:
fg_inds = npr.choice(fg_inds, size=int(fg_rois_per_this_image), replace=False)
- replace the if statement in line 590 with:
if bg_inds.size > 0:
bg_inds = npr.choice(bg_inds, size=int(bg_rois_per_this_image), replace=False)
- replace line 597 with:
labels[int(fg_rois_per_this_image):] = 0
- replace line 305 with:
roi_mask = -1 * np.ones((int(h), int(w)), dtype=np.float32)
- replace line 330 with:
mask_overlap = np.zeros((int(y2o-y1o), int(x2o-x1o)), dtype=np.float32)
- replace line 339 with:
mask_overlap_draw = color_img[int(y1o):int(y2o), int(x1o):int(x2o), :]
- replace line 347 with:
mask_overlap[:, :] = gt_mask[int(y1o):int(y2o), int(x1o):int(x2o)]
- replace line 352 with:
roi_mask[int(y1o-y1):int(y2o-y1), int(x1o-x1):int(x2o-x1)] = mask_overlap
@nqanh I did 'pip install numpy==1.11.2
' but it doesn't work
if i did 'pip install numpy==1.11.2'
then error message is
ImportError: numpy.core.multiarray failed to import
so, I reinstall >1.11 numpy then error message is
TypeError: slice indices must be integers or None or have an index method
so I fixed the code, then it worked Could I solve this problem?
My environment is
anaconda2 python2.7.12 ubuntu14 cuda8.0 cudnn5.1
You can manually convert the float indexes to integers. If it works, then it's ok.