Pro_gram_mer
Pro_gram_mer

Reputation: 817

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 (heightwidthchannels) 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!

Upvotes: 1

Views: 493

Answers (1)

Daniel Möller
Daniel Möller

Reputation: 86600

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.

Upvotes: 1

Related Questions