void BinaryDataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  const int new_height = this->layer_param_.image_data_param().new_height();
  const int new_width  = this->layer_param_.image_data_param().new_width();
  const bool is_color  = this->layer_param_.image_data_param().is_color();
  string root_folder = this->layer_param_.image_data_param().root_folder();

  CHECK((new_height == 0 && new_width == 0) && is_color)
    << "Current implementation requires "
    "none of new_height or new_width or is_color.";
  // Read the file with filenames and labels
  const string& source = this->layer_param_.image_data_param().source();
  LOG(INFO) << "Opening file " << source;
  std::ifstream infile(source.c_str());
  string filename;
  int label;
  while (infile >> filename >> label) {
    lines_.push_back(std::make_pair(filename, label));
  }

  if (this->layer_param_.image_data_param().shuffle()) {
    // randomly shuffle data
    LOG(INFO) << "Shuffling data";
    const unsigned int prefetch_rng_seed = caffe_rng_rand();
    prefetch_rng_.reset(new Caffe::RNG(prefetch_rng_seed));
    ShuffleImages();
  }
  LOG(INFO) << "A total of " << lines_.size() << " images.";

  lines_id_ = 0;
  // Check if we would need to randomly skip a few data points
  if (this->layer_param_.image_data_param().rand_skip()) {
    unsigned int skip = caffe_rng_rand() %
        this->layer_param_.image_data_param().rand_skip();
    LOG(INFO) << "Skipping first " << skip << " data points.";
    CHECK_GT(lines_.size(), skip) << "Not enough points to skip";
    lines_id_ = skip;
  }
  // Load meta data
  std::ifstream metafile((root_folder + "meta").c_str());
  std::string dtype;
  metafile >> dtype;
  // int n, c, h, w;
  // metafile << n << c << h << w;
  vector<int> & top_shape = this->top_shape_;
  top_shape.resize(4);
  for (int i = 0; i < top_shape.size(); ++i) {
    metafile >> this->top_shape_[i];
  }
  // Reshape prefetch_data and top[0] according to the batch_size.
  const int batch_size = this->layer_param_.image_data_param().batch_size();
  CHECK_GT(batch_size, 0) << "Positive batch size required";
  this->top_shape_[0] = batch_size;
  for (int i = 0; i < this->PREFETCH_COUNT; ++i) {
    this->prefetch_[i].data_.Reshape(this->top_shape_);
  }
  top[0]->Reshape(this->top_shape_);

  LOG(INFO) << "output data size: " << top[0]->num() << ","
      << top[0]->channels() << "," << top[0]->height() << ","
      << top[0]->width();
  // label
  vector<int> label_shape(1, batch_size);
  top[1]->Reshape(label_shape);
  for (int i = 0; i < this->PREFETCH_COUNT; ++i) {
    this->prefetch_[i].label_.Reshape(label_shape);
  }
}
Exemple #2
0
int CWmfLoaderImpl::LoadImage(const wstring& wstrFilePath, bool* pbStop, bool bScale, int nWidth, int nHeight)
{
	Gdiplus::Metafile metafile(wstrFilePath.c_str());
	if (metafile.GetLastStatus() != Gdiplus::Ok)
	{
		return 4;
	}
	Gdiplus::MetafileHeader metafileheader;
	metafile.GetMetafileHeader(&metafileheader);
	m_nHeight = metafileheader.Height;
	m_nWidth = metafileheader.Width;
	if ( m_nWidth == 0 || m_nHeight == 0 )
	{
		return 4;
	}
	int nCurWidth = m_nWidth;
	int nCurHeight = m_nHeight;
	// 做缩放
	if (bScale)// 计算缩放率
	{
		double dRatio = 0;
		double nMaxLength = nWidth>nHeight?nWidth:nHeight;
		double nMinLength = nWidth<nHeight?nWidth:nHeight;

		double nImageMaxLength = m_nWidth>m_nHeight?m_nWidth:m_nHeight;
		double nImageMinLength = m_nWidth<m_nHeight?m_nWidth:m_nHeight;

		if (nMaxLength >= nImageMaxLength && nMinLength >= nImageMinLength) // 不缩放
		{
		}
		else	// 缩放
		{
			if (nMaxLength/nImageMaxLength > nMinLength/nImageMinLength)
			{
				dRatio = nMinLength/nImageMinLength;
			}
			else
			{
				dRatio = nMaxLength/nImageMaxLength;
			}
			nCurWidth = nCurWidth * dRatio;
			nCurHeight = nCurHeight * dRatio;
		}
	}

	m_hBitmap = XL_CreateBitmap(XLGRAPHIC_CT_ARGB32, nCurWidth, nCurHeight);
	if (m_hBitmap == NULL)
	{
		return 4;
	}

	XLBitmapInfo newBitmapInfo;
	XL_GetBitmapInfo(m_hBitmap, &newBitmapInfo);
	Gdiplus::Bitmap newBitmap(newBitmapInfo.Width, newBitmapInfo.Height, newBitmapInfo.ScanLineLength, PixelFormat32bppARGB, XL_GetBitmapBuffer(m_hBitmap, 0, 0));
	Gdiplus::Graphics graphics(&newBitmap);
	Gdiplus::Rect rect(0, 0, nCurWidth, nCurHeight);
	graphics.DrawImage(&metafile, rect);

	if (m_hBitmap != NULL)
	{
		return 0;
	}
	return 4;
}