TensorFlow会话->使用EXC_BAD_ACCESS

时间:2018-08-31 07:27:43

标签: ios objective-c session tensorflow exc-bad-access

我正在使用TensorFlow做一个iOS应用,并且正在使用Objective-c。

在一个简单的应用程序中工作正常(以TensorFlow示例为例),但在另一个应用程序中添加此功能时则无效。

-(NSArray<NSNumber *> *)LoadImageandGetFaceFeature:(UIImage*)image{
NSMutableArray *dataArray = [NSMutableArray array];

const int img_dim = 160;
const int wanted_channels = 3; //3 for RGB image
const float input_mean = 128;
const float input_std = 128;
tensorflow::SessionOptions options;

tensorflow::Session* session_pointer = nullptr;
tensorflow::Status session_status = tensorflow::NewSession(options, &session_pointer);
if (!session_status.ok()) {
    std::string status_string = session_status.ToString();
    LOG(ERROR) <<"[Alyson log] Session create failed -> %s" <<status_string.c_str();
}
LOG(INFO) << "[Alyson log] Creating session.";
std::unique_ptr<tensorflow::Session> tf_sessionTest(session_pointer);
LOG(INFO) << "[Alyson log] Session created.";

tensorflow::GraphDef tensorflow_graph;
LOG(INFO) << "[Alyson log] Graph created.";

NSString* network_path = FilePathForResourceNameThree(model_file_name, model_file_type);
PortableReadFileToProtoThree([network_path UTF8String], &tensorflow_graph);

LOG(INFO) << "[Alyson log] Creating session.";
tensorflow::Status s = tf_sessionTest->Create(tensorflow_graph);
if (!s.ok()) {
    LOG(ERROR) << "[Alyson log] Could not create TensorFlow Graph: " << s;
}
//Alyson modify --END--0824

NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.

tensorflow::Tensor image_tensor(
                                tensorflow::DT_FLOAT,
                                tensorflow::TensorShape({1,160,160,3}));
auto image_tensor_data = image_tensor.tensor<float,4>();

std::vector<tensorflow::uint8> image_data = LoadImageFromFileThree(imageData);
tensorflow::uint8* in = image_data.data() ;
float* out = image_tensor_data.data();
int image_height = image.size.height;
int image_width = image.size.width;
int image_channels = 3;
for (int y = 0; y < img_dim; ++y) {
    const int in_y = (y * image_height) / img_dim;
    tensorflow::uint8* in_row = in + (in_y * image_width * image_channels);
    float* out_row = out + (y * img_dim * wanted_channels);
    for (int x = 0; x < img_dim; ++x) {
        const int in_x = (x * image_width) / img_dim;
        tensorflow::uint8* in_pixel = in_row + (in_x * image_channels);
        float* out_pixel = out_row + (x * wanted_channels);
        for (int c = 0; c < wanted_channels; ++c) {
            out_pixel[c] = (in_pixel[c] - input_mean) / input_std;
        }
    }
}

//Setting input_layers and out_layer name
std::string bool_input = "phase_train";
std::string image_input = "input";
std::string output_name = "embeddings";
std::vector<tensorflow::Tensor> outputs;

//Feed bool_input for not training model
tensorflow::Tensor bool_input_tensor(tensorflow::DT_BOOL,tensorflow::TensorShape());
auto bool_data = bool_input_tensor.scalar<bool>()();
bool_data=false;
//Run TensorFlow model
if(tf_sessionTest.get()&&out!=nil){ ////Alyson modify --ADD--0824
    //        LOG(INFO) << "[Alyson log] image tensor =" << image_tensor_data;
    LOG(INFO) << "[Alyson log] session->Run START ";

    tensorflow::Status run_status = tf_sessionTest->Run({{image_input, image_tensor},{bool_input,bool_input_tensor}},
                                                        {output_name},{}, &outputs);
    //        [NSThread sleepForTimeInterval:1.000];
    LOG(INFO) << "000---------dataArray ="<<dataArray << std::endl;

    LOG(INFO) << "[Alyson log] session->Run OK ";
    // RumTimesssThree= RumTimesssThree+1;
    //  LOG(INFO) << "[Alyson log] session->Run times = "<<RumTimesssThree;

    if (!run_status.ok()) {
        return nil;
    }
    //Get Face Feature data
    tensorflow::Tensor *output = &outputs[0];
    auto FaceFeature = output->flat<float>();
    for (int index = 0; index < FaceFeature.size(); index += 1) {
        const float FaceFeatureValue = FaceFeature(index);
        [dataArray addObject:[NSNumber numberWithFloat:FaceFeatureValue]];
    }
    LOG(INFO) << "[Alyson log] Get Face Feature Data Done !! ";

}//Alyson modify --ADD--0824
std::vector<tensorflow::uint8>().swap(image_data);
std::vector<tensorflow::Tensor>().swap(outputs);
tf_sessionTest->Close();
return dataArray;}

这是关于崩溃的图像:

enter image description here

我尝试了Zombie Objects来查找有关EXC_BAD_ACCESS的日志,但没有显示任何内容。

如果我的描述不清楚,请告诉我

0 个答案:

没有答案