RuRo opened a new issue #16925: ImageRecordIter random_resized_crop artifacts URL: https://github.com/apache/incubator-mxnet/issues/16925 ## Description The `random_crop` augmentation in `ImageRecordIter` seems to sometimes corrupt the image. This happens with both `random_resized_crop=True` and `random_resized_crop=False`, with `min_random_area` and `min_crop_size` respectively. I think, that this is caused by the `cv::resize` calls on [line 390](https://github.com/apache/incubator-mxnet/blob/a98cefc74c10985a78050b35d9f888d215a3ff8e/src/io/image_aug_default.cc#L390) and [line 416](https://github.com/apache/incubator-mxnet/blob/a98cefc74c10985a78050b35d9f888d215a3ff8e/src/io/image_aug_default.cc#L416) of io/image_aug_default.cc. ```c++ cv::resize(res(roi), res, cv::Size(param_.data_shape[2], param_.data_shape[1]) , 0, 0, interpolation_method); ``` Here, `res(roi)` is the source image and `res` is the destination. However, according to the CV docs, the [`cv::Mat::Mat(const Mat &m, const Rect &roi)`](https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html#aa7ec97373406215f2d4bc72cc1d27036) constructor doesn't actually copy the data, but simply returns a view into it. Therefore, `res(roi)` and `res` actually share the same underlying array. I think, that the resize call sequentially writes into the destination, moving from the top of the image to the bottom, however, since the source overlaps the destination, at some point, part of the source gets overwritten by the already resized image. Before | After (`random_resized_crop=True`) | After (`random_resized_crop=False`) :---: | :---: | :---:  |  |  ## To Reproduce ```python import mxnet as mx import numpy as np from PIL import Image def main(): random_resized_crop = True for random_resized_crop in [True, False]: if random_resized_crop: resize_kwargs = { 'max_random_area': 1.0, 'min_random_area': 0.4, } else: resize_kwargs = { 'max_crop_size': 112, 'min_crop_size': 45, } data_iterator = mx.io.ImageRecordIter( seed_aug=31, seed=31, batch_size=1, data_shape=(3, 112, 112), path_imgrec='test.rec', label_width=1, shuffle=False, inter_method=1, min_aspect_ratio=0.8, max_aspect_ratio=1.2, random_resized_crop=random_resized_crop, **resize_kwargs ) data = next(data_iterator) img = data.data[0].asnumpy() img = np.moveaxis(np.squeeze(img), 0, 2).astype(np.uint8) Image.fromarray(img).save('result_' + str(random_resized_crop) + '.jpg') if __name__ == '__main__': main() ``` ### Steps to reproduce 1. Create or [download](https://github.com/apache/incubator-mxnet/files/3896886/example.zip) an example Rec dataset. 2. Run the above script. 3. Observe, that both `result_True.jpg` and `result_False.jpg` are corrupted. ## Environment ``` curl --retry 10 -s https://raw.githubusercontent.com/dmlc/gluon-nlp/master/tools/diagnose.py | python ----------Python Info---------- Version : 3.7.4 Compiler : GCC 9.2.0 Build : ('default', 'Oct 4 2019 06:57:26') Arch : ('64bit', 'ELF') ------------Pip Info----------- Version : 19.2.3 Directory : /usr/lib/python3.7/site-packages/pip ----------MXNet Info----------- Version : 1.5.1 Directory : /usr/lib/python3.7/site-packages/mxnet Num GPUs : 1 Commit Hash : c9818480680f84daa6e281a974ab263691302ba8 ----------System Info---------- Platform : Linux-4.19.85-1-MANJARO-x86_64-with-arch-Manjaro-Linux system : Linux node : RuRo-Laptop release : 4.19.85-1-MANJARO version : #1 SMP PREEMPT Thu Nov 21 10:38:39 UTC 2019 ----------Hardware Info---------- machine : x86_64 processor : Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian Address sizes: 39 bits physical, 48 bits virtual CPU(s): 8 On-line CPU(s) list: 0-7 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 158 Model name: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz Stepping: 9 CPU MHz: 3299.831 CPU max MHz: 3800.0000 CPU min MHz: 800.0000 BogoMIPS: 5618.00 Virtualization: VT-x L1d cache: 128 KiB L1i cache: 128 KiB L2 cache: 1 MiB L3 cache: 6 MiB NUMA node0 CPU(s): 0-7 Vulnerability Itlb multihit: KVM: Mitigation: Split huge pages Vulnerability L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Meltdown: Mitigation; PTI Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Full generic retpoline, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling Vulnerability Tsx async abort: Not affected Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rd tscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monit or ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_ lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_noti fy hwp_act_window hwp_epp md_clear flush_l1d ----------Network Test---------- Setting timeout: 10 Timing for MXNet: https://github.com/apache/incubator-mxnet, DNS: 0.0504 sec, LOAD: 0.5867 sec. Timing for GluonNLP GitHub: https://github.com/dmlc/gluon-nlp, DNS: 0.0012 sec, LOAD: 0.5724 sec. Timing for GluonNLP: http://gluon-nlp.mxnet.io, DNS: 0.2389 sec, LOAD: 0.5781 sec. Timing for D2L: http://d2l.ai, DNS: 0.0959 sec, LOAD: 0.4981 sec. Timing for D2L (zh-cn): http://zh.d2l.ai, DNS: 0.0490 sec, LOAD: 0.2652 sec. Timing for FashionMNIST: https://repo.mxnet.io/gluon/dataset/fashion-mnist/train-labels-idx1-ubyte.gz, DNS: 0.2532 sec, LOAD: 1.0932 sec. Timing for PYPI: https://pypi.python.org/pypi/pip, DNS: 0.0011 sec, LOAD: 0.8089 sec. Timing for Conda: https://repo.continuum.io/pkgs/free/, DNS: 0.1930 sec, LOAD: 0.3074 sec. ```
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
