|
| 1 | +using Tensorflow.Keras.ArgsDefinition; |
| 2 | +using Tensorflow.Keras.Engine; |
| 3 | + |
| 4 | +namespace Tensorflow.Keras.Layers { |
| 5 | + /// <summary> |
| 6 | + /// Crop the input along axis 1 and 2. |
| 7 | + /// <para> For example: </para> |
| 8 | + /// <para> shape (1, 5, 5, 5) -- crop2D ((1, 2), (1, 3)) --> shape (1, 2, 1, 5) </para> |
| 9 | + /// </summary> |
| 10 | + public class Cropping2D : Layer { |
| 11 | + Cropping2DArgs args; |
| 12 | + public Cropping2D ( Cropping2DArgs args ) : base(args) { |
| 13 | + this.args = args; |
| 14 | + } |
| 15 | + protected override void build ( Tensors inputs ) { |
| 16 | + built = true; |
| 17 | + } |
| 18 | + protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) { |
| 19 | + Tensor output = inputs; |
| 20 | + if ( output.rank != 4 ) { |
| 21 | + // throw an ValueError exception |
| 22 | + throw new ValueError("Expected dim=4, found dim=" + output.rank); |
| 23 | + } |
| 24 | + if ( args.cropping.shape == new Shape(1) ) { |
| 25 | + int crop = args.cropping[0]; |
| 26 | + if ( args.data_format == Cropping2DArgs.DataFormat.channels_last ) { |
| 27 | + output = output[new Slice(), |
| 28 | + new Slice(crop, ( int ) output.shape[1] - crop), |
| 29 | + new Slice(crop, ( int ) output.shape[2] - crop), |
| 30 | + new Slice()]; |
| 31 | + } |
| 32 | + else { |
| 33 | + output = output[new Slice(), |
| 34 | + new Slice(), |
| 35 | + new Slice(crop, ( int ) output.shape[2] - crop), |
| 36 | + new Slice(crop, ( int ) output.shape[3] - crop)]; |
| 37 | + } |
| 38 | + } |
| 39 | + // a tuple of 2 integers |
| 40 | + else if ( args.cropping.shape == new Shape(2) ) { |
| 41 | + int crop_1 = args.cropping[0]; |
| 42 | + int crop_2 = args.cropping[1]; |
| 43 | + if ( args.data_format == Cropping2DArgs.DataFormat.channels_last ) { |
| 44 | + output = output[new Slice(), |
| 45 | + new Slice(crop_1, ( int ) output.shape[1] - crop_1), |
| 46 | + new Slice(crop_2, ( int ) output.shape[2] - crop_2), |
| 47 | + new Slice()]; |
| 48 | + } |
| 49 | + else { |
| 50 | + output = output[new Slice(), |
| 51 | + new Slice(), |
| 52 | + new Slice(crop_1, ( int ) output.shape[2] - crop_1), |
| 53 | + new Slice(crop_2, ( int ) output.shape[3] - crop_2)]; |
| 54 | + } |
| 55 | + } |
| 56 | + else if ( args.cropping.shape[0] == 2 && args.cropping.shape[1] == 2 ) { |
| 57 | + int x_start = args.cropping[0, 0], x_end = args.cropping[0, 1]; |
| 58 | + int y_start = args.cropping[1, 0], y_end = args.cropping[1, 1]; |
| 59 | + if ( args.data_format == Cropping2DArgs.DataFormat.channels_last ) { |
| 60 | + output = output[new Slice(), |
| 61 | + new Slice(x_start, ( int ) output.shape[1] - x_end), |
| 62 | + new Slice(y_start, ( int ) output.shape[2] - y_end), |
| 63 | + new Slice()]; |
| 64 | + } |
| 65 | + else { |
| 66 | + output = output[new Slice(), |
| 67 | + new Slice(), |
| 68 | + new Slice(x_start, ( int ) output.shape[2] - x_end), |
| 69 | + new Slice(y_start, ( int ) output.shape[3] - y_end) |
| 70 | + ]; |
| 71 | + } |
| 72 | + } |
| 73 | + return output; |
| 74 | + } |
| 75 | + |
| 76 | + public override Shape ComputeOutputShape ( Shape input_shape ) { |
| 77 | + if ( args.cropping.shape == new Shape(1) ) { |
| 78 | + int crop = args.cropping[0]; |
| 79 | + if ( args.data_format == Cropping2DArgs.DataFormat.channels_last ) { |
| 80 | + return new Shape(( int ) input_shape[0], ( int ) input_shape[1] - crop * 2, ( int ) input_shape[2] - crop * 2, ( int ) input_shape[3]); |
| 81 | + } |
| 82 | + else { |
| 83 | + return new Shape(( int ) input_shape[0], ( int ) input_shape[1], ( int ) input_shape[2] - crop * 2, ( int ) input_shape[3] - crop * 2); |
| 84 | + } |
| 85 | + } |
| 86 | + // a tuple of 2 integers |
| 87 | + else if ( args.cropping.shape == new Shape(2) ) { |
| 88 | + int crop_1 = args.cropping[0], crop_2 = args.cropping[1]; |
| 89 | + if ( args.data_format == Cropping2DArgs.DataFormat.channels_last ) { |
| 90 | + return new Shape(( int ) input_shape[0], ( int ) input_shape[1] - crop_1 * 2, ( int ) input_shape[2] - crop_2 * 2, ( int ) input_shape[3]); |
| 91 | + } |
| 92 | + else { |
| 93 | + return new Shape(( int ) input_shape[0], ( int ) input_shape[1], ( int ) input_shape[2] - crop_1 * 2, ( int ) input_shape[3] - crop_2 * 2); |
| 94 | + } |
| 95 | + } |
| 96 | + else if ( args.cropping.shape == new Shape(2, 2) ) { |
| 97 | + int crop_1_start = args.cropping[0, 0], crop_1_end = args.cropping[0, 1]; |
| 98 | + int crop_2_start = args.cropping[1, 0], crop_2_end = args.cropping[1, 1]; |
| 99 | + if ( args.data_format == Cropping2DArgs.DataFormat.channels_last ) { |
| 100 | + return new Shape(( int ) input_shape[0], ( int ) input_shape[1] - crop_1_start - crop_1_end, |
| 101 | + ( int ) input_shape[2] - crop_2_start - crop_2_end, ( int ) input_shape[3]); |
| 102 | + } |
| 103 | + else { |
| 104 | + return new Shape(( int ) input_shape[0], ( int ) input_shape[1], |
| 105 | + ( int ) input_shape[2] - crop_1_start - crop_1_end, ( int ) input_shape[3] - crop_2_start - crop_2_end); |
| 106 | + } |
| 107 | + } |
| 108 | + else { |
| 109 | + throw new ValueError(); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments