示例#1
0
void CosSimVecMatLayer::forward(PassType passType) {
  Layer::forward(passType);
  CHECK_EQ(forward_.size(), 1UL) << "Only one forward function needed";

  MatrixPtr inV0 = getInputValue(0);
  MatrixPtr inV1 = getInputValue(1);

  size_t batchSize = inV0->getHeight();
  size_t numKeys = getSize();

  CHECK_EQ(batchSize, inV1->getHeight());

  {
    REGISTER_TIMER_INFO("FwResetTimer", getName().c_str());
    reserveOutput(batchSize, numKeys);
  }

  MatrixPtr outV = getOutputValue();
  CHECK(outV && inV0 && inV1);
  REGISTER_TIMER_INFO("FwCosVMTimer", getName().c_str());
  for (size_t i = 0; i < batchSize; i++) {
    tmpRow0->setData(inV0->rowBuf(i));
    tmpMtx0->setData(inV1->rowBuf(i));
    tmpRow2->setData(outV->rowBuf(i));

    BufferArgs inputs;
    BufferArgs outputs;
    inputs.addArg(*tmpMtx0);
    inputs.addArg(*tmpRow0);
    outputs.addArg(*tmpRow2, ASSIGN_TO);
    forward_[0]->calc(inputs, outputs);
  }
}
示例#2
0
TEST(Layer, convTransLayerFwd2) {
    MatrixPtr result;
    result = Matrix::create(1, 5 * 5, false, false);
    result->zeroMem();
    result->add(1.0);
    doOneConvtTest(/* imgSize */ 5,
                   /* output_x */ 1,
                   /* stride */ 1,
                   /* padding */ 0,
                   /* filter_size */ 5,
                   result);

    float resultData[] = {1, 2, 2, 2, 1,
                          2, 4, 4, 4, 2,
                          2, 4, 4, 4, 2,
                          2, 4, 4, 4, 2,
                          1, 2, 2, 2, 1};
    result->setData(resultData);
    doOneConvtTest(/* imgSize */ 5,
                   /* output_x */ 2,
                   /* stride */ 1,
                   /* padding */ 0,
                   /* filter_size */ 4,
                   result);

    float resultData2[] = {1, 2, 2, 2, 1,
                           2, 4, 4, 4, 2,
                           2, 4, 4, 4, 2,
                           2, 4, 4, 4, 2,
                           1, 2, 2, 2, 1};
    result->setData(resultData2);
    doOneConvtTest(/* imgSize */ 5,
                   /* output_x */ 2,
                   /* stride */ 2,
                   /* padding */ 1,
                   /* filter_size */ 5,
                   result);

    float resultData3[] = {1, 1, 2, 1, 1,
                           1, 1, 2, 1, 1,
                           2, 2, 4, 2, 2,
                           1, 1, 2, 1, 1,
                           1, 1, 2, 1, 1};
    result->setData(resultData3);
    doOneConvtTest(/* imgSize */ 5,
                   /* output_x */ 2,
                   /* stride */ 2,
                   /* padding */ 0,
                   /* filter_size */ 3,
                   result);}
示例#3
0
void CosSimVecMatLayer::backward(const UpdateCallback& callback) {
  CHECK_EQ(backward_.size(), 1UL) << "Only one forward function needed";

  MatrixPtr inV0 = getInputValue(0);
  MatrixPtr inV1 = getInputValue(1);
  MatrixPtr inG0 = getInputGrad(0);
  MatrixPtr inG1 = getInputGrad(1);
  MatrixPtr outV = getOutputValue();
  MatrixPtr outG = getOutputGrad();

  size_t batchSize = inV0->getHeight();
  CHECK(inV0 && inV1 && inG0 && inG1 && outV && outG);
  REGISTER_TIMER_INFO("BwCosVMTimer", getName().c_str());

  for (size_t i = 0; i < batchSize; i++) {
    tmpRow0->setData(inV0->rowBuf(i));
    tmpRow1->setData(inG0->rowBuf(i));
    tmpMtx0->setData(inV1->rowBuf(i));
    tmpMtx1->setData(inG1->rowBuf(i));
    tmpRow2->setData(outV->rowBuf(i));
    tmpRow3->setData(outG->rowBuf(i));

    BufferArgs inputs;
    BufferArgs outputs;
    inputs.addArg(*tmpRow3);
    inputs.addArg(*tmpRow2);
    inputs.addArg(*tmpMtx0);
    inputs.addArg(*tmpRow0);
    outputs.addArg(*tmpMtx1, ADD_TO);
    outputs.addArg(*tmpRow1, ADD_TO);

    backward_[0]->calc(inputs, outputs);
  }
}
示例#4
0
TEST(Layer, priorBoxLayerFwd) {
  vector<int> minSize;
  vector<int> maxSize;
  vector<real> aspectRatio;
  vector<real> variance;
  bool useGpu = false;

  minSize.push_back(276);
  maxSize.push_back(330);
  variance.push_back(0.1);
  variance.push_back(0.1);
  variance.push_back(0.2);
  variance.push_back(0.2);

  // CPU case 1.
  MatrixPtr result;
  real resultData[] = {0.04,
                       0.04,
                       0.96,
                       0.96,
                       0.1,
                       0.1,
                       0.2,
                       0.2,
                       0,
                       0,
                       1,
                       1,
                       0.1,
                       0.1,
                       0.2,
                       0.2};
  result = Matrix::create(1, 2 * 8, false, useGpu);
  result->setData(resultData);
  doOnePriorBoxTest(/* feature_map_width */ 1,
                    /* feature_map_height */ 1,
                    /* image_width */ 300,
                    /* image_height */ 300,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    useGpu,
                    result);
  // CPU case 2.
  variance[1] = 0.2;
  variance[3] = 0.1;
  maxSize.pop_back();
  real resultData2[] = {0,     0,     0.595, 0.595, 0.1, 0.2, 0.2, 0.1,
                        0.405, 0,     1,     0.595, 0.1, 0.2, 0.2, 0.1,
                        0,     0.405, 0.595, 1,     0.1, 0.2, 0.2, 0.1,
                        0.405, 0.405, 1,     1,     0.1, 0.2, 0.2, 0.1};
  Matrix::resizeOrCreate(result, 1, 4 * 8, false, useGpu);
  result->setData(resultData2);
  doOnePriorBoxTest(/* feature_map_width */ 2,
                    /* feature_map_height */ 2,
                    /* image_width */ 400,
                    /* image_height */ 400,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    useGpu,
                    result);
  // CPU case 3.
  aspectRatio.push_back(2);
  real resultData3[] = {0.04,     0.04, 0.96, 0.96,       0.1,        0.2,
                        0.2,      0.1,  0,    0.17473088, 1,          0.825269,
                        0.1,      0.2,  0.2,  0.1,        0.17473088, 0,
                        0.825269, 1,    0.1,  0.2,        0.2,        0.1};
  Matrix::resizeOrCreate(result, 1, 3 * 8, false, useGpu);
  result->setData(resultData3);
  doOnePriorBoxTest(/* feature_map_width */ 1,
                    /* feature_map_height */ 1,
                    /* image_width */ 300,
                    /* image_height */ 300,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    useGpu,
                    result);

#ifndef PADDLE_ONLY_CPU
  // reset the input parameters
  variance[1] = 0.1;
  variance[3] = 0.2;
  maxSize.push_back(330);
  aspectRatio.pop_back();
  MatrixPtr resultGpu;
  useGpu = true;
  // GPU case 1.
  resultGpu = Matrix::create(1, 2 * 8, false, useGpu);
  resultGpu->copyFrom(resultData, 2 * 8);
  doOnePriorBoxTest(/* feature_map_width */ 1,
                    /* feature_map_height */ 1,
                    /* image_width */ 300,
                    /* image_height */ 300,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    useGpu,
                    resultGpu);
  // GPU case 2.
  variance[1] = 0.2;
  variance[3] = 0.1;
  maxSize.pop_back();
  Matrix::resizeOrCreate(resultGpu, 1, 4 * 8, false, useGpu);
  resultGpu->copyFrom(resultData2, 4 * 8);
  doOnePriorBoxTest(/* feature_map_width */ 2,
                    /* feature_map_height */ 2,
                    /* image_width */ 400,
                    /* image_height */ 400,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    useGpu,
                    resultGpu);
  // GPU case 3.
  aspectRatio.push_back(2);
  Matrix::resizeOrCreate(resultGpu, 1, 3 * 8, false, useGpu);
  resultGpu->copyFrom(resultData3, 3 * 8);
  doOnePriorBoxTest(/* feature_map_width */ 1,
                    /* feature_map_height */ 1,
                    /* image_width */ 300,
                    /* image_height */ 300,
                    minSize,
                    maxSize,
                    aspectRatio,
                    variance,
                    useGpu,
                    resultGpu);
#endif
}