UpSampling layer in Swift

What is the proper way to do an upsampling (i.e. opposite of convolution) in Swift.

I tried the UpSampling layer but it seems that it is only multiplying the rows/columns by the giving factor:

let up2d1 = UpSampling2D<Float>(size: 2)
x = up2d1(x)
x.shape

For an input X with shape:

▿ [10, 7, 7, 256]
  ▿ dimensions : 4 elements
    - 0 : 10
    - 1 : 7
    - 2 : 7
    - 3 : 256

Gives this result:

▿ [10, 14, 14, 256]
  ▿ dimensions : 4 elements
    - 0 : 10
    - 1 : 14
    - 2 : 14
    - 3 : 256

Channels are not divided by the factor!!!

I guess you could try 1x1 conv layers to change the number of channels.

It does not seems to work, this is what I have tried

> Conv2D<Float>(filterShape: (1, 1, 1, 512), padding: .same, activation: identity)(x).shape

▿ [10, 14, 14, 512]
  ▿ dimensions : 4 elements
    - 0 : 10
    - 1 : 14
    - 2 : 14
    - 3 : 512
> Conv2D<Float>(filterShape: (1, 1, 1, 128), padding: .same, activation: identity)(x).shape

Fatal error: No algorithm worked!: file /swift-base/swift/stdlib/public/TensorFlow/CompilerRuntime.swift, line 2123
Current stack trace:
0    libswiftCore.so                    0x00007f60a3bf14a0 _swift_stdlib_reportFatalErrorInFile + 115
1    libswiftCore.so                    0x00007f60a3b3930c <unavailable> + 3035916
2    libswiftCore.so                    0x00007f60a3b393fe <unavailable> + 3036158
3    libswiftCore.so                    0x00007f60a39806c2 <unavailable> + 1230530
4    libswiftCore.so                    0x00007f60a3b06292 <unavailable> + 2826898
5    libswiftCore.so                    0x00007f60a397fba9 <unavailable> + 1227689
6    libswiftTensorFlow.so              0x00007f60a0f6e572 <unavailable> + 599410
7    libswiftTensorFlow.so              0x00007f60a0f6ccc0 checkOk(_:file:line:) + 508
8    libswiftTensorFlow.so              0x00007f60a0f91ad0 _TFCCheckOk(_:) + 81
9    libswiftTensorFlow.so              0x00007f60a0f91ac0 _swift_tfc_CheckOk + 9
Current stack trace:
	frame #9: 0x00007f60d12632b6 $__lldb_expr119`main at <Cell 14>:1:80

@bachir this worked for me

let conv = Conv2D<Float>(filterShape: (1, 1, 256, 128), padding: .same, activation: identity)
let up2d = UpSampling2D<Float>(size: 2)
let x = Tensor<Float>(zeros:[10, 7, 7, 256])
conv(up2d(x)).shape 

The output

▿ [10, 14, 14, 128]
  ▿ dimensions : 4 elements
    - 0 : 10
    - 1 : 14
    - 2 : 14
    - 3 : 128

You can test it on colab

https://colab.research.google.com/drive/1GxcUIkn6axe68AHZjJcp-3ea7ARBJBAZ

1 Like

OK that works, thanks @zaidalyafeai