5

I'd like to upsample one layer with size of (w,h,channels) to size of (w',h',channels), but the Upsample2D layer just can upsample to the double size.

Anybody could tell me how do any size upsampling?

1 Answer 1

5

The Keras UpSample2D can upsample to different sizes, not just double size. From the Keras docs we can see this is indicated for such layer:

keras.layers.UpSampling2D(size=(2, 2), data_format=None)

Upsampling layer for 2D inputs.

Repeats the rows and columns of the data by size[0] and size[1] respectively.

The default size value is indeed (2,2), so in that case your upsampling will be double. By specifying the size you desire you can manage to upsample to different sizes according to your needs. So, if you want an upsample factor of say, 3 then you should use size=(3,3), etc.

As alternatives, you can also define your own custom layers if you want something really specific to your case. For example, here is a Github issue about creating custom pooling function (opposite of upsampling layers, so easily comparable), which could help you in case you needed such custom layer.

4
  • Thanks for your reply. I did'n clarify my problem. I just want to upsample to five times. So I'll try your suggest to define custon layers.
    – spider
    Commented Nov 16, 2017 at 0:55
  • 2
    @spider if you want to upsample five times (that is for each row and column obtain five) then try with size=(5,5). Glad it helped :)
    – DarkCygnus
    Commented Nov 16, 2017 at 0:57
  • 1
    I just want to upsample to five times after two upsample layers, so I upsample to 2 times after one upsample layer, and upsample to 2.5 times in the successive another layer. I defined one custom upsample layer which can upsample to float number times according to your suggestion. Thank you!
    – spider
    Commented Nov 16, 2017 at 11:04
  • @spider sweet, I guess just be careful with rounding errors that may result in erratic upsampling. I once had problems upsampling, where I ended up with more rows,cols than desired because I was using prime numbers as image inputs, make sure your inputs and sample size don't give you this problem. Cheers.
    – DarkCygnus
    Commented Nov 16, 2017 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.