You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by jx...@apache.org on 2018/03/20 22:45:37 UTC

[incubator-mxnet] branch master updated: add label augmentation support in image recordio (#9960)

This is an automated email from the ASF dual-hosted git repository.

jxie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new a196c2f  add label augmentation support in image recordio (#9960)
a196c2f is described below

commit a196c2f0d4f9e5d70767f465d1c65bda3f85f241
Author: nihui <sh...@126.com>
AuthorDate: Wed Mar 21 06:45:33 2018 +0800

    add label augmentation support in image recordio (#9960)
---
 src/io/image_iter_common.h      |  8 ++++++++
 src/io/iter_image_recordio_2.cc | 33 ++++++++++++++++++---------------
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/src/io/image_iter_common.h b/src/io/image_iter_common.h
index c61e3d1..5682288 100644
--- a/src/io/image_iter_common.h
+++ b/src/io/image_iter_common.h
@@ -85,6 +85,14 @@ class ImageLabelMap {
     CHECK(it != idx2label_.end()) << "fail to find imagelabel for id " << imid;
     return mshadow::Tensor<cpu, 1>(it->second, mshadow::Shape1(label_width));
   }
+  /*! \brief find a label for corresponding index, return vector as copy */
+  inline std::vector<float> FindCopy(size_t imid) const {
+    std::unordered_map<size_t, real_t*>::const_iterator it
+        = idx2label_.find(imid);
+    CHECK(it != idx2label_.end()) << "fail to find imagelabel for id " << imid;
+    const real_t *ptr = it->second;
+    return std::vector<float>(ptr, ptr + label_width);
+  }
 
  private:
   // label with_
diff --git a/src/io/iter_image_recordio_2.cc b/src/io/iter_image_recordio_2.cc
index fd8b6d7..b6ff6e9 100644
--- a/src/io/iter_image_recordio_2.cc
+++ b/src/io/iter_image_recordio_2.cc
@@ -538,8 +538,23 @@ inline unsigned ImageRecordIOParser2<DType>::ParseChunk(DType* data_dptr, real_t
         LOG(FATAL) << "Invalid output shape " << param_.data_shape;
       }
       const int n_channels = res.channels();
+      // load label before augmentations
+      std::vector<float> label_buf;
+      if (label_map_ != nullptr) {
+        label_buf = label_map_->FindCopy(rec.image_index());
+      } else if (rec.label != NULL) {
+        CHECK_EQ(param_.label_width, rec.num_label)
+          << "rec file provide " << rec.num_label << "-dimensional label "
+             "but label_width is set to " << param_.label_width;
+        label_buf.assign(rec.label, rec.label + rec.num_label);
+      } else {
+        CHECK_EQ(param_.label_width, 1)
+          << "label_width must be 1 unless an imglist is provided "
+             "or the rec file is packed with multi dimensional label";
+        label_buf.assign(&rec.header.label, &rec.header.label + 1);
+      }
       for (auto& aug : augmenters_[tid]) {
-        res = aug->Process(res, nullptr, prnds_[tid].get());
+        res = aug->Process(res, &label_buf, prnds_[tid].get());
       }
       mshadow::Tensor<cpu, 3, DType> data;
       if (idx < batch_param_.batch_size) {
@@ -584,20 +599,8 @@ inline unsigned ImageRecordIOParser2<DType>::ParseChunk(DType* data_dptr, real_t
         label = out_tmp.label().Back();
       }
 
-      if (label_map_ != nullptr) {
-        mshadow::Copy(label, label_map_->Find(rec.image_index()));
-      } else if (rec.label != NULL) {
-        CHECK_EQ(param_.label_width, rec.num_label)
-          << "rec file provide " << rec.num_label << "-dimensional label "
-             "but label_width is set to " << param_.label_width;
-        mshadow::Copy(label, mshadow::Tensor<cpu, 1>(rec.label,
-                                                     mshadow::Shape1(rec.num_label)));
-      } else {
-        CHECK_EQ(param_.label_width, 1)
-          << "label_width must be 1 unless an imglist is provided "
-             "or the rec file is packed with multi dimensional label";
-        label[0] = rec.header.label;
-      }
+      mshadow::Copy(label, mshadow::Tensor<cpu, 1>(dmlc::BeginPtr(label_buf),
+        mshadow::Shape1(label_buf.size())));
       res.release();
     }
   }

-- 
To stop receiving notification emails like this one, please contact
jxie@apache.org.