5
$\begingroup$

The problem I am stuck with is in cropping tensors at certain parts of the network to obtain specific dimensions.

I am trying to construct the U-net convolutional neural network. Almost all of the components of this are well covered in the MMA neural network framework; the contraction part of the network was straightforward but the expansion path is more difficult.

At several locations in the network I need to crop a layer to match that of another in a different part of the network. For example, I need to concatenate a layer with dimensions {1,568,568} with another of dimensions {1,392,392} and need to match the dimensions of the two by cropping from the first.

PaddingLayer is close but (unlike ArrayPad) does not permit negative padding values, that is, something like PaddingLayer[{-88,-88}] would do the trick but it isn't possible. ResizeLayer is not the answer because it introduces some form of interpolation.

Is there some other way of dealing with this?

$\endgroup$

1 Answer 1

8
$\begingroup$

PartLayer is close, but it can only crop the first dimension. You can use PartLayer in combination with TransposeLayer to crop tensors:

data = {Table[i + j, {i, 10}, {j, 10}]};
Dimensions[data]

{1, 10, 10}

NetChain[{TransposeLayer[3 -> 1],
   TransposeLayer[1 -> 2],
   PartLayer[2 ;; 3],
   TransposeLayer[1 -> 2],
   PartLayer[2 ;; 3],
   TransposeLayer[1 -> 3]}]@data

{{{4.}, {5.}}, {{5.}, {6.}}}

$\endgroup$
5
  • $\begingroup$ That looks pretty close to what I needed (I should have looked more closely at PartLayer) thanks. Shouldn't the last TransposeLayer applied be TransposeLayer[3->1] (to get dimensions of {1,2,2}? $\endgroup$
    – EstabanW
    Commented Jan 14, 2018 at 16:16
  • $\begingroup$ You're right, the result was transposed, should be fixed now. It would be more comfortable if PartLayer could take crop tensors they way Part does $\endgroup$ Commented Jan 14, 2018 at 17:41
  • $\begingroup$ I'll leave the post for a day or two in case another answer shows up before accepting $\endgroup$
    – EstabanW
    Commented Jan 14, 2018 at 18:22
  • 2
    $\begingroup$ Just an FYI: PartLayer in 11.3 supports operation on any number of dimensions. $\endgroup$
    – Sebastian
    Commented Jan 15, 2018 at 14:38
  • $\begingroup$ @NikiEstner can you kindly edit your answer to provide a minimal example in which you take outputs from two layers of a network and crop the first layer with regards to the dimensions of the second layer such that dimensions of first equals the second (of course we dont know the arguments to the PartLayer in advance). I dont know but your current answer shows that the arguments to the PartLayer need to be known. How can we implement it as a function keeping things symbolic? thanks ! $\endgroup$
    – Ali Hashmi
    Commented May 1, 2018 at 10:57

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.