caffe icon indicating copy to clipboard operation
caffe copied to clipboard

Not getting results same as classify.py when using c++ code

Open sohail1436 opened this issue 11 years ago • 1 comments

Hello There, I have been trying to classify images using c++ and opencv and here's my code and deploy.prototxt

include

include

include

include

include

include <stdio.h>

include </home/sohail/workspace/opencv-2.4.6.1/include/opencv2/opencv.hpp>

include "caffe/caffe.hpp"

include "caffe/util/io.hpp"

include "caffe/blob.hpp"

include "hdf5.h"

#include "leveldb/db.h" #include "lmdb.h" #include "caffe/common.hpp"

using namespace caffe; using namespace std; using namespace cv; Mat frame; int main(int argc, char** argv) {

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

frame=imread("/home/sohail/caffe/examples/images/cat.jpg",1); resize(frame,frame,Size(227,227)); Net caffe_test_net("./examples/opencvclassification/dep.prototxt"); //get the net

string traied_net = argc > 3 ? argv[3] : "/home/sohail/caffe/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel"; caffe_test_net.CopyTrainedLayersFrom(traied_net);

cout << "reading file" << endl; Datum datum; Datum* datum1=&datum; datum1->set_channels(3); datum1->set_height(frame.rows); datum1->set_width(frame.cols); datum1->clear_data(); datum1->clear_float_data(); string* datum1_string = datum1->mutable_data(); for (int c = 0; c < 3; ++c) { for (int h = 0; h < frame.rows; ++h) { for (int w = 0; w < frame.cols; ++w) { datum1_string->push_back( static_cast(frame.atcv::Vec3b(h, w)[c])); } } } std::vector images; images.push_back(datum);

float loss = 0.0;

cout << "adding images" << endl; boost::dynamic_pointer_cast< caffe::MemoryDataLayer >(caffe_test_net.layers()[0])->AddDatumVector(images);

cout << "running net" << endl; std::vector<Blob*> result = caffe_test_net.ForwardPrefilled(&loss);

cout << "got results" << endl; LOG(INFO)<< "Output result size: "<< result.size();

// Get probabilities const boost::shared_ptr<Blob >& probLayer = caffe_test_net.blob_by_name("prob"); const float* probs_out = probLayer->cpu_data(); // Get argmax results const boost::shared_ptr<Blob >& argmaxLayer = caffe_test_net.blob_by_name("argmax");

const float* argmaxs = argmaxLayer->cpu_data(); for (int i = 0; i < argmaxLayer->num(); ++i) { LOG(INFO)<< " Image: "<< i << " class:" << argmaxs[i]<< " Prob=" << probs_out[i*probLayer->height() + 0]; }

return 0; }

Deploy.txt

name: "opencv_classify" layers { name: "data" type: MEMORY_DATA top: "data" top: "label" memory_data_param { batch_size: 1 channels: 3 height: 227 width: 227 } transform_param { mirror: false mean_file: "./data/ilsvrc12/imagenet_mean.binaryproto" } } layers { name: "conv1" type: CONVOLUTION bottom: "data" top: "conv1" convolution_param { num_output: 96 kernel_size: 11 stride: 4 } } layers { name: "relu1" type: RELU bottom: "conv1" top: "conv1" } layers { name: "pool1" type: POOLING bottom: "conv1" top: "pool1" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layers { name: "norm1" type: LRN bottom: "pool1" top: "norm1" lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } } layers { name: "conv2" type: CONVOLUTION bottom: "norm1" top: "conv2" convolution_param { num_output: 256 pad: 2 kernel_size: 5 group: 2 } } layers { name: "relu2" type: RELU bottom: "conv2" top: "conv2" } layers { name: "pool2" type: POOLING bottom: "conv2" top: "pool2" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layers { name: "norm2" type: LRN bottom: "pool2" top: "norm2" lrn_param { local_size: 5 alpha: 0.0001 beta: 0.75 } } layers { name: "conv3" type: CONVOLUTION bottom: "norm2" top: "conv3" convolution_param { num_output: 384 pad: 1 kernel_size: 3 } } layers { name: "relu3" type: RELU bottom: "conv3" top: "conv3" } layers { name: "conv4" type: CONVOLUTION bottom: "conv3" top: "conv4" convolution_param { num_output: 384 pad: 1 kernel_size: 3 group: 2 } } layers { name: "relu4" type: RELU bottom: "conv4" top: "conv4" } layers { name: "conv5" type: CONVOLUTION bottom: "conv4" top: "conv5" convolution_param { num_output: 256 pad: 1 kernel_size: 3 group: 2 } } layers { name: "relu5" type: RELU bottom: "conv5" top: "conv5" } layers { name: "pool5" type: POOLING bottom: "conv5" top: "pool5" pooling_param { pool: MAX kernel_size: 3 stride: 2 } } layers { name: "fc6" type: INNER_PRODUCT bottom: "pool5" top: "fc6" inner_product_param { num_output: 4096 } } layers { name: "relu6" type: RELU bottom: "fc6" top: "fc6" } layers { name: "drop6" type: DROPOUT bottom: "fc6" top: "fc6" dropout_param { dropout_ratio: 0.5 } } layers { name: "fc7" type: INNER_PRODUCT bottom: "fc6" top: "fc7" inner_product_param { num_output: 4096 } } layers { name: "relu7" type: RELU bottom: "fc7" top: "fc7" } layers { name: "drop7" type: DROPOUT bottom: "fc7" top: "fc7" dropout_param { dropout_ratio: 0.5 } } layers { name: "fc8" type: INNER_PRODUCT bottom: "fc7" top: "fc8" inner_product_param { num_output: 1000 } } layers { name: "accuracy" type: ACCURACY bottom: "fc8" bottom: "label" top: "accuracy" include: { phase: TEST } } layers { name: "loss" type: SOFTMAX_LOSS bottom: "fc8" bottom: "label" top: "loss" } layers { name: "prob" type: SOFTMAX bottom: "fc8" top: "prob" include: { phase: TEST } } layers { name: "argmax" type: ARGMAX bottom: "prob" top: "argmax" argmax_param { top_k: 5 } include: { phase: TEST } }

But the problem is I am not getting proper results as I was getting with Classify.py. Can Someone help me

Thanks

sohail1436 avatar Mar 20 '15 14:03 sohail1436

please ask usage questions on caffe-users -- this issues tracker is primarily for Caffe development discussion. Thanks!

pathak22 avatar Mar 25 '15 19:03 pathak22