int ObjMerger::chooseRectGroup(int num, CB_RectT *pt_rects, int idx) { int is_found = 0; for (int i=0; i<rect_group_num; i++) { for (int j=0; j<rect_groups[i].num; j++) { OverlapT overlap; computeOverlap(pt_rects[idx], rect_groups[i].rects[j], overlap); if (overlap.overlap.x < param.overlap_r_x || overlap.overlap.y < param.overlap_r_y) { continue; } int cur_idx = rect_groups[i].num; rect_groups[i].rects[cur_idx] = pt_rects[idx]; rect_groups[i].num++; is_found = 1; break; } } if (is_found != 0) { return 1; } rect_groups[rect_group_num].num = 1; rect_groups[rect_group_num].rects[0] = pt_rects[idx]; rect_groups[rect_group_num].is_valid = 1; rect_group_num++; return 0; }
vector<Detection> NonMaximumSuppression::extractOverlappingDetections(Detection detection, vector<Detection>& candidates) const { vector<Detection> overlappingDetections; auto firstOverlapping = std::stable_partition(candidates.begin(), candidates.end(), [&](const Detection& candidate) { return computeOverlap(detection.bounds, candidate.bounds) <= overlapThreshold; }); std::move(firstOverlapping, candidates.end(), std::back_inserter(overlappingDetections)); std::reverse(overlappingDetections.begin(), overlappingDetections.end()); candidates.erase(firstOverlapping, candidates.end()); return overlappingDetections; }
int main() { int test, cs, slen, plen; //freopen("in.txt", "r", stdin); fread_unlocked(buff, 1, 0x800000, stdin); ptr = buff; nextint(test); for(cs = 1; cs <= test; cs++) { nextstr(str,slen); nextstr(pattern,plen); if(plen > slen) { printf("Case %d: 0\n", cs); continue; } computeOverlap(plen); printf("Case %d: %d\n", cs, kmpMatcher(plen, slen)); } return 0; }
/** * Perform one time step of SpatialPooling for the current input in this Region. * The result will be a subset of Columns being set as active as well * as (proximal) synapses in all Columns having updated permanences and * boosts, and the Region will update inhibitionRadius. * * From the Numenta Docs: * Phase 1: compute the overlap with the current input for each column. * Given an input vector, the first phase calculates the overlap of each * column with that vector. The overlap for each column is simply the number * of connected synapses with active inputs, multiplied by its boost. If * this value is below minOverlap, we set the overlap score to zero. * * Phase 2: compute the winning columns after inhibition. * The second phase calculates which columns remain as winners after the * inhibition step. desiredLocalActivity is a parameter that controls the * number of columns that end up winning. For example, if desiredLocalActivity * is 10, a column will be a winner if its overlap score is greater than the * score of the 10'th highest column within its inhibition radius. * * Phase 3: update synapse permanence and internal variables. * The third phase performs learning; it updates the permanence values of all * synapses as necessary, as well as the boost and inhibition radius. * * The main learning rule is implemented in lines 20-26. For winning columns, * if a synapse is active, its permanence value is incremented, otherwise it * is decremented. Permanence values are constrained to be between 0 and 1. * * Lines 28-36 implement boosting. There are two separate boosting mechanisms * in place to help a column learn connections. If a column does not win often * enough (as measured by activeDutyCycle), its overall boost value is * increased (line 30-32). Alternatively, if a column's connected synapses * do not overlap well with any inputs often enough (as measured by * overlapDutyCycle), its permanence values are boosted (line 34-36). * Note: once learning is turned off, boost(c) is frozen. * Finally at the end of Phase 3 the inhibition radius is recomputed (line 38). */ void performSpatialPooling(Region* region) { int i; /*If hardcoded, we assume the input bits correspond directly to active columns*/ if(region->spatialHardcoded) { #pragma omp parallel for for(i=0; i<region->numCols; ++i) region->columns[i].isActive = region->inputData[i]==1; return; } /*First toggle isActive flag per input cell based on state of data array*/ #pragma omp parallel for for(i=0; i<region->nInput; ++i) region->inputCells[i].isActive = region->inputData[i]==1; /*Phase 1: Compute Column Input Overlaps*/ #pragma omp parallel for for(i=0; i<region->numCols; ++i) computeOverlap(&(region->columns[i])); /*Phase 2: Compute Active Columns (Winners after inhibition)*/ #pragma omp parallel for for(i=0; i<region->numCols; ++i) { Column* col = &(region->columns[i]); col->isActive = false; if(col->overlap > 0) { /*only activate column if its overlap is within kth largest of its neighbors*/ if(isWithinKthScore(region, col, region->desiredLocalActivity)) { col->isActive = true; /*Phase 3.1: Synapse learning occurs on active/winning columns*/ if(region->spatialLearning) updateColumnPermanences(col); } } /*Phase 3.2: Column Boosting (only if learning enabled)*/ if(region->spatialLearning) performBoosting(col); } /*Phase 3.3: Recalculate inhibition radius for columns*/ if(region->spatialLearning) region->inhibitionRadius = averageReceptiveFieldSize(region); }
int ObjMerger::processOverlapCluster() { int count = 0; for (int i=0; i<rect_group_num; i++) { if (rect_groups[i].is_valid == 0) { continue; } for (int j=i+1; j<rect_group_num; j++) { if (rect_groups[j].is_valid == 0) { continue; } OverlapT overlap; computeOverlap(rect_groups[i].rect, rect_groups[j].rect, overlap); int w1 = abs(rect_groups[i].rect.right - rect_groups[i].rect.left); int w2 = abs(rect_groups[j].rect.right - rect_groups[j].rect.left); if ( min(overlap.overlap1.x, overlap.overlap1.y) > 0.3 && max(overlap.overlap1.x, overlap.overlap1.y) > 0.6 && w2 > w1 * 1.8 ) { rect_groups[i].is_valid = 0; count++; } else if ( min(overlap.overlap2.x, overlap.overlap2.y) > 0.3 && max(overlap.overlap2.x, overlap.overlap2.y) > 0.6 && w1 > w2 * 1.8 ) { rect_groups[j].is_valid = 0; count++; } if (overlap.overlap.x < MAX_OBJ_OVERLAP_X || overlap.overlap.y < MAX_OBJ_OVERLAP_Y) { continue; } if (overlap.overlap.x > 0.8 && overlap.overlap.y > 0.8) { mergeTwoGroups(rect_groups[i], rect_groups[j]); count++; } else if (rect_groups[i].weight > rect_groups[j].weight) { rect_groups[j].is_valid = 0; count++; } else if (rect_groups[j].weight > rect_groups[i].weight) { rect_groups[i].is_valid = 0; count++; } else if (rect_groups[i].rect.bottom > rect_groups[j].rect.bottom) { rect_groups[j].is_valid = 0; count++; } else { rect_groups[i].is_valid = 0; count++; } } } return count; }
void variables( TString fin = "TMVA.root", TString dirName = "InputVariables_Id", TString title = "TMVA Input Variables", Bool_t isRegression = kFALSE, Bool_t useTMVAStyle = kTRUE ) { TString outfname = dirName; outfname.ToLower(); outfname.ReplaceAll( "input", "" ); // set style and remove existing canvas' TMVAGlob::Initialize( useTMVAStyle ); // obtain shorter histogram title TString htitle = title; htitle.ReplaceAll("variables ","variable"); htitle.ReplaceAll("and target(s)",""); htitle.ReplaceAll("(training sample)",""); // checks if file with name "fin" is already open, and if not opens one TFile* file = TMVAGlob::OpenFile( fin ); TDirectory* dir = (TDirectory*)file->Get( dirName ); if (dir==0) { cout << "No information about " << title << " available in directory " << dirName << " of file " << fin << endl; return; } dir->cd(); // how many plots are in the directory? Int_t noPlots = TMVAGlob::GetNumberOfInputVariables( dir ) + TMVAGlob::GetNumberOfTargets( dir ); // define Canvas layout here! // default setting Int_t xPad; // no of plots in x Int_t yPad; // no of plots in y Int_t width; // size of canvas Int_t height; switch (noPlots) { case 1: xPad = 1; yPad = 1; width = 550; height = 0.90*width; break; case 2: xPad = 2; yPad = 1; width = 600; height = 0.50*width; break; case 3: xPad = 3; yPad = 1; width = 900; height = 0.4*width; break; case 4: xPad = 2; yPad = 2; width = 600; height = width; break; default: xPad = 3; yPad = 2; width = 800; height = 0.55*width; break; } Int_t noPadPerCanv = xPad * yPad ; // counter variables Int_t countCanvas = 0; Int_t countPad = 0; // loop over all objects in directory TCanvas* canv = 0; TKey* key = 0; Bool_t createNewFig = kFALSE; TIter next(dir->GetListOfKeys()); while ((key = (TKey*)next())) { if (key->GetCycle() != 1) continue; if (!TString(key->GetName()).Contains("__Signal") && !(isRegression && TString(key->GetName()).Contains("__Regression"))) continue; // make sure, that we only look at histograms TClass *cl = gROOT->GetClass(key->GetClassName()); if (!cl->InheritsFrom("TH1")) continue; TH1 *sig = (TH1*)key->ReadObj(); TString hname(sig->GetName()); // create new canvas if (countPad%noPadPerCanv==0) { ++countCanvas; canv = new TCanvas( Form("canvas%d", countCanvas), title, countCanvas*50+50, countCanvas*20, width, height ); canv->Divide(xPad,yPad); canv->Draw(); } TPad* cPad = (TPad*)canv->cd(countPad++%noPadPerCanv+1); // find the corredponding backgrouns histo TString bgname = hname; bgname.ReplaceAll("__Signal","__Background"); TH1 *bgd = (TH1*)dir->Get(bgname); if (bgd == NULL) { cout << "ERROR!!! couldn't find background histo for" << hname << endl; exit; } // this is set but not stored during plot creation in MVA_Factory TMVAGlob::SetSignalAndBackgroundStyle( sig, (isRegression ? 0 : bgd) ); sig->SetTitle( TString( htitle ) + ": " + sig->GetTitle() ); TMVAGlob::SetFrameStyle( sig, 1.2 ); // normalise both signal and background if (!isRegression) TMVAGlob::NormalizeHists( sig, bgd ); else { // change histogram title for target TString nme = sig->GetName(); if (nme.Contains( "_target" )) { TString tit = sig->GetTitle(); sig->SetTitle( tit.ReplaceAll("Input variable", "Regression target" ) ); } } // finally plot and overlay Float_t sc = 1.1; if (countPad == 1) sc = 1.3; sig->SetMaximum( TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*sc ); sig->Draw( "hist" ); cPad->SetLeftMargin( 0.17 ); sig->GetYaxis()->SetTitleOffset( 1.70 ); if (!isRegression) { bgd->Draw("histsame"); TString ytit = TString("(1/N) ") + sig->GetYaxis()->GetTitle(); sig->GetYaxis()->SetTitle( ytit ); // histograms are normalised } TLatex *text = new TLatex(); text->SetNDC(); text->SetTextSize(0.06); float overlap = computeOverlap(sig,bgd); text->DrawLatex(0.8,0.8,Form("%.2f",overlap)); // Draw legend if (countPad == 1 && !isRegression) { TLegend *legend= new TLegend( cPad->GetLeftMargin(), 1-cPad->GetTopMargin()-.15, cPad->GetLeftMargin()+.4, 1-cPad->GetTopMargin() ); legend->SetFillStyle(1); legend->AddEntry(sig,"Signal","F"); legend->AddEntry(bgd,"Background","F"); legend->SetBorderSize(1); legend->SetMargin( 0.3 ); legend->Draw("same"); } // redraw axes sig->Draw("sameaxis"); // text for overflows Int_t nbin = sig->GetNbinsX(); Double_t dxu = sig->GetBinWidth(0); Double_t dxo = sig->GetBinWidth(nbin+1); TString uoflow = ""; if (isRegression) { uoflow = Form( "U/O-flow: %.1f%% / %.1f%%", sig->GetBinContent(0)*dxu*100, sig->GetBinContent(nbin+1)*dxo*100 ); } else { uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)%% / (%.1f, %.1f)%%", sig->GetBinContent(0)*dxu*100, bgd->GetBinContent(0)*dxu*100, sig->GetBinContent(nbin+1)*dxo*100, bgd->GetBinContent(nbin+1)*dxo*100 ); } TText* t = new TText( 0.98, 0.14, uoflow ); t->SetNDC(); t->SetTextSize( 0.040 ); t->SetTextAngle( 90 ); t->AppendPad(); // save canvas to file if (countPad%noPadPerCanv==0) { TString fname = Form( "plots/%s_c%i", outfname.Data(), countCanvas ); TMVAGlob::plot_logo(); TMVAGlob::imgconv( canv, fname ); createNewFig = kFALSE; } else { createNewFig = kTRUE; } } if (createNewFig) { TString fname = Form( "plots/%s_c%i", outfname.Data(), countCanvas ); TMVAGlob::plot_logo(); TMVAGlob::imgconv( canv, fname ); createNewFig = kFALSE; } return; }