CByteImage HOGFeatureExtractor::render(const Feature& f) const { CShape cellShape = _oriMarkers[0].Shape(); CFloatImage hogImgF(CShape(cellShape.width * f.Shape().width, cellShape.height * f.Shape().height, 1)); hogImgF.ClearPixels(); float minBinValue, maxBinValue; f.getRangeOfValues(minBinValue, maxBinValue); // For every cell in the HOG for(int hi = 0; hi < f.Shape().height; hi++) { for(int hj = 0; hj < f.Shape().width; hj++) { // Now _oriMarkers, multiplying contribution by bin level for(int hc = 0; hc < _nAngularBins; hc++) { float v = f.Pixel(hj, hi, hc) / maxBinValue; for(int ci = 0; ci < cellShape.height; ci++) { float* cellIt = (float*) _oriMarkers[hc].PixelAddress(0, ci, 0); float* hogIt = (float*) hogImgF.PixelAddress(hj * cellShape.height, hi * cellShape.height + ci, 0); for(int cj = 0; cj < cellShape.width; cj++, hogIt++, cellIt++) { (*hogIt) += v * (*cellIt); } } } } } CByteImage hogImg; TypeConvert(hogImgF, hogImg); return hogImg; }
void SupportVectorMachine::train(const std::vector<float>& labels, const FeatureSet& fset, double C) { if(labels.size() != fset.size()) throw std::runtime_error("Database size is different from feature set size!"); _fVecShape = fset[0].Shape(); // Figure out size and number of feature vectors int nVecs = labels.size(); CShape shape = fset[0].Shape(); int dim = shape.width * shape.height * shape.nBands; // Parameters for SVM svm_parameter parameter; parameter.svm_type = C_SVC; parameter.kernel_type = LINEAR; parameter.degree = 0; parameter.gamma = 0; parameter.coef0 = 0; parameter.nu = 0.5; parameter.cache_size = 100; // In MB parameter.C = C; parameter.eps = 1e-3; parameter.p = 0.1; parameter.shrinking = 1; parameter.probability = 0; parameter.nr_weight = 0; // ? parameter.weight_label = NULL; parameter.weight = NULL; //cross_validation = 0; // Allocate memory svm_problem problem; problem.l = nVecs; problem.y = new double[nVecs]; problem.x = new svm_node*[nVecs]; if(_data) delete [] _data; /******** BEGIN TODO ********/ // Copy the data used for training the SVM into the libsvm data structures "problem". // Put the feature vectors in _data and labels in problem.y. Also, problem.x[k] // should point to the address in _data where the k-th feature vector starts (i.e., // problem.x[k] = &_data[starting index of k-th feature]) // // Hint: // * Don't forget to set _data[].index to the corresponding dimension in // the original feature vector. You also need to set _data[].index to -1 // right after the last element of each feature vector // Vector containing all feature vectors. svm_node is a struct with // two fields, index and value. Index entry indicates position // in feature vector while value is the value in the original feature vector, // each feature vector of size k takes up k+1 svm_node's in _data // the last one being simply to indicate that the feature has ended by setting the index // entry to -1 _data = new svm_node[nVecs * (dim + 1)]; int j = 0; for(int i=0; i<nVecs; i++){ Feature feat = fset.at(i); int index=0; problem.x[i] = &_data[j]; problem.y[i] = labels.at(i); for (int y=0; y<feat.Shape().height; y++){ for (int x=0; x<feat.Shape().width; x++){ for(int b=0; b<feat.Shape().nBands; b++){ _data[j].index = index; _data[j].value = feat.Pixel(x,y,b); j++; index++; } } } _data[j++].index = -1; } //printf("TODO: SupportVectorMachine.cpp:87\n"); exit(EXIT_FAILURE); /******** END TODO ********/ // Train the model if(_model != NULL) svm_free_and_destroy_model(&_model); _model = svm_train(&problem, ¶meter); // Cleanup delete [] problem.y; delete [] problem.x; }