【问题标题】:OpenCV insufficient memory crash on iOSiOS上的OpenCV内存不足崩溃
【发布时间】:2018-07-28 06:37:27
【问题描述】:

我使用 opencv contrib 框架创建了一个 Fisher 人脸识别器。我在训练模型时遇到问题。当我调用 train 函数时,应用程序崩溃并显示以下错误消息。

 FaceDetector(1872,0x1b625bb80) malloc: *** mach_vm_map(size=2016002048) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
OpenCV(3.4.0-dev) Error: Insufficient memory (Failed to allocate 2016000000 bytes) in OutOfMemoryError, file /Users/Name/Desktop/OpenCVBuild/opencv/modules/core/src/alloc.cpp, line 55
OpenCV(3.4.0-dev) Error: Assertion failed (u != 0) in create, file /Users/mustafa/Desktop/OpenCVBuild/opencv/modules/core/src/matrix.cpp, line 436
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(3.4.0-dev) /Users/Name/Desktop/OpenCVBuild/opencv/modules/core/src/matrix.cpp:436: error: (-215) u != 0 in function create

(lldb)

这是我用来训练模型的代码...

-(void) trainFaceRecognitionModel2 {
    NSArray * imageArray2;
    imageArray2 = [NSArray arrayWithObjects:[UIImage imageNamed:@"IMG_1781"], [UIImage imageNamed:@"IMG_1782"], [UIImage imageNamed:@"IMG_1783"], [UIImage imageNamed:@"IMG_1784"], [UIImage imageNamed:@"IMG_1785"], [UIImage imageNamed:@"IMG_1786"], [UIImage imageNamed:@"IMG_1787"], [UIImage imageNamed:@"IMG_1788"], [UIImage imageNamed:@"IMG_1789"], [UIImage imageNamed:@"IMG_1790"], [UIImage imageNamed:@"IMG_1791"], [UIImage imageNamed:@"IMG_1792"], [UIImage imageNamed:@"IMG_1793"], [UIImage imageNamed:@"IMG_1794"], [UIImage imageNamed:@"IMG_1795"], [UIImage imageNamed:@"IMG_1796"], [UIImage imageNamed:@"IMG_1797"], [UIImage imageNamed:@"IMG_1798"], [UIImage imageNamed:@"IMG_1799"], [UIImage imageNamed:@"IMG_1800"], [UIImage imageNamed:@"IMG_1801"], [UIImage imageNamed:@"1801"], [UIImage imageNamed:@"1802"], [UIImage imageNamed:@"IMG_1803"], nil];

    std::vector<cv::Mat> images = vector<cv::Mat>();
    std::vector<int> labels;

    for (UIImage* image in imageArray2) {
         cv::Mat grayMat;
        cv::Mat imageMat;

        UIImageToMat(image, imageMat);

        cv::cvtColor(imageMat, grayMat, CV_BGR2GRAY);

        images.push_back(grayMat);
        labels.push_back(2);


      //  [trainArray addObject:images];
        printf("training model...");

    }

    model2 -> train(images, labels);

}

我使用了一些 Xcode 的工具来确定在训练的高峰期我使用了多达 6 GB 的内存!如何减少所需的内存量并消除错误消息?

【问题讨论】:

    标签: ios objective-c opencv out-of-memory objective-c++


    【解决方案1】:

    同样的问题,我已经看到了 UIImageToMat(image, imageMat);添加大量内存,之后不会释放。即使在垫子上手动 .release 也不会清除所有内存。

    【讨论】:

    • 作为临时解决方案,我降低了图像数量。我看看能不能找到更实用的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    相关资源
    最近更新 更多