Ask Your Question
0

error : Dnn.forward() in Android

asked 2019-01-24 12:30:15 -0500

Charleyszc gravatar image

updated 2019-01-25 02:45:11 -0500

I don't know what problem and how to fix it, help please!

error: (-215:Assertion failed) ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0 in function 'virtual bool cv::dnn::ConvolutionLayerImpl::getMemoryShapes(const std::vector<std::vector<int> >&, int, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&) const'

my code:

    // Load a network.
public void onCameraViewStarted(int width, int height) {

    String prototxt = getPath("deploy.prototxt", this);                             //获取prototxt文件路径
    String caffemodel = getPath("snapshot_iter_13800.caffemodel",this);             //获取caffemodel文件路径
    net = Dnn.readNetFromCaffe(prototxt, caffemodel);                                           //从Caffe中读取神经网络

    if (net.empty()){                                                                           //if net不存在
        Log.i(TAG, "Network loaded failure");
    }

    Log.i(TAG, "Network loaded successfully");

}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    final int IN_WIDTH = 28;                                                                    
    final int IN_HEIGHT = 28;                                                                   
    final float WH_RATIO = (float)IN_WIDTH / IN_HEIGHT;                                         
    //final double IN_SCALE_FACTOR = 0.00390625;
    final double IN_SCALE_FACTOR = 0.0125000001863;
    final double THRESHOLD = 0.1;                                                               


    // Get a new frame
    Mat frame = (inputFrame.rgba());                                                            
    Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB);

    // Forward image through network.
    Mat blob = Dnn.blobFromImage(frame,1.0/234, new Size(IN_WIDTH, IN_HEIGHT), new Scalar(104,117,123),false,true);
    net.setInput(blob,"data");

    Mat detections = net.forward("Softmax").reshape(1,1);

    int cols = frame.cols();
    int rows = frame.rows();
    Size cropSize;
    if ((float)cols / rows > WH_RATIO) {
        cropSize = new Size(rows * WH_RATIO, rows);
    } else {
        cropSize = new Size(cols, cols / WH_RATIO);
    }
    int y1 = (int)(rows - cropSize.height) / 2;
    int y2 = (int)(y1 + cropSize.height);
    int x1 = (int)(cols - cropSize.width) / 2;
    int x2 = (int)(x1 + cropSize.width);
    Mat subFrame = frame.submat(y1, y2, x1, x2);
    cols = subFrame.cols();
    rows = subFrame.rows();
    //detections = detections.reshape(1, 1);
    for (int i = 0; i < detections.rows(); ++i) {
        double confidence = detections.get(i, 2)[0];
        if (confidence > THRESHOLD) {
            int classId = (int)detections.get(i, 1)[0];
            int xLeftBottom = (int)(detections.get(i, 1)[0] * cols);
            int yLeftBottom = (int)(detections.get(i, 3)[0] * rows);
            int xRightTop   = (int)(detections.get(i, 5)[0] * cols);
            int yRightTop   = (int)(detections.get(i, 7)[0] * rows);

            // Draw rectangle around detected object.
            Imgproc.rectangle(subFrame, new Point(xLeftBottom, yLeftBottom),
                    new Point(xRightTop, yRightTop),
                    new Scalar(0, 234, 0));

            String label = classNames[classId] + ": " + confidence;
            Log.i("",label);
            int[] baseLine = new int[1];
            Size labelSize = Imgproc.getTextSize(label, Core.FONT_HERSHEY_SIMPLEX, 0.7, 1, baseLine);



            // Draw background for label.
            Imgproc.rectangle(subFrame, new Point(xLeftBottom, yLeftBottom - labelSize.height),
                    new Point(xLeftBottom + labelSize.width, yLeftBottom + baseLine[0]),
                    new Scalar(208,234,246), Core.FILLED);



            // Write class name and confidence.
            Imgproc.putText(subFrame, label, new Point(xLeftBottom, yLeftBottom), Core.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(0, 0, 0));
        }
    }
    return frame;
}
public void onCameraViewStopped() {}
// Upload file to storage and return a path.

