mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Creating NDArray and reshape it. And saving it using NDArray::Save

Open amanaman118 opened this issue 10 years ago • 2 comments

Hello i have tried creating ndarray object and passing data to it as float values. What is best possible way of creating an ndarray object and store values to it and then reshape it with mshadow::Shape4 TShape type with some specific dimension.

Below is what i have passed from java to jni layer to do reshape

float[] fData1 = {1.0f, 2.0f, 3.0f, 1.0f, 2.0f, 3.0f, 1.0f, 2.0f};
Object[] fArr = {fData1};

int[] iData1 = {2, 2, 2, 1};
Object[] iArr = {iData1};

String[] sArr = {"key1"};

Below is what i am trying in predictor.cc

        int objectSize = env->GetArrayLength(jkeyArr);
    std::vector<NDArray> ndFinalArr(objectSize);

    for (int i=0; i<objectSize; i++) {
        // NDArray *tempArr = new NDArray(mshadow::Shape1(objectSize), Context::CPU());
        NDArray tempArr;

        // Fetch float array info and add it to ndarray
        jfloatArray tempfArr = (jfloatArray) (env->GetObjectArrayElement(jfloatArr, i));
        jfloat *fArr = env->GetFloatArrayElements(tempfArr,0);
        int tempLen = env->GetArrayLength(tempfArr);
        for (int j=0; j<tempLen; j++) {
            float val = fArr[j];
            tempArr = tempArr.operator +=(val);
        }
        env->ReleaseFloatArrayElements(tempfArr, fArr, 0);

        // Fetch dimension and reshape ndarray
        jintArray tempdArr = (jintArray) (env->GetObjectArrayElement(jdimArr, i));
        jint *dArr = env->GetIntArrayElements(tempdArr,0);
        if (env->GetArrayLength(tempdArr) == 4) {
            TShape shape = mshadow::Shape4(dArr[0],dArr[1],dArr[2],dArr[3]);
            tempArr = tempArr.Reshape(shape);
            ndFinalArr.push_back(tempArr);
        }
        env->ReleaseIntArrayElements(tempdArr, dArr, 0);
    }

I am facing issue with below code line. Need some help with below line. What all wrong i am doing.

tempArr = tempArr.Reshape(shape);

and below is what all i get in logcat about crash 08-02 19:54:15.572 com.sample.mxnet A: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x8 in tid 2957

Also is their any way to add android logging enabled in predictor.cc file as far as i have looked in files couldn't find direct Android.mk file to add up flag for it and setting up libraries required for logging. Tried adding <android/log.h> which resolved compile issue but at runtime it crashes due to library not found sort of issue.

Also for saving ndarray i need to call NDArray::Save is their any example on how to use dmlc::Stream will be helpfull to save content to some file in filesystem.

amanaman118 avatar Aug 02 '16 19:08 amanaman118

@SlipknotTN can you help me with this issue.

amanaman118 avatar Aug 04 '16 05:08 amanaman118

This issue is closed due to lack of activity in the last 90 days. Feel free to reopen if this is still an active issue. Thanks!

phunterlau avatar Sep 28 '17 06:09 phunterlau