-   Notifications  You must be signed in to change notification settings 
- Fork 5.9k
Implement a bilinear initializer for transposed convolution to do upsampling. #11404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
   Merged  
     Merged  
 Changes from all commits
 Commits 
  Show all changes 
  3 commits   Select commit Hold shift + click to select a range 
  File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
There are no files selected for viewing
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -15,11 +15,13 @@ | |
| import framework | ||
| import numpy as np | ||
| import contextlib | ||
| from framework import convert_np_dtype_to_dtype_ | ||
| from core import VarDesc | ||
|  | ||
| __all__ = [ | ||
| 'Constant', 'Uniform', 'Normal', 'Xavier', 'force_init_on_cpu', | ||
| 'Constant', 'Uniform', 'Normal', 'Xavier', 'Bilinear', 'force_init_on_cpu', | ||
| 'init_on_cpu', 'ConstantInitializer', 'UniformInitializer', | ||
| 'NormalInitializer', 'XavierInitializer' | ||
| 'NormalInitializer', 'XavierInitializer', 'BilinearInitializer' | ||
| ] | ||
|  | ||
| _force_init_on_cpu_ = False | ||
|  | @@ -422,6 +424,101 @@ def __call__(self, var, block): | |
| return op | ||
|  | ||
|  | ||
| class BilinearInitializer(Initializer): | ||
| """Implements the bilinear initializer. | ||
|  | ||
| This initializer can be used in transposed convolution operator to | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BilinearInitializer强绑定到“用conv_transpose做upsampling”的场景了? | ||
| act as upsampling. Users can upsample a feature map with shape of | ||
| (B, C, H, W) by any integer factor. The usage is: | ||
|  | ||
| >>> factor = 2 | ||
| >>> w_attr = ParamAttr(learning_rate=0., regularizer=L2Decay(0.), | ||
| >>> initializer=Bilinear()) | ||
| >>> conv_up = fluid.layers.conv2d_transpose( | ||
| >>> input, | ||
| >>> num_filters=C, | ||
| >>> output_size=None, | ||
| >>> filter_size=2 * factor - factor % 2, | ||
| >>> padding=ceil((factor - 1) / 2.), | ||
| >>> stride=factor, | ||
| >>> groups=C, | ||
| >>> param_attr=w_attr, | ||
| >>> bias_attr=False) | ||
|  | ||
|  | ||
| Where, `num_filters=C` and `groups=C` means this is channel-wise tranposed | ||
| convolution. The filter shape will be (C, 1, K, K) where K is `filer_size`, | ||
| This initializer will set a (K, K) interpolation kernel for every channel | ||
| of the filter identically. The resulting shape of the output feature map | ||
| will be (B, C, factor * H, factor * W). Note that the learning rate and the | ||
| weight decay are set to 0 in order to keep coefficient values of bilinear | ||
| interpolation unchanged during training. | ||
| """ | ||
|  | ||
| def __init__(self): | ||
| """Constructor for BilinearInitializer. | ||
| """ | ||
| super(BilinearInitializer, self).__init__() | ||
|  | ||
| def __call__(self, var, block): | ||
| """Add biliear initialization ops for a variable | ||
|  | ||
| Args: | ||
| var (Variable): Variable that needs to be initialized. | ||
| block (Block): The block in which initialization ops should | ||
| be added. | ||
|  | ||
| Returns: | ||
| the initialization op | ||
|  | ||
| Raises: | ||
| ValueError: If type of `var` and `block` is not right. | ||
| If the shape of `var` size is not 4 and | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and -> or ? | ||
| var.shape[2] != var.shape[3]. | ||
| """ | ||
| if not isinstance(var, framework.Variable): | ||
| raise ValueError("var must be framework.Variable.") | ||
|  | ||
| if not isinstance(block, framework.Block): | ||
| raise ValueError("block must be framework.Block.") | ||
|  | ||
| shape = var.shape | ||
| if len(shape) != 4: | ||
| raise ValueError("the length of shape must be 4.") | ||
| if shape[2] != shape[3]: | ||
| raise ValueError("shape[2] must be equal to shape[3].") | ||
|  | ||
| weight = np.zeros(np.prod(var.shape), dtype='float32') | ||
| size = shape[3] | ||
| # factor | ||
| f = np.ceil(size / 2.) | ||
| # center | ||
| c = (2 * f - 1 - f % 2) / (2. * f) | ||
| for i in range(np.prod(shape)): | ||
| x = i % size | ||
| y = (i / size) % size | ||
| weight[i] = (1 - abs(x / f - c)) * (1 - abs(y / f - c)) | ||
| weight = np.reshape(weight, shape) | ||
|  | ||
| if var.dtype == VarDesc.VarType.FP32: | ||
| value_name = "fp32_values" | ||
| values = [float(v) for v in weight.flat] | ||
| else: | ||
| raise ValueError("Unsupported dtype %s", input.dtype) | ||
| if np.prod(shape) > 1024 * 1024: | ||
| raise ValueError("The size of input is too big. ") | ||
| op = block.append_op( | ||
| type='assign_value', | ||
| outputs={'Out': [var]}, | ||
| attrs={ | ||
| 'dtype': var.dtype, | ||
| 'shape': list(shape), | ||
| value_name: values | ||
| }) | ||
| var.op = op | ||
| return op | ||
|  | ||
|  | ||
| # We short the class name, since users will use the initializer with the package | ||
| # name. The sample code: | ||
| # | ||
|  | @@ -436,3 +533,4 @@ def __call__(self, var, block): | |
| Normal = NormalInitializer | ||
| Xavier = XavierInitializer | ||
| MSRA = MSRAInitializer | ||
| Bilinear = BilinearInitializer | ||
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
      Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.    
 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的排版用改成一列么?