简体   繁体   中英

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.

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:

                 (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).

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. Thanks in advance!

Keras is already channels_last by default, so you're probably doing nothing with that reshape.

Check the model.summary() to see the shapes.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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