Esempio n. 1
0
Model::Model( const char * filename )
  : Geometry( GL_LINES, filename, FLAG_LINE )
{
  // LOGI("Model::cstr() file %s", filename );
  FILE * fp = fopen( filename, "r" );
  if ( fp != NULL ) {
    int ns;
    int nl;
    int nx;
    fscanf(fp, "%d %d %d", &ns, &nl, &nx );  // Nr.Stations Nr.Legs Nr.splays
    my_vertex = new float[ 3 * ( ns + nx ) ];
    my_index  = new unsigned short[ 2 * (nl + nx) ];

    char name[32];
    char name2[32];
    for ( int k=0; k<ns; ++k ) {
      float x, y, z;
      if ( fscanf( fp, "%s %f %f %f", name, &x, &y, &z ) != 4 ) break;
      my_vertex[ 3*k + 0 ] = x;
      my_vertex[ 3*k + 1 ] = y;
      my_vertex[ 3*k + 2 ] = z;
      stations.insert( std::pair<std::string, int>( name, k ) );
    }
    for ( int k=0; k<nl; ++k ) {
      int a, b;
      fscanf(fp, "%s %s", name, name2 );
      StationMapIterator it1 = stations.find( name );
      StationMapIterator it2 = stations.find( name2 );
      if ( it1 != stations.end() && it2 != stations.end() ) {
        my_index[ 2*k + 0 ] = it1->second;
        my_index[ 2*k + 1 ] = it2->second;
      } else {
        break;
      }
    }
    int cx = 0;
    for ( int k=0; k<nx; ++k ) {
      int a, b;
      float x, y, z;
      fscanf(fp, "%s %f %f %f", name, &x, &y, &z );
      StationMapIterator it1 = stations.find( name );
      if ( it1 == stations.end() ) break;
      sprintf(name2, "%s-%d", name, cx );
      ++ cx;
      my_vertex[ 3*ns + 3*k + 0 ] = x;
      my_vertex[ 3*ns + 3*k + 1 ] = y;
      my_vertex[ 3*ns + 3*k + 2 ] = z;
      // station name not inserted in the stations map for the TO of the splay 
      my_index[ 2 * nl + 2*k + 0 ] = it1->second;
      my_index[ 2 * nl + 2*k + 1 ] = ns + k;
    }  
    SetVertex( my_vertex );
    SetNVertex( ns + nx );
    SetIndex( my_index );
    SetNIndex( 2 * (nl+nx) );
    
    InitBBox();
  }
}
void ScaleRouteLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
    const vector<Blob<Dtype>*>& top) {
  ScaleRouteParameter scale_route_param = this->layer_param_.scale_route_param();
  float low_scale, high_scale, mid_scale;
  if (scale_route_param.has_low_scale()) {
    low_scale = scale_route_param.low_scale();
    CHECK_GT(low_scale,0);
  } else {
    low_scale = 0.;
  }
  if (scale_route_param.has_high_scale()) {
    high_scale = scale_route_param.high_scale();
    CHECK_LT(high_scale,FLT_MAX);
  } else {
    high_scale = FLT_MAX;
  }
  if (scale_route_param.has_low_scale() && scale_route_param.has_high_scale()) {
    mid_scale = sqrt(low_scale*high_scale);
  } else if (scale_route_param.has_low_scale()) {
    mid_scale = low_scale;
  } else if (scale_route_param.has_high_scale()) {
    mid_scale = high_scale;
  } else {
    mid_scale = 256.;
  }
  CHECK_GE(high_scale,low_scale); CHECK_GT(mid_scale,0);
  
  const int num_rois = bottom[0]->num();
  const Dtype* rois_data = bottom[0]->cpu_data();
  const int rois_dim = bottom[0]->channels();
  vector<int> select_index;
  float min_mid_dist = FLT_MAX; int min_mid_idx = -1;
  for (int i = 0; i < num_rois; i++) {
    BBox bb = InitBBox(rois_data[i*rois_dim+1],rois_data[i*rois_dim+2],
            rois_data[i*rois_dim+3],rois_data[i*rois_dim+4]);
    float bb_size = BBoxSize(bb);
    float bb_scale = sqrt(bb_size);
    if (bb_scale>=low_scale && bb_scale<high_scale) {
      select_index.push_back(i);
    }
    float mid_dist = std::abs(log2(bb_scale/mid_scale));
    if (mid_dist<min_mid_dist) {
      min_mid_dist = mid_dist;
      min_mid_idx = i;
    }
  }
  // in case of no selected index
  if (select_index.size() == 0) {
    DLOG(INFO) <<"layer: "<<this->layer_param().name()<<", No samples between : "
            <<low_scale<<" and "<<high_scale<<"!!!";
    CHECK_GE(min_mid_idx,0);
    select_index.push_back(min_mid_idx);
  }
 
  // copy bottoms to tops
  const int select_num = select_index.size();
  for (int k = 0; k < top.size(); k++) {
    const int channels = bottom[k]->channels();
    const int height = bottom[k]->height();
    const int width = bottom[k]->width();
    const int dim = bottom[k]->count() / bottom[k]->num();   
    top[k]->Reshape(select_num, channels, height, width);
    
    const Dtype* bottom_data = bottom[k]->cpu_data();
    Dtype* top_data = top[k]->mutable_cpu_data();
    for (int i = 0; i < select_num; i++) {
      int idx = select_index[i]; CHECK_GT(bottom[k]->num(),idx);
      caffe_copy(dim, bottom_data+idx*dim, top_data+i*dim);
    }
  }
}