int NumSequenceMatches(const TransformationParameter transform_param,
                           const Datum& datum, Phase phase) {
        // Get crop sequence with Caffe seed 1701.
        DataTransformer<Dtype> transformer(transform_param, phase);
        const int crop_size = transform_param.crop_size();
        Caffe::set_random_seed(seed_);
        transformer.InitRand();
        Blob<Dtype> blob(1, datum.channels(), datum.height(), datum.width());
        if (transform_param.crop_size() > 0) {
            blob.Reshape(1, datum.channels(), crop_size, crop_size);
        }

        vector<vector<Dtype> > crop_sequence;
        for (int iter = 0; iter < this->num_iter_; ++iter) {
            vector<Dtype> iter_crop_sequence;
            transformer.Transform(datum, &blob);
            for (int j = 0; j < blob.count(); ++j) {
                iter_crop_sequence.push_back(blob.cpu_data()[j]);
            }
            crop_sequence.push_back(iter_crop_sequence);
        }
        // Check if the sequence differs from the previous
        int num_sequence_matches = 0;
        for (int iter = 0; iter < this->num_iter_; ++iter) {
            vector<Dtype> iter_crop_sequence = crop_sequence[iter];
            transformer.Transform(datum, &blob);
            for (int j = 0; j < blob.count(); ++j) {
                num_sequence_matches += (crop_sequence[iter][j] == blob.cpu_data()[j]);
            }
        }
        return num_sequence_matches;
    }
