Make channel first data channel last

Introduction

When rewriting the program written on the premise of Pytorch to Keras, it was necessary to replace the axes of the image data array, so I will share the method.

Image Channel Order

Channels First : (N, C, H, W) ← PyTorch Channels Last : (N, H, W, C) ← Keras

** N **: Number of images ** C **: Number of channels (color, etc.) ** H **: Image Height ** W **: Image Width

Channels First

When learning a model with an image data array such as PyTorch, the ** Channels First ** format is common. The order of the dimensions of the image is ** (Channel, Height, Width) **. You can tell from the name that the Channel (Color) dimension is at the beginning of the array.

Channels Last

When dealing with image data arrays in Keras, PIL, OpenCV, etc., the ** Channels Last ** format is common. The order of the dimensions of the image is ** (Height, Width, Channel) **. You can tell from the name that the Channel dimension is at the end of the array.

Channels First → Channels Last

Create a temporary ** Channels First ** image array data.

Temporary image array data


img = np.arange(100*64*64*3).reshape(-1,3,64,64)
img.shape 
(100, 3, 64, 64)

Method 1: np.transpose ()

np.transpose()


%%timeit
img.transpose(0,2,3,1).shape
(100, 64, 64, 3)
791 ns ± 92.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Method 2: np.swapaxes ()

np.swapaxes()


%%timeit
np.swapaxes(img, 1, 3).shape
(100, 64, 64, 3)
1.54 µs ± 410 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Method 3: np.moveaxes ()

np.moveaxes()


%%timeit
np.moveaxes(img, 1, 3).shape
(100, 64, 64, 3)
9.29 µs ± 956 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Method 4: np.rollaxes ()

np.rollaxes()


%%timeit
np.rollaxes(img, 1, 4).shape
(100, 64, 64, 3)
2.89 µs ± 358 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Method 5: np.einsum ()

np.einsum()


%%timeit
np.einsum('ijkl->ilkj', img).shape
(100, 64, 64, 3)
1.77 µs ± 210 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Finally

np.transpose () was the fastest. Please use the one you like.

[what-is-the-correct-way-to-change-image-channel-ordering-between-channels-first](https://stackoverflow.com/questions/43829711/what-is-the-correct-way- I referred to to-change-image-channel-ordering-between-channels-first).

Recommended Posts

Make channel first data channel last
First satellite data analysis by Tellus