facebookarchive / caffe2

Caffe2 is a lightweight, modular, and scalable deep learning framework.
https://caffe2.ai
Apache License 2.0
8.42k stars 1.95k forks source link

Message from debugger: Terminated due to memory issue - iOS 11 #2277

Open ghost opened 6 years ago

ghost commented 6 years ago

Hi, I am trying to load a caffe2 DenseNet model in iPhone SE running iOS 11. the init and predict model files are 4MB and 115KB respectively. This is my code snippet.

Caffe2.h file contents


#import <Foundation/Foundation.h>
@interface Caffe2 : NSObject

@property NSString *iNetName;
@property NSString *pNetName;

-(instancetype) initWithNets: (NSString *) iname
                 predictNets: (NSString *) pname;

@end

Caffe2.mm file Contents


#import "/Users/partha/Desktop/caffe2iosFinalTest/caffe2/caffe2/core/predictor.h"
#import "/Users/partha/Desktop/caffe2iosFinalTest/caffe2/caffe2/utils/proto_utils.h"
#include <iostream>
#include "/Users/partha/Desktop/caffe2iosFinalTest/caffe2/caffe2/proto/caffe2.pb.h"
#import "Caffe2.h"

@interface Caffe2()
{
    caffe2::NetDef _initNet;
    caffe2::NetDef _predictNet;
    caffe2::Predictor *_predictor;
}
@end

void readProtoIntoNet(std::string fname, caffe2::NetDef* net )
{
    int file = open(fname.c_str(), O_RDONLY);
    CAFFE_ENFORCE(net->ParseFromFileDescriptor(file));
    close(file);
}

@implementation Caffe2

- (instancetype)initWithNets:(NSString *)iname predictNets:(NSString *)pname
{

    self = [super init];
    if (self) {
        self.iNetName = [NSBundle.mainBundle pathForResource:iname ofType:@"pb"];
        self.pNetName = [NSBundle.mainBundle pathForResource:pname ofType:@"pb"];
        readProtoIntoNet(self.iNetName.UTF8String, &_initNet);
        readProtoIntoNet(self.pNetName.UTF8String, &_predictNet);
        _predictNet.set_name("PredictNet");
        _predictor = new caffe2::Predictor(_initNet,_predictNet);
        caffe2::Predictor::TensorVector input_vec,output_vec;
        std::cout<<"hi";

    /*  std::vector<float> inputPlanar(1*2*2*2);
      caffe2::TensorCPU input;
     input.Resize(std::vector<int>({1,2,2,2}));
    input.ShareExternalPointer(inputPlanar.data());
    caffe2::Predictor::TensorVector inputVec{&input};
     caffe2::Predictor::TensorVector output_vec; */

    _predictor->run(input_vec,&output_vec);
        std::cout<<"\n"<<output_vec;
    }
    return self;
}
@end

Contents of viewController.swift


 override func viewDidLoad() {
        super.viewDidLoad()
        let caffe2Object = Caffe2(nets: "init_net", predictNets: "predict_net")
    }

I know I have commented the input to the model.But even if I uncomment it it is giving the same error "Message from debugger: Terminated due to memory issue".

if I load other caffe2 models(.pb files) taken from the internet It is not giving any memory error.

The error is basically from _predictor->run(input_vec,&output_vec);

if I comment this line, there are no errors..so I think the model has Benn loaded successfully in the line _predictor = new caffe2::Predictor(_initNet,_predictNet);

Any help is really appreciated.Thanks

vinayak618 commented 6 years ago

Maybe the model is expecting the input in certain format. Try changing the input values, maybe in the format of (1,32,2,1600). since iPhone SE has RAM of 2GB, a larger input will terminate the process.