Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

error : Dnn.forward() in Android

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:

    String prototxt = getPath("deploy.prototxt", this);                             
    String caffemodel = getPath("snapshot_iter_13800.caffemodel",this);          //LeNet   
    net = Dnn.readNetFromCaffe(prototxt, caffemodel); 

    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 THRESHOLD = 0.1;                                                               


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


    // Forward image through network.

    Mat blob = Dnn.blobFromImage(frame,IN_SCALE_FACTOR, new Size(IN_WIDTH, IN_HEIGHT), new Scalar(1,3),false);
    net.setInput(blob,"data");

    Mat detections = net.forward("Softmax");
click to hide/show revision 2
retagged

updated 2019-01-24 14:05:43 -0600

berak gravatar image

error : Dnn.forward() in Android

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:

    String prototxt = getPath("deploy.prototxt", this);                             
    String caffemodel = getPath("snapshot_iter_13800.caffemodel",this);          //LeNet   
    net = Dnn.readNetFromCaffe(prototxt, caffemodel); 

    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 THRESHOLD = 0.1;                                                               


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


    // Forward image through network.

    Mat blob = Dnn.blobFromImage(frame,IN_SCALE_FACTOR, new Size(IN_WIDTH, IN_HEIGHT), new Scalar(1,3),false);
    net.setInput(blob,"data");

    Mat detections = net.forward("Softmax");

error : Dnn.forward() in Android

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);          //LeNet   
   //获取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;                                         
IN_HEIGHT;

// final double IN_SCALE_FACTOR = 0.00390625; final double IN_SCALE_FACTOR = 0.0125000001863; // final double MEAN_VAL = 127.5; final double THRESHOLD = 0.1;

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

Imgproc.COLOR_RGBA2RGB);

    // Forward image through network.
 

// Mat blob = Dnn.blobFromImage(frame,IN_SCALE_FACTOR, Dnn.blobFromImage(frame ,IN_SCALE_FACTOR, new Size(IN_WIDTH, IN_HEIGHT), new Scalar(1,3),false); net.setInput(blob,"data"); Scalar(1,3,28,28),false,true); 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");
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.
        return outFile.getAbsolutePath();
    } catch (IOException ex) {
        Log.i(TAG, "Failed to upload a file");
    }
    return "";
}
private static final String TAG = "OpenCV/Sample/LeNet";
private static final String[] classNames = {"00000","00001","00002","00003","00004",
                                            "00005","00006","00007","00008","00009",
                                            "00010","00011","00012","00013","00014",
                                            "00015","00016","00017","00018","00019",
                                            "00020","00021","00022","00023","00024",
                                            "00025","00026","00027","00028","00029",
                                            "00030","00031","00032","00033","00034",
                                            "00035","00036","00037","00038","00039",
                                            "00040","00041","00042"};
private Net net;
private CameraBridgeViewBase mOpenCvCameraView;

error : Dnn.forward() in Android

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;

// IN_HEIGHT; //final double IN_SCALE_FACTOR = 0.00390625; final double IN_SCALE_FACTOR = 0.00390625; final double IN_SCALE_FACTOR = 0.0125000001863; // final double MEAN_VAL = 127.5; 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 ,IN_SCALE_FACTOR, new Size(IN_WIDTH, IN_HEIGHT), new Scalar(1,3,28,28),false,true); 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");

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 = 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);

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.
        return outFile.getAbsolutePath();
    } catch (IOException ex) {
        Log.i(TAG, "Failed to upload a file");
    }
    return "";
}
private static final String TAG = "OpenCV/Sample/LeNet";
private static final String[] classNames = {"00000","00001","00002","00003","00004",
                                            "00005","00006","00007","00008","00009",
                                            "00010","00011","00012","00013","00014",
                                            "00015","00016","00017","00018","00019",
                                            "00020","00021","00022","00023","00024",
                                            "00025","00026","00027","00028","00029",
                                            "00030","00031","00032","00033","00034",
                                            "00035","00036","00037","00038","00039",
                                            "00040","00041","00042"};
private Net net;
private CameraBridgeViewBase mOpenCvCameraView;