Skip to content

Commit

Permalink
inst iter
Browse files Browse the repository at this point in the history
  • Loading branch information
antinucleon committed Mar 29, 2015
1 parent ce2281a commit e223c84
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include mshadow/make/mshadow.mk
# all tge possible warning tread
WARNFLAGS= -Wall
CFLAGS = -DMSHADOW_FORCE_STREAM $(WARNFLAGS)
CFLAGS += -g -O3 -I./mshadow/ -fPIC $(MSHADOW_CFLAGS)
CFLAGS += -g -O3 -I./mshadow/ -fPIC $(MSHADOW_CFLAGS)
LDFLAGS = -pthread $(MSHADOW_LDFLAGS)
NVCCFLAGS = --use_fast_math -g -O3 -ccbin $(CXX) $(MSHADOW_NVCCFLAGS)

Expand All @@ -33,6 +33,9 @@ else
CFLAGS+= -DCXXNET_USE_OPENCV_DECODER=0
endif

ifeq ($(USE_OPENMP_ITER), 1)
CFLAGS += -fopenmp
endif
# customize cudnn path
ifneq ($(USE_CUDNN_PATH), NONE)
CFLAGS += -I$(USE_CUDNN_PATH)
Expand Down
2 changes: 2 additions & 0 deletions make/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ USE_DIST_PS = 0
PS_PATH = NONE
PS_THIRD_PATH = NONE

# use openmp iterator
USE_OPENMP_ITER = 0
# the additional link flags you want to add
ADD_LDFLAGS = -ljpeg

Expand Down
6 changes: 6 additions & 0 deletions src/io/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#if CXXNET_USE_OPENCV
#include "iter_thread_imbin-inl.hpp"
#include "iter_thread_imbin_x-inl.hpp"
#include "iter_thread_iminst-inl.hpp"
#include "iter_img-inl.hpp"
#endif

Expand Down Expand Up @@ -44,6 +45,11 @@ IIterator<DataBatch> *CreateIterator(const std::vector< std::pair<std::string, s
it = new BatchAdaptIterator(new AugmentIterator(new ThreadImagePageIteratorX()));
continue;
}
if (!strcmp(val, "imginst")) {
utils::Assert(it == NULL, "image binary can not chain over other iterator");
it = new BatchAdaptIterator(new AugmentIterator(new ThreadImageInstIterator(), 1));
continue;
}
if (!strcmp(val, "img")) {
utils::Assert(it == NULL, "image list iterator can not chain over other iterator");
it = new BatchAdaptIterator(new AugmentIterator(new ImageIterator()));
Expand Down
20 changes: 19 additions & 1 deletion src/io/image_augmenter-inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class ImageAugmenter {
// new width and height
float new_width = std::max(min_img_size_, std::min(max_img_size_, scale * src.cols));
float new_height = std::max(min_img_size_, std::min(max_img_size_, scale * src.rows));
//printf("%f %f %f %f %f %f %f %f %f\n", s, a, b, scale, ratio, hs, ws, new_width, new_height);
cv::Mat M(2, 3, CV_32F);
M.at<float>(0, 0) = hs * a - s * b * ws;
M.at<float>(1, 0) = -b * ws;
Expand All @@ -102,7 +103,7 @@ class ImageAugmenter {
M.at<float>(0, 2) = (new_width - ori_center_width) / 2;
M.at<float>(1, 2) = (new_height - ori_center_height) / 2;
cv::warpAffine(src, temp, M, cv::Size(new_width, new_height),
cv::INTER_CUBIC,
cv::INTER_LINEAR,
cv::BORDER_CONSTANT,
cv::Scalar(fill_value_, fill_value_, fill_value_));
cv::Mat res = temp;
Expand Down Expand Up @@ -150,6 +151,23 @@ class ImageAugmenter {
return tmpres;
}

virtual void Process(unsigned char *dptr, size_t sz, mshadow::TensorContainer<cpu, 3> *p_data,
utils::RandomSampler *prnd) {
cv::Mat buf(1, sz, CV_8U, dptr);
cv::Mat res = cv::imdecode(buf, 1);
res = this->Process(res, prnd);
p_data->Resize(mshadow::Shape3(3, res.rows, res.cols));
for (index_t i = 0; i < p_data->size(1); ++i) {
for (index_t j = 0; j < p_data->size(2); ++j) {
cv::Vec3b bgr = res.at<cv::Vec3b>(i, j);
(*p_data)[0][i][j] = bgr[2];
(*p_data)[1][i][j] = bgr[1];
(*p_data)[2][i][j] = bgr[0];
}
}
res.release();
}

private:
// whether skip processing
inline bool NeedProcess(void) const {
Expand Down
7 changes: 4 additions & 3 deletions src/io/iter_augment_proc-inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace cxxnet {
/*! \brief create a batch iterator from single instance iterator */
class AugmentIterator: public IIterator<DataInst> {
public:
AugmentIterator(IIterator<DataInst> *base)
: base_(base) {
AugmentIterator(IIterator<DataInst> *base, int no_aug=0)
: base_(base), no_aug_(no_aug) {
rand_crop_ = 0;
rand_mirror_ = 0;
crop_y_start_ = -1;
Expand Down Expand Up @@ -101,7 +101,7 @@ class AugmentIterator: public IIterator<DataInst> {
out_.index = d.index;
mshadow::Tensor<cpu, 3> data = d.data;
#if CXXNET_USE_OPENCV
data = aug.Process(data, &rnd);
if (!no_aug_) data = aug.Process(data, &rnd);
#endif

img_.Resize(mshadow::Shape3(data.shape_[0], shape_[1], shape_[2]));
Expand Down Expand Up @@ -235,6 +235,7 @@ class AugmentIterator: public IIterator<DataInst> {
int mirror_;
/*! \brief whether mean file is ready */
bool meanfile_ready_;
int no_aug_;
// augmenter
#if CXXNET_USE_OPENCV
ImageAugmenter aug;
Expand Down
Loading

0 comments on commit e223c84

Please sign in to comment.