{"action":"created","comment":{"url":"https://api.github.com/repos/dmlc/mxnet/pulls/comments/75913867","id":75913867,"diff_hunk":"@@ -0,0 +1,217 @@\n+/*!\n+ * Copyright (c) 2015 by Contributors\n+ * \\file caffe_data_iter.cc\n+ * \\brief register mnist iterator\n+*/\n+#include <caffe/proto/caffe.pb.h>\n+#include <dmlc/logging.h>\n+#include <dmlc/parameter.h>\n+#include <mxnet/operator.h>\n+#include <atomic>\n+\n+#include \"../../src/operator/operator_common.h\"\n+#include \"caffe_common.h\"\n+#include \"caffe_stream.h\"\n+#include \"caffe_fieldentry.h\"\n+#include \"caffe_blob.h\"\n+#include \"../../src/io/inst_vector.h\"\n+#include \"../../src/io/iter_prefetcher.h\"\n+#include \"../../src/operator/cast-inl.h\"\n+\n+namespace mxnet {\n+namespace io {\n+\n+struct CaffeDataParam : public dmlc::Parameter<CaffeDataParam> {\n+  /*! \\brief protobuf text */\n+  ::caffe::LayerParameter prototxt;\n+  /*! \\brief maximum iteration count */\n+  int max_iterations;\n+  /*! \\brief data mode */\n+  bool flat;\n+\n+  DMLC_DECLARE_PARAMETER(CaffeDataParam) {\n+    DMLC_DECLARE_FIELD(prototxt).set_default(\"layer{}\")\n+      .describe(\"Caffe's layer parameter\");\n+    DMLC_DECLARE_FIELD(flat).set_default(false)\n+      .describe(\"Augmentation Param: Whether to flat the data into 1D.\");\n+    DMLC_DECLARE_FIELD(max_iterations).set_lower_bound(1).set_default(10000)\n+      .describe(\"Maximum number of iterations per epoch.\");\n+  }\n+};\n+\n+struct CaffePrefetchParam : dmlc::Parameter<CaffePrefetchParam> {\n+    /*! \\brief data type */\n+    int dtype;\n+\n+    DMLC_DECLARE_PARAMETER(CaffePrefetchParam) {\n+      DMLC_DECLARE_FIELD(dtype)\n+        .add_enum(\"float32\", mshadow::kFloat32)\n+        .add_enum(\"float64\", mshadow::kFloat64)\n+        .add_enum(\"float16\", mshadow::kFloat16)\n+        .add_enum(\"uint8\", mshadow::kUint8)\n+        .add_enum(\"int32\", mshadow::kInt32)\n+        .set_default(mshadow::default_type_flag)\n+        .describe(\"Data type.\");\n+    }\n+};\n+\n+template<typename Dtype>\n+class CaffeDataIter : public IIterator<TBlobBatch> {\n+ public:\n+  CaffeDataIter(void) : loc_(0), batch_size_(0), channels_(0), width_(0), height_(0) {}\n+  virtual ~CaffeDataIter(void) {}\n+\n+  // intialize iterator loads data in\n+  virtual void Init(const std::vector<std::pair<std::string, std::string> >& kwargs) {\n+    std::map<std::string, std::string> kmap(kwargs.begin(), kwargs.end());\n+    param_.InitAllowUnknown(kmap);\n+\n+    // Caffe seems to understand phase inside an \"include {}\" block\n+    if (!param_.prototxt.has_phase()) {\n+      if (param_.prototxt.include().size()) {\n+        if (param_.prototxt.include(0).has_phase()) {\n+          param_.prototxt.set_phase(param_.prototxt.include(0).phase());\n+        }\n+      }\n+    }\n+\n+    std::string type = param_.prototxt.type();\n+    caffe_data_layer_ = caffe::LayerRegistry<Dtype>::CreateLayer(param_.prototxt);\n+    if (caffe_data_layer_) {\n+      const size_t top_size = param_.prototxt.top_size();\n+      if (top_size > 0) {\n+        top_.reserve(top_size);\n+        for (size_t x = 0; x < top_size; ++x) {\n+          ::caffe::Blob<Dtype> *blob = new ::caffe::Blob<Dtype>();\n+          cleanup_blobs_.push_back(std::unique_ptr<::caffe::Blob<Dtype>>(blob));\n+          top_.push_back(blob);\n+        }\n+        caffe_data_layer_->SetUp(bottom_, top_);\n+        const std::vector<int> &shape = top_[0]->shape();\n+        const size_t shapeDimCount = shape.size();\n+        if (shapeDimCount > 0) {\n+          batch_size_ = shape[0];\n+          if (shapeDimCount > 1) {\n+            channels_ = shape[1];\n+            if (shapeDimCount > 2) {\n+              width_ = shape[2];\n+              if (shapeDimCount > 3) {\n+                height_ = shape[3];\n+              }\n+            }\n+          }\n+        }\n+\n+        if (param_.flat) {\n+          batch_data_.shape_ = mshadow::Shape4(batch_size_, 1, 1, width_ * height_);\n+        } else {\n+          batch_data_.shape_ = mshadow::Shape4(batch_size_, channels_, width_, height_);\n+        }\n+\n+        out_.data.clear();\n+        batch_label_.shape_ = mshadow::Shape2(batch_size_, 1);\n+        batch_label_.stride_ = 1;\n+        batch_data_.stride_ = batch_data_.size(3);\n+        out_.batch_size = batch_size_;\n+      }\n+    }\n+  }\n+\n+  virtual void BeforeFirst(void) {\n+    loc_ = 0;\n+  }\n+\n+  enum { DATA = 0, LABEL = 1 };\n+\n+  virtual bool Next(void) {\n+    if (caffe_data_layer_) {\n+      // MxNet iterator is expected to return CPU-accessible memory\n+      assert(::caffe::Caffe::mode() == ::caffe::Caffe::CPU);\n+      caffe_data_layer_->Forward(bottom_, top_);\n+      assert(batch_size_ > 0);\n+\n+      if (loc_ + batch_size_ <= param_.max_iterations) {\n+        batch_data_.dptr_ = top_[DATA]->mutable_cpu_data();\n+        batch_label_.dptr_ = top_[LABEL]->mutable_cpu_data();\n+\n+        out_.data.clear();\n+        if (param_.flat) {\n+          out_.data.push_back(TBlob(batch_data_.FlatTo2D()));\n+        } else {\n+          out_.data.push_back(TBlob(batch_data_));\n+        }\n+        assert(out_.batch_size == batch_size_);\n+        out_.data.push_back(TBlob(batch_label_));\n+        loc_ += batch_size_;\n+        return true;\n+      }\n+    }\n+    return false;\n+  }\n+\n+  virtual const TBlobBatch &Value(void) const {\n+    return out_;\n+  }\n+\n+ private:\n+  /*! \\brief MNISTCass iter params */\n+  CaffeDataParam param_;\n+  /*! Shape scalar values */\n+  size_t batch_size_, channels_, width_, height_;\n+  /*! \\brief Caffe data layer */\n+  boost::shared_ptr<caffe::Layer<Dtype> >  caffe_data_layer_;\n+  /*! \\brief batch data tensor */\n+  mshadow::Tensor<cpu, 4, Dtype> batch_data_;","path":"plugin/caffe/caffe_data_iter.cc","position":164,"original_position":164,"commit_id":"b85288e70cf536b934dfbda96d3422519d39a951","original_commit_id":"88b4ac0671371b8ec7e1516b64f183251c4a8efa","user":{"login":"cjolivier01","id":11234557,"avatar_url":"https://avatars.githubusercontent.com/u/11234557?v=3","gravatar_id":"","url":"https://api.github.com/users/cjolivier01","html_url":"https://github.com/cjolivier01","followers_url":"https://api.github.com/users/cjolivier01/followers","following_url":"https://api.github.com/users/cjolivier01/following{/other_user}","gists_url":"https://api.github.com/users/cjolivier01/gists{/gist_id}","starred_url":"https://api.github.com/users/cjolivier01/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cjolivier01/subscriptions","organizations_url":"https://api.github.com/users/cjolivier01/orgs","repos_url":"https://api.github.com/users/cjolivier01/repos","events_url":"https://api.github.com/users/cjolivier01/events{/privacy}","received_events_url":"https://api.github.com/users/cjolivier01/received_events","type":"User","site_admin":false},"body":"Done (this actually turned out to be kind of messy, but it is changed now)","created_at":"2016-08-23T17:32:54Z","updated_at":"2016-08-23T17:32:54Z","html_url":"https://github.com/dmlc/mxnet/pull/3103#discussion_r75913867","pull_request_url":"https://api.github.com/repos/dmlc/mxnet/pulls/3103","_links":{"self":{"href":"https://api.github.com/repos/dmlc/mxnet/pulls/comments/75913867"},"html":{"href":"https://github.com/dmlc/mxnet/pull/3103#discussion_r75913867"},"pull_request":{"href":"https://api.github.com/repos/dmlc/mxnet/pulls/3103"}}},"pull_request":{"url":"https://api.github.com/repos/dmlc/mxnet/pulls/3103","id":82232565,"html_url":"https://github.com/dmlc/mxnet/pull/3103","diff_url":"https://github.com/dmlc/mxnet/pull/3103.diff","patch_url":"https://github.com/dmlc/mxnet/pull/3103.patch","issue_url":"https://api.github.com/repos/dmlc/mxnet/issues/3103","number":3103,"state":"open","locked":false,"title":"Caffe data layer to MXIter converter","user":{"login":"cjolivier01","id":11234557,"avatar_url":"https://avatars.githubusercontent.com/u/11234557?v=3","gravatar_id":"","url":"https://api.github.com/users/cjolivier01","html_url":"https://github.com/cjolivier01","followers_url":"https://api.github.com/users/cjolivier01/followers","following_url":"https://api.github.com/users/cjolivier01/following{/other_user}","gists_url":"https://api.github.com/users/cjolivier01/gists{/gist_id}","starred_url":"https://api.github.com/users/cjolivier01/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cjolivier01/subscriptions","organizations_url":"https://api.github.com/users/cjolivier01/orgs","repos_url":"https://api.github.com/users/cjolivier01/repos","events_url":"https://api.github.com/users/cjolivier01/events{/privacy}","received_events_url":"https://api.github.com/users/cjolivier01/received_events","type":"User","site_admin":false},"body":"Supply prototxt for caffe model's data layer and it is created with an MXNet data iterator interface.  In addition, added caffe-converter-tool converted caffe model to json file loading in caffe_net.py\r\n","created_at":"2016-08-22T17:42:25Z","updated_at":"2016-08-23T17:32:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5b40afb2c109b3cf1f7c16cc7e904761468c0982","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/dmlc/mxnet/pulls/3103/commits","review_comments_url":"https://api.github.com/repos/dmlc/mxnet/pulls/3103/comments","review_comment_url":"https://api.github.com/repos/dmlc/mxnet/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dmlc/mxnet/issues/3103/comments","statuses_url":"https://api.github.com/repos/dmlc/mxnet/statuses/b85288e70cf536b934dfbda96d3422519d39a951","head":{"label":"cjolivier01:master","ref":"master","sha":"b85288e70cf536b934dfbda96d3422519d39a951","user":{"login":"cjolivier01","id":11234557,"avatar_url":"https://avatars.githubusercontent.com/u/11234557?v=3","gravatar_id":"","url":"https://api.github.com/users/cjolivier01","html_url":"https://github.com/cjolivier01","followers_url":"https://api.github.com/users/cjolivier01/followers","following_url":"https://api.github.com/users/cjolivier01/following{/other_user}","gists_url":"https://api.github.com/users/cjolivier01/gists{/gist_id}","starred_url":"https://api.github.com/users/cjolivier01/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cjolivier01/subscriptions","organizations_url":"https://api.github.com/users/cjolivier01/orgs","repos_url":"https://api.github.com/users/cjolivier01/repos","events_url":"https://api.github.com/users/cjolivier01/events{/privacy}","received_events_url":"https://api.github.com/users/cjolivier01/received_events","type":"User","site_admin":false},"repo":{"id":66292371,"name":"mxnet","full_name":"cjolivier01/mxnet","owner":{"login":"cjolivier01","id":11234557,"avatar_url":"https://avatars.githubusercontent.com/u/11234557?v=3","gravatar_id":"","url":"https://api.github.com/users/cjolivier01","html_url":"https://github.com/cjolivier01","followers_url":"https://api.github.com/users/cjolivier01/followers","following_url":"https://api.github.com/users/cjolivier01/following{/other_user}","gists_url":"https://api.github.com/users/cjolivier01/gists{/gist_id}","starred_url":"https://api.github.com/users/cjolivier01/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cjolivier01/subscriptions","organizations_url":"https://api.github.com/users/cjolivier01/orgs","repos_url":"https://api.github.com/users/cjolivier01/repos","events_url":"https://api.github.com/users/cjolivier01/events{/privacy}","received_events_url":"https://api.github.com/users/cjolivier01/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/cjolivier01/mxnet","description":"Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more","fork":true,"url":"https://api.github.com/repos/cjolivier01/mxnet","forks_url":"https://api.github.com/repos/cjolivier01/mxnet/forks","keys_url":"https://api.github.com/repos/cjolivier01/mxnet/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cjolivier01/mxnet/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cjolivier01/mxnet/teams","hooks_url":"https://api.github.com/repos/cjolivier01/mxnet/hooks","issue_events_url":"https://api.github.com/repos/cjolivier01/mxnet/issues/events{/number}","events_url":"https://api.github.com/repos/cjolivier01/mxnet/events","assignees_url":"https://api.github.com/repos/cjolivier01/mxnet/assignees{/user}","branches_url":"https://api.github.com/repos/cjolivier01/mxnet/branches{/branch}","tags_url":"https://api.github.com/repos/cjolivier01/mxnet/tags","blobs_url":"https://api.github.com/repos/cjolivier01/mxnet/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cjolivier01/mxnet/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cjolivier01/mxnet/git/refs{/sha}","trees_url":"https://api.github.com/repos/cjolivier01/mxnet/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cjolivier01/mxnet/statuses/{sha}","languages_url":"https://api.github.com/repos/cjolivier01/mxnet/languages","stargazers_url":"https://api.github.com/repos/cjolivier01/mxnet/stargazers","contributors_url":"https://api.github.com/repos/cjolivier01/mxnet/contributors","subscribers_url":"https://api.github.com/repos/cjolivier01/mxnet/subscribers","subscription_url":"https://api.github.com/repos/cjolivier01/mxnet/subscription","commits_url":"https://api.github.com/repos/cjolivier01/mxnet/commits{/sha}","git_commits_url":"https://api.github.com/repos/cjolivier01/mxnet/git/commits{/sha}","comments_url":"https://api.github.com/repos/cjolivier01/mxnet/comments{/number}","issue_comment_url":"https://api.github.com/repos/cjolivier01/mxnet/issues/comments{/number}","contents_url":"https://api.github.com/repos/cjolivier01/mxnet/contents/{+path}","compare_url":"https://api.github.com/repos/cjolivier01/mxnet/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cjolivier01/mxnet/merges","archive_url":"https://api.github.com/repos/cjolivier01/mxnet/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cjolivier01/mxnet/downloads","issues_url":"https://api.github.com/repos/cjolivier01/mxnet/issues{/number}","pulls_url":"https://api.github.com/repos/cjolivier01/mxnet/pulls{/number}","milestones_url":"https://api.github.com/repos/cjolivier01/mxnet/milestones{/number}","notifications_url":"https://api.github.com/repos/cjolivier01/mxnet/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cjolivier01/mxnet/labels{/name}","releases_url":"https://api.github.com/repos/cjolivier01/mxnet/releases{/id}","deployments_url":"https://api.github.com/repos/cjolivier01/mxnet/deployments","created_at":"2016-08-22T17:12:33Z","updated_at":"2016-08-22T17:12:36Z","pushed_at":"2016-08-23T15:41:17Z","git_url":"git://github.com/cjolivier01/mxnet.git","ssh_url":"git@github.com:cjolivier01/mxnet.git","clone_url":"https://github.com/cjolivier01/mxnet.git","svn_url":"https://github.com/cjolivier01/mxnet","homepage":"http://mxnet.io","size":8503,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"dmlc:master","ref":"master","sha":"f6fa98d645d2b9871e7ac5f0ad977c1e5af80738","user":{"login":"dmlc","id":11508361,"avatar_url":"https://avatars.githubusercontent.com/u/11508361?v=3","gravatar_id":"","url":"https://api.github.com/users/dmlc","html_url":"https://github.com/dmlc","followers_url":"https://api.github.com/users/dmlc/followers","following_url":"https://api.github.com/users/dmlc/following{/other_user}","gists_url":"https://api.github.com/users/dmlc/gists{/gist_id}","starred_url":"https://api.github.com/users/dmlc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dmlc/subscriptions","organizations_url":"https://api.github.com/users/dmlc/orgs","repos_url":"https://api.github.com/users/dmlc/repos","events_url":"https://api.github.com/users/dmlc/events{/privacy}","received_events_url":"https://api.github.com/users/dmlc/received_events","type":"Organization","site_admin":false},"repo":{"id":34864402,"name":"mxnet","full_name":"dmlc/mxnet","owner":{"login":"dmlc","id":11508361,"avatar_url":"https://avatars.githubusercontent.com/u/11508361?v=3","gravatar_id":"","url":"https://api.github.com/users/dmlc","html_url":"https://github.com/dmlc","followers_url":"https://api.github.com/users/dmlc/followers","following_url":"https://api.github.com/users/dmlc/following{/other_user}","gists_url":"https://api.github.com/users/dmlc/gists{/gist_id}","starred_url":"https://api.github.com/users/dmlc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dmlc/subscriptions","organizations_url":"https://api.github.com/users/dmlc/orgs","repos_url":"https://api.github.com/users/dmlc/repos","events_url":"https://api.github.com/users/dmlc/events{/privacy}","received_events_url":"https://api.github.com/users/dmlc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dmlc/mxnet","description":"Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more","fork":false,"url":"https://api.github.com/repos/dmlc/mxnet","forks_url":"https://api.github.com/repos/dmlc/mxnet/forks","keys_url":"https://api.github.com/repos/dmlc/mxnet/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dmlc/mxnet/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dmlc/mxnet/teams","hooks_url":"https://api.github.com/repos/dmlc/mxnet/hooks","issue_events_url":"https://api.github.com/repos/dmlc/mxnet/issues/events{/number}","events_url":"https://api.github.com/repos/dmlc/mxnet/events","assignees_url":"https://api.github.com/repos/dmlc/mxnet/assignees{/user}","branches_url":"https://api.github.com/repos/dmlc/mxnet/branches{/branch}","tags_url":"https://api.github.com/repos/dmlc/mxnet/tags","blobs_url":"https://api.github.com/repos/dmlc/mxnet/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dmlc/mxnet/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dmlc/mxnet/git/refs{/sha}","trees_url":"https://api.github.com/repos/dmlc/mxnet/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dmlc/mxnet/statuses/{sha}","languages_url":"https://api.github.com/repos/dmlc/mxnet/languages","stargazers_url":"https://api.github.com/repos/dmlc/mxnet/stargazers","contributors_url":"https://api.github.com/repos/dmlc/mxnet/contributors","subscribers_url":"https://api.github.com/repos/dmlc/mxnet/subscribers","subscription_url":"https://api.github.com/repos/dmlc/mxnet/subscription","commits_url":"https://api.github.com/repos/dmlc/mxnet/commits{/sha}","git_commits_url":"https://api.github.com/repos/dmlc/mxnet/git/commits{/sha}","comments_url":"https://api.github.com/repos/dmlc/mxnet/comments{/number}","issue_comment_url":"https://api.github.com/repos/dmlc/mxnet/issues/comments{/number}","contents_url":"https://api.github.com/repos/dmlc/mxnet/contents/{+path}","compare_url":"https://api.github.com/repos/dmlc/mxnet/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dmlc/mxnet/merges","archive_url":"https://api.github.com/repos/dmlc/mxnet/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dmlc/mxnet/downloads","issues_url":"https://api.github.com/repos/dmlc/mxnet/issues{/number}","pulls_url":"https://api.github.com/repos/dmlc/mxnet/pulls{/number}","milestones_url":"https://api.github.com/repos/dmlc/mxnet/milestones{/number}","notifications_url":"https://api.github.com/repos/dmlc/mxnet/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dmlc/mxnet/labels{/name}","releases_url":"https://api.github.com/repos/dmlc/mxnet/releases{/id}","deployments_url":"https://api.github.com/repos/dmlc/mxnet/deployments","created_at":"2015-04-30T16:21:15Z","updated_at":"2016-08-23T16:15:13Z","pushed_at":"2016-08-23T15:41:17Z","git_url":"git://github.com/dmlc/mxnet.git","ssh_url":"git@github.com:dmlc/mxnet.git","clone_url":"https://github.com/dmlc/mxnet.git","svn_url":"https://github.com/dmlc/mxnet","homepage":"http://mxnet.io","size":12234,"stargazers_count":4814,"watchers_count":4814,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1812,"mirror_url":null,"open_issues_count":578,"forks":1812,"open_issues":578,"watchers":4814,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dmlc/mxnet/pulls/3103"},"html":{"href":"https://github.com/dmlc/mxnet/pull/3103"},"issue":{"href":"https://api.github.com/repos/dmlc/mxnet/issues/3103"},"comments":{"href":"https://api.github.com/repos/dmlc/mxnet/issues/3103/comments"},"review_comments":{"href":"https://api.github.com/repos/dmlc/mxnet/pulls/3103/comments"},"review_comment":{"href":"https://api.github.com/repos/dmlc/mxnet/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dmlc/mxnet/pulls/3103/commits"},"statuses":{"href":"https://api.github.com/repos/dmlc/mxnet/statuses/b85288e70cf536b934dfbda96d3422519d39a951"}}}}
{"actor":{"display_login":"cjolivier01"}}