DataTransformer.cpp 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "DataTransformer.h"
#include <time.h>
#include <limits>

DataTransformer::DataTransformer(int threadNum,
                                 int capacity,
                                 bool isTest,
                                 bool isColor,
                                 int cropHeight,
                                 int cropWidth,
                                 int imgSize,
                                 bool isEltMean,
                                 bool isChannelMean,
                                 float* meanValues)
    : isTest_(isTest),
      isColor_(isColor),
      cropHeight_(cropHeight),
      cropWidth_(cropWidth),
      imgSize_(imgSize),
      capacity_(capacity),
      prefetchFree_(capacity),
      prefetchFull_(capacity) {
  fetchCount_ = -1;
  scale_ = 1.0;
  isChannelMean_ = isChannelMean;
  isEltMean_ = isEltMean;
  loadMean(meanValues);

  imgPixels_ = cropHeight * cropWidth * (isColor_ ? 3 : 1);

  prefetch_.reserve(capacity);
  for (int i = 0; i < capacity; i++) {
    auto d = std::make_shared<DataType>(new float[imgPixels_ * 3], 0);
    prefetch_.push_back(d);
    memset(prefetch_[i]->first, 0, imgPixels_ * sizeof(float));
    prefetchFree_.enqueue(prefetch_[i]);
  }

53
  numThreads_ = threadNum;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  syncThreadPool_.reset(new SyncThreadPool(numThreads_, false));
}

void DataTransformer::loadMean(float* values) {
  if (values) {
    int c = isColor_ ? 3 : 1;
    int sz = isChannelMean_ ? c : cropHeight_ * cropWidth_ * c;
    meanValues_ = new float[sz];
    memcpy(meanValues_, values, sz * sizeof(float));
  }
}

void DataTransformer::startFetching(const char* src,
                                    const int size,
                                    float* trg) {
  vector<char> imbuf(src, src + size);
  int cvFlag = (isColor_ ? CV_LOAD_IMAGE_COLOR : CV_LOAD_IMAGE_GRAYSCALE);
  cv::Mat im = cv::imdecode(cv::Mat(imbuf), cvFlag);
  if (!im.data) {
    LOG(ERROR) << "Could not decode image";
    LOG(ERROR) << im.channels() << " " << im.rows << " " << im.cols;
  }
  this->transform(im, trg);
}

int DataTransformer::Rand(int min, int max) {
  std::random_device source;
  std::mt19937 rng(source());
  std::uniform_int_distribution<int> dist(min, max);
  return dist(rng);
}

void DataTransformer::transform(Mat& cvImgOri, float* target) {
  const int imgChannels = cvImgOri.channels();
  const int imgHeight = cvImgOri.rows;
  const int imgWidth = cvImgOri.cols;
  const bool doMirror = (!isTest_) && Rand(0, 1);
  int h_off = 0;
  int w_off = 0;
  int th = imgHeight;
  int tw = imgWidth;
  cv::Mat img;
  if (imgSize_ > 0) {
    if (imgHeight > imgWidth) {
      tw = imgSize_;
      th = int(double(imgHeight) / imgWidth * tw);
      th = th > imgSize_ ? th : imgSize_;
    } else {
      th = imgSize_;
      tw = int(double(imgWidth) / imgHeight * th);
      tw = tw > imgSize_ ? tw : imgSize_;
    }
    cv::resize(cvImgOri, img, cv::Size(tw, th));
  } else {
    cv::Mat img = cvImgOri;
  }

  cv::Mat cv_cropped_img = img;
  if (cropHeight_ && cropWidth_) {
    if (!isTest_) {
      h_off = Rand(0, th - cropHeight_);
      w_off = Rand(0, tw - cropWidth_);
    } else {
      h_off = (th - cropHeight_) / 2;
      w_off = (tw - cropWidth_) / 2;
    }
    cv::Rect roi(w_off, h_off, cropWidth_, cropHeight_);
    cv_cropped_img = img(roi);
  } else {
    CHECK_EQ(cropHeight_, imgHeight);
    CHECK_EQ(cropWidth_, imgWidth);
  }
  int height = cropHeight_;
  int width = cropWidth_;
  int top_index;
  for (int h = 0; h < height; ++h) {
    const uchar* ptr = cv_cropped_img.ptr<uchar>(h);
    int img_index = 0;
    for (int w = 0; w < width; ++w) {
      for (int c = 0; c < imgChannels; ++c) {
        if (doMirror) {
          top_index = (c * height + h) * width + width - 1 - w;
        } else {
          top_index = (c * height + h) * width + w;
        }
        float pixel = static_cast<float>(ptr[img_index++]);
        if (isEltMean_) {
          int mean_index = (c * imgHeight + h) * imgWidth + w;
          target[top_index] = (pixel - meanValues_[mean_index]) * scale_;
        } else {
          if (isChannelMean_) {
            target[top_index] = (pixel - meanValues_[c]) * scale_;
          } else {
            target[top_index] = pixel * scale_;
          }
        }
      }
    }
  }  // target: BGR
}

void DataTransformer::start(vector<char*>& data, int* datalen, int* labels) {
  auto job = [&](int tid, int numThreads) {
157
    for (size_t i = tid; i < data.size(); i += numThreads) {
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
      DataTypePtr ret = prefetchFree_.dequeue();
      char* buf = data[i];
      int size = datalen[i];
      ret->second = labels[i];
      this->startFetching(buf, size, ret->first);
      prefetchFull_.enqueue(ret);
    }
  };
  syncThreadPool_->exec(job);
  fetchCount_ = data.size();
}

void DataTransformer::obtain(float* data, int* label) {
  fetchCount_--;
  if (fetchCount_ < 0) {
    LOG(FATAL) << "Empty data";
  }
  DataTypePtr ret = prefetchFull_.dequeue();
  *label = ret->second;
  memcpy(data, ret->first, sizeof(float) * imgPixels_);
  prefetchFree_.enqueue(ret);
}