Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to classify a single image? #566

Closed
marc40000 opened this issue Jun 30, 2014 · 4 comments
Closed

How to classify a single image? #566

marc40000 opened this issue Jun 30, 2014 · 4 comments
Labels

Comments

@marc40000
Copy link

I'm trying to write a function that classifies a single image. I looked into convert_imageset.cpp, test_net.cpp and wrapper.py while trying to figure it out.

So far I managed to load the net, load the image, resize it and convert it into a caffe::Blob. But when I call net::Forward(), it crashes:

template <typename Dtype>
const vector<Blob<Dtype>*>& Net<Dtype>::Forward(
    const vector<Blob<Dtype>*> & bottom, Dtype* loss) {
  // Copy bottom to internal bottom
  for (int i = 0; i < bottom.size(); ++i) {
    net_input_blobs_[i]->CopyFrom(*bottom[i]);
  }
  return ForwardPrefilled(loss);
}

The vector bottom holds 1 entry, the blob I created from the loaded image. However net_input_blobs has 0 entries, and therefore doing [i] crashes. How do I have to set things up correctly so net_input_blobs is set correcly?

My code so far

caffe::Caffe::set_phase(caffe::Caffe::TEST);
caffe::Caffe::set_mode(caffe::Caffe::CPU);

// read trained net
caffe::Net<Dtype> * net = new caffe::Net<Dtype>("lenet_test.prototxt");
net->CopyTrainedLayersFrom("lenet_iter_50");

// load and resize image to 256^2
const int width = 256;
const int height = 256;
cv::Mat cv_img;
cv::Mat cv_img_origin = cv::imread("valpics\\Banane.jpg", CV_LOAD_IMAGE_COLOR);
cv::resize(cv_img_origin, cv_img, cv::Size(height, width));
if (!cv_img.data) {
    printf("Could not open or find input file.\n");
    return 1;
}

// convert the image to a caffe::Blob
caffe::Blob<Dtype> * blob = new caffe::Blob<Dtype>(1, 3, width, height);
for (int c = 0; c < 3; ++c) {
    for (int h = 0; h < cv_img.rows; ++h) {
        for (int w = 0; w < cv_img.cols; ++w) {
            blob->mutable_cpu_data()[blob->offset(0, c, h, w)] = cv_img.at<cv::Vec3b>(h, w)[c];
        }
    }
}

// create input and output vectors for the Forward call
vector<caffe::Blob<Dtype>*> bottom_vecs;
vector<caffe::Blob<Dtype>*> top_vecs;
bottom_vecs.push_back(blob);

Dtype loss;
top_vecs = net->Forward(bottom_vecs, &loss);

// calculate mean of the resulting blob, which is the classification result?
caffe::Blob<Dtype>* topvec = top_vecs[0];
float mean = 0;
for (int c = 0; c < topvec->channels(); ++c) {
    for (int h = 0; h < topvec->height(); ++h) {
        for (int w = 0; w < topvec->width(); ++w) {
            mean += topvec->data_at(0, c, h, w);
        }
    }
}
mean /= (topvec->channels() * topvec->height() * topvec->width());

printf("mean=%f\n", mean);

delete blob;
delete net;
@bhack
Copy link
Contributor

bhack commented Jun 30, 2014

Take a look here: #499

@shelhamer
Copy link
Member

This is most easily done by the Python or MATLAB interfaces but #499 does give you the details on how to do this purely in Caffe.

@wguo68
Copy link

wguo68 commented May 30, 2015

How to solve the problem that net_input_blobs_.size is 0?

@linson007
Copy link

Is there any java sample code for classifying a single image?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants