# Cropping in neural networks

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?

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.}}}

• 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}? – EstabanW Jan 14 '18 at 16:16
• 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 – Niki Estner Jan 14 '18 at 17:41
• I'll leave the post for a day or two in case another answer shows up before accepting – EstabanW Jan 14 '18 at 18:22
• Just an FYI: PartLayer in 11.3 supports operation on any number of dimensions. – Sebastian Jan 15 '18 at 14:38
• @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 ! – Ali Hashmi May 1 '18 at 10:57