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.

simplified_conv.py 33 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from tensorlayer.layers.core import Module
  4. import tensorlayer as tl
  5. from tensorlayer import logging
  6. __all__ = [
  7. 'Conv1d',
  8. 'Conv2d',
  9. 'Conv3d',
  10. 'DeConv1d',
  11. 'DeConv2d',
  12. 'DeConv3d',
  13. ]
  14. class Conv1d(Module):
  15. """Simplified version of :class:`Conv1dLayer`.
  16. Parameters
  17. ----------
  18. n_filter : int
  19. The number of filters
  20. filter_size : int
  21. The filter size
  22. stride : int
  23. The stride step
  24. dilation_rate : int
  25. Specifying the dilation rate to use for dilated convolution.
  26. act : activation function
  27. The function that is applied to the layer activations
  28. padding : str
  29. The padding algorithm type: "SAME" or "VALID".
  30. data_format : str
  31. "channel_last" (NWC, default) or "channels_first" (NCW).
  32. W_init : initializer
  33. The initializer for the weight matrix.
  34. b_init : initializer or None
  35. The initializer for the bias vector. If None, skip biases.
  36. in_channels : int
  37. The number of in channels.
  38. name : None or str
  39. A unique layer name
  40. Examples
  41. --------
  42. With TensorLayer
  43. >>> net = tl.layers.Input([8, 100, 1], name='input')
  44. >>> conv1d = tl.layers.Conv1d(n_filter=32, filter_size=5, stride=2, b_init=None, in_channels=1, name='conv1d_1')
  45. >>> print(conv1d)
  46. >>> tensor = tl.layers.Conv1d(n_filter=32, filter_size=5, stride=2, act=tl.ops.relu, name='conv1d_2')(net)
  47. >>> print(tensor)
  48. """
  49. def __init__(
  50. self,
  51. n_filter=32,
  52. filter_size=5,
  53. stride=1,
  54. act=None,
  55. padding='SAME',
  56. data_format="channels_last",
  57. dilation_rate=1,
  58. W_init=tl.initializers.truncated_normal(stddev=0.02),
  59. b_init=tl.initializers.constant(value=0.0),
  60. in_channels=None,
  61. name=None # 'conv1d'
  62. ):
  63. super().__init__(name, act=act)
  64. self.n_filter = n_filter
  65. self.filter_size = filter_size
  66. self.stride = stride
  67. self.padding = padding
  68. self.data_format = data_format
  69. self.dilation_rate = dilation_rate
  70. self.W_init = W_init
  71. self.b_init = b_init
  72. self.in_channels = in_channels
  73. if self.in_channels:
  74. self.build(None)
  75. self._built = True
  76. logging.info(
  77. "Conv1d %s: n_filter: %d filter_size: %s stride: %d pad: %s act: %s" % (
  78. self.name, n_filter, filter_size, stride, padding,
  79. self.act.__class__.__name__ if self.act is not None else 'No Activation'
  80. )
  81. )
  82. def __repr__(self):
  83. actstr = self.act.__class__.__name__ if self.act is not None else 'No Activation'
  84. s = (
  85. '{classname}(in_channels={in_channels}, out_channels={n_filter}, kernel_size={filter_size}'
  86. ', stride={stride}, padding={padding}'
  87. )
  88. if self.dilation_rate != 1:
  89. s += ', dilation={dilation_rate}'
  90. if self.b_init is None:
  91. s += ', bias=False'
  92. s += (', ' + actstr)
  93. if self.name is not None:
  94. s += ', name=\'{name}\''
  95. s += ')'
  96. return s.format(classname=self.__class__.__name__, **self.__dict__)
  97. def build(self, inputs_shape):
  98. if self.data_format == 'channels_last':
  99. self.data_format = 'NWC'
  100. if self.in_channels is None:
  101. self.in_channels = inputs_shape[-1]
  102. elif self.data_format == 'channels_first':
  103. self.data_format = 'NCW'
  104. if self.in_channels is None:
  105. self.in_channels = inputs_shape[1]
  106. else:
  107. raise Exception("data_format should be either channels_last or channels_first")
  108. self.filter_shape = (self.filter_size, self.in_channels, self.n_filter)
  109. # TODO : check
  110. self.W = self._get_weights("filters", shape=self.filter_shape, init=self.W_init)
  111. self.b_init_flag = False
  112. if self.b_init:
  113. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  114. self.bias_add = tl.ops.BiasAdd(self.data_format)
  115. self.b_init_flag = True
  116. self.conv1d = tl.ops.Conv1D(
  117. stride=self.stride, padding=self.padding, data_format=self.data_format, dilations=self.dilation_rate,
  118. out_channel=self.n_filter, k_size=self.filter_size
  119. )
  120. self.act_init_flag = False
  121. if self.act:
  122. self.act_init_flag = True
  123. def forward(self, inputs):
  124. if self._forward_state == False:
  125. if self._built == False:
  126. self.build(tl.get_tensor_shape(inputs))
  127. self._built = True
  128. self._forward_state = True
  129. outputs = self.conv1d(inputs, self.W)
  130. if self.b_init_flag:
  131. outputs = self.bias_add(outputs, self.b)
  132. if self.act_init_flag:
  133. outputs = self.act(outputs)
  134. return outputs
  135. class Conv2d(Module):
  136. """Simplified version of :class:`Conv2dLayer`.
  137. Parameters
  138. ----------
  139. n_filter : int
  140. The number of filters.
  141. filter_size : tuple of int
  142. The filter size (height, width).
  143. strides : tuple of int
  144. The sliding window strides of corresponding input dimensions.
  145. It must be in the same order as the ``shape`` parameter.
  146. dilation_rate : tuple of int
  147. Specifying the dilation rate to use for dilated convolution.
  148. act : activation function
  149. The activation function of this layer.
  150. padding : str
  151. The padding algorithm type: "SAME" or "VALID".
  152. data_format : str
  153. "channels_last" (NHWC, default) or "channels_first" (NCHW).
  154. W_init : initializer
  155. The initializer for the the weight matrix.
  156. b_init : initializer or None
  157. The initializer for the the bias vector. If None, skip biases.
  158. in_channels : int
  159. The number of in channels.
  160. name : None or str
  161. A unique layer name.
  162. Examples
  163. --------
  164. With TensorLayer
  165. >>> net = tl.layers.Input([8, 3, 400, 400], name='input')
  166. >>> conv2d = tl.layers.Conv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), b_init=None, in_channels=3, name='conv2d_1')
  167. >>> print(conv2d)
  168. >>> tensor = tl.layers.Conv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), act=tl.ops.relu, name='conv2d_2')(net)
  169. >>> print(tensor)
  170. """
  171. def __init__(
  172. self,
  173. n_filter=32,
  174. filter_size=(3, 3),
  175. strides=(1, 1),
  176. act=None,
  177. padding='SAME',
  178. data_format='channels_last',
  179. dilation_rate=(1, 1),
  180. W_init=tl.initializers.truncated_normal(stddev=0.02),
  181. b_init=tl.initializers.constant(value=0.0),
  182. in_channels=None,
  183. name=None, # 'conv2d',
  184. ):
  185. super(Conv2d, self).__init__(name, act=act)
  186. self.n_filter = n_filter
  187. self.filter_size = filter_size
  188. self._strides = self.strides = strides
  189. self.padding = padding
  190. self.data_format = data_format
  191. self._dilation_rate = self.dilation_rate = dilation_rate
  192. self.W_init = W_init
  193. self.b_init = b_init
  194. self.in_channels = in_channels
  195. if self.in_channels:
  196. self.build(None)
  197. self._built = True
  198. logging.info(
  199. "Conv2d %s: n_filter: %d filter_size: %s strides: %s pad: %s act: %s" % (
  200. self.name, n_filter, str(filter_size), str(strides), padding,
  201. self.act.__class__.__name__ if self.act is not None else 'No Activation'
  202. )
  203. )
  204. def __repr__(self):
  205. actstr = self.act.__class__.__name__ if self.act is not None else 'No Activation'
  206. s = (
  207. '{classname}(in_channels={in_channels}, out_channels={n_filter}, kernel_size={filter_size}'
  208. ', strides={strides}, padding={padding}'
  209. )
  210. if self.dilation_rate != (1, ) * len(self.dilation_rate):
  211. s += ', dilation={dilation_rate}'
  212. if self.b_init is None:
  213. s += ', bias=False'
  214. s += (', ' + actstr)
  215. if self.name is not None:
  216. s += ', name=\'{name}\''
  217. s += ')'
  218. return s.format(classname=self.__class__.__name__, **self.__dict__)
  219. def build(self, inputs_shape):
  220. if self.data_format == 'channels_last':
  221. self.data_format = 'NHWC'
  222. if self.in_channels is None:
  223. self.in_channels = inputs_shape[-1]
  224. self._strides = [1, self._strides[0], self._strides[1], 1]
  225. self._dilation_rate = [1, self._dilation_rate[0], self._dilation_rate[1], 1]
  226. elif self.data_format == 'channels_first':
  227. self.data_format = 'NCHW'
  228. if self.in_channels is None:
  229. self.in_channels = inputs_shape[1]
  230. self._strides = [1, 1, self._strides[0], self._strides[1]]
  231. self._dilation_rate = [1, 1, self._dilation_rate[0], self._dilation_rate[1]]
  232. else:
  233. raise Exception("data_format should be either channels_last or channels_first")
  234. #TODO channels first filter shape [out_channel, in_channel, filter_h, filter_w]
  235. self.filter_shape = (self.filter_size[0], self.filter_size[1], self.in_channels, self.n_filter)
  236. self.W = self._get_weights("filters", shape=self.filter_shape, init=self.W_init)
  237. self.b_init_flag = False
  238. if self.b_init:
  239. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  240. self.bias_add = tl.ops.BiasAdd(self.data_format)
  241. self.b_init_flag = True
  242. self.conv2d = tl.ops.Conv2D(
  243. strides=self._strides, padding=self.padding, data_format=self.data_format, dilations=self._dilation_rate,
  244. out_channel=self.n_filter, k_size=(self.filter_size[0], self.filter_size[1])
  245. )
  246. self.act_init_flag = False
  247. if self.act:
  248. self.act_init_flag = True
  249. def forward(self, inputs):
  250. if self._forward_state == False:
  251. if self._built == False:
  252. self.build(tl.get_tensor_shape(inputs))
  253. self._built = True
  254. self._forward_state = True
  255. outputs = self.conv2d(inputs, self.W)
  256. if self.b_init_flag:
  257. outputs = self.bias_add(outputs, self.b)
  258. if self.act_init_flag:
  259. outputs = self.act(outputs)
  260. return outputs
  261. class Conv3d(Module):
  262. """Simplified version of :class:`Conv3dLayer`.
  263. Parameters
  264. ---AppData\Local\Continuum\anaconda3\envs\ms_tf\lib\site-packages\mindspore\common\api.py", line 412, in compile
  265. result = self._executor.compile(obj, args_list, phase, use_vm)
  266. RuntimeError: Unable to cast from non-held to held instance (T& to Holder<T>) of type 'std:-------
  267. n_filter : int
  268. The number of filters.
  269. filter_size : tuple of int
  270. The filter size (height, width).
  271. strides : tuple of int
  272. The sliding window strides of corresponding input dimensions.
  273. It must be in the same order as the ``shape`` parameter.
  274. dilation_rate : tuple of int
  275. Specifying the dilation rate to use for dilated convolution.
  276. act : activation function
  277. The activation function of this layer.
  278. padding : str
  279. The padding algorithm type: "SAME" or "VALID".
  280. data_format : str
  281. "channels_last" (NDHWC, default) or "channels_first" (NCDHW).
  282. W_init : initializer
  283. The initializer for the the weight matrix.
  284. b_init : initializer or None
  285. The initializer for the the bias vector. If None, skip biases.
  286. in_channels : int
  287. The number of in channels.
  288. name : None or str
  289. A unique layer name.
  290. Examples
  291. --------
  292. With TensorLayer
  293. >>> net = tl.layers.Input([8, 20, 20, 20, 3], name='input')
  294. >>> conv3d = tl.layers.Conv3d(n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), b_init=None, in_channels=3, name='conv3d_1')
  295. >>> print(conv3d)
  296. >>> tensor = tl.layers.Conv3d(n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), act=tl.ops.relu, name='conv3d_2')(net)
  297. >>> print(tensor)
  298. """
  299. def __init__(
  300. self,
  301. n_filter=32,
  302. filter_size=(3, 3, 3),
  303. strides=(1, 1, 1),
  304. act=None,
  305. padding='SAME',
  306. data_format='channels_last',
  307. dilation_rate=(1, 1, 1),
  308. W_init=tl.initializers.truncated_normal(stddev=0.02),
  309. b_init=tl.initializers.constant(value=0.0),
  310. in_channels=None,
  311. name=None # 'conv3d',
  312. ):
  313. super().__init__(name, act=act)
  314. self.n_filter = n_filter
  315. self.filter_size = filter_size
  316. self._strides = self.strides = strides
  317. self.padding = padding
  318. self.data_format = data_format
  319. self._dilation_rate = self.dilation_rate = dilation_rate
  320. self.W_init = W_init
  321. self.b_init = b_init
  322. self.in_channels = in_channels
  323. if self.in_channels:
  324. self.build(None)
  325. self._built = True
  326. logging.info(
  327. "Conv3d %s: n_filter: %d filter_size: %s strides: %s pad: %s act: %s" % (
  328. self.name, n_filter, str(filter_size), str(strides), padding,
  329. self.act.__class__.__name__ if self.act is not None else 'No Activation'
  330. )
  331. )
  332. def __repr__(self):
  333. actstr = self.act.__class__.__name__ if self.act is not None else 'No Activation'
  334. s = (
  335. '{classname}(in_channels={in_channels}, out_channels={n_filter}, kernel_size={filter_size}'
  336. ', strides={strides}, padding={padding}'
  337. )
  338. if self.dilation_rate != (1, ) * len(self.dilation_rate):
  339. s += ', dilation={dilation_rate}'
  340. if self.b_init is None:
  341. s += ', bias=False'
  342. s += (', ' + actstr)
  343. if self.name is not None:
  344. s += ', name=\'{name}\''
  345. s += ')'
  346. return s.format(classname=self.__class__.__name__, **self.__dict__)
  347. def build(self, inputs_shape):
  348. if self.data_format == 'channels_last':
  349. self.data_format = 'NDHWC'
  350. if self.in_channels is None:
  351. self.in_channels = inputs_shape[-1]
  352. self._strides = [1, self._strides[0], self._strides[1], self._strides[2], 1]
  353. self._dilation_rate = [1, self.dilation_rate[0], self.dilation_rate[1], self.dilation_rate[2], 1]
  354. elif self.data_format == 'channels_first':
  355. self.data_format = 'NCDHW'
  356. if self.in_channels is None:
  357. self.in_channels = inputs_shape[1]
  358. self._strides = [1, 1, self._strides[0], self._strides[1], self._strides[2]]
  359. self._dilation_rate = [1, 1, self._dilation_rate[0], self._dilation_rate[1], self._dilation_rate[2]]
  360. else:
  361. raise Exception("data_format should be either channels_last or channels_first")
  362. self.filter_shape = (
  363. self.filter_size[0], self.filter_size[1], self.filter_size[2], self.in_channels, self.n_filter
  364. )
  365. self.W = self._get_weights("filters", shape=self.filter_shape, init=self.W_init)
  366. if self.b_init:
  367. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  368. self.b_init_flag = False
  369. if self.b_init:
  370. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  371. self.bias_add = tl.ops.BiasAdd(self.data_format)
  372. self.b_init_flag = True
  373. self.conv3d = tl.ops.Conv3D(
  374. strides=self._strides, padding=self.padding, data_format=self.data_format, dilations=self._dilation_rate,
  375. out_channel=self.n_filter, k_size=(self.filter_size[0], self.filter_size[1])
  376. )
  377. self.act_init_flag = False
  378. if self.act:
  379. self.act_init_flag = True
  380. def forward(self, inputs):
  381. if self._forward_state == False:
  382. if self._built == False:
  383. self.build(tl.get_tensor_shape(inputs))
  384. self._built = True
  385. self._forward_state = True
  386. outputs = self.conv3d(inputs, self.W)
  387. if self.b_init_flag:
  388. outputs = self.bias_add(outputs, self.b)
  389. if self.act_init_flag:
  390. outputs = self.act(outputs)
  391. return outputs
  392. class DeConv1d(Module):
  393. """Simplified version of :class:`Deconv1dlayer`.
  394. Parameters
  395. ----------
  396. n_filter : int
  397. The number of filters
  398. filter_size : int
  399. The filter size
  400. strides : int or list
  401. An int or list of `ints` that has length `1` or `3`. The number of entries by which the filter is moved right at each step.
  402. output_shape : a 1-D Tensor
  403. containing three elements, representing the output shape of the deconvolution op.
  404. dilation_rate : int or list
  405. Specifying the dilation rate to use for dilated convolution.
  406. act : activation function
  407. The function that is applied to the layer activations
  408. padding : str
  409. The padding algorithm type: "SAME" or "VALID".
  410. data_format : str
  411. "channel_last" (NWC, default) or "channels_first" (NCW).
  412. W_init : initializer
  413. The initializer for the weight matrix.
  414. b_init : initializer or None
  415. The initializer for the bias vector. If None, skip biases.
  416. in_channels : int
  417. The number of in channels.
  418. name : None or str
  419. A unique layer name
  420. Examples
  421. --------
  422. With TensorLayer
  423. >>> net = tl.layers.Input([8, 100, 1], name='input')
  424. >>> conv1d = tl.layers.DeConv1d(n_filter=32, filter_size=5, stride=2, b_init=None, in_channels=1, name='Deonv1d_1')
  425. >>> print(conv1d)
  426. >>> tensor = tl.layers.DeConv1d(n_filter=32, filter_size=5, stride=2, act=tl.ops.relu, name='Deconv1d_2')(net)
  427. >>> print(tensor)
  428. """
  429. def __init__(
  430. self,
  431. n_filter=32,
  432. filter_size=15,
  433. strides=1,
  434. act=None,
  435. padding='SAME',
  436. data_format="channels_last",
  437. dilation_rate=1,
  438. W_init=tl.initializers.truncated_normal(stddev=0.02),
  439. b_init=tl.initializers.constant(value=0.0),
  440. in_channels=None,
  441. name=None # 'conv1d_transpose'
  442. ):
  443. super(DeConv1d, self).__init__(name, act=act)
  444. self.n_filter = n_filter
  445. self.filter_size = filter_size
  446. self.strides = strides
  447. self.padding = padding
  448. self.data_format = data_format
  449. self.dilation_rate = dilation_rate
  450. self.W_init = W_init
  451. self.b_init = b_init
  452. self.in_channels = in_channels
  453. if self.in_channels:
  454. self.build(None)
  455. self._built = True
  456. logging.info(
  457. "DeConv1d %s: n_filter: %d filter_size: %s stride: %d pad: %s act: %s" % (
  458. self.name, n_filter, filter_size, strides, padding,
  459. self.act.__class__.__name__ if self.act is not None else 'No Activation'
  460. )
  461. )
  462. def __repr__(self):
  463. actstr = self.act.__class__.__name__ if self.act is not None else 'No Activation'
  464. s = (
  465. '{classname}(in_channels={in_channels}, out_channels={n_filter}, kernel_size={filter_size}'
  466. ', strides={strides}, padding={padding}'
  467. )
  468. if self.dilation_rate != 1:
  469. s += ', dilation={dilation_rate}'
  470. if self.b_init is None:
  471. s += ', bias=False'
  472. s += (', ' + actstr)
  473. if self.name is not None:
  474. s += ', name=\'{name}\''
  475. s += ')'
  476. return s.format(classname=self.__class__.__name__, **self.__dict__)
  477. def build(self, inputs_shape):
  478. if self.data_format == 'channels_last':
  479. self.data_format = 'NWC'
  480. if self.in_channels is None:
  481. self.in_channels = inputs_shape[-1]
  482. elif self.data_format == 'channels_first':
  483. self.data_format = 'NCW'
  484. if self.in_channels is None:
  485. self.in_channels = inputs_shape[1]
  486. else:
  487. raise Exception("data_format should be either channels_last or channels_first")
  488. self.filter_shape = (self.filter_size, self.n_filter, self.in_channels)
  489. # TODO : check
  490. self.W = self._get_weights("filters", shape=self.filter_shape, init=self.W_init)
  491. self.b_init_flag = False
  492. if self.b_init:
  493. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  494. self.bias_add = tl.ops.BiasAdd(self.data_format)
  495. self.b_init_flag = True
  496. self.conv1d_transpose = tl.ops.Conv1d_transpose(
  497. strides=self.strides,
  498. padding=self.padding,
  499. data_format=self.data_format,
  500. dilations=self.dilation_rate,
  501. out_channel=self.n_filter,
  502. k_size=self.filter_size,
  503. in_channels=self.in_channels,
  504. )
  505. self.act_init_flag = False
  506. if self.act:
  507. self.act_init_flag = True
  508. def forward(self, inputs):
  509. if self._forward_state == False:
  510. if self._built == False:
  511. self.build(tl.get_tensor_shape(inputs))
  512. self._built = True
  513. self._forward_state = True
  514. outputs = self.conv1d_transpose(inputs, self.W)
  515. if self.b_init_flag:
  516. outputs = self.bias_add(outputs, self.b)
  517. if self.act_init_flag:
  518. outputs = self.act(outputs)
  519. return outputs
  520. class DeConv2d(Module):
  521. """Simplified version of :class:`Deconv2dLayer`.
  522. Parameters
  523. ----------
  524. n_filter : int
  525. The number of filters.
  526. filter_size : tuple of int
  527. The filter size.
  528. strides : tuple of int
  529. The sliding window strides of corresponding input dimensions.
  530. It must be in the same order as the ``shape`` parameter.
  531. output_shape : A 1-D Tensor
  532. representing the output shape of the deconvolution op.
  533. dilation_rate : tuple of int
  534. Specifying the dilation rate to use for dilated convolution.
  535. act : activation function
  536. The activation function of this layer.
  537. padding : str
  538. The padding algorithm type: "SAME" or "VALID".
  539. data_format : str
  540. "channels_last" (NHWC, default) or "channels_first" (NCHW).
  541. W_init : initializer
  542. The initializer for the the weight matrix.
  543. b_init : initializer or None
  544. The initializer for the the bias vector. If None, skip biases.
  545. in_channels : int
  546. The number of in channels.
  547. name : None or str
  548. A unique layer name.
  549. Examples
  550. --------
  551. With TensorLayer
  552. >>> net = tl.layers.Input([8, 3, 400, 400], name='input')
  553. >>> conv2d_transpose = tl.layers.DeConv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), b_init=None, in_channels=3, name='conv2d_transpose_1')
  554. >>> print(conv2d_transpose)
  555. >>> tensor = tl.layers.DeConv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), act=tl.ops.relu, name='conv2d_transpose_2')(net)
  556. >>> print(tensor)
  557. """
  558. def __init__(
  559. self,
  560. n_filter=32,
  561. filter_size=(3, 3),
  562. strides=(1, 1),
  563. act=None,
  564. padding='SAME',
  565. data_format='channels_last',
  566. dilation_rate=(1, 1),
  567. W_init=tl.initializers.truncated_normal(stddev=0.02),
  568. b_init=tl.initializers.constant(value=0.0),
  569. in_channels=None,
  570. name=None, # 'conv2d_transpose',
  571. ):
  572. super(DeConv2d, self).__init__(name, act=act)
  573. self.n_filter = n_filter
  574. self.filter_size = filter_size
  575. self._strides = self.strides = strides
  576. self.padding = padding
  577. self.data_format = data_format
  578. self._dilation_rate = self.dilation_rate = dilation_rate
  579. self.W_init = W_init
  580. self.b_init = b_init
  581. self.in_channels = in_channels
  582. if self.in_channels:
  583. self.build(None)
  584. self._built = True
  585. logging.info(
  586. "DeConv2d %s: n_filter: %d filter_size: %s strides: %s pad: %s act: %s" % (
  587. self.name, n_filter, str(filter_size), str(strides), padding,
  588. self.act.__class__.__name__ if self.act is not None else 'No Activation'
  589. )
  590. )
  591. def __repr__(self):
  592. actstr = self.act.__class__.__name__ if self.act is not None else 'No Activation'
  593. s = (
  594. '{classname}(in_channels={in_channels}, out_channels={n_filter}, kernel_size={filter_size}'
  595. ', strides={strides}, padding={padding}'
  596. )
  597. if self.dilation_rate != (1, ) * len(self.dilation_rate):
  598. s += ', dilation={dilation_rate}'
  599. if self.b_init is None:
  600. s += ', bias=False'
  601. s += (', ' + actstr)
  602. if self.name is not None:
  603. s += ', name=\'{name}\''
  604. s += ')'
  605. return s.format(classname=self.__class__.__name__, **self.__dict__)
  606. def build(self, inputs_shape):
  607. if self.data_format == 'channels_last':
  608. self.data_format = 'NHWC'
  609. if self.in_channels is None:
  610. self.in_channels = inputs_shape[-1]
  611. self._strides = [1, self._strides[0], self._strides[1], 1]
  612. self._dilation_rate = [1, self._dilation_rate[0], self._dilation_rate[1], 1]
  613. elif self.data_format == 'channels_first':
  614. self.data_format = 'NCHW'
  615. if self.in_channels is None:
  616. self.in_channels = inputs_shape[1]
  617. self._strides = [1, 1, self._strides[0], self._strides[1]]
  618. self._dilation_rate = [1, 1, self._dilation_rate[0], self._dilation_rate[1]]
  619. else:
  620. raise Exception("data_format should be either channels_last or channels_first")
  621. #TODO channels first filter shape [out_channel, in_channel, filter_h, filter_w]
  622. self.filter_shape = (self.filter_size[0], self.filter_size[1], self.n_filter, self.in_channels)
  623. self.W = self._get_weights("filters", shape=self.filter_shape, init=self.W_init)
  624. self.b_init_flag = False
  625. if self.b_init:
  626. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  627. self.bias_add = tl.ops.BiasAdd(self.data_format)
  628. self.b_init_flag = True
  629. self.conv2d_transpose = tl.ops.Conv2d_transpose(
  630. strides=self._strides, padding=self.padding, data_format=self.data_format, dilations=self._dilation_rate,
  631. out_channel=self.n_filter, k_size=(self.filter_size[0], self.filter_size[1]), in_channels=self.in_channels
  632. )
  633. self.act_init_flag = False
  634. if self.act:
  635. self.act_init_flag = True
  636. def forward(self, inputs):
  637. if self._forward_state == False:
  638. if self._built == False:
  639. self.build(tl.get_tensor_shape(inputs))
  640. self._built = True
  641. self._forward_state = True
  642. outputs = self.conv2d_transpose(inputs, self.W)
  643. if self.b_init_flag:
  644. outputs = self.bias_add(outputs, self.b)
  645. if self.act_init_flag:
  646. outputs = self.act(outputs)
  647. return outputs
  648. class DeConv3d(Module):
  649. """Simplified version of :class:`Deconv3dLayer`.
  650. Parameters
  651. ---AppData\Local\Continuum\anaconda3\envs\ms_tf\lib\site-packages\mindspore\common\api.py", line 412, in compile
  652. result = self._executor.compile(obj, args_list, phase, use_vm)
  653. RuntimeError: Unable to cast from non-held to held instance (T& to Holder<T>) of type 'std:-------
  654. n_filter : int
  655. The number of filters.
  656. filter_size : tuple of int
  657. The filter size (depth, height, width).
  658. output_shape:
  659. A 1-D Tensor representing the output shape of the deconvolution op.
  660. strides : tuple of int
  661. The sliding window strides of corresponding input dimensions.
  662. It must be in the same order as the ``shape`` parameter.
  663. dilation_rate : tuple of int
  664. Specifying the dilation rate to use for dilated convolution.
  665. act : activation function
  666. The activation function of this layer.
  667. padding : str
  668. The padding algorithm type: "SAME" or "VALID".
  669. data_format : str
  670. "channels_last" (NDHWC, default) or "channels_first" (NCDHW).
  671. W_init : initializer
  672. The initializer for the the weight matrix.
  673. b_init : initializer or None
  674. The initializer for the the bias vector. If None, skip biases.
  675. in_channels : int
  676. The number of in channels.
  677. name : None or str
  678. A unique layer name.
  679. Examples
  680. --------
  681. With TensorLayer
  682. >>> net = tl.layers.Input([8, 20, 20, 20, 3], name='input')
  683. >>> deconv3d = tl.layers.DeConv3d(n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), b_init=None, in_channels=3, name='deconv3d_1')
  684. >>> print(deconv3d)
  685. >>> tensor = tl.layers.DeConv3d(n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), act=tl.ops.relu, name='deconv3d_2')(net)
  686. >>> print(tensor)
  687. """
  688. def __init__(
  689. self,
  690. n_filter=32,
  691. filter_size=(3, 3, 3),
  692. strides=(1, 1, 1),
  693. act=None,
  694. padding='SAME',
  695. data_format='channels_last',
  696. dilation_rate=(1, 1, 1),
  697. W_init=tl.initializers.truncated_normal(stddev=0.02),
  698. b_init=tl.initializers.constant(value=0.0),
  699. in_channels=None,
  700. name=None # 'deconv3d',
  701. ):
  702. super(DeConv3d, self).__init__(name, act=act)
  703. self.n_filter = n_filter
  704. self.filter_size = filter_size
  705. self._strides = self.strides = strides
  706. self.padding = padding
  707. self.data_format = data_format
  708. self._dilation_rate = self.dilation_rate = dilation_rate
  709. self.W_init = W_init
  710. self.b_init = b_init
  711. self.in_channels = in_channels
  712. if self.in_channels:
  713. self.build(None)
  714. self._built = True
  715. logging.info(
  716. "DeConv3d %s: n_filter: %d filter_size: %s strides: %s pad: %s act: %s" % (
  717. self.name, n_filter, str(filter_size), str(strides), padding,
  718. self.act.__class__.__name__ if self.act is not None else 'No Activation'
  719. )
  720. )
  721. def __repr__(self):
  722. actstr = self.act.__class__.__name__ if self.act is not None else 'No Activation'
  723. s = (
  724. '{classname}(in_channels={in_channels}, out_channels={n_filter}, kernel_size={filter_size}'
  725. ', strides={strides}, padding={padding}'
  726. )
  727. if self.dilation_rate != (1, ) * len(self.dilation_rate):
  728. s += ', dilation={dilation_rate}'
  729. if self.b_init is None:
  730. s += ', bias=False'
  731. s += (', ' + actstr)
  732. if self.name is not None:
  733. s += ', name=\'{name}\''
  734. s += ')'
  735. return s.format(classname=self.__class__.__name__, **self.__dict__)
  736. def build(self, inputs_shape):
  737. if self.data_format == 'channels_last':
  738. self.data_format = 'NDHWC'
  739. if self.in_channels is None:
  740. self.in_channels = inputs_shape[-1]
  741. self._strides = [1, self._strides[0], self._strides[1], self._strides[2], 1]
  742. self._dilation_rate = [1, self.dilation_rate[0], self.dilation_rate[1], self.dilation_rate[2], 1]
  743. elif self.data_format == 'channels_first':
  744. self.data_format = 'NCDHW'
  745. if self.in_channels is None:
  746. self.in_channels = inputs_shape[1]
  747. self._strides = [1, 1, self._strides[0], self._strides[1], self._strides[2]]
  748. self._dilation_rate = [1, 1, self._dilation_rate[0], self._dilation_rate[1], self._dilation_rate[2]]
  749. else:
  750. raise Exception("data_format should be either channels_last or channels_first")
  751. self.filter_shape = (
  752. self.filter_size[0], self.filter_size[1], self.filter_size[2], self.n_filter, self.in_channels
  753. )
  754. self.W = self._get_weights("filters", shape=self.filter_shape, init=self.W_init)
  755. if self.b_init:
  756. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  757. self.b_init_flag = False
  758. if self.b_init:
  759. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  760. self.bias_add = tl.ops.BiasAdd(self.data_format)
  761. self.b_init_flag = True
  762. self.conv3d_transpose = tl.ops.Conv3d_transpose(
  763. strides=self._strides, padding=self.padding, data_format=self.data_format, dilations=self._dilation_rate,
  764. out_channel=self.n_filter, k_size=(self.filter_size[0], self.filter_size[1], self.filter_size[2])
  765. )
  766. self.act_init_flag = False
  767. if self.act:
  768. self.act_init_flag = True
  769. def forward(self, inputs):
  770. if self._forward_state == False:
  771. if self._built == False:
  772. self.build(tl.get_tensor_shape(inputs))
  773. self._built = True
  774. self._forward_state = True
  775. outputs = self.conv3d_transpose(inputs, self.W)
  776. if self.b_init_flag:
  777. outputs = self.bias_add(outputs, self.b)
  778. if self.act_init_flag:
  779. outputs = self.act(outputs)
  780. return outputs

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