Python 首先更改通道和最后通道之间的图像通道顺序的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43829711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:25:24  来源:igfitidea点击:

What is the correct way to change image channel ordering between channels first and channels last?

pythonnumpymachine-learningkerastheano

提问by Kuysea

I can not for the life of me figure out how to switch the image ordering. images are read in (x,x,3) format, theano requires it to be in (3,x,x) format. I tried changing the order with numpy.array([img[:,:,i] for i in range(3)])

我一生都无法弄清楚如何切换图像顺序。图像以 (x,x,3) 格式读取,theano 要求它以 (3,x,x) 格式读取。我尝试更改订单 numpy.array([img[:,:,i] for i in range(3)])

which i guess gets the job done, but it is both ugly and i can't figure out how to reverse it to get the original image back.

我想这可以完成工作,但它既丑陋又我不知道如何反转它以恢复原始图像。

回答by Daniel M?ller

To reorder data

重新排序数据

You can use numpy.rollaxisto roll the axis 3 to position 1 (considering you have the batch size as dimension 0).

您可以使用numpy.rollaxis将轴 3 滚动到位置 1(考虑到批量大小为维度 0)。

np.rollaxis(imagesArray, 3, 1)  

But, if you're using keras, you might want to change its configuration or define it per layer. Theano doesn't require anything from you if you're using Keras.

但是,如果您使用 keras,您可能需要更改其配置或按层定义它。如果您使用的是 Keras,Theano 不需要您提供任何东西。

Keras can be configured with channels first or channels last, besides allowing you to define it in every individual layer, so you don't have to change your data.

Keras 可以首先配置通道或最后配置通道,此外还允许您在每个单独的层中定义它,因此您不必更改数据。

To configure keras

配置keras

Find the keras.jsonfile and change it. The file is usually installed in C:\Users\yourusername\.kerasor ~/.kerasdepending on your OS.

找到keras.json文件并更改它。该文件通常安装在C:\Users\yourusername\.keras~/.keras取决于您的操作系统。

Change "image_data_format": "channels_last"to "channels_first"or vice-versa, as you wish.

根据需要更改"image_data_format": "channels_last""channels_first"或反之亦然。

Usually, working with "channels_last" is less troublesome because of a great amount of other (non convolutional) functions that work only on the last axis.

通常,使用“channels_last”不会那么麻烦,因为大量其他(非卷积)函数仅在最后一个轴上工作。

Defining channel order in layers.

在层中定义通道顺序。

The Keras documentationhas all information about parameters for layers, including the data_formatparameter.

Keras文档有关于层参数,包括所有的信息data_format参数。

回答by cemsazara

I agree with @Qualia 's comment, np.moveaxis(a, source, destination)is easier to understand. This does the job:

我同意 @Qualia 的评论,np.moveaxis(a, source, destination)更容易理解。这可以完成以下工作:

x = np.zeros((12, 12, 3))
x.shape
#yields: 
(12, 12, 3)

x = np.moveaxis(x, -1, 0)
x.shape
#yields: 
(3, 12, 12)

回答by kato9167

Using np.moveaxisis effective, but I have found that np.einsumis much faster.

使用np.moveaxis是有效的,但我发现它np.einsum要快得多。

x = np.zeros((12,12,3))
%timeit np.moveaxis(x,-1,0)
#yields 7.46 μs ± 312 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit np.einsum('ijk->kij',x)
#yields 1.11 μs ± 31 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

回答by Noosh

x = np.zeros((12, 12, 3))
y = np.rollaxis(x, 2, 0)
y.shape

(3, 12, 12)