private static String getPath(String file, Context context) {
    AssetManager assetManager = context.getAssets();
    BufferedInputStream inputStream = null;
    try {


        // Read data from assets.
        inputStream = new BufferedInputStream(assetManager.open(file));
        byte[] data = new byte[inputStream.available()];
        inputStream.read(data);
        inputStream.close();


        // Create copy file in storage.
        File outFile = new File(context.getFilesDir(), file);
        FileOutputStream os = new FileOutputStream(outFile);
        os.write(data);
        os.close();


        // Return a path to file which may be read in common way ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2019-01-24 12:35:35 -0500

berak gravatar image

updated 2019-01-27 02:01:53 -0500

the 1st convolution layer in your network complains, that the number of channels in your input did not fit. ;)

we don't know, what kind of model you're using, but most likely it expects BGR or RGB input, not grayscale, so try a

Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2BGR);

instead.

edit:

(see 1st comment !)

you probably took the code from the android sample here, but mistook something:

leNet is a classification model, NOT a detection one. you simply cannot infer bounding boxes from it.

all you can do is apply minMaxLoc() on the softmax output, and infer the classID from there:

Mat detections = net.forward("Softmax").reshape(1,1);
MinMaxLocResult mm = Core.minMaxLoc(detections);
int classID = mm.maxLoc.x;
double probability = mm.maxVal;
edit flag offensive delete link more

Comments

i'm using lenet and i'm trying to detect traffic signs , if i use Imgproc.COLOR_RGBA2BGR the app works but can not detect objects, i'm very confused :(

Charleyszc gravatar imageCharleyszc ( 2019-01-24 13:37:45 -0500 )edit

please check the other params to blobFromImage() , probably a couple of them are wrong (like the mean). also bgr vs. rgb, again

berak gravatar imageberak ( 2019-01-24 13:44:05 -0500 )edit

i change the params to Mat blob = Dnn.blobFromImage(frame,1.0/234, new Size(IN_WIDTH, IN_HEIGHT), new Scalar(104,117,123),false,true), but it doesn't work, thanks

Charleyszc gravatar imageCharleyszc ( 2019-01-24 14:39:44 -0500 )edit
1

@Charleyszc, It's hard to remotely debug your application. What means "it doesn't work"? If there a error message or network produces wrong predictions? Please provide minimal working example with reference to model, test image and expected output. Thank you in advance!

dkurt gravatar imagedkurt ( 2019-01-25 02:40:15 -0500 )edit

This method may be the possible way ,but i couldn't find the ".maxPos.x" ,and what should i do with

int classId = (int)detections.get(i, 1)[0];
        int xLeftBottom = (int)(detections.get(i, 1)[0] * cols);
        int yLeftBottom = (int)(detections.get(i, 3)[0] * rows);
        int xRightTop   = (int)(detections.get(i, 5)[0] * cols);
        int yRightTop   = (int)(detections.get(i, 7)[0] * rows);

thanks a lot

Charleyszc gravatar imageCharleyszc ( 2019-01-27 01:54:45 -0500 )edit

@Charleyszc, -- sorry it's maxLoc , not maxPos.

and no, you cannot use the code in your comment, again that's for detection not classification

berak gravatar imageberak ( 2019-01-27 01:58:40 -0500 )edit
1

so i can't use lenet to detect object ,i have to change the net right ?

Charleyszc gravatar imageCharleyszc ( 2019-01-27 03:29:18 -0500 )edit

yes, indeed. you need something like SSD-mobilenet, or YOLO to detect objects, a different network architecture.

berak gravatar imageberak ( 2019-01-27 03:37:32 -0500 )edit

i see ,thank you very much

Charleyszc gravatar imageCharleyszc ( 2019-01-27 03:51:28 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-01-24 12:29:18 -0500

Seen: 52 times

Last updated: Jan 27