You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

pooling.py 41 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import tensorlayer as tl
  4. from tensorlayer import logging
  5. from tensorlayer.layers.core import Module
  6. __all__ = [
  7. 'PoolLayer',
  8. 'MaxPool1d',
  9. 'MeanPool1d',
  10. 'MaxPool2d',
  11. 'MeanPool2d',
  12. 'MaxPool3d',
  13. 'MeanPool3d',
  14. 'GlobalMaxPool1d',
  15. 'GlobalMeanPool1d',
  16. 'GlobalMaxPool2d',
  17. 'GlobalMeanPool2d',
  18. 'GlobalMaxPool3d',
  19. 'GlobalMeanPool3d',
  20. 'AdaptiveMeanPool1d',
  21. 'AdaptiveMeanPool2d',
  22. 'AdaptiveMeanPool3d',
  23. 'AdaptiveMaxPool1d',
  24. 'AdaptiveMaxPool2d',
  25. 'AdaptiveMaxPool3d',
  26. 'CornerPool2d',
  27. ]
  28. class PoolLayer(Module):
  29. """
  30. The :class:`PoolLayer` class is a Pooling layer.
  31. You can choose ``tl.ops.max_pool`` and ``tl.ops.avg_pool`` for 2D input or
  32. ``tl.ops.max_pool3d`` and ``tl.ops.avg_pool3d`` for 3D input.
  33. Parameters
  34. ----------
  35. filter_size : tuple of int
  36. The size of the window for each dimension of the input tensor.
  37. Note that: len(filter_size) >= 4.
  38. strides : tuple of int
  39. The stride of the sliding window for each dimension of the input tensor.
  40. Note that: len(strides) >= 4.
  41. padding : str
  42. The padding algorithm type: "SAME" or "VALID".
  43. pool : pooling function
  44. One of ``tl.ops.max_pool``, ``tl.ops.avg_pool``, ``tl.ops.max_pool3d`` and ``f.ops.avg_pool3d``.
  45. See `TensorFlow pooling APIs <https://tensorflow.google.cn/versions/r2.0/api_docs/python/tf/nn/>`__
  46. name : None or str
  47. A unique layer name.
  48. Examples
  49. ---------
  50. With TensorLayer
  51. >>> net = tl.layers.Input([10, 50, 50, 32], name='input')
  52. >>> net = tl.layers.PoolLayer()(net)
  53. >>> output shape : [10, 25, 25, 32]
  54. """
  55. def __init__(
  56. self,
  57. filter_size=(1, 2, 2, 1),
  58. strides=(1, 2, 2, 1),
  59. padding='SAME',
  60. pool=tl.ops.MaxPool,
  61. name=None # 'pool_pro',
  62. ):
  63. super().__init__(name)
  64. self.filter_size = filter_size
  65. self.strides = strides
  66. self.padding = padding
  67. self.pool = pool
  68. self.build()
  69. self._built = True
  70. logging.info(
  71. "PoolLayer %s: filter_size: %s strides: %s padding: %s pool: %s" %
  72. (self.name, str(self.filter_size), str(self.strides), self.padding, pool.__name__)
  73. )
  74. def __repr__(self):
  75. s = '{classname}(pool={poolname}, filter_size={strides}, padding={padding}'
  76. if self.name is not None:
  77. s += ', name=\'{name}\''
  78. s += ')'
  79. return s.format(classname=self.__class__.__name__, poolname=self.pool.__name__, **self.__dict__)
  80. def build(self, inputs_shape=None):
  81. self._pool = self.pool(ksize=self.filter_size, strides=self.strides, padding=self.padding)
  82. def forward(self, inputs):
  83. outputs = self._pool(inputs)
  84. return outputs
  85. class MaxPool1d(Module):
  86. """Max pooling for 1D signal.
  87. Parameters
  88. ----------
  89. filter_size : int
  90. Pooling window size.
  91. strides : int
  92. Stride of the pooling operation.
  93. padding : str
  94. The padding method: 'VALID' or 'SAME'.
  95. data_format : str
  96. One of channels_last (default, [batch, length, channel]) or channels_first. The ordering of the dimensions in the inputs.
  97. name : None or str
  98. A unique layer name.
  99. Examples
  100. ---------
  101. With TensorLayer
  102. >>> net = tl.layers.Input([10, 50, 32], name='input')
  103. >>> net = tl.layers.MaxPool1d(filter_size=3, strides=2, padding='SAME', name='maxpool1d')(net)
  104. >>> output shape : [10, 25, 32]
  105. """
  106. def __init__(
  107. self,
  108. filter_size=3,
  109. strides=2,
  110. padding='SAME',
  111. data_format='channels_last',
  112. name=None # 'maxpool1d'
  113. ):
  114. super().__init__(name)
  115. self.filter_size = self._filter_size = filter_size
  116. self.strides = self._strides = strides
  117. self.padding = padding
  118. self.data_format = data_format
  119. self.build()
  120. self._built = True
  121. logging.info(
  122. "MaxPool1d %s: filter_size: %s strides: %s padding: %s" %
  123. (self.name, str(filter_size), str(strides), str(padding))
  124. )
  125. def __repr__(self):
  126. s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
  127. if self.name is not None:
  128. s += ', name=\'{name}\''
  129. s += ')'
  130. return s.format(classname=self.__class__.__name__, **self.__dict__)
  131. def build(self, inputs_shape=None):
  132. # https://tensorflow.google.cn/versions/r2.0/api_docs/python/tf/nn/pool
  133. if self.data_format == 'channels_last':
  134. self.data_format = 'NWC'
  135. elif self.data_format == 'channels_first':
  136. self.data_format = 'NCW'
  137. else:
  138. raise Exception("unsupported data format")
  139. self._filter_size = [self.filter_size]
  140. self._strides = [self.strides]
  141. self.max_pool = tl.ops.MaxPool1d(
  142. ksize=self._filter_size, strides=self._strides, padding=self.padding, data_format=self.data_format
  143. )
  144. def forward(self, inputs):
  145. outputs = self.max_pool(inputs)
  146. return outputs
  147. class MeanPool1d(Module):
  148. """Mean pooling for 1D signal.
  149. Parameters
  150. ------------
  151. filter_size : int
  152. Pooling window size.
  153. strides : int
  154. Strides of the pooling operation.
  155. padding : str
  156. The padding method: 'VALID' or 'SAME'.
  157. data_format : str
  158. One of channels_last (default, [batch, length, channel]) or channels_first. The ordering of the dimensions in the inputs.
  159. name : None or str
  160. A unique layer name.
  161. Examples
  162. ---------
  163. With TensorLayer
  164. >>> net = tl.layers.Input([10, 50, 32], name='input')
  165. >>> net = tl.layers.MeanPool1d(filter_size=3, strides=2, padding='SAME')(net)
  166. >>> output shape : [10, 25, 32]
  167. """
  168. def __init__(
  169. self,
  170. filter_size=3,
  171. strides=2,
  172. padding='SAME',
  173. data_format='channels_last',
  174. dilation_rate=1,
  175. name=None # 'meanpool1d'
  176. ):
  177. super().__init__(name)
  178. self.filter_size = self._filter_size = filter_size
  179. self.strides = self._strides = strides
  180. self.padding = padding
  181. self.data_format = data_format
  182. self.build()
  183. self._built = True
  184. logging.info(
  185. "MeanPool1d %s: filter_size: %s strides: %s padding: %s" %
  186. (self.name, str(filter_size), str(strides), str(padding))
  187. )
  188. def __repr__(self):
  189. s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
  190. if self.name is not None:
  191. s += ', name=\'{name}\''
  192. s += ')'
  193. return s.format(classname=self.__class__.__name__, **self.__dict__)
  194. def build(self, inputs_shape=None):
  195. # https://tensorflow.google.cn/versions/r2.0/api_docs/python/tf/nn/pool
  196. if self.data_format == 'channels_last':
  197. self.data_format = 'NWC'
  198. elif self.data_format == 'channels_first':
  199. self.data_format = 'NCW'
  200. else:
  201. raise Exception("unsupported data format")
  202. self._filter_size = [self.filter_size]
  203. self._strides = [self.strides]
  204. self.avg_pool = tl.ops.AvgPool1d(
  205. ksize=self._filter_size, strides=self._strides, padding=self.padding, data_format=self.data_format
  206. )
  207. def forward(self, inputs):
  208. outputs = self.avg_pool(inputs)
  209. return outputs
  210. class MaxPool2d(Module):
  211. """Max pooling for 2D image.
  212. Parameters
  213. -----------
  214. filter_size : tuple of int
  215. (height, width) for filter size.
  216. strides : tuple of int
  217. (height, width) for strides.
  218. padding : str
  219. The padding method: 'VALID' or 'SAME'.
  220. data_format : str
  221. One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  222. name : None or str
  223. A unique layer name.
  224. Examples
  225. ---------
  226. With TensorLayer
  227. >>> net = tl.layers.Input([10, 50, 50, 32], name='input')
  228. >>> net = tl.layers.MaxPool2d(filter_size=(3, 3), strides=(2, 2), padding='SAME')(net)
  229. >>> output shape : [10, 25, 25, 32]
  230. """
  231. def __init__(
  232. self,
  233. filter_size=(3, 3),
  234. strides=(2, 2),
  235. padding='SAME',
  236. data_format='channels_last',
  237. name=None # 'maxpool2d'
  238. ):
  239. super().__init__(name)
  240. self.filter_size = filter_size
  241. if strides is None:
  242. strides = filter_size
  243. self.strides = self._strides = strides
  244. self.padding = padding
  245. self.data_format = data_format
  246. self.build()
  247. self._built = True
  248. logging.info(
  249. "MaxPool2d %s: filter_size: %s strides: %s padding: %s" %
  250. (self.name, str(filter_size), str(strides), str(padding))
  251. )
  252. def __repr__(self):
  253. s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
  254. if self.name is not None:
  255. s += ', name=\'{name}\''
  256. s += ')'
  257. return s.format(classname=self.__class__.__name__, **self.__dict__)
  258. def build(self, inputs_shape=None):
  259. if self.data_format == 'channels_last':
  260. self.data_format = 'NHWC'
  261. self._strides = [1, self.strides[0], self.strides[1], 1]
  262. elif self.data_format == 'channels_first':
  263. self.data_format = 'NCHW'
  264. self._strides = [1, 1, self.strides[0], self.strides[1]]
  265. else:
  266. raise Exception("unsupported data format")
  267. self.max_pool = tl.ops.MaxPool(
  268. ksize=self.filter_size, strides=self._strides, padding=self.padding, data_format=self.data_format
  269. )
  270. def forward(self, inputs):
  271. outputs = self.max_pool(inputs)
  272. return outputs
  273. class MeanPool2d(Module):
  274. """Mean pooling for 2D image [batch, height, width, channel].
  275. Parameters
  276. -----------
  277. filter_size : tuple of int
  278. (height, width) for filter size.
  279. strides : tuple of int
  280. (height, width) for strides.
  281. padding : str
  282. The padding method: 'VALID' or 'SAME'.
  283. data_format : str
  284. One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  285. name : None or str
  286. A unique layer name.
  287. Examples
  288. ---------
  289. With TensorLayer
  290. >>> net = tl.layers.Input([10, 50, 50, 32], name='input')
  291. >>> net = tl.layers.MeanPool2d(filter_size=(3, 3), strides=(2, 2), padding='SAME')(net)
  292. >>> output shape : [10, 25, 25, 32]
  293. """
  294. def __init__(
  295. self,
  296. filter_size=(3, 3),
  297. strides=(2, 2),
  298. padding='SAME',
  299. data_format='channels_last',
  300. name=None # 'meanpool2d'
  301. ):
  302. super().__init__(name)
  303. self.filter_size = filter_size
  304. if strides is None:
  305. strides = filter_size
  306. self.strides = self._strides = strides
  307. self.padding = padding
  308. self.data_format = data_format
  309. self.build()
  310. self._built = True
  311. logging.info(
  312. "MeanPool2d %s: filter_size: %s strides: %s padding: %s" %
  313. (self.name, str(filter_size), str(strides), str(padding))
  314. )
  315. def __repr__(self):
  316. s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
  317. if self.name is not None:
  318. s += ', name=\'{name}\''
  319. s += ')'
  320. return s.format(classname=self.__class__.__name__, **self.__dict__)
  321. def build(self, inputs_shape=None):
  322. if self.data_format == 'channels_last':
  323. self.data_format = 'NHWC'
  324. self._strides = [1, self.strides[0], self.strides[1], 1]
  325. elif self.data_format == 'channels_first':
  326. self.data_format = 'NCHW'
  327. self._strides = [1, 1, self.strides[0], self.strides[1]]
  328. else:
  329. raise Exception("unsupported data format")
  330. self.avg_pool = tl.ops.AvgPool(
  331. ksize=self.filter_size, strides=self._strides, padding=self.padding, data_format=self.data_format
  332. )
  333. def forward(self, inputs):
  334. outputs = self.avg_pool(inputs)
  335. return outputs
  336. class MaxPool3d(Module):
  337. """Max pooling for 3D volume.
  338. Parameters
  339. ------------
  340. filter_size : tuple of int
  341. Pooling window size.
  342. strides : tuple of int
  343. Strides of the pooling operation.
  344. padding : str
  345. The padding method: 'VALID' or 'SAME'.
  346. data_format : str
  347. One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  348. name : None or str
  349. A unique layer name.
  350. Returns
  351. -------
  352. :class:`tf.Tensor`
  353. A max pooling 3-D layer with a output rank as 5.
  354. Examples
  355. ---------
  356. With TensorLayer
  357. >>> net = tl.layers.Input([10, 50, 50, 50, 32], name='input')
  358. >>> net = tl.layers.MaxPool3d(filter_size=(3, 3, 3), strides=(2, 2, 2), padding='SAME')(net)
  359. >>> output shape : [10, 25, 25, 25, 32]
  360. """
  361. def __init__(
  362. self,
  363. filter_size=(3, 3, 3),
  364. strides=(2, 2, 2),
  365. padding='VALID',
  366. data_format='channels_last',
  367. name=None # 'maxpool3d'
  368. ):
  369. super().__init__(name)
  370. self.filter_size = filter_size
  371. self.strides = self._strides = strides
  372. self.padding = padding
  373. self.data_format = data_format
  374. self.build()
  375. self._built = True
  376. logging.info(
  377. "MaxPool3d %s: filter_size: %s strides: %s padding: %s" %
  378. (self.name, str(filter_size), str(strides), str(padding))
  379. )
  380. def __repr__(self):
  381. s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
  382. if self.name is not None:
  383. s += ', name=\'{name}\''
  384. s += ')'
  385. return s.format(classname=self.__class__.__name__, **self.__dict__)
  386. def build(self, inputs_shape=None):
  387. if self.data_format == 'channels_last':
  388. self.data_format = 'NDHWC'
  389. self._strides = [1, self.strides[0], self.strides[1], self.strides[2], 1]
  390. elif self.data_format == 'channels_first':
  391. self.data_format = 'NCDHW'
  392. self._strides = [1, 1, self.strides[0], self.strides[1], self.strides[2]]
  393. else:
  394. raise Exception("unsupported data format")
  395. self.max_pool3d = tl.ops.MaxPool3d(
  396. ksize=self.filter_size, strides=self._strides, padding=self.padding, data_format=self.data_format
  397. )
  398. def forward(self, inputs):
  399. outputs = self.max_pool3d(inputs)
  400. return outputs
  401. class MeanPool3d(Module):
  402. """Mean pooling for 3D volume.
  403. Parameters
  404. ------------
  405. filter_size : tuple of int
  406. Pooling window size.
  407. strides : tuple of int
  408. Strides of the pooling operation.
  409. padding : str
  410. The padding method: 'VALID' or 'SAME'.
  411. data_format : str
  412. One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  413. name : None or str
  414. A unique layer name.
  415. Returns
  416. -------
  417. :class:`tf.Tensor`
  418. A mean pooling 3-D layer with a output rank as 5.
  419. Examples
  420. ---------
  421. With TensorLayer
  422. >>> net = tl.layers.Input([10, 50, 50, 50, 32], name='input')
  423. >>> net = tl.layers.MeanPool3d(filter_size=(3, 3, 3), strides=(2, 2, 2), padding='SAME')(net)
  424. >>> output shape : [10, 25, 25, 25, 32]
  425. """
  426. def __init__(
  427. self,
  428. filter_size=(3, 3, 3),
  429. strides=(2, 2, 2),
  430. padding='VALID',
  431. data_format='channels_last',
  432. name=None # 'meanpool3d'
  433. ):
  434. super().__init__(name)
  435. self.filter_size = filter_size
  436. self.strides = self._strides = strides
  437. self.padding = padding
  438. self.data_format = data_format
  439. self.build()
  440. self._built = True
  441. logging.info(
  442. "MeanPool3d %s: filter_size: %s strides: %s padding: %s" %
  443. (self.name, str(filter_size), str(strides), str(padding))
  444. )
  445. def __repr__(self):
  446. s = ('{classname}(filter_size={filter_size}' ', strides={strides}, padding={padding}')
  447. if self.name is not None:
  448. s += ', name=\'{name}\''
  449. s += ')'
  450. return s.format(classname=self.__class__.__name__, **self.__dict__)
  451. def build(self, inputs_shape=None):
  452. self._strides = [1, self.strides[0], self.strides[1], self.strides[2], 1]
  453. if self.data_format == 'channels_last':
  454. self.data_format = 'NDHWC'
  455. elif self.data_format == 'channels_first':
  456. self.data_format = 'NCDHW'
  457. else:
  458. raise Exception("unsupported data format")
  459. self.avg_pool3d = tl.ops.AvgPool3d(
  460. ksize=self.filter_size, strides=self._strides, padding=self.padding, data_format=self.data_format
  461. )
  462. def forward(self, inputs):
  463. outputs = self.avg_pool3d(inputs)
  464. return outputs
  465. class GlobalMaxPool1d(Module):
  466. """The :class:`GlobalMaxPool1d` class is a 1D Global Max Pooling layer.
  467. Parameters
  468. ------------
  469. data_format : str
  470. One of channels_last (default, [batch, length, channel]) or channels_first. The ordering of the dimensions in the inputs.
  471. name : None or str
  472. A unique layer name.
  473. Examples
  474. ---------
  475. With TensorLayer
  476. >>> net = tl.layers.Input([10, 100, 30], name='input')
  477. >>> net = tl.layers.GlobalMaxPool1d()(net)
  478. >>> output shape : [10, 30]
  479. """
  480. def __init__(
  481. self,
  482. data_format="channels_last",
  483. name=None # 'globalmaxpool1d'
  484. ):
  485. super().__init__(name)
  486. self.data_format = data_format
  487. self.build()
  488. self._built = True
  489. logging.info("GlobalMaxPool1d %s" % self.name)
  490. def __repr__(self):
  491. s = '{classname}('
  492. if self.name is not None:
  493. s += 'name=\'{name}\''
  494. s += ')'
  495. return s.format(classname=self.__class__.__name__, **self.__dict__)
  496. def build(self, inputs_shape=None):
  497. if self.data_format == 'channels_last':
  498. self.reduce_max = tl.ReduceMax(axis=1)
  499. elif self.data_format == 'channels_first':
  500. self.reduce_max = tl.ReduceMax(axis=2)
  501. else:
  502. raise ValueError(
  503. "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
  504. )
  505. def forward(self, inputs):
  506. outputs = self.reduce_max(inputs)
  507. return outputs
  508. class GlobalMeanPool1d(Module):
  509. """The :class:`GlobalMeanPool1d` class is a 1D Global Mean Pooling layer.
  510. Parameters
  511. ------------
  512. data_format : str
  513. One of channels_last (default, [batch, length, channel]) or channels_first. The ordering of the dimensions in the inputs.
  514. name : None or str
  515. A unique layer name.
  516. Examples
  517. ---------
  518. With TensorLayer
  519. >>> net = tl.layers.Input([10, 100, 30], name='input')
  520. >>> net = tl.layers.GlobalMeanPool1d()(net)
  521. >>> output shape : [10, 30]
  522. """
  523. def __init__(
  524. self,
  525. data_format='channels_last',
  526. name=None # 'globalmeanpool1d'
  527. ):
  528. super().__init__(name)
  529. self.data_format = data_format
  530. self.build()
  531. self._built = True
  532. logging.info("GlobalMeanPool1d %s" % self.name)
  533. def __repr__(self):
  534. s = '{classname}('
  535. if self.name is not None:
  536. s += 'name=\'{name}\''
  537. s += ')'
  538. return s.format(classname=self.__class__.__name__, **self.__dict__)
  539. def build(self, inputs_shape=None):
  540. if self.data_format == 'channels_last':
  541. self.reduce_mean = tl.ReduceMean(axis=1)
  542. elif self.data_format == 'channels_first':
  543. self.reduce_mean = tl.ReduceMean(axis=2)
  544. else:
  545. raise ValueError(
  546. "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
  547. )
  548. def forward(self, inputs):
  549. outputs = self.reduce_mean(inputs)
  550. return outputs
  551. class GlobalMaxPool2d(Module):
  552. """The :class:`GlobalMaxPool2d` class is a 2D Global Max Pooling layer.
  553. Parameters
  554. ------------
  555. data_format : str
  556. One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  557. name : None or str
  558. A unique layer name.
  559. Examples
  560. ---------
  561. With TensorLayer
  562. >>> net = tl.layers.Input([10, 100, 100, 30], name='input')
  563. >>> net = tl.layers.GlobalMaxPool2d()(net)
  564. >>> output shape : [10, 30]
  565. """
  566. def __init__(
  567. self,
  568. data_format='channels_last',
  569. name=None # 'globalmaxpool2d'
  570. ):
  571. super().__init__(name)
  572. self.data_format = data_format
  573. self.build()
  574. self._built = True
  575. logging.info("GlobalMaxPool2d %s" % self.name)
  576. def __repr__(self):
  577. s = '{classname}('
  578. if self.name is not None:
  579. s += 'name=\'{name}\''
  580. s += ')'
  581. return s.format(classname=self.__class__.__name__, **self.__dict__)
  582. def build(self, inputs_shape=None):
  583. if self.data_format == 'channels_last':
  584. self.reduce_max = tl.ReduceMax(axis=[1, 2])
  585. elif self.data_format == 'channels_first':
  586. self.reduce_max = tl.ReduceMax(axis=[2, 3])
  587. else:
  588. raise ValueError(
  589. "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
  590. )
  591. def forward(self, inputs):
  592. outputs = self.reduce_max(inputs)
  593. return outputs
  594. class GlobalMeanPool2d(Module):
  595. """The :class:`GlobalMeanPool2d` class is a 2D Global Mean Pooling layer.
  596. Parameters
  597. ------------
  598. data_format : str
  599. One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  600. name : None or str
  601. A unique layer name.
  602. Examples
  603. ---------
  604. With TensorLayer
  605. >>> net = tl.layers.Input([10, 100, 100, 30], name='input')
  606. >>> net = tl.layers.GlobalMeanPool2d()(net)
  607. >>> output shape : [10, 30]
  608. """
  609. def __init__(
  610. self,
  611. data_format='channels_last',
  612. name=None # 'globalmeanpool2d'
  613. ):
  614. super().__init__(name)
  615. self.data_format = data_format
  616. self.build()
  617. self._built = True
  618. logging.info("GlobalMeanPool2d %s" % self.name)
  619. def __repr__(self):
  620. s = '{classname}('
  621. if self.name is not None:
  622. s += 'name=\'{name}\''
  623. s += ')'
  624. return s.format(classname=self.__class__.__name__, **self.__dict__)
  625. def build(self, inputs_shape=None):
  626. if self.data_format == 'channels_last':
  627. self.reduce_mean = tl.ReduceMean(axis=[1, 2])
  628. elif self.data_format == 'channels_first':
  629. self.reduce_mean = tl.ReduceMean(axis=[2, 3])
  630. else:
  631. raise ValueError(
  632. "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
  633. )
  634. def forward(self, inputs):
  635. outputs = self.reduce_mean(inputs)
  636. return outputs
  637. class GlobalMaxPool3d(Module):
  638. """The :class:`GlobalMaxPool3d` class is a 3D Global Max Pooling layer.
  639. Parameters
  640. ------------
  641. data_format : str
  642. One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  643. name : None or str
  644. A unique layer name.
  645. Examples
  646. ---------
  647. With TensorLayer
  648. >>> net = tl.layers.Input([10, 100, 100, 100, 30], name='input')
  649. >>> net = tl.layers.GlobalMaxPool3d()(net)
  650. >>> output shape : [10, 30]
  651. """
  652. def __init__(
  653. self,
  654. data_format='channels_last',
  655. name=None # 'globalmaxpool3d'
  656. ):
  657. super().__init__(name)
  658. self.data_format = data_format
  659. self.build()
  660. self._built = True
  661. logging.info("GlobalMaxPool3d %s" % self.name)
  662. def __repr__(self):
  663. s = '{classname}('
  664. if self.name is not None:
  665. s += 'name=\'{name}\''
  666. s += ')'
  667. return s.format(classname=self.__class__.__name__, **self.__dict__)
  668. def build(self, inputs_shape=None):
  669. if self.data_format == 'channels_last':
  670. self.reduce_max = tl.ReduceMax(axis=[1, 2, 3])
  671. elif self.data_format == 'channels_first':
  672. self.reduce_max = tl.ReduceMax(axis=[2, 3, 4])
  673. else:
  674. raise ValueError(
  675. "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
  676. )
  677. def forward(self, inputs):
  678. outputs = self.reduce_max(inputs)
  679. return outputs
  680. class GlobalMeanPool3d(Module):
  681. """The :class:`GlobalMeanPool3d` class is a 3D Global Mean Pooling layer.
  682. Parameters
  683. ------------
  684. data_format : str
  685. One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  686. name : None or str
  687. A unique layer name.
  688. Examples
  689. ---------
  690. With TensorLayer
  691. >>> net = tl.layers.Input([10, 100, 100, 100, 30], name='input')
  692. >>> net = tl.layers.GlobalMeanPool3d()(net)
  693. >>> output shape : [10, 30]
  694. """
  695. def __init__(
  696. self,
  697. data_format='channels_last',
  698. name=None # 'globalmeanpool3d'
  699. ):
  700. super().__init__(name)
  701. self.data_format = data_format
  702. self.build()
  703. self._built = True
  704. logging.info("GlobalMeanPool3d %s" % self.name)
  705. def __repr__(self):
  706. s = '{classname}('
  707. if self.name is not None:
  708. s += 'name=\'{name}\''
  709. s += ')'
  710. return s.format(classname=self.__class__.__name__, **self.__dict__)
  711. def build(self, inputs_shape=None):
  712. if self.data_format == 'channels_last':
  713. self.reduce_mean = tl.ReduceMean(axis=[1, 2, 3])
  714. elif self.data_format == 'channels_first':
  715. self.reduce_mean = tl.ReduceMean(axis=[2, 3, 4])
  716. else:
  717. raise ValueError(
  718. "`data_format` should have one of the following values: [`channels_last`, `channels_first`]"
  719. )
  720. def forward(self, inputs):
  721. outputs = self.reduce_mean(inputs)
  722. return outputs
  723. class CornerPool2d(Module):
  724. """Corner pooling for 2D image [batch, height, width, channel], see `here <https://arxiv.org/abs/1808.01244>`__.
  725. Parameters
  726. ----------
  727. mode : str
  728. TopLeft for the top left corner,
  729. Bottomright for the bottom right corner.
  730. name : None or str
  731. A unique layer name.
  732. Examples
  733. ---------
  734. With TensorLayer
  735. >>> net = tl.layers.Input([10, 32, 32, 8], name='input')
  736. >>> net = tl.layers.CornerPool2d(mode='TopLeft',name='cornerpool2d')(net)
  737. >>> output shape : [10, 32, 32, 8]
  738. """
  739. def __init__(
  740. self,
  741. mode='TopLeft',
  742. name=None # 'cornerpool2d'
  743. ):
  744. super().__init__(name)
  745. self.mode = mode
  746. self.build()
  747. self._built = True
  748. logging.info("CornerPool2d %s : mode: %s" % (self.name, str(mode)))
  749. def __repr__(self):
  750. s = ('{classname}(mode={mode}')
  751. if self.name is not None:
  752. s += ', name=\'{name}\''
  753. s += ')'
  754. return s.format(classname=self.__class__.__name__, **self.__dict__)
  755. def build(self, inputs_shape=None):
  756. pass
  757. def forward(self, inputs):
  758. _, input_width, input_height, _ = tl.get_tensor_shape(inputs)
  759. # input_width = inputs.shape[2]
  760. # input_height = inputs.shape[1]
  761. batch_min = tl.reduce_min(inputs)
  762. if self.mode == 'TopLeft':
  763. temp_bottom = tl.pad(
  764. inputs, tl.constant([[0, 0], [0, input_height - 1], [0, 0], [0, 0]]), constant_values=batch_min
  765. )
  766. temp_right = tl.pad(
  767. inputs, tl.constant([[0, 0], [0, 0], [0, input_width - 1], [0, 0]]), constant_values=batch_min
  768. )
  769. temp_bottom = tl.ops.max_pool(temp_bottom, ksize=(input_height, 1), strides=(1, 1), padding='VALID')
  770. temp_right = tl.ops.max_pool(temp_right, ksize=(1, input_width), strides=(1, 1), padding='VALID')
  771. outputs = tl.add(temp_bottom, temp_right) #, name=self.name)
  772. elif self.mode == 'BottomRight':
  773. temp_top = tl.pad(
  774. inputs, tl.constant([[0, 0], [input_height - 1, 0], [0, 0], [0, 0]]), constant_values=batch_min
  775. )
  776. temp_left = tl.pad(
  777. inputs, tl.constant([[0, 0], [0, 0], [input_width - 1, 0], [0, 0]]), constant_values=batch_min
  778. )
  779. temp_top = tl.ops.max_pool(temp_top, ksize=(input_height, 1), strides=(1, 1), padding='VALID')
  780. temp_left = tl.ops.max_pool(temp_left, ksize=(1, input_width), strides=(1, 1), padding='VALID')
  781. outputs = tl.add(temp_top, temp_left)
  782. else:
  783. outputs = tl.identity(inputs)
  784. return outputs
  785. class AdaptiveMeanPool1d(Module):
  786. """The :class:`AdaptiveMeanPool1d` class is a 1D Adaptive Mean Pooling layer.
  787. Parameters
  788. ------------
  789. output_size : int
  790. The target output size. It must be an integer.
  791. data_format : str
  792. One of channels_last (default, [batch, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  793. name : None or str
  794. A unique layer name.
  795. Examples
  796. ---------
  797. With TensorLayer
  798. >>> net = tl.layers.Input([10, 32, 3], name='input')
  799. >>> net = tl.layers.AdaptiveMeanPool1d(output_size=16)(net)
  800. >>> output shape : [10, 16, 3]
  801. """
  802. def __init__(self, output_size, data_format='channels_last', name=None):
  803. super(AdaptiveMeanPool1d, self).__init__(name)
  804. self.output_size = output_size
  805. self.data_format = data_format
  806. self.build()
  807. self._built = True
  808. logging.info("AdaptiveMeanPool1d %s: output_size: %s " % (self.name, str(output_size)))
  809. def __repr__(self):
  810. s = ('{classname}(output_size={output_size}')
  811. if self.name is not None:
  812. s += ', name=\'{name}\''
  813. s += ')'
  814. return s.format(classname=self.__class__.__name__, **self.__dict__)
  815. def build(self, inputs_shape=None):
  816. if self.data_format == 'channels_last':
  817. self.data_format = 'NWC'
  818. elif self.data_format == 'channels_first':
  819. self.data_format = 'NCW'
  820. else:
  821. raise Exception("unsupported data format")
  822. self.adaptivemeanpool1d = tl.ops.AdaptiveMeanPool1D(output_size=self.output_size, data_format=self.data_format)
  823. def forward(self, inputs):
  824. outputs = self.adaptivemeanpool1d(inputs)
  825. return outputs
  826. class AdaptiveMeanPool2d(Module):
  827. """The :class:`AdaptiveMeanPool2d` class is a 2D Adaptive Mean Pooling layer.
  828. Parameters
  829. ------------
  830. output_size : int or list or tuple
  831. The target output size. It cloud be an int \[int,int]\(int, int).
  832. data_format : str
  833. One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  834. name : None or str
  835. A unique layer name.
  836. Examples
  837. ---------
  838. With TensorLayer
  839. >>> net = tl.layers.Input([10,32, 32, 3], name='input')
  840. >>> net = tl.layers.AdaptiveMeanPool2d(output_size=16)(net)
  841. >>> output shape : [10,16, 16, 3]
  842. """
  843. def __init__(self, output_size, data_format='channels_last', name=None):
  844. super(AdaptiveMeanPool2d, self).__init__(name)
  845. self.output_size = output_size
  846. self.data_format = data_format
  847. self.build()
  848. self._built = True
  849. logging.info("AdaptiveMeanPool2d %s: output_size: %s " % (self.name, str(output_size)))
  850. def __repr__(self):
  851. s = ('{classname}(output_size={output_size}')
  852. if self.name is not None:
  853. s += ', name=\'{name}\''
  854. s += ')'
  855. return s.format(classname=self.__class__.__name__, **self.__dict__)
  856. def build(self, inputs_shape=None):
  857. if self.data_format == 'channels_last':
  858. self.data_format = 'NHWC'
  859. elif self.data_format == 'channels_first':
  860. self.data_format = 'NCHW'
  861. else:
  862. raise Exception("unsupported data format")
  863. if isinstance(self.output_size, int):
  864. self.output_size = (self.output_size, ) * 2
  865. self.adaptivemeanpool2d = tl.ops.AdaptiveMeanPool2D(output_size=self.output_size, data_format=self.data_format)
  866. def forward(self, inputs):
  867. outputs = self.adaptivemeanpool2d(inputs)
  868. return outputs
  869. class AdaptiveMeanPool3d(Module):
  870. """The :class:`AdaptiveMeanPool3d` class is a 3D Adaptive Mean Pooling layer.
  871. Parameters
  872. ------------
  873. output_size : int or list or tuple
  874. The target output size. It cloud be an int \[int,int,int]\(int, int, int).
  875. data_format : str
  876. One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  877. name : None or str
  878. A unique layer name.
  879. Examples
  880. ---------
  881. With TensorLayer
  882. >>> net = tl.layers.Input([10,32, 32, 32, 3], name='input')
  883. >>> net = tl.layers.AdaptiveMeanPool3d(output_size=16)(net)
  884. >>> output shape : [10, 16, 16, 16, 3]
  885. """
  886. def __init__(self, output_size, data_format='channels_last', name=None):
  887. super(AdaptiveMeanPool3d, self).__init__(name)
  888. self.output_size = output_size
  889. self.data_format = data_format
  890. self.build()
  891. self._built = True
  892. logging.info("AdaptiveMeanPool3d %s: output_size: %s " % (self.name, str(output_size)))
  893. def __repr__(self):
  894. s = ('{classname}(output_size={output_size}')
  895. if self.name is not None:
  896. s += ', name=\'{name}\''
  897. s += ')'
  898. return s.format(classname=self.__class__.__name__, **self.__dict__)
  899. def build(self, inputs_shape=None):
  900. if self.data_format == 'channels_last':
  901. self.data_format = 'NDHWC'
  902. elif self.data_format == 'channels_first':
  903. self.data_format = 'NCDHW'
  904. else:
  905. raise Exception("unsupported data format")
  906. if isinstance(self.output_size, int):
  907. self.output_size = (self.output_size, ) * 3
  908. self.adaptivemeanpool3d = tl.ops.AdaptiveMeanPool3D(output_size=self.output_size, data_format=self.data_format)
  909. def forward(self, inputs):
  910. outputs = self.adaptivemeanpool3d(inputs)
  911. return outputs
  912. class AdaptiveMaxPool1d(Module):
  913. """The :class:`AdaptiveMaxPool1d` class is a 1D Adaptive Max Pooling layer.
  914. Parameters
  915. ------------
  916. output_size : int
  917. The target output size. It must be an integer.
  918. data_format : str
  919. One of channels_last (default, [batch, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  920. name : None or str
  921. A unique layer name.
  922. Examples
  923. ---------
  924. With TensorLayer
  925. >>> net = tl.layers.Input([10, 32, 3], name='input')
  926. >>> net = tl.layers.AdaptiveMaxPool1d(output_size=16)(net)
  927. >>> output shape : [10, 16, 3]
  928. """
  929. def __init__(self, output_size, data_format='channels_last', name=None):
  930. super(AdaptiveMaxPool1d, self).__init__(name)
  931. self.output_size = output_size
  932. self.data_format = data_format
  933. self.build()
  934. self._built = True
  935. logging.info("AdaptiveMaxPool1d %s: output_size: %s " % (self.name, str(output_size)))
  936. def __repr__(self):
  937. s = ('{classname}(output_size={output_size}')
  938. if self.name is not None:
  939. s += ', name=\'{name}\''
  940. s += ')'
  941. return s.format(classname=self.__class__.__name__, **self.__dict__)
  942. def build(self, inputs_shape=None):
  943. if self.data_format == 'channels_last':
  944. self.data_format = 'NWC'
  945. elif self.data_format == 'channels_first':
  946. self.data_format = 'NCW'
  947. else:
  948. raise Exception("unsupported data format")
  949. self.adaptivemaxpool1d = tl.ops.AdaptiveMaxPool1D(output_size=self.output_size, data_format=self.data_format)
  950. def forward(self, inputs):
  951. outputs = self.adaptivemaxpool1d(inputs)
  952. return outputs
  953. class AdaptiveMaxPool2d(Module):
  954. """The :class:`AdaptiveMaxPool2d` class is a 2D Adaptive Max Pooling layer.
  955. Parameters
  956. ------------
  957. output_size : int or list or tuple
  958. The target output size. It cloud be an int \[int,int]\(int, int).
  959. data_format : str
  960. One of channels_last (default, [batch, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  961. name : None or str
  962. A unique layer name.
  963. Examples
  964. ---------
  965. With TensorLayer
  966. >>> net = tl.layers.Input([10, 32, 32, 3], name='input')
  967. >>> net = tl.layers.AdaptiveMaxPool2d(output_size=16)(net)
  968. >>> output shape : [10, 16, 16, 3]
  969. """
  970. def __init__(self, output_size, data_format='channels_last', name=None):
  971. super(AdaptiveMaxPool2d, self).__init__(name)
  972. self.output_size = output_size
  973. self.data_format = data_format
  974. self.build()
  975. self._built = True
  976. logging.info("AdaptiveMaxPool1d %s: output_size: %s " % (self.name, str(output_size)))
  977. def __repr__(self):
  978. s = ('{classname}(output_size={output_size}')
  979. if self.name is not None:
  980. s += ', name=\'{name}\''
  981. s += ')'
  982. return s.format(classname=self.__class__.__name__, **self.__dict__)
  983. def build(self, inputs_shape=None):
  984. if self.data_format == 'channels_last':
  985. self.data_format = 'NHWC'
  986. elif self.data_format == 'channels_first':
  987. self.data_format = 'NCHW'
  988. else:
  989. raise Exception("unsupported data format")
  990. if isinstance(self.output_size, int):
  991. self.output_size = (self.output_size, ) * 2
  992. self.adaptivemaxpool2d = tl.ops.AdaptiveMaxPool2D(output_size=self.output_size, data_format=self.data_format)
  993. def forward(self, inputs):
  994. outputs = self.adaptivemaxpool2d(inputs)
  995. return outputs
  996. class AdaptiveMaxPool3d(Module):
  997. """The :class:`AdaptiveMaxPool3d` class is a 3D Adaptive Max Pooling layer.
  998. Parameters
  999. ------------
  1000. output_size : int or list or tuple
  1001. The target output size. It cloud be an int \[int,int,int]\(int, int, int).
  1002. data_format : str
  1003. One of channels_last (default, [batch, depth, height, width, channel]) or channels_first. The ordering of the dimensions in the inputs.
  1004. name : None or str
  1005. A unique layer name.
  1006. Examples
  1007. ---------
  1008. With TensorLayer
  1009. >>> net = tl.layers.Input([10,32, 32, 32, 3], name='input')
  1010. >>> net = tl.layers.AdaptiveMaxPool3d(output_size=16)(net)
  1011. >>> output shape : [10, 16, 16, 16, 3]
  1012. """
  1013. def __init__(self, output_size, data_format='channels_last', name=None):
  1014. super(AdaptiveMaxPool3d, self).__init__(name)
  1015. self.output_size = output_size
  1016. self.data_format = data_format
  1017. self.build()
  1018. self._built = True
  1019. logging.info("AdaptiveMaxPool3d %s: output_size: %s " % (self.name, str(output_size)))
  1020. def __repr__(self):
  1021. s = ('{classname}(output_size={output_size}')
  1022. if self.name is not None:
  1023. s += ', name=\'{name}\''
  1024. s += ')'
  1025. return s.format(classname=self.__class__.__name__, **self.__dict__)
  1026. def build(self, inputs_shape=None):
  1027. if self.data_format == 'channels_last':
  1028. self.data_format = 'NDHWC'
  1029. elif self.data_format == 'channels_first':
  1030. self.data_format = 'NCDHW'
  1031. else:
  1032. raise Exception("unsupported data format")
  1033. if isinstance(self.output_size, int):
  1034. self.output_size = (self.output_size, ) * 3
  1035. self.adaptivemaxpool3d = tl.ops.AdaptiveMaxPool3D(output_size=self.output_size, data_format=self.data_format)
  1036. def forward(self, inputs):
  1037. outputs = self.adaptivemaxpool3d(inputs)
  1038. return outputs

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.