|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320 |
- #! /usr/bin/python
- # -*- coding: utf-8 -*-
-
- import tensorlayer as tl
- from tensorlayer import logging
- from tensorlayer.layers.core import Module
-
- __all__ = [
- 'PoolLayer',
- 'MaxPool1d',
- 'MeanPool1d',
- 'MaxPool2d',
- 'MeanPool2d',
- 'MaxPool3d',
- 'MeanPool3d',
- 'GlobalMaxPool1d',
- 'GlobalMeanPool1d',
- 'GlobalMaxPool2d',
- 'GlobalMeanPool2d',
- 'GlobalMaxPool3d',
- 'GlobalMeanPool3d',
- 'AdaptiveMeanPool1d',
- 'AdaptiveMeanPool2d',
- 'AdaptiveMeanPool3d',
- 'AdaptiveMaxPool1d',
- 'AdaptiveMaxPool2d',
- 'AdaptiveMaxPool3d',
- 'CornerPool2d',
- ]
-
-
- class PoolLayer(Module):
- """
- The :class:`PoolLayer` class is a Pooling layer.
- You can choose ``tl.ops.max_pool`` and ``tl.ops.avg_pool`` for 2D input or
- ``tl.ops.max_pool3d`` and ``tl.ops.avg_pool3d`` for 3D input.
-
- Parameters
- ----------
- filter_size : tuple of int
- The size of the window for each dimension of the input tensor.
- Note that: len(filter_size) >= 4.
- strides : tuple of int
- The stride of the sliding window for each dimension of the input tensor.
- Note that: len(strides) >= 4.
- padding : str
- The padding algorithm type: "SAME" or "VALID".
- pool : pooling function
- One of ``tl.ops.max_pool``, ``tl.ops.avg_pool``, ``tl.ops.max_pool3d`` and ``f.ops.avg_pool3d``.
- See `TensorFlow pooling APIs <https://tensorflow.google.cn/versions/r2.0/api_docs/python/tf/nn/>`__
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 50, 50, 32], name='input')
- >>> net = tl.layers.PoolLayer()(net)
- >>> output shape : [None, 25, 25, 32]
-
- """
-
- def __init__(
- self,
- filter_size=(1, 2, 2, 1),
- strides=(1, 2, 2, 1),
- padding='SAME',
- pool=tl.ops.MaxPool,
- name=None # 'pool_pro',
- ):
- super().__init__(name)
- self.filter_size = filter_size
- self.strides = strides
- self.padding = padding
- self.pool = pool
-
- self.build()
- self._built = True
-
- logging.info(
- "PoolLayer %s: filter_size: %s strides: %s padding: %s pool: %s" %
- (self.name, str(self.filter_size), str(self.strides), self.padding, pool.__name__)
- )
-
- def __repr__(self):
- s = '{classname}(pool={poolname}, filter_size={strides}, padding={padding}'
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, poolname=self.pool.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- self._pool = self.pool(ksize=self.filter_size, strides=self.strides, padding=self.padding)
-
- def forward(self, inputs):
- outputs = self._pool(inputs)
- return outputs
-
-
- class MaxPool1d(Module):
- """Max pooling for 1D signal.
-
- Parameters
- ----------
- filter_size : int
- Pooling window size.
- strides : int
- Stride of the pooling operation.
- padding : str
- The padding method: 'VALID' or 'SAME'.
- data_format : str
- One of channels_last (default, [batch, length, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 50, 32], name='input')
- >>> net = tl.layers.MaxPool1d(filter_size=3, strides=2, padding='SAME', name='maxpool1d')(net)
- >>> output shape : [None, 25, 32]
-
- """
-
- def __init__(
- self,
- filter_size=3,
- strides=2,
- padding='SAME',
- data_format='channels_last',
- dilation_rate=1,
- name=None # 'maxpool1d'
- ):
- super().__init__(name)
- self.filter_size = self._filter_size = filter_size
- self.strides = self._strides = strides
- self.padding = padding
- self.data_format = data_format
- self.dilation_rate = self._dilation_rate = dilation_rate
-
- self.build()
- self._built = True
-
- logging.info(
- "MaxPool1d %s: filter_size: %s strides: %s padding: %s" %
- (self.name, str(filter_size), str(strides), str(padding))
- )
-
- def __repr__(self):
- s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
- if self.dilation_rate != 1:
- s += ', dilation={dilation_rate}'
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- # https://tensorflow.google.cn/versions/r2.0/api_docs/python/tf/nn/pool
- if self.data_format == 'channels_last':
- self.data_format = 'NWC'
- elif self.data_format == 'channels_first':
- self.data_format = 'NCW'
- else:
- raise Exception("unsupported data format")
- self._filter_size = [self.filter_size]
- self._strides = [self.strides]
- self._dilation_rate = [self.dilation_rate]
-
- def forward(self, inputs):
- outputs = tl.ops.pool(
- input=inputs,
- window_shape=self._filter_size,
- pooling_type="MAX",
- strides=self._strides,
- padding=self.padding,
- data_format=self.data_format,
- dilations=self._dilation_rate,
- )
- return outputs
-
-
- class MeanPool1d(Module):
- """Mean pooling for 1D signal.
-
- Parameters
- ------------
- filter_size : int
- Pooling window size.
- strides : int
- Strides of the pooling operation.
- padding : str
- The padding method: 'VALID' or 'SAME'.
- data_format : str
- One of channels_last (default, [batch, length, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 50, 32], name='input')
- >>> net = tl.layers.MeanPool1d(filter_size=3, strides=2, padding='SAME')(net)
- >>> output shape : [None, 25, 32]
-
- """
-
- def __init__(
- self,
- filter_size=3,
- strides=2,
- padding='SAME',
- data_format='channels_last',
- dilation_rate=1,
- name=None # 'meanpool1d'
- ):
- super().__init__(name)
- self.filter_size = self._filter_size = filter_size
- self.strides = self._strides = strides
- self.padding = padding
- self.data_format = data_format
- self.dilation_rate = self._dilation_rate = dilation_rate
-
- self.build()
- self._built = True
-
- logging.info(
- "MeanPool1d %s: filter_size: %s strides: %s padding: %s" %
- (self.name, str(filter_size), str(strides), str(padding))
- )
-
- def __repr__(self):
- s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
- if self.dilation_rate != 1:
- s += ', dilation={dilation_rate}'
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- # https://tensorflow.google.cn/versions/r2.0/api_docs/python/tf/nn/pool
- if self.data_format == 'channels_last':
- self.data_format = 'NWC'
- elif self.data_format == 'channels_first':
- self.data_format = 'NCW'
- else:
- raise Exception("unsupported data format")
- self._filter_size = [self.filter_size]
- self._strides = [self.strides]
- self._dilation_rate = [self.dilation_rate]
-
- def forward(self, inputs):
- outputs = tl.ops.pool(
- input=inputs, window_shape=self._filter_size, pooling_type="AVG", padding=self.padding,
- dilations=self._dilation_rate, strides=self._strides, data_format=self.data_format
- )
- return outputs
-
-
- class MaxPool2d(Module):
- """Max pooling for 2D image.
-
- Parameters
- -----------
- filter_size : tuple of int
- (height, width) for filter size.
- strides : tuple of int
- (height, width) for strides.
- padding : str
- The padding method: 'VALID' or 'SAME'.
- data_format : str
- One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 50, 50, 32], name='input')
- >>> net = tl.layers.MaxPool2d(filter_size=(3, 3), strides=(2, 2), padding='SAME')(net)
- >>> output shape : [None, 25, 25, 32]
-
- """
-
- def __init__(
- self,
- filter_size=(3, 3),
- strides=(2, 2),
- padding='SAME',
- data_format='channels_last',
- name=None # 'maxpool2d'
- ):
- super().__init__(name)
- self.filter_size = filter_size
- if strides is None:
- strides = filter_size
- self.strides = self._strides = strides
- self.padding = padding
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info(
- "MaxPool2d %s: filter_size: %s strides: %s padding: %s" %
- (self.name, str(filter_size), str(strides), str(padding))
- )
-
- def __repr__(self):
- s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.data_format = 'NHWC'
- self._strides = [1, self.strides[0], self.strides[1], 1]
- elif self.data_format == 'channels_first':
- self.data_format = 'NCHW'
- self._strides = [1, 1, self.strides[0], self.strides[1]]
- else:
- raise Exception("unsupported data format")
-
- self.max_pool = tl.ops.MaxPool(
- ksize=self.filter_size, strides=self._strides, padding=self.padding, data_format=self.data_format
- )
-
- def forward(self, inputs):
- outputs = self.max_pool(inputs)
- return outputs
-
-
- class MeanPool2d(Module):
- """Mean pooling for 2D image [batch, height, width, channel].
-
- Parameters
- -----------
- filter_size : tuple of int
- (height, width) for filter size.
- strides : tuple of int
- (height, width) for strides.
- padding : str
- The padding method: 'VALID' or 'SAME'.
- data_format : str
- One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 50, 50, 32], name='input')
- >>> net = tl.layers.MeanPool2d(filter_size=(3, 3), strides=(2, 2), padding='SAME')(net)
- >>> output shape : [None, 25, 25, 32]
-
- """
-
- def __init__(
- self,
- filter_size=(3, 3),
- strides=(2, 2),
- padding='SAME',
- data_format='channels_last',
- name=None # 'meanpool2d'
- ):
- super().__init__(name)
- self.filter_size = filter_size
- if strides is None:
- strides = filter_size
- self.strides = self._strides = strides
- self.padding = padding
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info(
- "MeanPool2d %s: filter_size: %s strides: %s padding: %s" %
- (self.name, str(filter_size), str(strides), str(padding))
- )
-
- def __repr__(self):
- s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.data_format = 'NHWC'
- self._strides = [1, self.strides[0], self.strides[1], 1]
- elif self.data_format == 'channels_first':
- self.data_format = 'NCHW'
- self._strides = [1, 1, self.strides[0], self.strides[1]]
- else:
- raise Exception("unsupported data format")
- self.avg_pool = tl.ops.AvgPool(
- ksize=self.filter_size, strides=self._strides, padding=self.padding, data_format=self.data_format
- )
-
- def forward(self, inputs):
- outputs = self.avg_pool(inputs)
- return outputs
-
-
- class MaxPool3d(Module):
- """Max pooling for 3D volume.
-
- Parameters
- ------------
- filter_size : tuple of int
- Pooling window size.
- strides : tuple of int
- Strides of the pooling operation.
- padding : str
- The padding method: 'VALID' or 'SAME'.
- data_format : str
- One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Returns
- -------
- :class:`tf.Tensor`
- A max pooling 3-D layer with a output rank as 5.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 50, 50, 50, 32], name='input')
- >>> net = tl.layers.MaxPool3d(filter_size=(3, 3, 3), strides=(2, 2, 2), padding='SAME')(net)
- >>> output shape : [None, 25, 25, 25, 32]
-
- """
-
- def __init__(
- self,
- filter_size=(3, 3, 3),
- strides=(2, 2, 2),
- padding='VALID',
- data_format='channels_last',
- name=None # 'maxpool3d'
- ):
- super().__init__(name)
- self.filter_size = filter_size
- self.strides = self._strides = strides
- self.padding = padding
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info(
- "MaxPool3d %s: filter_size: %s strides: %s padding: %s" %
- (self.name, str(filter_size), str(strides), str(padding))
- )
-
- def __repr__(self):
- s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.data_format = 'NDHWC'
- self._strides = [1, self.strides[0], self.strides[1], self.strides[2], 1]
- elif self.data_format == 'channels_first':
- self.data_format = 'NCDHW'
- self._strides = [1, 1, self.strides[0], self.strides[1], self.strides[2]]
- else:
- raise Exception("unsupported data format")
-
- def forward(self, inputs):
- outputs = tl.ops.max_pool3d(
- input=inputs,
- ksize=self.filter_size,
- strides=self._strides,
- padding=self.padding,
- data_format=self.data_format,
- )
- return outputs
-
-
- class MeanPool3d(Module):
- """Mean pooling for 3D volume.
-
- Parameters
- ------------
- filter_size : tuple of int
- Pooling window size.
- strides : tuple of int
- Strides of the pooling operation.
- padding : str
- The padding method: 'VALID' or 'SAME'.
- data_format : str
- One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Returns
- -------
- :class:`tf.Tensor`
- A mean pooling 3-D layer with a output rank as 5.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 50, 50, 50, 32], name='input')
- >>> net = tl.layers.MeanPool3d(filter_size=(3, 3, 3), strides=(2, 2, 2), padding='SAME')(net)
- >>> output shape : [None, 25, 25, 25, 32]
-
- """
-
- def __init__(
- self,
- filter_size=(3, 3, 3),
- strides=(2, 2, 2),
- padding='VALID',
- data_format='channels_last',
- name=None # 'meanpool3d'
- ):
- super().__init__(name)
- self.filter_size = filter_size
- self.strides = self._strides = strides
- self.padding = padding
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info(
- "MeanPool3d %s: filter_size: %s strides: %s padding: %s" %
- (self.name, str(filter_size), str(strides), str(padding))
- )
-
- def __repr__(self):
- s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- self._strides = [1, self.strides[0], self.strides[1], self.strides[2], 1]
- if self.data_format == 'channels_last':
- self.data_format = 'NDHWC'
- elif self.data_format == 'channels_first':
- self.data_format = 'NCDHW'
- else:
- raise Exception("unsupported data format")
-
- def forward(self, inputs):
- outputs = tl.ops.avg_pool3d(
- input=inputs,
- ksize=self.filter_size,
- strides=self._strides,
- padding=self.padding,
- data_format=self.data_format,
- )
- return outputs
-
-
- class GlobalMaxPool1d(Module):
- """The :class:`GlobalMaxPool1d` class is a 1D Global Max Pooling layer.
-
- Parameters
- ------------
- data_format : str
- One of channels_last (default, [batch, length, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 100, 30], name='input')
- >>> net = tl.layers.GlobalMaxPool1d()(net)
- >>> output shape : [None, 30]
-
- """
-
- def __init__(
- self,
- data_format="channels_last",
- name=None # 'globalmaxpool1d'
- ):
- super().__init__(name)
-
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("GlobalMaxPool1d %s" % self.name)
-
- def __repr__(self):
- s = '{classname}('
- if self.name is not None:
- s += 'name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.reduce_max = tl.ReduceMax(axis=1)
- elif self.data_format == 'channels_first':
- self.reduce_max = tl.ReduceMax(axis=2)
- else:
- raise ValueError(
- "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
- )
-
- def forward(self, inputs):
- outputs = self.reduce_max(inputs)
- return outputs
-
-
- class GlobalMeanPool1d(Module):
- """The :class:`GlobalMeanPool1d` class is a 1D Global Mean Pooling layer.
-
- Parameters
- ------------
- data_format : str
- One of channels_last (default, [batch, length, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 100, 30], name='input')
- >>> net = tl.layers.GlobalMeanPool1d()(net)
- >>> output shape : [None, 30]
-
- """
-
- def __init__(
- self,
- data_format='channels_last',
- name=None # 'globalmeanpool1d'
- ):
- super().__init__(name)
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("GlobalMeanPool1d %s" % self.name)
-
- def __repr__(self):
- s = '{classname}('
- if self.name is not None:
- s += 'name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.reduce_mean = tl.ReduceMean(axis=1)
- elif self.data_format == 'channels_first':
- self.reduce_mean = tl.ReduceMean(axis=2)
- else:
- raise ValueError(
- "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
- )
-
- def forward(self, inputs):
- outputs = self.reduce_mean(inputs)
- return outputs
-
-
- class GlobalMaxPool2d(Module):
- """The :class:`GlobalMaxPool2d` class is a 2D Global Max Pooling layer.
-
- Parameters
- ------------
- data_format : str
- One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 100, 100, 30], name='input')
- >>> net = tl.layers.GlobalMaxPool2d()(net)
- >>> output shape : [None, 30]
-
- """
-
- def __init__(
- self,
- data_format='channels_last',
- name=None # 'globalmaxpool2d'
- ):
- super().__init__(name)
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("GlobalMaxPool2d %s" % self.name)
-
- def __repr__(self):
- s = '{classname}('
- if self.name is not None:
- s += 'name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.reduce_max = tl.ReduceMax(axis=[1, 2])
- elif self.data_format == 'channels_first':
- self.reduce_max = tl.ReduceMax(axis=[2, 3])
- else:
- raise ValueError(
- "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
- )
-
- def forward(self, inputs):
- outputs = self.reduce_max(inputs)
- return outputs
-
-
- class GlobalMeanPool2d(Module):
- """The :class:`GlobalMeanPool2d` class is a 2D Global Mean Pooling layer.
-
- Parameters
- ------------
- data_format : str
- One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 100, 100, 30], name='input')
- >>> net = tl.layers.GlobalMeanPool2d()(net)
- >>> output shape : [None, 30]
-
- """
-
- def __init__(
- self,
- data_format='channels_last',
- name=None # 'globalmeanpool2d'
- ):
- super().__init__(name)
-
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("GlobalMeanPool2d %s" % self.name)
-
- def __repr__(self):
- s = '{classname}('
- if self.name is not None:
- s += 'name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.reduce_mean = tl.ReduceMean(axis=[1, 2])
- elif self.data_format == 'channels_first':
- self.reduce_mean = tl.ReduceMean(axis=[2, 3])
- else:
- raise ValueError(
- "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
- )
-
- def forward(self, inputs):
- outputs = self.reduce_mean(inputs)
- return outputs
-
-
- class GlobalMaxPool3d(Module):
- """The :class:`GlobalMaxPool3d` class is a 3D Global Max Pooling layer.
-
- Parameters
- ------------
- data_format : str
- One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 100, 100, 100, 30], name='input')
- >>> net = tl.layers.GlobalMaxPool3d()(net)
- >>> output shape : [None, 30]
-
- """
-
- def __init__(
- self,
- data_format='channels_last',
- name=None # 'globalmaxpool3d'
- ):
- super().__init__(name)
-
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("GlobalMaxPool3d %s" % self.name)
-
- def __repr__(self):
- s = '{classname}('
- if self.name is not None:
- s += 'name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.reduce_max = tl.ReduceMax(axis=[1, 2, 3])
- elif self.data_format == 'channels_first':
- self.reduce_max = tl.ReduceMax(axis=[2, 3, 4])
- else:
- raise ValueError(
- "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
- )
-
- def forward(self, inputs):
- outputs = self.reduce_max(inputs)
- return outputs
-
-
- class GlobalMeanPool3d(Module):
- """The :class:`GlobalMeanPool3d` class is a 3D Global Mean Pooling layer.
-
- Parameters
- ------------
- data_format : str
- One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 100, 100, 100, 30], name='input')
- >>> net = tl.layers.GlobalMeanPool3d()(net)
- >>> output shape : [None, 30]
-
- """
-
- def __init__(
- self,
- data_format='channels_last',
- name=None # 'globalmeanpool3d'
- ):
- super().__init__(name)
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("GlobalMeanPool3d %s" % self.name)
-
- def __repr__(self):
- s = '{classname}('
- if self.name is not None:
- s += 'name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- pass
-
- def forward(self, inputs):
- if self.data_format == 'channels_last':
- outputs = tl.reduce_mean(input_tensor=inputs, axis=[1, 2, 3])
- elif self.data_format == 'channels_first':
- outputs = tl.reduce_mean(input_tensor=inputs, axis=[2, 3, 4])
- else:
- raise ValueError(
- "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
- )
- return outputs
-
-
- class CornerPool2d(Module):
- """Corner pooling for 2D image [batch, height, width, channel], see `here <https://arxiv.org/abs/1808.01244>`__.
-
- Parameters
- ----------
- mode : str
- TopLeft for the top left corner,
- Bottomright for the bottom right corner.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 32, 32, 8], name='input')
- >>> net = tl.layers.CornerPool2d(mode='TopLeft',name='cornerpool2d')(net)
- >>> output shape : [None, 32, 32, 8]
-
- """
-
- def __init__(
- self,
- mode='TopLeft',
- name=None # 'cornerpool2d'
- ):
- super().__init__(name)
- self.mode = mode
- self.build()
- self._built = True
-
- logging.info("CornerPool2d %s : mode: %s" % (self.name, str(mode)))
-
- def __repr__(self):
- s = ('{classname}(mode={mode}')
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- pass
-
- def forward(self, inputs):
- _, input_width, input_height, _ = tl.get_tensor_shape(inputs)
- # input_width = inputs.shape[2]
- # input_height = inputs.shape[1]
- batch_min = tl.reduce_min(inputs)
- if self.mode == 'TopLeft':
- temp_bottom = tl.pad(
- inputs, tl.constant([[0, 0], [0, input_height - 1], [0, 0], [0, 0]]), constant_values=batch_min
- )
- temp_right = tl.pad(
- inputs, tl.constant([[0, 0], [0, 0], [0, input_width - 1], [0, 0]]), constant_values=batch_min
- )
- temp_bottom = tl.ops.max_pool(temp_bottom, ksize=(input_height, 1), strides=(1, 1), padding='VALID')
- temp_right = tl.ops.max_pool(temp_right, ksize=(1, input_width), strides=(1, 1), padding='VALID')
- outputs = tl.add(temp_bottom, temp_right) #, name=self.name)
- elif self.mode == 'BottomRight':
- temp_top = tl.pad(
- inputs, tl.constant([[0, 0], [input_height - 1, 0], [0, 0], [0, 0]]), constant_values=batch_min
- )
- temp_left = tl.pad(
- inputs, tl.constant([[0, 0], [0, 0], [input_width - 1, 0], [0, 0]]), constant_values=batch_min
- )
- temp_top = tl.ops.max_pool(temp_top, ksize=(input_height, 1), strides=(1, 1), padding='VALID')
- temp_left = tl.ops.max_pool(temp_left, ksize=(1, input_width), strides=(1, 1), padding='VALID')
- outputs = tl.add(temp_top, temp_left)
- else:
- outputs = tl.identity(inputs)
- return outputs
-
-
- class AdaptiveMeanPool1d(Module):
- """The :class:`AdaptiveMeanPool1d` class is a 1D Adaptive Mean Pooling layer.
-
- Parameters
- ------------
- output_size : int
- The target output size. It must be an integer.
- data_format : str
- One of channels_last (default, [batch, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 32, 3], name='input')
- >>> net = tl.layers.AdaptiveMeanPool1d(output_size=16)(net)
- >>> output shape : [None, 16, 3]
-
- """
-
- def __init__(self, output_size, data_format='channels_last', name=None):
- super(AdaptiveMeanPool1d, self).__init__(name)
- self.output_size = output_size
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("AdaptiveMeanPool1d %s: output_size: %s " % (self.name, str(output_size)))
-
- def __repr__(self):
- s = ('{classname}(output_size={output_size}')
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.data_format = 'NWC'
- elif self.data_format == 'channels_first':
- self.data_format = 'NCW'
- else:
- raise Exception("unsupported data format")
-
- self.adaptivemeanpool1d = tl.ops.AdaptiveMeanPool1D(output_size=self.output_size, data_format=self.data_format)
-
- def forward(self, inputs):
-
- outputs = self.adaptivemeanpool1d(inputs)
- return outputs
-
-
- class AdaptiveMeanPool2d(Module):
- """The :class:`AdaptiveMeanPool2d` class is a 2D Adaptive Mean Pooling layer.
-
- Parameters
- ------------
- output_size : int or list or tuple
- The target output size. It cloud be an int \[int,int]\(int, int).
- data_format : str
- One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None,32, 32, 3], name='input')
- >>> net = tl.layers.AdaptiveMeanPool2d(output_size=16)(net)
- >>> output shape : [None,16, 16, 3]
-
- """
-
- def __init__(self, output_size, data_format='channels_last', name=None):
- super(AdaptiveMeanPool2d, self).__init__(name)
- self.output_size = output_size
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("AdaptiveMeanPool2d %s: output_size: %s " % (self.name, str(output_size)))
-
- def __repr__(self):
- s = ('{classname}(output_size={output_size}')
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.data_format = 'NHWC'
- elif self.data_format == 'channels_first':
- self.data_format = 'NCHW'
- else:
- raise Exception("unsupported data format")
-
- if isinstance(self.output_size, int):
- self.output_size = (self.output_size, ) * 2
-
- self.adaptivemeanpool2d = tl.ops.AdaptiveMeanPool2D(output_size=self.output_size, data_format=self.data_format)
-
- def forward(self, inputs):
-
- outputs = self.adaptivemeanpool2d(inputs)
- return outputs
-
-
- class AdaptiveMeanPool3d(Module):
- """The :class:`AdaptiveMeanPool3d` class is a 3D Adaptive Mean Pooling layer.
-
- Parameters
- ------------
- output_size : int or list or tuple
- The target output size. It cloud be an int \[int,int,int]\(int, int, int).
- data_format : str
- One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None,32, 32, 32, 3], name='input')
- >>> net = tl.layers.AdaptiveMeanPool3d(output_size=16)(net)
- >>> output shape : [None, 16, 16, 16, 3]
-
- """
-
- def __init__(self, output_size, data_format='channels_last', name=None):
- super(AdaptiveMeanPool3d, self).__init__(name)
- self.output_size = output_size
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("AdaptiveMeanPool3d %s: output_size: %s " % (self.name, str(output_size)))
-
- def __repr__(self):
- s = ('{classname}(output_size={output_size}')
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.data_format = 'NDHWC'
- elif self.data_format == 'channels_first':
- self.data_format = 'NCDHW'
- else:
- raise Exception("unsupported data format")
-
- if isinstance(self.output_size, int):
- self.output_size = (self.output_size, ) * 3
-
- self.adaptivemeanpool3d = tl.ops.AdaptiveMeanPool3D(output_size=self.output_size, data_format=self.data_format)
-
- def forward(self, inputs):
-
- outputs = self.adaptivemeanpool3d(inputs)
- return outputs
-
-
- class AdaptiveMaxPool1d(Module):
- """The :class:`AdaptiveMaxPool1d` class is a 1D Adaptive Max Pooling layer.
-
- Parameters
- ------------
- output_size : int
- The target output size. It must be an integer.
- data_format : str
- One of channels_last (default, [batch, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 32, 3], name='input')
- >>> net = tl.layers.AdaptiveMaxPool1d(output_size=16)(net)
- >>> output shape : [None, 16, 3]
-
- """
-
- def __init__(self, output_size, data_format='channels_last', name=None):
- super(AdaptiveMaxPool1d, self).__init__(name)
- self.output_size = output_size
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("AdaptiveMaxPool1d %s: output_size: %s " % (self.name, str(output_size)))
-
- def __repr__(self):
- s = ('{classname}(output_size={output_size}')
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.data_format = 'NWC'
- elif self.data_format == 'channels_first':
- self.data_format = 'NCW'
- else:
- raise Exception("unsupported data format")
-
- self.adaptivemaxpool1d = tl.ops.AdaptiveMaxPool1D(output_size=self.output_size, data_format=self.data_format)
-
- def forward(self, inputs):
-
- outputs = self.adaptivemaxpool1d(inputs)
- return outputs
-
-
- class AdaptiveMaxPool2d(Module):
- """The :class:`AdaptiveMaxPool2d` class is a 2D Adaptive Max Pooling layer.
-
- Parameters
- ------------
- output_size : int or list or tuple
- The target output size. It cloud be an int \[int,int]\(int, int).
- data_format : str
- One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None, 32, 32, 3], name='input')
- >>> net = tl.layers.AdaptiveMaxPool2d(output_size=16)(net)
- >>> output shape : [None, 16, 16, 3]
-
- """
-
- def __init__(self, output_size, data_format='channels_last', name=None):
- super(AdaptiveMaxPool2d, self).__init__(name)
- self.output_size = output_size
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("AdaptiveMaxPool1d %s: output_size: %s " % (self.name, str(output_size)))
-
- def __repr__(self):
- s = ('{classname}(output_size={output_size}')
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.data_format = 'NHWC'
- elif self.data_format == 'channels_first':
- self.data_format = 'NCHW'
- else:
- raise Exception("unsupported data format")
- if isinstance(self.output_size, int):
- self.output_size = (self.output_size, ) * 2
-
- self.adaptivemaxpool2d = tl.ops.AdaptiveMaxPool2D(output_size=self.output_size, data_format=self.data_format)
-
- def forward(self, inputs):
-
- outputs = self.adaptivemaxpool2d(inputs)
- return outputs
-
-
- class AdaptiveMaxPool3d(Module):
- """The :class:`AdaptiveMaxPool3d` class is a 3D Adaptive Max Pooling layer.
-
- Parameters
- ------------
- output_size : int or list or tuple
- The target output size. It cloud be an int \[int,int,int]\(int, int, int).
- data_format : str
- One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
- name : None or str
- A unique layer name.
-
- Examples
- ---------
- With TensorLayer
-
- >>> net = tl.layers.Input([None,32, 32, 32, 3], name='input')
- >>> net = tl.layers.AdaptiveMaxPool3d(output_size=16)(net)
- >>> output shape : [None, 16, 16, 16, 3]
-
- """
-
- def __init__(self, output_size, data_format='channels_last', name=None):
- super(AdaptiveMaxPool3d, self).__init__(name)
- self.output_size = output_size
- self.data_format = data_format
-
- self.build()
- self._built = True
-
- logging.info("AdaptiveMaxPool3d %s: output_size: %s " % (self.name, str(output_size)))
-
- def __repr__(self):
- s = ('{classname}(output_size={output_size}')
- if self.name is not None:
- s += ', name=\'{name}\''
- s += ')'
- return s.format(classname=self.__class__.__name__, **self.__dict__)
-
- def build(self, inputs_shape=None):
- if self.data_format == 'channels_last':
- self.data_format = 'NDHWC'
- elif self.data_format == 'channels_first':
- self.data_format = 'NCDHW'
- else:
- raise Exception("unsupported data format")
-
- if isinstance(self.output_size, int):
- self.output_size = (self.output_size, ) * 3
-
- self.adaptivemaxpool3d = tl.ops.AdaptiveMaxPool3D(output_size=self.output_size, data_format=self.data_format)
-
- def forward(self, inputs):
-
- outputs = self.adaptivemaxpool3d(inputs)
- return outputs
|