1 | /**
|
2 | * @license
|
3 | * Copyright 2020 Google LLC. All Rights Reserved.
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | * =============================================================================
|
16 | */
|
17 | import { ENGINE } from '../engine';
|
18 | import { MaxPool } from '../kernel_names';
|
19 | import { convertToTensor } from '../tensor_util_env';
|
20 | import * as util from '../util';
|
21 | import * as conv_util from './conv_util';
|
22 | import { op } from './operation';
|
23 | import { reshape } from './reshape';
|
24 | /**
|
25 | * Computes the 2D max pooling of an image.
|
26 | *
|
27 | * @param x The input tensor, of rank 4 or rank 3 of shape
|
28 | * `[batch, height, width, inChannels]`. If rank 3, batch of 1 is assumed.
|
29 | * @param filterSize The filter size: `[filterHeight, filterWidth]`. If
|
30 | * `filterSize` is a single number, then `filterHeight == filterWidth`.
|
31 | * @param strides The strides of the pooling: `[strideHeight, strideWidth]`. If
|
32 | * `strides` is a single number, then `strideHeight == strideWidth`.
|
33 | * @param dilations The dilation rates: `[dilationHeight, dilationWidth]`
|
34 | * in which we sample input values across the height and width dimensions
|
35 | * in dilated pooling. Defaults to `[1, 1]`. If `dilations` is a single
|
36 | * number, then `dilationHeight == dilationWidth`. If it is greater than
|
37 | * 1, then all values of `strides` must be 1.
|
38 | * @param pad The type of padding algorithm.
|
39 | * - `same` and stride 1: output will be of same size as input,
|
40 | * regardless of filter size.
|
41 | * - `valid`: output will be smaller than input if filter is larger
|
42 | * than 1x1.
|
43 | * - For more info, see this guide:
|
44 | * [https://www.tensorflow.org/api_guides/python/nn#Convolution](
|
45 | * https://www.tensorflow.org/api_guides/python/nn#Convolution)
|
46 | * @param dimRoundingMode A string from: 'ceil', 'round', 'floor'. If none is
|
47 | * provided, it will default to truncate.
|
48 | */
|
49 | function maxPool_(x, filterSize, strides, pad, dimRoundingMode) {
|
50 | const $x = convertToTensor(x, 'x', 'maxPool');
|
51 | const dilations = 1;
|
52 | let x4D = $x;
|
53 | let reshapedTo4D = false;
|
54 | if ($x.rank === 3) {
|
55 | reshapedTo4D = true;
|
56 | x4D = reshape($x, [1, $x.shape[0], $x.shape[1], $x.shape[2]]);
|
57 | }
|
58 | util.assert(x4D.rank === 4, () => `Error in maxPool: input must be rank 4 but got rank ${x4D.rank}.`);
|
59 | util.assert(conv_util.eitherStridesOrDilationsAreOne(strides, dilations), () => 'Error in maxPool: Either strides or dilations must be 1. ' +
|
60 | `Got strides ${strides} and dilations '${dilations}'`);
|
61 | if (dimRoundingMode != null) {
|
62 | util.assert(util.isInt(pad), () => `Error in maxPool: pad must be an integer when using, ` +
|
63 | `dimRoundingMode ${dimRoundingMode} but got pad ${pad}.`);
|
64 | }
|
65 | const inputs = { x: x4D };
|
66 | const attrs = { filterSize, strides, pad, dimRoundingMode };
|
67 | // tslint:disable-next-line: no-unnecessary-type-assertion
|
68 | const res = ENGINE.runKernel(MaxPool, inputs, attrs);
|
69 | if (reshapedTo4D) {
|
70 | return reshape(res, [res.shape[1], res.shape[2], res.shape[3]]);
|
71 | }
|
72 | return res;
|
73 | }
|
74 | export const maxPool = op({ maxPool_ });
|
75 | //# sourceMappingURL=max_pool.js.map |
\ | No newline at end of file |