Message Boards Message Boards

Build a DeconvolutionLayer in 3D?

There are a many examples of 2D Convolutional Neural Networks, but i was trying to build one that works on 3D data. Most common Layers (e.g. ConvolutionLayer) work on any dimension, however the DeconvolutionLayer only works in 2D.

Does anyone know a solution/workaround for this?


EDIT

One option i found was to use a ResizeLayer which has no trainable parameters. However this also does not work in 3D. So down-scaling is possible in 3D but up-scaling I cannot find a method for.


EDIT2

Using a combination of ResizeLayer and Convolution layer in 2D i was able to get similar results as using a DeconvolutionLayer in a UNET. With this in mind and the use of TranposeLayers, FlattenLayers, ResizeLayers and ReshapeLayers I was able to generate the needed behaviour in 3D. Although not the most elegant solution it seem to do its job.

The combination of ResizeLayer and ConvolutionLayer still maintains trainable weights but just n x n less where n is the scale factor (see image below).

The code of the NetChain Blocks:

DeconvLayer2D[n_, {dimInx_, dimIny_}] := Block[{sc = 2}, 
  NetChain[{
    DeconvolutionLayer[n, {sc, sc}, "Stride" -> {sc, sc}, "Input" -> {sc n, dimInx, dimIny}]
}]]

ResizeLayer2D[n_, {dimInx_, dimIny_}] := Block[{sc = 2}, 
  NetChain[{
    ResizeLayer[{Scaled[sc], Scaled[sc]}, "Input" -> {sc n, dimInx, dimIny}], 
    ConvolutionLayer[n, 1]
}]]

ResizeLayer3D[n_, {dimInx_, dimIny_, dimInz_}] := Block[{sc = 2},
 NetChain[ {
    FlattenLayer[1, "Input" -> {n sc, dimInx, dimIny, dimInz}],
    ResizeLayer[{Scaled[sc], Scaled[sc]}], 
    ReshapeLayer[{n sc, dimInx, sc dimIny, sc dimInz}],
    TransposeLayer[2 <-> 3], 
    FlattenLayer[1],
    ResizeLayer[{Scaled[sc], Scaled[1]}], 
    ReshapeLayer[{n sc, sc dimIny, sc dimInx, sc dimInz}],
    TransposeLayer[2 <-> 3], 
    ConvolutionLayer[n, 1]
}]]

{DeconvLayer2D[16, {2, 4}], ResizeLayer2D[16, {2, 4}], ResizeLayer3D[16, {2, 4, 6}]}

Deconvolution3D

POSTED BY: Martijn Froeling
2 Replies

Just a note, ResizeLayer has been updated to work on arbitrary rank

ResizeLayer[{1, 2, 3, 4, 5}][RandomReal[1, {3, 2, 2, 2, 2, 2}]] // Dimensions

(* {3, 1, 2, 3, 4, 5} *)

Awesome! Time for some recoding then.

POSTED BY: Martijn Froeling
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract