简体   繁体   English

numpy和Keras中“重塑”功能的差异

[英]Difference of function of “reshape” in numpy and Keras

My situation is using FCN to do pixel-wise predictions ,I have 7 classes ,so I feed image 512*512 and then provide the 512*512*7 for model to predict,I would like to reshape it in terms of (height width channels) to save the final outcome. 我的情况是使用FCN进行像素预测,我有7个类别,因此我输入图像512 * 512,然后提供512 * 512 * 7进行模型预测,我想根据(高度宽度渠道)以保存最终结果。

Since I use channels last,so the input shape makes sense to me.But the form of output shape that Keras splits out which I believe is: 因为我最后使用通道,所以输入形状对我来说很有意义。但是Keras拆分出的输出形状的形式我相信是:

                 (channels*height *width).

My try is using the function "reshape" and it works......which really confuses me,in my experiments,when I use reshape function the image would be totally messed up ,when in this case,it turns out working really well(using reshape function in Keras). 我的尝试是使用“重塑”功能,它起作用了……这确实使我感到困惑,在我的实验中,当我使用重塑功能时,图像会被完全弄乱,在这种情况下,它确实可以正常工作好(在Keras中使用重塑功能)。

pr = m.predict( np.array([X]))[0]

#reshape to channel last and take the largest index in 7 predictions for each piexl 
pr = pr.reshape(( output_height ,  output_width , n_classes ) ).argmax(axis=-1)

What I expected the way that should work well is supposed to be something like np moveaxis or numpy.rollaxis. 我所期望的工作方式应该是np moveaxis或numpy.rollaxis。 Thanks in advance! 提前致谢!

Keras is already channels_last by default, so you're probably doing nothing with that reshape. 默认情况下,Keras已经是channels_last ,因此您可能不执行任何重塑操作。

Check the model.summary() to see the shapes. 检查model.summary()以查看形状。

You're correct that reshaping will mess up the images if you intend to change channel order. 您是正确的,如果您打算更改频道顺序,则重塑会弄乱图像。 So you will be looking for a Permute((3,1,2)) layer to move last to first or Permute((2,3,1) to move first to last. 因此,您将寻找一个Permute((3,1,2))层以最后移至第一个或Permute((2,3,1)层以移至最后一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM