【caffe for Python】classifier.py分类填坑

心血来潮,利用classifier.py做MNIST手写数据分类时出现两个坑

使用的代码出处http://blog.csdn.net/longji/article/details/70242230

分类代码

import os
import sys
import numpy as np
import matplotlib.pyplot as plt
caffe_root = '******/caffe/'
sys.path.insert(0, caffe_root + 'python')
import caffe
MODEL_FILE='******/caffe/examples/mnist/lenet.prototxt'
PRETRAINED = '******/caffe/examples/mnist/lenet_iter_10000.caffemodel'
IMAGE_FILE='******/MNIST_test/train_0.bmp' # 7
input_image = caffe.io.load_image(IMAGE_FILE, color=False)
net = caffe.Classifier(MODEL_FILE, PRETRAINED)

# 预测图片分类,没有crop时,oversample过采样为false
prediction = net.predict([input_image], oversample = False)
caffe.set_mode_cpu()
# 打印分类结果
print('predicted class:', prediction[0].argmax())

第一坑

File "******\python\caffe\classifier.py", line 30, in __init__
	in_ = self.inputs[0]

IndexError: list index out of range

报错位置:classifier.py 第30行 在初始化函数中
索引错误:self.inputs的索引超出范围

print self.inputs   #输出为[]空列表

在网上找了老半天的毫无所获,直到看到了这个:
https://stackoverflow.com/questions/34651620/pycaffe-index-out-of-range-when-using-original-train-prototxt-as-the-pretrained
原因是说我的MODEL_FILE没有外部输入,应该使用deploy.prototxt部署文件,更改完后

print self.inputs   #输出为["data"]

第一坑填完

第二坑


File "******\python\caffe\classifier.py", line 85, in predict
    input_ = input_[:, crop[0]:crop[2], crop[1]:crop[3], :]

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

报错位置:classifier.py 第85行 在预测函数中
类型错误:切片索引必须使用整形或None或有__index__方法

print crop     #[  0.   0.  28.  28.]
print crop.dtype    #float64

所以改起来也很简单,在85行上加一个类型转换就好

crop=crop.astype(np.int32)
input_ = input_[:, crop[0]:crop[2], crop[1]:crop[3], :]

第二坑填完

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值