// Training SVM with feature vector X and label Y. // Each row of X is a feature vector, with corresponding label in Y. // Return a CV_32F weight Mat Mat Objectness::trainSVM(CMat &X1f, const vecI &Y, int sT, double C, double bias, double eps) { // Set SVM parameters parameter param; { param.solver_type = sT; // L2R_L2LOSS_SVC_DUAL; param.C = C; param.eps = eps; // see setting below param.p = 0.1; param.nr_weight = 0; param.weight_label = NULL; param.weight = NULL; set_print_string_function(print_null); CV_Assert(X1f.rows == Y.size() && X1f.type() == CV_32F); } // Initialize a problem feature_node *x_space = NULL; problem prob;{ prob.l = X1f.rows; prob.bias = bias; prob.y = Malloc(double, prob.l); prob.x = Malloc(feature_node*, prob.l); const int DIM_FEA = X1f.cols; prob.n = DIM_FEA + (bias >= 0 ? 1 : 0); x_space = Malloc(feature_node, (prob.n + 1) * prob.l); int j = 0; for (int i = 0; i < prob.l; i++){ prob.y[i] = Y[i]; prob.x[i] = &x_space[j]; const float* xData = X1f.ptr<float>(i); for (int k = 0; k < DIM_FEA; k++){ x_space[j].index = k + 1; x_space[j++].value = xData[k]; } if (bias >= 0){ x_space[j].index = prob.n; x_space[j++].value = bias; } x_space[j++].index = -1; } CV_Assert(j == (prob.n + 1) * prob.l); } // Training SVM for current problem const char* error_msg = check_parameter(&prob, ¶m); if(error_msg){ fprintf(stderr,"ERROR: %s\n",error_msg); exit(1); } model *svmModel = train(&prob, ¶m); Mat wMat(1, prob.n, CV_64F, svmModel->w); wMat.convertTo(wMat, CV_32F); free_and_destroy_model(&svmModel); destroy_param(¶m); free(prob.y); free(prob.x); free(x_space); return wMat; }
bool CIntersectionAssistanceUnit::GetHighestScoringLastKnownGoodPosition( const QuatT& baseOrientation, QuatT& outQuat ) const { bool bFlippedIsBest = false; if(!m_lastKnownGoodPositions.empty()) { // Higher is better float fBestScore = 0.0f; int bestIndex = -1; Vec3 vBaseUpDir = baseOrientation.q.GetColumn2().GetNormalized(); for(uint8 i = 0; i < m_lastKnownGoodPositions.size(); ++i) { const QuatT& qLastKnownGood = m_lastKnownGoodPositions[i]; if(IsPositionWithinAcceptedLimits(qLastKnownGood.t, baseOrientation.t, kDistanceTolerance)) { // Generate [0.0f,1.0f] score for distance const Vec3 distVec = (qLastKnownGood.t - baseOrientation.t); const float length = max(distVec.GetLengthFast(),0.0001f); const float distanceScore = max(1.0f - (length * kInverseDistanceTolerance) * kDistanceWeight, 0.0f); Vec3 vUpDir = qLastKnownGood.q.GetColumn2(); const float regularOrientationScore = vBaseUpDir.Dot(vUpDir); const float flippedOrientationScore = vBaseUpDir.Dot(-vUpDir); float orientationScore = max(regularOrientationScore, flippedOrientationScore); orientationScore *= kOrientationWeight; const float fCandidateScore = distanceScore + orientationScore; #ifndef _RELEASE if(g_pGameCVars->pl_pickAndThrow.intersectionAssistDebugEnabled == 2) { CryWatch("[INDEX(%d)] : D[%.3f] O[%.3f] T[%.3f] (%s)", i, distanceScore, orientationScore, fCandidateScore, flippedOrientationScore > regularOrientationScore ? "*F*" : "R"); } #endif //#ifndef _RELEASE if(fCandidateScore > fBestScore) { bestIndex = i; fBestScore = fCandidateScore; bFlippedIsBest = (flippedOrientationScore > regularOrientationScore); } } } if(bestIndex >= 0) { outQuat = m_lastKnownGoodPositions[bestIndex]; if(bFlippedIsBest) { Matrix34 wMat(outQuat); Vec3 vFlippedUpDir = -outQuat.q.GetColumn2().GetNormalized(); Vec3 vForwardDir = outQuat.q.GetColumn1().GetNormalized(); Vec3 vSideDir = -outQuat.q.GetColumn0().GetNormalized(); Matrix34 wFlippedMat; wFlippedMat = Matrix34::CreateFromVectors(vSideDir, vForwardDir, vFlippedUpDir, wMat.GetTranslation()); outQuat = QuatT(wFlippedMat); // Adjust pos (rotating around OOBB centre effectively) const IEntity* pSubjectEntity = gEnv->pEntitySystem->GetEntity(m_subjectEntityId); if(pSubjectEntity) { AABB entAABB; OBB entOBB; pSubjectEntity->GetLocalBounds(entAABB); entOBB.SetOBBfromAABB(Quat(IDENTITY), entAABB); Vec3 Centre = wMat.TransformPoint(entOBB.c); Vec3 toCentre = Centre - outQuat.t; outQuat.t += (toCentre * 2.0f); } } #ifndef _RELEASE if(g_pGameCVars->pl_pickAndThrow.intersectionAssistDebugEnabled == 2) { m_currentBestIndex = bestIndex; CryWatch("[BEST INDEX] : %d", bestIndex); } #endif // ifndef _RELEASE return true; } } #ifndef _RELEASE m_currentBestIndex = -1; #endif // ifndef _RELEASE return false; }
void Objectness::trainStateII(int numPerSz) { loadTrainedModel(); const int NUM_TRAIN = _voc.trainNum; vector<vecI> SZ(NUM_TRAIN), Y(NUM_TRAIN); vector<vecF> VAL(NUM_TRAIN); #pragma omp parallel for for (int i = 0; i < _voc.trainNum; i++) { const vector<Vec4i> &bbgts = _voc.gtTrainBoxes[i]; ValStructVec<float, Vec4i> valBoxes; vecI &sz = SZ[i], &y = Y[i]; vecF &val = VAL[i]; CStr imgPath = format(_S(_voc.imgPathW), _S(_voc.trainSet[i])); predictBBoxSI(imread(imgPath), valBoxes, sz, numPerSz, false); const int num = valBoxes.size(); CV_Assert(sz.size() == num); y.resize(num), val.resize(num); for (int j = 0; j < num; j++) { Vec4i bb = valBoxes[j]; val[j] = valBoxes(j); y[j] = maxIntUnion(bb, bbgts) >= 0.5 ? 1 : -1; } } const int NUM_SZ = _svmSzIdxs.size(); const int maxTrainNum = 100000; vector<vecM> rXP(NUM_SZ), rXN(NUM_SZ); for (int r = 0; r < NUM_SZ; r++) { rXP[r].reserve(maxTrainNum); rXN[r].reserve(1000000); } for (int i = 0; i < NUM_TRAIN; i++) { const vecI &sz = SZ[i], &y = Y[i]; vecF &val = VAL[i]; int num = sz.size(); for (int j = 0; j < num; j++) { int r = sz[j]; CV_Assert(r >= 0 && r < NUM_SZ); if (y[j] == 1) rXP[r].push_back(Mat(1, 1, CV_32F, &val[j])); else rXN[r].push_back(Mat(1, 1, CV_32F, &val[j])); } } Mat wMat(NUM_SZ, 2, CV_32F); for (int i = 0; i < NUM_SZ; i++) { const vecM &xP = rXP[i], &xN = rXN[i]; if (xP.size() < 10 || xN.size() < 10) printf("Warning %s:%d not enough training sample for r[%d] = %d. P = %d, N = %d\n", __FILE__, __LINE__, i, _svmSzIdxs[i], xP.size(), xN.size()); for (size_t k = 0; k < xP.size(); k++) CV_Assert(xP[k].size() == Size(1, 1) && xP[k].type() == CV_32F); Mat wr = trainSVM(xP, xN, L1R_L2LOSS_SVC, 100, 1); CV_Assert(wr.size() == Size(2, 1)); wr.copyTo(wMat.row(i)); } matWrite(_modelName + ".wS2", wMat); _svmReW1f = wMat; }