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 32 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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.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, 400, 400, 3], 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.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.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. self.b_init_flag = False
  367. if self.b_init:
  368. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  369. self.bias_add = tl.ops.BiasAdd(self.data_format)
  370. self.b_init_flag = True
  371. self.conv3d = tl.ops.Conv3D(
  372. strides=self._strides, padding=self.padding, data_format=self.data_format, dilations=self._dilation_rate,
  373. out_channel=self.n_filter, k_size=(self.filter_size[0], self.filter_size[1], self.filter_size[2])
  374. )
  375. self.act_init_flag = False
  376. if self.act:
  377. self.act_init_flag = True
  378. def forward(self, inputs):
  379. if self._forward_state == False:
  380. if self._built == False:
  381. self.build(tl.get_tensor_shape(inputs))
  382. self._built = True
  383. self._forward_state = True
  384. outputs = self.conv3d(inputs, self.W)
  385. if self.b_init_flag:
  386. outputs = self.bias_add(outputs, self.b)
  387. if self.act_init_flag:
  388. outputs = self.act(outputs)
  389. return outputs
  390. class DeConv1d(Module):
  391. """Simplified version of :class:`Deconv1dlayer`.
  392. Parameters
  393. ----------
  394. n_filter : int
  395. The number of filters
  396. filter_size : int
  397. The filter size
  398. strides : int or list
  399. 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.
  400. output_shape : a 1-D Tensor
  401. containing three elements, representing the output shape of the deconvolution op.
  402. dilation_rate : int or list
  403. Specifying the dilation rate to use for dilated convolution.
  404. act : activation function
  405. The function that is applied to the layer activations
  406. padding : str
  407. The padding algorithm type: "SAME" or "VALID".
  408. data_format : str
  409. "channel_last" (NWC, default) or "channels_first" (NCW).
  410. W_init : initializer
  411. The initializer for the weight matrix.
  412. b_init : initializer or None
  413. The initializer for the bias vector. If None, skip biases.
  414. in_channels : int
  415. The number of in channels.
  416. name : None or str
  417. A unique layer name
  418. Examples
  419. --------
  420. With TensorLayer
  421. >>> net = tl.layers.Input([8, 100, 1], name='input')
  422. >>> conv1d = tl.layers.DeConv1d(n_filter=32, filter_size=5, stride=2, b_init=None, in_channels=1, name='Deonv1d_1')
  423. >>> print(conv1d)
  424. >>> tensor = tl.layers.DeConv1d(n_filter=32, filter_size=5, stride=2, act=tl.ReLU, name='Deconv1d_2')(net)
  425. >>> print(tensor)
  426. """
  427. def __init__(
  428. self,
  429. n_filter=32,
  430. filter_size=15,
  431. stride=1,
  432. act=None,
  433. padding='SAME',
  434. data_format="channels_last",
  435. dilation_rate=1,
  436. W_init=tl.initializers.truncated_normal(stddev=0.02),
  437. b_init=tl.initializers.constant(value=0.0),
  438. in_channels=None,
  439. name=None # 'conv1d_transpose'
  440. ):
  441. super(DeConv1d, self).__init__(name, act=act)
  442. self.n_filter = n_filter
  443. self.filter_size = filter_size
  444. self.stride = stride
  445. self.padding = padding
  446. self.data_format = data_format
  447. self.dilation_rate = dilation_rate
  448. self.W_init = W_init
  449. self.b_init = b_init
  450. self.in_channels = in_channels
  451. if self.in_channels:
  452. self.build(None)
  453. self._built = True
  454. logging.info(
  455. "DeConv1d %s: n_filter: %d filter_size: %s stride: %d pad: %s act: %s" % (
  456. self.name, n_filter, filter_size, stride, padding,
  457. self.act.__class__.__name__ if self.act is not None else 'No Activation'
  458. )
  459. )
  460. def __repr__(self):
  461. actstr = self.act.__class__.__name__ if self.act is not None else 'No Activation'
  462. s = (
  463. '{classname}(in_channels={in_channels}, out_channels={n_filter}, kernel_size={filter_size}'
  464. ', stride={stride}, padding={padding}'
  465. )
  466. if self.dilation_rate != 1:
  467. s += ', dilation={dilation_rate}'
  468. if self.b_init is None:
  469. s += ', bias=False'
  470. s += (', ' + actstr)
  471. if self.name is not None:
  472. s += ', name=\'{name}\''
  473. s += ')'
  474. return s.format(classname=self.__class__.__name__, **self.__dict__)
  475. def build(self, inputs_shape):
  476. if self.data_format == 'channels_last':
  477. self.data_format = 'NWC'
  478. if self.in_channels is None:
  479. self.in_channels = inputs_shape[-1]
  480. elif self.data_format == 'channels_first':
  481. self.data_format = 'NCW'
  482. if self.in_channels is None:
  483. self.in_channels = inputs_shape[1]
  484. else:
  485. raise Exception("data_format should be either channels_last or channels_first")
  486. self.filter_shape = (self.filter_size, self.n_filter, self.in_channels)
  487. # TODO : check
  488. self.W = self._get_weights("filters", shape=self.filter_shape, init=self.W_init)
  489. self.b_init_flag = False
  490. if self.b_init:
  491. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  492. self.bias_add = tl.ops.BiasAdd(self.data_format)
  493. self.b_init_flag = True
  494. self.conv1d_transpose = tl.ops.Conv1d_transpose(
  495. stride=self.stride,
  496. padding=self.padding,
  497. data_format=self.data_format,
  498. dilations=self.dilation_rate,
  499. out_channel=self.n_filter,
  500. k_size=self.filter_size,
  501. in_channels=self.in_channels,
  502. )
  503. self.act_init_flag = False
  504. if self.act:
  505. self.act_init_flag = True
  506. def forward(self, inputs):
  507. if self._forward_state == False:
  508. if self._built == False:
  509. self.build(tl.get_tensor_shape(inputs))
  510. self._built = True
  511. self._forward_state = True
  512. outputs = self.conv1d_transpose(inputs, self.W)
  513. if self.b_init_flag:
  514. outputs = self.bias_add(outputs, self.b)
  515. if self.act_init_flag:
  516. outputs = self.act(outputs)
  517. return outputs
  518. class DeConv2d(Module):
  519. """Simplified version of :class:`Deconv2dLayer`.
  520. Parameters
  521. ----------
  522. n_filter : int
  523. The number of filters.
  524. filter_size : tuple of int
  525. The filter size.
  526. strides : tuple of int
  527. The sliding window strides of corresponding input dimensions.
  528. It must be in the same order as the ``shape`` parameter.
  529. output_shape : A 1-D Tensor
  530. representing the output shape of the deconvolution op.
  531. dilation_rate : tuple of int
  532. Specifying the dilation rate to use for dilated convolution.
  533. act : activation function
  534. The activation function of this layer.
  535. padding : str
  536. The padding algorithm type: "SAME" or "VALID".
  537. data_format : str
  538. "channels_last" (NHWC, default) or "channels_first" (NCHW).
  539. W_init : initializer
  540. The initializer for the the weight matrix.
  541. b_init : initializer or None
  542. The initializer for the the bias vector. If None, skip biases.
  543. in_channels : int
  544. The number of in channels.
  545. name : None or str
  546. A unique layer name.
  547. Examples
  548. --------
  549. With TensorLayer
  550. >>> net = tl.layers.Input([8, 400, 400, 3], name='input')
  551. >>> 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')
  552. >>> print(conv2d_transpose)
  553. >>> tensor = tl.layers.DeConv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), act=tl.ReLU, name='conv2d_transpose_2')(net)
  554. >>> print(tensor)
  555. """
  556. def __init__(
  557. self,
  558. n_filter=32,
  559. filter_size=(3, 3),
  560. strides=(1, 1),
  561. act=None,
  562. padding='SAME',
  563. data_format='channels_last',
  564. dilation_rate=(1, 1),
  565. W_init=tl.initializers.truncated_normal(stddev=0.02),
  566. b_init=tl.initializers.constant(value=0.0),
  567. in_channels=None,
  568. name=None, # 'conv2d_transpose',
  569. ):
  570. super(DeConv2d, self).__init__(name, act=act)
  571. self.n_filter = n_filter
  572. self.filter_size = filter_size
  573. self.strides = strides
  574. self.padding = padding
  575. self.data_format = data_format
  576. self.dilation_rate = dilation_rate
  577. self.W_init = W_init
  578. self.b_init = b_init
  579. self.in_channels = in_channels
  580. if self.in_channels:
  581. self.build(None)
  582. self._built = True
  583. logging.info(
  584. "DeConv2d %s: n_filter: %d filter_size: %s strides: %s pad: %s act: %s" % (
  585. self.name, n_filter, str(filter_size), str(strides), padding,
  586. self.act.__class__.__name__ if self.act is not None else 'No Activation'
  587. )
  588. )
  589. def __repr__(self):
  590. actstr = self.act.__class__.__name__ if self.act is not None else 'No Activation'
  591. s = (
  592. '{classname}(in_channels={in_channels}, out_channels={n_filter}, kernel_size={filter_size}'
  593. ', strides={strides}, padding={padding}'
  594. )
  595. if self.dilation_rate != (1, ) * len(self.dilation_rate):
  596. s += ', dilation={dilation_rate}'
  597. if self.b_init is None:
  598. s += ', bias=False'
  599. s += (', ' + actstr)
  600. if self.name is not None:
  601. s += ', name=\'{name}\''
  602. s += ')'
  603. return s.format(classname=self.__class__.__name__, **self.__dict__)
  604. def build(self, inputs_shape):
  605. if self.data_format == 'channels_last':
  606. self.data_format = 'NHWC'
  607. if self.in_channels is None:
  608. self.in_channels = inputs_shape[-1]
  609. elif self.data_format == 'channels_first':
  610. self.data_format = 'NCHW'
  611. if self.in_channels is None:
  612. self.in_channels = inputs_shape[1]
  613. else:
  614. raise Exception("data_format should be either channels_last or channels_first")
  615. #TODO channels first filter shape [out_channel, in_channel, filter_h, filter_w]
  616. self.filter_shape = (self.filter_size[0], self.filter_size[1], self.n_filter, self.in_channels)
  617. self.W = self._get_weights("filters", shape=self.filter_shape, init=self.W_init, transposed=True)
  618. self.b_init_flag = False
  619. if self.b_init:
  620. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  621. self.bias_add = tl.ops.BiasAdd(self.data_format)
  622. self.b_init_flag = True
  623. self.conv2d_transpose = tl.ops.Conv2d_transpose(
  624. strides=self.strides, padding=self.padding, data_format=self.data_format, dilations=self.dilation_rate,
  625. out_channel=self.n_filter, k_size=(self.filter_size[0], self.filter_size[1]), in_channels=self.in_channels
  626. )
  627. self.act_init_flag = False
  628. if self.act:
  629. self.act_init_flag = True
  630. def forward(self, inputs):
  631. if self._forward_state == False:
  632. if self._built == False:
  633. self.build(tl.get_tensor_shape(inputs))
  634. self._built = True
  635. self._forward_state = True
  636. outputs = self.conv2d_transpose(inputs, self.W)
  637. if self.b_init_flag:
  638. outputs = self.bias_add(outputs, self.b)
  639. if self.act_init_flag:
  640. outputs = self.act(outputs)
  641. return outputs
  642. class DeConv3d(Module):
  643. """Simplified version of :class:`Deconv3dLayer`.
  644. Parameters
  645. ---AppData\Local\Continuum\anaconda3\envs\ms_tf\lib\site-packages\mindspore\common\api.py", line 412, in compile
  646. result = self._executor.compile(obj, args_list, phase, use_vm)
  647. RuntimeError: Unable to cast from non-held to held instance (T& to Holder<T>) of type 'std:-------
  648. n_filter : int
  649. The number of filters.
  650. filter_size : tuple of int
  651. The filter size (depth, height, width).
  652. output_shape:
  653. A 1-D Tensor representing the output shape of the deconvolution op.
  654. strides : tuple of int
  655. The sliding window strides of corresponding input dimensions.
  656. It must be in the same order as the ``shape`` parameter.
  657. dilation_rate : tuple of int
  658. Specifying the dilation rate to use for dilated convolution.
  659. act : activation function
  660. The activation function of this layer.
  661. padding : str
  662. The padding algorithm type: "SAME" or "VALID".
  663. data_format : str
  664. "channels_last" (NDHWC, default) or "channels_first" (NCDHW).
  665. W_init : initializer
  666. The initializer for the the weight matrix.
  667. b_init : initializer or None
  668. The initializer for the the bias vector. If None, skip biases.
  669. in_channels : int
  670. The number of in channels.
  671. name : None or str
  672. A unique layer name.
  673. Examples
  674. --------
  675. With TensorLayer
  676. >>> net = tl.layers.Input([8, 20, 20, 20, 3], name='input')
  677. >>> 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')
  678. >>> print(deconv3d)
  679. >>> tensor = tl.layers.DeConv3d(n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), act=tl.ReLU, name='deconv3d_2')(net)
  680. >>> print(tensor)
  681. """
  682. def __init__(
  683. self,
  684. n_filter=32,
  685. filter_size=(3, 3, 3),
  686. strides=(1, 1, 1),
  687. act=None,
  688. padding='SAME',
  689. data_format='channels_last',
  690. dilation_rate=(1, 1, 1),
  691. W_init=tl.initializers.truncated_normal(stddev=0.02),
  692. b_init=tl.initializers.constant(value=0.0),
  693. in_channels=None,
  694. name=None # 'deconv3d',
  695. ):
  696. super(DeConv3d, self).__init__(name, act=act)
  697. self.n_filter = n_filter
  698. self.filter_size = filter_size
  699. self.strides = strides
  700. self.padding = padding
  701. self.data_format = data_format
  702. self.dilation_rate = dilation_rate
  703. self.W_init = W_init
  704. self.b_init = b_init
  705. self.in_channels = in_channels
  706. if self.in_channels:
  707. self.build(None)
  708. self._built = True
  709. logging.info(
  710. "DeConv3d %s: n_filter: %d filter_size: %s strides: %s pad: %s act: %s" % (
  711. self.name, n_filter, str(filter_size), str(strides), padding,
  712. self.act.__class__.__name__ if self.act is not None else 'No Activation'
  713. )
  714. )
  715. def __repr__(self):
  716. actstr = self.act.__class__.__name__ if self.act is not None else 'No Activation'
  717. s = (
  718. '{classname}(in_channels={in_channels}, out_channels={n_filter}, kernel_size={filter_size}'
  719. ', strides={strides}, padding={padding}'
  720. )
  721. if self.dilation_rate != (1, ) * len(self.dilation_rate):
  722. s += ', dilation={dilation_rate}'
  723. if self.b_init is None:
  724. s += ', bias=False'
  725. s += (', ' + actstr)
  726. if self.name is not None:
  727. s += ', name=\'{name}\''
  728. s += ')'
  729. return s.format(classname=self.__class__.__name__, **self.__dict__)
  730. def build(self, inputs_shape):
  731. if self.data_format == 'channels_last':
  732. self.data_format = 'NDHWC'
  733. if self.in_channels is None:
  734. self.in_channels = inputs_shape[-1]
  735. elif self.data_format == 'channels_first':
  736. self.data_format = 'NCDHW'
  737. if self.in_channels is None:
  738. self.in_channels = inputs_shape[1]
  739. else:
  740. raise Exception("data_format should be either channels_last or channels_first")
  741. self.filter_shape = (
  742. self.filter_size[0], self.filter_size[1], self.filter_size[2], self.n_filter, self.in_channels
  743. )
  744. self.W = self._get_weights("filters", shape=self.filter_shape, init=self.W_init, transposed=True)
  745. if self.b_init:
  746. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  747. self.b_init_flag = False
  748. if self.b_init:
  749. self.b = self._get_weights("biases", shape=(self.n_filter, ), init=self.b_init)
  750. self.bias_add = tl.ops.BiasAdd(self.data_format)
  751. self.b_init_flag = True
  752. self.conv3d_transpose = tl.ops.Conv3d_transpose(
  753. strides=self.strides, padding=self.padding, data_format=self.data_format, dilations=self.dilation_rate,
  754. out_channel=self.n_filter, k_size=(self.filter_size[0], self.filter_size[1], self.filter_size[2]),
  755. in_channels=self.in_channels
  756. )
  757. self.act_init_flag = False
  758. if self.act:
  759. self.act_init_flag = True
  760. def forward(self, inputs):
  761. if self._forward_state == False:
  762. if self._built == False:
  763. self.build(tl.get_tensor_shape(inputs))
  764. self._built = True
  765. self._forward_state = True
  766. outputs = self.conv3d_transpose(inputs, self.W)
  767. if self.b_init_flag:
  768. outputs = self.bias_add(outputs, self.b)
  769. if self.act_init_flag:
  770. outputs = self.act(outputs)
  771. return outputs

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