Exemplo n.º 1
0
Deep_Classifier::Deep_Classifier(const string& model_file,
                       const string& trained_file,
                       const string& mean_file,
                       const string& label_file) {

    Caffe::set_mode(Caffe::GPU);

    /* Load the network. */
    net_.reset(new Net<float>(model_file, TEST));
    net_->CopyTrainedLayersFrom(trained_file);
    

    
    Blob<float>* input_layer = net_->input_blobs()[0];
    num_channels_ = input_layer->channels();

    input_geometry_ = cv::Size(input_layer->width(), input_layer->height());

    /* Load the binaryproto mean file. */
    SetMean(mean_file);

    /* Load labels. */
    std::ifstream labels(label_file.c_str());
    string line;
    while (std::getline(labels, line))
        labels_.push_back(string(line));

    Blob<float>* output_layer = net_->output_blobs()[0];
}
Classifier::Classifier(const std::string& model_file, const std::string& trained_file, const std::string& mean_file) {
#ifdef CPU_ONLY
	Caffe::set_mode(Caffe::CPU);
#else
	caffe::Caffe::set_mode(caffe::Caffe::GPU);
#endif

	/* Load the network. */
	net_.reset(new caffe::Net<float>(model_file, caffe::TEST));
	net_->CopyTrainedLayersFrom(trained_file);

	CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input.";
	CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output.";

	caffe::Blob<float>* input_layer = net_->input_blobs()[0];
	num_channels_ = input_layer->channels();
	CHECK(num_channels_ == 3 || num_channels_ == 1)
		<< "Input layer should have 1 or 3 channels.";
	input_geometry_ = cv::Size(input_layer->width(), input_layer->height());

	/* Load the binaryproto mean file. */
	SetMean(mean_file);

	/* Load labels. */
	/*std::ifstream labels(label_file.c_str());
	CHECK(labels) << "Unable to open labels file " << label_file;
	string line;
	while (std::getline(labels, line))
		labels_.push_back(string(line));

	Blob<float>* output_layer = net_->output_blobs()[0];
	CHECK_EQ(labels_.size(), output_layer->channels())
		<< "Number of labels is different from the output layer dimension.";
		*/
}
Exemplo n.º 3
0
void Classifier::init(const std::string& model_def,
        const std::string& trained_weights,
        const std::string& mean_file,
        const std::string& label_file,
        const int &gpu_id) {
    if(gpu_id>=0) {
        caffe::Caffe::set_mode(caffe::Caffe::GPU);
        caffe::Caffe::SetDevice(gpu_id);
    }
    else
        caffe::Caffe::set_mode(caffe::Caffe::CPU);

    /* Load the network. */
    net_.reset(new caffe::Net<float>(model_def, caffe::TEST));
    net_->CopyTrainedLayersFrom(trained_weights);

    CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input.";
    CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output.";

    caffe::Blob<float>* input_layer = net_->input_blobs()[0];
    num_channels_ = input_layer->channels();
    CHECK(num_channels_ == 3 || num_channels_ == 1)
        << "Input layer should have 1 or 3 channels.";
    input_geometry_ = cv::Size(input_layer->width(), input_layer->height());

    /* Load the binaryproto mean file. */
    if(mean_file!="")
        SetMean(mean_file);
    else {
        mean_ = cv::Mat::zeros(input_geometry_, CV_MAKE_TYPE(CV_32F, num_channels_));
    }

    /* Load labels. */
    caffe::Blob<float>* output_layer = net_->output_blobs()[0];
    if(label_file!="") {
        std::ifstream labels(label_file.c_str());
        CHECK(labels) << "Unable to open labels file " << label_file;
        std::string line;
        while (std::getline(labels, line))
            labels_.push_back(std::string(line));

        CHECK_EQ(labels_.size(), output_layer->channels())
            << "Number of labels is different from the output layer dimension.";
    }
    else {
        for (int i = 0; i < output_layer->channels(); ++i) {
            std::stringstream ss;
            ss << i;
            labels_.push_back(ss.str());
        }
    }
    is_ready = true;
}