使用现有的caffemodel进行测试时会发生访问冲突

时间:2016-04-24 13:29:24

标签: c++ caffe

这是一个简单的测试人员,我写的是为了加载快照,然后用户可以用不同的图像测试网络,看看它是如何推广的。

int test()
{
    cout << "\n\t\t<Test Section>";
    string path = "";
    string prototextPath = "";
    string modelPath = "";
    cout << "\nEnter the test file (e.g mnist_5.png)";
    getline(cin, path);



    cout << "\nEnter the rototext file (e.g lenet_test-memory-1.prototxt)";
    getline(cin, prototextPath);


    cout << "\nEnter the model file (e.g lenet_iter_10000.caffemodel)";
    getline(cin, modelPath);


    int mode = 0;
    cout << "\n Enter Processing Mode"
        << "\n1.GPU"
        << "\n2.CPU";
    do{
        cout << "\n";
        cin >> mode;
    } while (mode < 0 || mode >2);

    if (mode == 1)
    {
        // Set up Caffe
        Caffe::set_mode(Caffe::GPU);
        int device_id = 0;
        Caffe::SetDevice(device_id);
        LOG(INFO) << "Using GPU";
    }
    else
    {
        // Set up Caffe
        Caffe::set_mode(Caffe::CPU);
        //int device_id = 0;
        //Caffe::SetDevice(device_id);
        LOG(INFO) << "Using CPU";

    }
    // get a testing image and display
    Mat img = imread(path);//(CAFFE_ROOT + "/examples/images/mnist_5.png");
    //cvtColor(img, img, CV_BGR2GRAY);
    imshow("img", img);
    waitKey(1);


    // Load net
    Net<float> net(prototextPath, caffe::TEST);//(CAFFE_ROOT + "/examples/mnist/lenet_test-memory-1.prototxt");
    string model_file = modelPath;//CAFFE_ROOT + "/examples/mnist/lenet_iter_10000.caffemodel";
    net.CopyTrainedLayersFrom(model_file);

    // set the patch for testing
    vector<Mat> patches;
    patches.push_back(img);

    // push vector<Mat> to data layer
    float loss = 0.0;
    boost::shared_ptr<MemoryDataLayer<float> > memory_data_layer;
    memory_data_layer = boost::static_pointer_cast<MemoryDataLayer<float>>(net.layer_by_name("data"));

    vector<int> labels(patches.size());
    memory_data_layer->AddMatVector(patches, labels);

    // Net forward
    const vector<Blob<float>*> & results = net.Forward(&loss);
    float *output = results[1]->mutable_cpu_data();

    // Display the output
    for (int i = 0; i < 10; i++) {
        printf("Probability to be Number %d is %.3f\n", i, output[i]);
    }
    return waitKey(0);
}

当应用想要执行这行代码时发生访问冲突:

memory_data_layer->AddMatVector(patches, labels);
  

caffeTrainer.exe中0x0000000000000000处的未处理异常:   0xC0000005:访问冲突执行位置0x0000000000000000。

我试图使用CIFAR10图像来测试这种概括的方式 我在这做错了什么?

0 个答案:

没有答案