kshitij12345 edited a comment on issue #15710: Inconsistent behaviour of 
ImageRecordIter depending on encoding format png vs jpeg
URL: 
https://github.com/apache/incubator-mxnet/issues/15710#issuecomment-517787707
 
 
   Hi,
   > NOTA: if we use (0,100,200) instead of [0,1,2] we can easily guess where 
the channels are)
   
   Thanks. Nice Idea.
   
   ImageRecordIter gives you data with channels first as mentioned in the docs.
   
https://mxnet.incubator.apache.org/versions/master/api/python/io/io.html#mxnet.io.ImageRecordIter
   
   Also you are correct that all channels are flipped i.e. if original is BGR 
then after loading it is RGB. Maybe due to use of OpenCV. (Not really sure).
   So you are indeed correct that the two transformations are channel first and 
also the flipping of channels.
   
   **Note** : The behaviour is same for JPEG as well as PNG.
   
   ```python
   import mxnet as mx
   import numpy as np
   
   shape = (3, 3, 3)
   label = 1
   
   record = mx.recordio.MXRecordIO("temp_png.rec", 'w')
   header = mx.recordio.IRHeader(0, label, 1, 0)
   img = np.ones(shape) * 255
   img[:, :, 0] = 0
   img[:, :, 1] = 100
   img[:, :, 2] = 200
   print('------Original---------------')
   print(img)
   packed_s = mx.recordio.pack_img(header, img, quality=9, img_fmt=".png")
   record.write(packed_s)
   record.close()
   
   iter = mx.io.ImageRecordIter(path_imgrec="temp_png.rec", data_shape=shape, 
batch_size=1)
   batch = iter.next()
   data = batch.data[0]
   print('--------PNG---------------')
   print(data.shape)
   recover_data = mx.ndarray.transpose(data, (0, 3, 2, 1)) # From channels 
first to channel last.
   print(recover_data[:,:,:,::-1]) # Reverse the channels.
   # Same as original
   
   record = mx.recordio.MXRecordIO("temp_jpeg.rec", 'w')
   header = mx.recordio.IRHeader(0, label, 1, 0)
   img = np.ones(shape) * 255
   img[:, :, 0] = 0
   img[:, :, 1] = 100
   img[:, :, 2] = 200
   packed_s = mx.recordio.pack_img(header, img, quality=100, img_fmt=".jpeg") # 
default
   record.write(packed_s)
   record.close()
   
   iter = mx.io.ImageRecordIter(path_imgrec="temp_jpeg.rec", data_shape=shape, 
batch_size=1)
   batch = iter.next()
   data = batch.data[0]
   print('-------JPEG---------------')
   print(data.shape)
   recover_data = mx.ndarray.transpose(data, (0, 3, 2, 1))  # From channels 
first to channel last.
   print(recover_data[[:,:,:,::-1]) # Reverse the channels.
   #  Same as original
   ```
   
   Output
   ```
   ------Original---------------
   [[[  0. 100. 200.]
     [  0. 100. 200.]
     [  0. 100. 200.]]
   
    [[  0. 100. 200.]
     [  0. 100. 200.]
     [  0. 100. 200.]]
   
    [[  0. 100. 200.]
     [  0. 100. 200.]
     [  0. 100. 200.]]]
   
   --------PNG---------------
   (1, 3, 3, 3)
   
   [[[[  0. 100. 200.]
      [  0. 100. 200.]
      [  0. 100. 200.]]
   
     [[  0. 100. 200.]
      [  0. 100. 200.]
      [  0. 100. 200.]]
   
     [[  0. 100. 200.]
      [  0. 100. 200.]
      [  0. 100. 200.]]]]
   <NDArray 1x3x3x3 @cpu(0)>
   
   -------JPEG---------------
   (1, 3, 3, 3)
   
   [[[[  0. 100. 199.]
      [  0. 100. 199.]
      [  0. 100. 199.]]
   
     [[  0. 100. 199.]
      [  0. 100. 199.]
      [  0. 100. 199.]]
   
     [[  0. 100. 199.]
      [  0. 100. 199.]
      [  0. 100. 199.]]]]
   <NDArray 1x3x3x3 @cpu(0)>
   ```

----------------------------------------------------------------
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

Reply via email to