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

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