示例#2
0
DataTransformer<Dtype>::DataTransformer(const TransformationParameter& param,
    Phase phase)
    : param_(param), phase_(phase) {
  // check if we want to use mean_file
  if (param_.has_mean_file()) {
    CHECK_EQ(param_.mean_value_size(), 0) <<
      "Cannot specify mean_file and mean_value at the same time";
    const string& mean_file = param.mean_file();
    if (Caffe::root_solver()) {
      LOG(INFO) << "Loading mean file from: " << mean_file;
    }
    if ( mean_file.substr(mean_file.length()-3,3) == string("jpg") || mean_file.substr(mean_file.length()-3,3) == string("bmp") ) {
        cv::Mat mean_img;
        if ( param.mean_color() ) {
             mean_img = cv::imread(mean_file.c_str(), CV_LOAD_IMAGE_COLOR);
             data_mean_.Reshape(1, mean_img.channels(), mean_img.rows, mean_img.cols);
            int mean_data_index = 0;
            for ( int k = 0; k < 3; ++k )
                for ( int r = 0; r < mean_img.rows; ++r )
                    for ( int c = 0; c < mean_img.cols; ++c )
                        data_mean_.mutable_cpu_data()[mean_data_index++] = mean_img.at<cv::Vec3b>(r,c)[k];
        } else { 
             mean_img = cv::imread(mean_file.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
             data_mean_.Reshape(1, mean_img.channels(), mean_img.rows, mean_img.cols);
            int mean_data_index = 0;
                for ( int r = 0; r < mean_img.rows; ++r )
                    for ( int c = 0; c < mean_img.cols; ++c )
                        data_mean_.mutable_cpu_data()[mean_data_index++] = mean_img.at<unsigned char>(r,c);
        }
    } else {
        BlobProto blob_proto;
        ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);
        data_mean_.FromProto(blob_proto);
    }
  }
  offset_w_ = 0;
  offset_h_ = 0;
  apply_mirror_ = false;
  // check if we want to use mean_value
  if (param_.mean_value_size() > 0) {
    CHECK(param_.has_mean_file() == false) <<
      "Cannot specify mean_file and mean_value at the same time";
    for (int c = 0; c < param_.mean_value_size(); ++c) {
      mean_values_.push_back(param_.mean_value(c));
    }
  }
}
DataTransformer<Dtype>::DataTransformer(const TransformationParameter& param,
    Phase phase)
    : param_(param), phase_(phase) {
  // check if we want to use mean_file
  if (param_.has_mean_file()) {
    CHECK_EQ(param_.mean_value_size(), 0) <<
      "Cannot specify mean_file and mean_value at the same time";
    const string& mean_file = param.mean_file();
    LOG(INFO) << "Loading mean file from: " << mean_file;
    BlobProto blob_proto;
    ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);
    data_mean_.FromProto(blob_proto);
  }
  // check if we want to use mean_value
  if (param_.mean_value_size() > 0) {
    CHECK(param_.has_mean_file() == false) <<
      "Cannot specify mean_file and mean_value at the same time";
    for (int c = 0; c < param_.mean_value_size(); ++c) {
      mean_values_.push_back(param_.mean_value(c));
    }
  }
}
CaffeFeatExtractor<Dtype>::CaffeFeatExtractor(string _caffemodel_file,
        string _prototxt_file, int _resizeWidth, int _resizeHeight,
        string _blob_names,
        string _compute_mode,
        int _device_id,
        bool _timing) {

    // Setup the GPU or the CPU mode for Caffe
    if (strcmp(_compute_mode.c_str(), "GPU") == 0 || strcmp(_compute_mode.c_str(), "gpu") == 0) {

        std::cout << "CaffeFeatExtractor::CaffeFeatExtractor(): using GPU" << std::endl;

        gpu_mode = true;
        device_id = _device_id;

        Caffe::CheckDevice(device_id);

        std::cout << "CaffeFeatExtractor::CaffeFeatExtractor(): using device_id = " << device_id << std::endl;

        Caffe::set_mode(Caffe::GPU);
        Caffe::SetDevice(device_id);

        // Optional: to check that the GPU is working properly...
        Caffe::DeviceQuery();

    } else
    {
        std::cout << "CaffeFeatExtractor::CaffeFeatExtractor(): using CPU" << std::endl;

        gpu_mode = false;
        device_id = -1;

        Caffe::set_mode(Caffe::CPU);
    }

    // Assign specified .caffemodel and .prototxt files
    caffemodel_file = _caffemodel_file;
    prototxt_file = _prototxt_file;

    // Network creation using the specified .prototxt
    feature_extraction_net = boost::make_shared<Net<Dtype> > (prototxt_file, caffe::TEST);

    // Network initialization using the specified .caffemodel
    feature_extraction_net->CopyTrainedLayersFrom(caffemodel_file);

    // Mean image initialization

    mean_width = 0;
    mean_height = 0;
    mean_channels = 0;

    caffe::shared_ptr<MemoryDataLayer<Dtype> > memory_data_layer = boost::dynamic_pointer_cast<caffe::MemoryDataLayer<Dtype> >(feature_extraction_net->layers()[0]);

    TransformationParameter tp = memory_data_layer->layer_param().transform_param();

    if (tp.has_mean_file())
    {
        const string& mean_file = tp.mean_file();
        std::cout << "CaffeFeatExtractor::CaffeFeatExtractor(): loading mean file from " << mean_file << std::endl;

        BlobProto blob_proto;
        ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);

        Blob<Dtype> data_mean;
        data_mean.FromProto(blob_proto);

        mean_channels = data_mean.channels();
        mean_width = data_mean.width();
        mean_height = data_mean.height();

    } else if (tp.mean_value_size()>0)
    {

        const int b = tp.mean_value(0);
        const int g = tp.mean_value(1);
        const int r = tp.mean_value(2);

        mean_channels = tp.mean_value_size();
        mean_width = _resizeWidth;
        mean_height = _resizeHeight;

        std::cout << "CaffeFeatExtractor::CaffeFeatExtractor(): B " << b << "   G " << g << "   R " << r << std::endl;
        std::cout << "CaffeFeatExtractor::CaffeFeatExtractor(): resizing anysotropically to " << " W: " << mean_width << " H: " << mean_height << std::endl;
    }
    else
    {
        std::cout << "CaffeFeatExtractor::CaffeFeatExtractor(): Error: neither mean file nor mean value in prototxt!" << std::endl;
    }

    // Check that requested blobs exist
    boost::split(blob_names, _blob_names, boost::is_any_of(","));
    for (size_t i = 0; i < blob_names.size(); i++) {
        if (!feature_extraction_net->has_blob(blob_names[i]))
        {
            std::cout << "CaffeFeatExtractor::CaffeFeatExtractor(): unknown feature blob name " << blob_names[i] << " in the network " << prototxt_file << std::endl;
        }
    }

    // Initialize timing flag
    timing = _timing;

}