Example #1
0
File: svmrbf.cpp Project: infnty/ys
    virtual void Classify(IDataSet* data) const {
        vector< vector<double> > features = PreprocessFeatures(data);
        svm_problem_xx test_prob(features, vector<int>(data->GetObjectCount(), 0));

        for (int i = 0; i < data->GetObjectCount(); i++)
            data->SetTarget(i, svm_predict(model->model, test_prob.x[i]));
    }
int DenseSegmentation::ProcessFrame(
    bool flush,
    const std::vector<cv::Mat>* features,
    const cv::Mat* flow,  // optional.
    std::vector<std::unique_ptr<SegmentationDesc>>* results) {
  if (seg_ == nullptr) {
    SegmentationOptions seg_options;
    GetSegmentationOptions(&seg_options);
    seg_.reset(new Segmentation(seg_options, frame_width_, frame_height_, chunk_id_));
    seg_->InitializeOverSegmentation(*this, options_.chunk_size);
  }

  LOG(INFO) << "Processing frame " << input_frames_;

  if (features) {
    // Parallel construction of the graph, buffer features and flow.
    vector<cv::Mat> processed_features(features->size());
    PreprocessFeatures(*features, &processed_features);
    feature_buffer_.push_back(processed_features);

    if (flow) {
      if (input_frames_ == 0) {
        DCHECK(flow->empty()) << "First frame's flow should be empty cv::Mat";
        flow_buffer_.push_back(cv::Mat());
      } else {
        DCHECK_EQ(frame_height_, flow->rows);
        DCHECK_EQ(frame_width_, flow->cols);
        cv::Mat flow_copy(flow->rows, flow->cols, CV_32FC2);
        // Deep copy, as we need to keep the memory around during parallel graph
        // construction.
        flow->copyTo(flow_copy);
        flow_buffer_.push_back(flow_copy);
        CHECK_EQ(flow_buffer_.size(), feature_buffer_.size())
          << "Flow always has to be passed or be absent.";
      }
    }

    AddFrameToSegmentationFromFeatures(feature_buffer_.back(), nullptr);

    if (feature_buffer_.size() > 1) {
      // Connect temporally.
      ConnectSegmentationTemporallyFromFeatures(
          feature_buffer_.end()[-1],
          feature_buffer_.end()[-2],
          flow_buffer_.empty() ? nullptr : &flow_buffer_.back());
    }
    ++input_frames_;
  }

  if (flush || feature_buffer_.size() - curr_chunk_start_ >= options_.chunk_size) {
    ChunkBoundaryOutput(flush, results);
    return results->size();
  }
  return 0;
}
Example #3
0
File: svmrbf.cpp Project: infnty/ys
    virtual void Learn(IDataSet* data) {
        vector< vector<double> > features = PreprocessFeatures(data);

        vector<int> targets;
        for (int i = 0; i < data->GetObjectCount(); i++)
            targets.push_back(data->GetTarget(i));

        train_prob = sh_ptr<svm_problem_xx>(new svm_problem_xx(features, targets));

        double c_values[] = { 2048, 32768 };
        double gamma_values[] = { 3.0517578125e-05, 0.0001220703125 };

        svm_parameter best_param;
        double best_accuracy = -1;

        // select hyperparameters (regularization coefficient C and kernel parameter gamma) by 10-fold crossvalidation
        for (int c_index = 0; c_index < ARRAY_SIZE(c_values); c_index++) {
            for (int gamma_index = 0; gamma_index < ARRAY_SIZE(gamma_values); gamma_index++) {
                svm_parameter param;
                memset(&param, 0, sizeof(param));
                param.svm_type = C_SVC;
                param.kernel_type = RBF;
                param.C = c_values[c_index];
                param.gamma = gamma_values[gamma_index];
                param.cache_size = 100;
                param.eps = 1e-3;
                param.shrinking = 1;

                vector<double> res(targets.size());
                svm_cross_validation(train_prob.get(), &param, 10, &res[0]);

                double accuracy = 0;
                for (int i = 0; i < res.size(); i++)
                    if (res[i] == targets[i])
                        accuracy += 1;
                accuracy /= res.size();

                if (accuracy > best_accuracy) {
                    best_accuracy = accuracy;
                    best_param = param;
                }
            }
        }

        model = sh_ptr<svm_model_xx>(new svm_model_xx(svm_train(train_prob.get(), &best_param)));
    }