|
| 1 | +submodule(nf_maxpool2d_layer) nf_maxpool2d_layer_submodule |
| 2 | + |
| 3 | + implicit none |
| 4 | + |
| 5 | +contains |
| 6 | + |
| 7 | + pure module function maxpool2d_layer_cons(pool_size, stride) result(res) |
| 8 | + implicit none |
| 9 | + integer, intent(in) :: pool_size |
| 10 | + integer, intent(in) :: stride |
| 11 | + type(maxpool2d_layer) :: res |
| 12 | + res % pool_size = pool_size |
| 13 | + res % stride = stride |
| 14 | + end function maxpool2d_layer_cons |
| 15 | + |
| 16 | + |
| 17 | + module subroutine init(self, input_shape) |
| 18 | + implicit none |
| 19 | + class(maxpool2d_layer), intent(in out) :: self |
| 20 | + integer, intent(in) :: input_shape(:) |
| 21 | + |
| 22 | + self % channels = input_shape(1) |
| 23 | + self % width = input_shape(2) / self % stride |
| 24 | + self % height = input_shape(3) / self % stride |
| 25 | + |
| 26 | + allocate(self % maxloc_x(self % channels, self % width, self % height)) |
| 27 | + self % maxloc_x = 0 |
| 28 | + |
| 29 | + allocate(self % maxloc_y(self % channels, self % width, self % height)) |
| 30 | + self % maxloc_y = 0 |
| 31 | + |
| 32 | + allocate(self % output(self % channels, self % width, self % height)) |
| 33 | + self % output = 0 |
| 34 | + |
| 35 | + end subroutine init |
| 36 | + |
| 37 | + |
| 38 | + pure module subroutine forward(self, input) |
| 39 | + implicit none |
| 40 | + class(maxpool2d_layer), intent(in out) :: self |
| 41 | + real, intent(in) :: input(:,:,:) |
| 42 | + integer :: input_width, input_height |
| 43 | + integer :: i, j, n |
| 44 | + integer :: ii, jj |
| 45 | + integer :: iend, jend |
| 46 | + integer :: maxloc_xy(2) |
| 47 | + |
| 48 | + input_width = size(input, dim=2) |
| 49 | + input_height = size(input, dim=2) |
| 50 | + |
| 51 | + ! Stride along the width and height of the input image |
| 52 | + stride_over_input: do concurrent( & |
| 53 | + i = 1:input_width:self % stride, & |
| 54 | + j = 1:input_height:self % stride & |
| 55 | + ) |
| 56 | + |
| 57 | + ! Indices of the pooling layer |
| 58 | + ii = i / self % stride + 1 |
| 59 | + jj = j / self % stride + 1 |
| 60 | + |
| 61 | + iend = i + self % pool_size - 1 |
| 62 | + jend = j + self % pool_size - 1 |
| 63 | + |
| 64 | + maxpool_for_each_channel: do concurrent(n = 1:self % channels) |
| 65 | + |
| 66 | + ! Get and store the location of the maximum value |
| 67 | + maxloc_xy = maxloc(input(n,i:iend,j:jend)) |
| 68 | + self % maxloc_x(n,ii,jj) = maxloc_xy(1) + i - 1 |
| 69 | + self % maxloc_y(n,ii,jj) = maxloc_xy(2) + j - 1 |
| 70 | + |
| 71 | + self % output(n,ii,jj) = & |
| 72 | + input(n,self % maxloc_x(n,ii,jj),self % maxloc_y(n,ii,jj)) |
| 73 | + |
| 74 | + end do maxpool_for_each_channel |
| 75 | + |
| 76 | + end do stride_over_input |
| 77 | + |
| 78 | + end subroutine forward |
| 79 | + |
| 80 | + |
| 81 | + module subroutine backward(self, input, gradient) |
| 82 | + implicit none |
| 83 | + class(maxpool2d_layer), intent(in out) :: self |
| 84 | + real, intent(in) :: input(:,:,:) |
| 85 | + real, intent(in) :: gradient(:,:,:) |
| 86 | + print *, 'Warning: maxpool2d backward pass not implemented' |
| 87 | + end subroutine backward |
| 88 | + |
| 89 | +end submodule nf_maxpool2d_layer_submodule |
0 commit comments