[GitHub] piiswrong commented on a change in pull request #11027: Add standard ResNet data augmentation for ImageRecordIter

2018-06-18 Thread GitBox
piiswrong commented on a change in pull request #11027: Add standard ResNet 
data augmentation for ImageRecordIter
URL: https://github.com/apache/incubator-mxnet/pull/11027#discussion_r196255546
 
 

 ##
 File path: example/image-classification/common/data.py
 ##
 @@ -43,18 +43,20 @@ def add_data_args(parser):
 def add_data_aug_args(parser):
 aug = parser.add_argument_group(
 'Image augmentations', 'implemented in src/io/image_aug_default.cc')
-aug.add_argument('--random-crop', type=int, default=1,
+aug.add_argument('--random-crop', type=int, default=0,
  help='if or not randomly crop the image')
-aug.add_argument('--random-mirror', type=int, default=1,
+aug.add_argument('--random-mirror', type=int, default=0,
  help='if or not randomly flip horizontally')
 aug.add_argument('--max-random-h', type=int, default=0,
  help='max change of hue, whose range is [0, 180]')
 aug.add_argument('--max-random-s', type=int, default=0,
  help='max change of saturation, whose range is [0, 255]')
 aug.add_argument('--max-random-l', type=int, default=0,
  help='max change of intensity, whose range is [0, 255]')
-aug.add_argument('--max-random-aspect-ratio', type=float, default=0,
- help='max change of aspect ratio, whose range is [0, 1]')
+aug.add_argument('--min-random-aspect-ratio', type=float, default=1,
+ help='min value of aspect ratio, whose value should be 
positive.')
+aug.add_argument('--max-random-aspect-ratio', type=float, default=1,
 
 Review comment:
   should the default be max-random-aspect-ratio = 0 and 
min-random-aspect-ratio = None so that we don't break previous API?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] piiswrong commented on a change in pull request #11027: Add standard ResNet data augmentation for ImageRecordIter

2018-06-14 Thread GitBox
piiswrong commented on a change in pull request #11027: Add standard ResNet 
data augmentation for ImageRecordIter
URL: https://github.com/apache/incubator-mxnet/pull/11027#discussion_r195585197
 
 

 ##
 File path: src/io/image_aug_default.cc
 ##
 @@ -277,7 +413,8 @@ class DefaultImageAugmenter : public ImageAugmenter {
 }
 
 // crop logic
-if (param_.max_crop_size != -1 || param_.min_crop_size != -1) {
+if (!param_.random_resized_crop &&
 
 Review comment:
   This looks weird. Why not resize in the random sized crop section?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] piiswrong commented on a change in pull request #11027: Add standard ResNet data augmentation for ImageRecordIter

2018-06-14 Thread GitBox
piiswrong commented on a change in pull request #11027: Add standard ResNet 
data augmentation for ImageRecordIter
URL: https://github.com/apache/incubator-mxnet/pull/11027#discussion_r195584938
 
 

 ##
 File path: src/io/image_aug_default.cc
 ##
 @@ -218,10 +265,93 @@ class DefaultImageAugmenter : public ImageAugmenter {
   res = src;
 }
 
+if (param_.random_resized_crop) {
+  // random resize crop
+  CHECK(param_.min_random_scale == 1.0f &&
+param_.max_random_scale == 1.0f &&
+param_.min_crop_size == -1 &&
+param_.max_crop_size == -1 &&
+!param_.rand_crop) <<
+"\nSetting random_resized_crop to true conflicts with "
+"min_random_scale, max_random_scale, "
+"min_crop_size, max_crop_size, "
+"and rand_crop.";
+  if (param_.max_random_area != 1.0f || param_.min_random_area != 1.0f
+  || param_.max_aspect_ratio != 1.0f || param_.min_aspect_ratio != 
1.0f) {
+CHECK(param_.min_aspect_ratio > 0.0f);
+CHECK(param_.min_random_area <= param_.max_random_area);
+CHECK(param_.min_aspect_ratio <= param_.max_aspect_ratio);
+std::uniform_real_distribution 
rand_uniform_area(param_.min_random_area,
+
param_.max_random_area);
+std::uniform_real_distribution 
rand_uniform_ratio(param_.min_aspect_ratio,
+ 
param_.max_aspect_ratio);
+std::uniform_real_distribution rand_uniform(0, 1);
+float area = res.rows * res.cols;
+bool attemp = false;
+for (int i = 0; i < 10; ++i) {
+  float rand_area = rand_uniform_area(*prnd);
+  float ratio = rand_uniform_ratio(*prnd);
+  float target_area = area * rand_area;
+  int y_area = std::round(std::sqrt(target_area / ratio));
+  int x_area = std::round(std::sqrt(target_area * ratio));
+  if (rand_uniform(*prnd) > 0.5) {
+float temp_y_area = y_area;
+y_area = x_area;
+x_area = temp_y_area;
+  }
+  if (y_area <= res.rows && x_area <= res.cols) {
+// random crop
+index_t rand_y_area =
+std::uniform_int_distribution(0, res.rows - 
y_area)(*prnd);
+index_t rand_x_area =
+std::uniform_int_distribution(0, res.cols - 
x_area)(*prnd);
+cv::Rect roi(rand_x_area, rand_y_area, x_area, y_area);
+int interpolation_method = GetInterMethod(param_.inter_method, 
x_area, y_area,
+  param_.data_shape[2],
+  
param_.data_shape[1], prnd);
+cv::resize(res(roi), res, cv::Size(param_.data_shape[2], 
param_.data_shape[1]),
+   0, 0, interpolation_method);
+attemp = true;
+break;
+  }
+}
+if (!attemp) {
+  // center crop
+  int interpolation_method = GetInterMethod(param_.inter_method, 
res.cols, res.rows,
+param_.data_shape[2],
+param_.data_shape[1], 
prnd);
+  if (res.rows < param_.data_shape[1]) {
+index_t new_cols = 
static_cast(static_cast(param_.data_shape[1]) /
+
static_cast(res.rows) *
+
static_cast(res.cols));
+cv::resize(res, res, cv::Size(new_cols, param_.data_shape[1]),
+   0, 0, interpolation_method);
+  }
+  if (res.cols < param_.data_shape[2]) {
+index_t new_rows = 
static_cast(static_cast(param_.data_shape[2]) /
+
static_cast(res.cols) *
+
static_cast(res.rows));
+cv::resize(res, res, cv::Size(param_.data_shape[2], new_rows),
+   0, 0, interpolation_method);
+  }
+  CHECK(static_cast(res.rows) >= param_.data_shape[1]
+&& static_cast(res.cols) >= param_.data_shape[2])
+<< "input image size smaller than input shape";
+  index_t center_y = res.rows - param_.data_shape[1];
+  index_t center_x = res.cols - param_.data_shape[2];
+  center_y /= 2;
+  center_x /= 2;
+  cv::Rect roi(center_x, center_y, param_.data_shape[2], 
param_.data_shape[1]);
+  res = res(roi);
+}
+random_resized_crop_exec = true;
+  }
+}
 // norma

[GitHub] piiswrong commented on a change in pull request #11027: Add standard ResNet data augmentation for ImageRecordIter

2018-06-14 Thread GitBox
piiswrong commented on a change in pull request #11027: Add standard ResNet 
data augmentation for ImageRecordIter
URL: https://github.com/apache/incubator-mxnet/pull/11027#discussion_r195583935
 
 

 ##
 File path: src/io/image_aug_default.cc
 ##
 @@ -218,10 +265,93 @@ class DefaultImageAugmenter : public ImageAugmenter {
   res = src;
 }
 
+if (param_.random_resized_crop) {
+  // random resize crop
+  CHECK(param_.min_random_scale == 1.0f &&
+param_.max_random_scale == 1.0f &&
+param_.min_crop_size == -1 &&
+param_.max_crop_size == -1 &&
+!param_.rand_crop) <<
+"\nSetting random_resized_crop to true conflicts with "
+"min_random_scale, max_random_scale, "
+"min_crop_size, max_crop_size, "
+"and rand_crop.";
+  if (param_.max_random_area != 1.0f || param_.min_random_area != 1.0f
+  || param_.max_aspect_ratio != 1.0f || param_.min_aspect_ratio != 
1.0f) {
 
 Review comment:
   if these are all 1.0, you still need to resize the image right?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] piiswrong commented on a change in pull request #11027: Add standard ResNet data augmentation for ImageRecordIter

2018-06-11 Thread GitBox
piiswrong commented on a change in pull request #11027: Add standard ResNet 
data augmentation for ImageRecordIter
URL: https://github.com/apache/incubator-mxnet/pull/11027#discussion_r194592109
 
 

 ##
 File path: src/io/image_aug_default.cc
 ##
 @@ -296,7 +436,27 @@ class DefaultImageAugmenter : public ImageAugmenter {
 param_.data_shape[2], 
param_.data_shape[1], prnd);
   cv::resize(res(roi), res, cv::Size(param_.data_shape[2], 
param_.data_shape[1])
 , 0, 0, interpolation_method);
-} else {
+} else if (!random_resized_crop_exec) {
+  if (res.rows < param_.data_shape[1]) {
+index_t new_cols = 
static_cast(static_cast(param_.data_shape[1]) /
+static_cast(res.rows) *
+static_cast(res.cols));
+int interpolation_method = GetInterMethod(param_.inter_method, 
res.cols, res.rows,
 
 Review comment:
   Looks like the three calls to GetInterMethod can be extracted outside


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] piiswrong commented on a change in pull request #11027: Add standard ResNet data augmentation for ImageRecordIter

2018-06-11 Thread GitBox
piiswrong commented on a change in pull request #11027: Add standard ResNet 
data augmentation for ImageRecordIter
URL: https://github.com/apache/incubator-mxnet/pull/11027#discussion_r194591950
 
 

 ##
 File path: src/io/image_aug_default.cc
 ##
 @@ -218,6 +257,80 @@ class DefaultImageAugmenter : public ImageAugmenter {
   res = src;
 }
 
+if (param_.random_resized_crop) {
 
 Review comment:
   sanity checks


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] piiswrong commented on a change in pull request #11027: Add standard ResNet data augmentation for ImageRecordIter

2018-05-24 Thread GitBox
piiswrong commented on a change in pull request #11027: Add standard ResNet 
data augmentation for ImageRecordIter
URL: https://github.com/apache/incubator-mxnet/pull/11027#discussion_r190674963
 
 

 ##
 File path: src/io/image_aug_default.cc
 ##
 @@ -218,6 +257,80 @@ class DefaultImageAugmenter : public ImageAugmenter {
   res = src;
 }
 
+if (param_.random_resized_crop) {
 
 Review comment:
   Add checks for the mutual exclusion relationship between random_resized_crop 
and (resize, crop, ctc)


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] piiswrong commented on a change in pull request #11027: Add standard ResNet data augmentation for ImageRecordIter

2018-05-24 Thread GitBox
piiswrong commented on a change in pull request #11027: Add standard ResNet 
data augmentation for ImageRecordIter
URL: https://github.com/apache/incubator-mxnet/pull/11027#discussion_r190675340
 
 

 ##
 File path: tests/python/train/test_resnet_aug.py
 ##
 @@ -0,0 +1,172 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+# pylint: skip-file
+import sys
+sys.path.insert(0, '../../python')
+import mxnet as mx
+import numpy as np
+import os, pickle, gzip
+import logging
+from mxnet.test_utils import get_cifar10
+
+batch_size = 128
+
+# small mlp network
+def get_net():
+data = mx.symbol.Variable('data')
+float_data = mx.symbol.Cast(data=data, dtype="float32")
+fc1 = mx.symbol.FullyConnected(float_data, name='fc1', num_hidden=128)
+act1 = mx.symbol.Activation(fc1, name='relu1', act_type="relu")
+fc2 = mx.symbol.FullyConnected(act1, name = 'fc2', num_hidden = 64)
+act2 = mx.symbol.Activation(fc2, name='relu2', act_type="relu")
+fc3 = mx.symbol.FullyConnected(act2, name='fc3', num_hidden=10)
+softmax = mx.symbol.SoftmaxOutput(fc3, name="softmax")
+return softmax
+
+# check data
+get_cifar10()
+
+def get_iterator(kv):
+data_shape = (3, 28, 28)
+
+train = mx.io.ImageRecordIter(
+path_imgrec = "data/cifar/train.rec",
+mean_img= "data/cifar/mean.bin",
+data_shape  = data_shape,
+batch_size  = batch_size,
+random_resized_crop = True,
+max_aspect_ratio = 0.25,
 
 Review comment:
   Is it the same as (3/4, 4/3)?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] piiswrong commented on a change in pull request #11027: Add standard ResNet data augmentation for ImageRecordIter

2018-05-24 Thread GitBox
piiswrong commented on a change in pull request #11027: Add standard ResNet 
data augmentation for ImageRecordIter
URL: https://github.com/apache/incubator-mxnet/pull/11027#discussion_r190675272
 
 

 ##
 File path: tests/python/train/test_resnet_aug.py
 ##
 @@ -0,0 +1,172 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+# pylint: skip-file
+import sys
+sys.path.insert(0, '../../python')
+import mxnet as mx
+import numpy as np
+import os, pickle, gzip
+import logging
+from mxnet.test_utils import get_cifar10
+
+batch_size = 128
+
+# small mlp network
+def get_net():
+data = mx.symbol.Variable('data')
+float_data = mx.symbol.Cast(data=data, dtype="float32")
+fc1 = mx.symbol.FullyConnected(float_data, name='fc1', num_hidden=128)
+act1 = mx.symbol.Activation(fc1, name='relu1', act_type="relu")
+fc2 = mx.symbol.FullyConnected(act1, name = 'fc2', num_hidden = 64)
+act2 = mx.symbol.Activation(fc2, name='relu2', act_type="relu")
+fc3 = mx.symbol.FullyConnected(act2, name='fc3', num_hidden=10)
+softmax = mx.symbol.SoftmaxOutput(fc3, name="softmax")
+return softmax
+
+# check data
+get_cifar10()
+
+def get_iterator(kv):
+data_shape = (3, 28, 28)
+
+train = mx.io.ImageRecordIter(
+path_imgrec = "data/cifar/train.rec",
+mean_img= "data/cifar/mean.bin",
+data_shape  = data_shape,
+batch_size  = batch_size,
+random_resized_crop = True,
+max_aspect_ratio = 0.25,
 
 Review comment:
   what does 0.25 mean?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] piiswrong commented on a change in pull request #11027: Add standard ResNet data augmentation for ImageRecordIter

2018-05-24 Thread GitBox
piiswrong commented on a change in pull request #11027: Add standard ResNet 
data augmentation for ImageRecordIter
URL: https://github.com/apache/incubator-mxnet/pull/11027#discussion_r190675004
 
 

 ##
 File path: example/image-classification/train_imagenet.py
 ##
 @@ -31,7 +31,7 @@
 data.add_data_args(parser)
 data.add_data_aug_args(parser)
 # use a large aug level
-data.set_data_aug_level(parser, 3)
+# data.set_data_aug_level(parser, 3)
 
 Review comment:
   ?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services