ubunut20.04运行mobilenetV2报错TypeError: slice indices must be integers or None or have an __index__

1. 基础信息

文件版本
操作系统ubuntu20.04
python3.8.5
opencv4.5.1
numpy1.17.4
caffe1.0.0
mobilenet v2https://github.com/shicai/MobileNet-Caffe

2. 报错

执行代码

python3 eval_image.py --proto mobilenet_v2_deploy.prototxt --model mobilenet_v2.caffemodel --image ./cat.jpg

出错

Traceback (most recent call last):
  File "eval_image.py", line 66, in <module>
    eval()
  File "eval_image.py", line 38, in eval
    im = im[:, off:off + h]
TypeError: slice indices must be integers or None or have an __index__ method

3. 原因分析

切片索引只支持 integers 或者 None

4. 解决

方法一: numpy 降版本使用

网友表示,numpy 1.10* 版本支持浮点数索引,可将numpy 降版本

方法二: 修改索引类型

修改文件 “eval_image.py” line 37, 如下所示

def eval():
    nh, nw = 224, 224
    img_mean = np.array([103.94, 116.78, 123.68], dtype=np.float32)

    # caffe._caffe.set_mode_cpu()
    caffe.set_mode_cpu()
    net = caffe.Net(args.proto, args.model, caffe.TEST)

    im = caffe.io.load_image(args.image)
    h, w, _ = im.shape
    if h < w:
-        off = (w - h) / 2
+        off = (w-h)//2
        im = im[:, off:off + h]

问题解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值