如何获得我对caffe-mobile预测的信心?

时间:2016-01-26 13:28:03

标签: android android-ndk convolution caffe conv-neural-network

我试图让我对卡菲移动的预测充满信心。在python中很容易得到它,我只是做了:

prediction = mNet.predict([image])
print prediction

我得到以下输出:

[ 0.32693869  0.20242123  0.26447725  0.20616281]

我有四个班级,所以这些是我班级的自信。

在Android上如果我这样做:

int[] prediction = caffeMobile.predictImage(imgPath);
for (int i = 0; i < prediction.length; i++) {
    Log.i("Caffe", String.valueOf(prediction[i]));
}

我的输出只是预测的等级。

Caffe 1

我认为我必须实现此方法,因为prediction的大小为1.但是我如何更改以及必须更改哪些文件以使其在android上运行?

那么,我怎样才能使用caffe-mobile for android?

更新:

我将以下内容添加到caffe_jni.cpp

JNIEXPORT jfloatArray JNICALL
Java_com_sh1r0_caffe_1android_1lib_CaffeMobile_getConfidence(JNIEnv *env,
                                                        jobject thiz,
                                                        jstring imgPath) {
  CaffeMobile *caffe_mobile = CaffeMobile::Get();
  vector<float> confidence = caffe_mobile->GetConfidence(jstring2string(env, imgPath));

  jfloatArray result;
  result = env->NewFloatArray(4);
  if (result == NULL) {
    return NULL; /* out of memory error thrown */
  }
  env->SetFloatArrayRegion(result, 0, 4, &confidence[0]);
  return result;
}

然后我将其添加到caffe_mobile.cpp

vector<float> CaffeMobile::GetConfidence(const string &img_path) {
  const vector<float> probs = Forward(img_path);
  return probs;
}

我将此添加到caffe安卓类:

public native float[] getConfidence(String imgPath);

并称这种方法

float[] confidence = caffeMobile.getConfidence(imgPath);

但是,它只返回一个舍入值(我认为)。

Log.i("Caffe","LENGTH: " + String.valueOf(confidence.length));

输出4,(我有4个类);

所以:

for (int i = 0; i < confidence.length; i++) {
    Log.i("Caffe", String.valueOf(confidence[i]));
}

如果尝试对标记为0的图像进行分类,我会得到以下输出:

Caffe 1.0
Caffe 0.0
Caffe 0.0
Caffe 0.0

或者如果尝试对标记为2的图像进行分类,则输出为:

Caffe 0.0
Caffe 1.0
Caffe 0.0
Caffe 0.0

我做错了什么,或者不可能?

0 个答案:

没有答案