| 43 | }; |
| 44 | |
| 45 | Classifier::Classifier(const string& model_file, const string& trained_file, const string& mean_file, |
| 46 | const string& label_file) |
| 47 | { |
| 48 | #ifdef CPU_ONLY |
| 49 | Caffe::set_mode(Caffe::CPU); |
| 50 | #else |
| 51 | Caffe::set_mode(Caffe::GPU); |
| 52 | #endif |
| 53 | |
| 54 | /* Load the network. */ |
| 55 | net_.reset(new Net<float>(model_file, TEST)); |
| 56 | net_->CopyTrainedLayersFrom(trained_file); |
| 57 | |
| 58 | CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input."; |
| 59 | CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output."; |
| 60 | |
| 61 | Blob<float>* input_layer = net_->input_blobs()[0]; |
| 62 | num_channels_ = input_layer->channels(); |
| 63 | CHECK(num_channels_ == 3 || num_channels_ == 1) << "Input layer should have 1 or 3 channels."; |
| 64 | input_geometry_ = cv::Size(input_layer->width(), input_layer->height()); |
| 65 | |
| 66 | /* Load the binaryproto mean file. */ |
| 67 | SetMean(mean_file); |
| 68 | |
| 69 | /* Load labels. */ |
| 70 | std::ifstream labels(label_file.c_str()); |
| 71 | CHECK(labels) << "Unable to open labels file " << label_file; |
| 72 | string line; |
| 73 | while(std::getline(labels, line)) |
| 74 | labels_.push_back(string(line)); |
| 75 | |
| 76 | Blob<float>* output_layer = net_->output_blobs()[0]; |
| 77 | CHECK_EQ(labels_.size(), output_layer->channels()) |
| 78 | << "Number of labels is different from the output layer dimension."; |
| 79 | } |
| 80 | |
| 81 | static bool PairCompare(const std::pair<float, int>& lhs, const std::pair<float, int>& rhs) |
| 82 | { |