Esempio n. 1
0
void Objectness::trainStageI()
{
    vecM pX, nX;
    pX.reserve(200000), nX.reserve(200000);
    Mat xP1f, xN1f;
    CV_Assert(matRead(_modelName + ".xP", xP1f) && matRead(_modelName + ".xN", xN1f));
    for (int r = 0; r < xP1f.rows; r++)
        pX.push_back(xP1f.row(r));
    for (int r = 0; r < xN1f.rows; r++)
        nX.push_back(xN1f.row(r));
    Mat crntW = trainSVM(pX, nX, L1R_L2LOSS_SVC, 10, 1);
    crntW = crntW.colRange(0, crntW.cols - 1).reshape(1, _W);
    CV_Assert(crntW.size() == Size(_W, _W));
    matWrite(_modelName + ".wS1", crntW);
}
Esempio n. 2
0
void Objectness::generateTrianData()
{
    const int NUM_TRAIN = _voc.trainNum;
    const int FILTER_SZ = _W*_W;
    vector<vector<Mat> > xTrainP(NUM_TRAIN), xTrainN(NUM_TRAIN);
    vector<vecI> szTrainP(NUM_TRAIN); // Corresponding size index.
    const int NUM_NEG_BOX = 100; // Number of negative windows sampled from each image

    #pragma omp parallel for
    for (int i = 0; i < NUM_TRAIN; i++)	{
        const int NUM_GT_BOX = (int)_voc.gtTrainBoxes[i].size();
        vector<Mat> &xP = xTrainP[i], &xN = xTrainN[i];
        vecI &szP = szTrainP[i];
        xP.reserve(NUM_GT_BOX*4), szP.reserve(NUM_GT_BOX*4), xN.reserve(NUM_NEG_BOX);
        Mat im3u = imread(format(_S(_voc.imgPathW), _S(_voc.trainSet[i])));

        // Get positive training data
        for (int k = 0; k < NUM_GT_BOX; k++) {
            const Vec4i& bbgt =  _voc.gtTrainBoxes[i][k];
            vector<Vec4i> bbs; // bounding boxes;
            vecI bbR; // Bounding box ratios
            int nS = gtBndBoxSampling(bbgt, bbs, bbR);
            for (int j = 0; j < nS; j++) {
                bbs[j][2] = min(bbs[j][2], im3u.cols);
                bbs[j][3] = min(bbs[j][3], im3u.rows);
                Mat mag1f = getFeature(im3u, bbs[j]), magF1f;
                flip(mag1f, magF1f, CV_FLIP_HORIZONTAL);
                xP.push_back(mag1f);
                xP.push_back(magF1f);
                szP.push_back(bbR[j]);
                szP.push_back(bbR[j]);
            }
        }
        // Get negative training data
        for (int k = 0; k < NUM_NEG_BOX; k++) {
            int x1 = rand() % im3u.cols + 1, x2 = rand() % im3u.cols + 1;
            int y1 = rand() % im3u.rows + 1, y2 = rand() % im3u.rows + 1;
            Vec4i bb(min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2));
            if (maxIntUnion(bb, _voc.gtTrainBoxes[i]) < 0.5)
                xN.push_back(getFeature(im3u, bb));
        }
    }

    const int NUM_R = _numT * _numT + 1;
    vecI szCount(NUM_R); // Object counts of each size (combination of scale and aspect ratio)
    int numP = 0, numN = 0, iP = 0, iN = 0;
    for (int i = 0; i < NUM_TRAIN; i++) {
        numP += xTrainP[i].size();
        numN += xTrainN[i].size();
        const vecI &rP = szTrainP[i];
        for (size_t j = 0; j < rP.size(); j++)
            szCount[rP[j]]++;
    }
    vecI szActive; // Indexes of active size
    for (int r = 1; r < NUM_R; r++) {
        if (szCount[r] > 50) // If only 50- positive samples at this size, ignore it.
            szActive.push_back(r-1);
    }
    matWrite(_modelName + ".idx", Mat(szActive));

    Mat xP1f(numP, FILTER_SZ, CV_32F), xN1f(numN, FILTER_SZ, CV_32F);
    for (int i = 0; i < NUM_TRAIN; i++)	{
        vector<Mat> &xP = xTrainP[i], &xN = xTrainN[i];
        for (size_t j = 0; j < xP.size(); j++)
            memcpy(xP1f.ptr(iP++), xP[j].data, FILTER_SZ*sizeof(float));
        for (size_t j = 0; j < xN.size(); j++)
            memcpy(xN1f.ptr(iN++), xN[j].data, FILTER_SZ*sizeof(float));
    }
    CV_Assert(numP == iP && numN == iN);
    matWrite(_modelName + ".xP", xP1f);
    matWrite(_modelName + ".xN", xN1f);
}
Esempio n. 3
0
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;
}
Esempio n. 4
0
int main(int argc, char **argv)
{
  char *inputFilename = NULL;
  int inputFiles = 0;
  char *matFilename = NULL;
  busAssignment_t *busAssignment = busAssignment_create();
  int bus = -1;
  signalFormat_t signalFormat = signalFormat_Name;
  measurement_t *measurement;
  int ret = 1;
  sint32 timeResolution = 10000;
  parserFunction_t parserFunction = NULL;

  program_name = argv[0];

  /* parse arguments */
  while (1) {
    static struct option long_options[] = {
      /* These options set a flag. */
      {"verbose", no_argument,       &verbose_flag, 1},
      {"brief",   no_argument,       &verbose_flag, 0},
      {"debug",   no_argument,       &debug_flag,   1},
      /* These options don't set a flag.
         We distinguish them by their indices. */
      {"asc",     required_argument, 0, 'a'},
      {"bus",     required_argument, 0, 'b'},
#ifdef HAVE_CLGREADER_H
      {"clg",     required_argument, 0, 'c'},
#endif
      {"dbc",     required_argument, 0, 'd'},
      {"format",  required_argument, 0, 'f'},
      {"mat",     required_argument, 0, 'm'},
      {"timeres", required_argument, 0, 't'},
      {"vsb",     required_argument, 0, 'v'},
      {"help",    no_argument,       NULL, 'h'},
      {0, 0, 0, 0}
    };

    /* getopt_long stores the option index here. */
    int option_index = 0;
    int c;

    c = getopt_long (argc, argv, "a:b:c:d:f:m:t:v:",
                     long_options, &option_index);

    /* Detect the end of the options. */
    if (c == -1) break;

    switch (c) {
    case 0:
      break;
    case 'a':
      inputFilename = optarg;
      parserFunction =ascReader_processFile;
      inputFiles++;
      break;
#ifdef HAVE_CLGREADER_H
    case 'c':
      inputFilename = optarg;
      parserFunction =clgReader_processFile;
      inputFiles++;
      break;
#endif
    case 'b':
      bus = atoi(optarg);
      break;
    case 'd':
      if(verbose_flag) {
        if(bus == -1) {
          fprintf(stderr, "Assigning DBC file %s to all busses\n", optarg);
        } else {
          fprintf(stderr, "Assigning DBC file %s to bus %d\n", optarg, bus);
        }
      }
      busAssignment_associate(busAssignment, bus, optarg);

      /* reset bus specification */
      bus = -1;
      break;
    case 'm':
      matFilename = optarg;
      break;
    case 'f':
      if(!strcmp(optarg, "n")) {
        signalFormat =  signalFormat_Name;
      } else if(!strcmp(optarg, "mn")) {
        signalFormat =  signalFormat_Message
                     |  signalFormat_Name;
      } else if(!strcmp(optarg, "dmn")) {
        signalFormat =  signalFormat_Database
                     |  signalFormat_Message
                     |  signalFormat_Name;
      } else {
        fprintf(stderr, "error: format must be 's', 'ms', or 'dms'\n");
        usage_error();
      }
      matFilename = optarg;
      break;
    case 't':
      timeResolution = atoi(optarg);
      break;
    case 'v':
      inputFilename = optarg;
      parserFunction = vsbReader_processFile;
      inputFiles++;
      break;
    case 'h': help(); exit(0);   break;
    case '?':
      /* getopt_long already printed an error message. */
      usage_error();
      break;
    default:
      fprintf(stderr, "error: unknown option %c\n", c);
      busAssignment_free(busAssignment);
      usage_error();
    }
  }

#ifdef YYDEBUG
  if(debug_flag) {
    extern int yydebug;
    yydebug=1;
  }
#endif

  /* diagnose options */
  if(inputFiles != 1) {
    fprintf(stderr, "error: please specify exactly one input file\n");
    busAssignment_free(busAssignment);
    usage_error();
  }

  if(matFilename == NULL) {
    fprintf(stderr, "error: MAT output filename not specified\n");
    busAssignment_free(busAssignment);
    usage_error();
  }
  
  /* parse DBC files */
  busAssignment_parseDBC(busAssignment);
  
  /* parse input file */
  if(verbose_flag) {
    if(inputFilename != NULL) {
      fprintf(stderr,
	      "Parsing input file %s\n",
	      inputFilename?inputFilename:"<stdin>");
    }
  }
  measurement = measurement_read(busAssignment,
				 inputFilename,
				 signalFormat,
				 timeResolution,
				 parserFunction);
  if(measurement != NULL) {

    /* write MAT file */
    if(verbose_flag) {
      fprintf(stderr, "Writing MAT file %s\n", matFilename);
    }
    matWrite(measurement, matFilename);

    /* free memory */
    measurement_free(measurement);
  }
  ret = 0;

usage_error:  
  busAssignment_free(busAssignment);
  return ret;
}