|
1 | 1 | import core
|
2 | 2 | import proto.framework_pb2 as framework_pb2
|
3 | 3 | from framework import OpProtoHolder, Variable, Program, Operator
|
4 |
| -from initializer import Constant, Normal, Xavier |
| 4 | +from initializer import Constant, Normal, Xavier, Initializer |
5 | 5 | from paddle.v2.fluid.layer_helper import LayerHelper, unique_name
|
6 | 6 | import re
|
7 | 7 | import cStringIO
|
@@ -1587,6 +1587,97 @@ def array_length(array, main_program=None):
|
1587 | 1587 | return tmp
|
1588 | 1588 |
|
1589 | 1589 |
|
| 1590 | +def conv2d_transpose(input, |
| 1591 | + num_filters, |
| 1592 | + output_size=None, |
| 1593 | + filter_size=None, |
| 1594 | + padding=None, |
| 1595 | + stride=None, |
| 1596 | + param_attr=None, |
| 1597 | + param_initializer=None, |
| 1598 | + main_program=None, |
| 1599 | + startup_program=None): |
| 1600 | + """ |
| 1601 | + The transpose of conv2d layer. |
| 1602 | + |
| 1603 | + This layer is also known as deconvolution layer. |
| 1604 | + |
| 1605 | + Args: |
| 1606 | + input(Variable): The input image with [N, C, H, W] format. |
| 1607 | + num_filters(int): The number of filter. It is as same as the output |
| 1608 | + image channel. |
| 1609 | + output_size(int|tuple|None): The output image size. If output size is a |
| 1610 | + tuple, it must contain two integers, (image_H, image_W). This |
| 1611 | + parameter only works when filter_size is None. |
| 1612 | + filter_size(int|tuple|None): The filter size. If filter_size is a tuple, |
| 1613 | + it must contain two integers, (filter_size_H, filter_size_W). |
| 1614 | + Otherwise, the filter will be a square. None if use output size to |
| 1615 | + calculate filter_size |
| 1616 | + padding(int|tuple): The padding size. If padding is a tuple, it must |
| 1617 | + contain two integers, (padding_H, padding_W). Otherwise, the |
| 1618 | + padding_H = padding_W = padding. |
| 1619 | + stride(int|tuple): The stride size. If stride is a tuple, it must |
| 1620 | + contain two integers, (stride_H, stride_W). Otherwise, the |
| 1621 | + stride_H = stride_W = stride. |
| 1622 | + param_attr: Parameter Attribute. |
| 1623 | + param_initializer(Initializer): Parameter Initializer. Default is Xavier |
| 1624 | + main_program(Program): the main program |
| 1625 | + startup_program(Program): the startup program |
| 1626 | +
|
| 1627 | + Returns: |
| 1628 | + Variable: Output image. |
| 1629 | + """ |
| 1630 | + helper = LayerHelper("conv2d_transpose", **locals()) |
| 1631 | + if not isinstance(input, Variable): |
| 1632 | + raise TypeError("Input of conv2d_transpose must be Variable") |
| 1633 | + input_channel = input.shape[1] |
| 1634 | + |
| 1635 | + op_attr = dict() |
| 1636 | + |
| 1637 | + if isinstance(padding, int): |
| 1638 | + op_attr['paddings'] = [padding, padding] |
| 1639 | + elif padding is not None: |
| 1640 | + op_attr['paddings'] = padding |
| 1641 | + |
| 1642 | + if isinstance(stride, int): |
| 1643 | + op_attr['strides'] = stride |
| 1644 | + elif stride is not None: |
| 1645 | + op_attr['strides'] = stride |
| 1646 | + |
| 1647 | + if filter_size is None: |
| 1648 | + if output_size is None: |
| 1649 | + raise ValueError("output_size must be set when filter_size is None") |
| 1650 | + if isinstance(output_size, int): |
| 1651 | + output_size = [output_size, output_size] |
| 1652 | + |
| 1653 | + padding = op_attr.get('paddings', [0, 0]) |
| 1654 | + stride = op_attr.get('strides', [1, 1]) |
| 1655 | + |
| 1656 | + h_in = input.shape[2] |
| 1657 | + w_in = input.shape[3] |
| 1658 | + filter_size_h = output_size[0] - (h_in - 1) * stride[0] + 2 * padding[0] |
| 1659 | + filter_size_w = output_size[1] - (w_in - 1) * stride[1] + 2 * padding[1] |
| 1660 | + filter_size = [filter_size_h, filter_size_w] |
| 1661 | + elif isinstance(filter_size, int): |
| 1662 | + filter_size = [filter_size, filter_size] |
| 1663 | + |
| 1664 | + filter_shape = [input_channel, num_filters] + filter_size |
| 1665 | + img_filter = helper.create_parameter( |
| 1666 | + dtype=input.dtype, |
| 1667 | + shape=filter_shape, |
| 1668 | + attr=helper.param_attr, |
| 1669 | + initializer=param_initializer) |
| 1670 | + |
| 1671 | + out = helper.create_tmp_variable(dtype=input.dtype) |
| 1672 | + helper.append_op( |
| 1673 | + type='conv2d_transpose', |
| 1674 | + inputs={'Input': [input], |
| 1675 | + 'Filter': [img_filter]}, |
| 1676 | + outputs={'Output': out}, |
| 1677 | + attrs=op_attr) |
| 1678 | + return out |
| 1679 | + |
| 1680 | + |
1590 | 1681 | class ConditionalBlockGuard(BlockGuard):
|
1591 | 1682 | def __init__(self, block):
|
1592 | 1683 | if not isinstance(block, ConditionalBlock):
|
|
0 commit comments