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