-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.py
102 lines (91 loc) · 3.12 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#-*- coding:utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import paddle
import paddle.fluid as fluid
import contextlib
def conv_layer(input, num_filters, filter_size, stride=1,
dilation=1,groups=1,act='relu'):
conv = fluid.layers.conv2d(
input=input,
num_filters=num_filters,
filter_size=filter_size,
stride=stride,
padding=dilation*(int((filter_size - 1) / 2)), #1
dilation=dilation,
groups=groups,
act=act,
param_attr=fluid.initializer.Xavier(uniform=False),
bias_attr=False)
return conv
def conv_bn_layer(input, num_filters, filter_size, stride=1,dilation=1,
groups=1,act='relu'):
conv = fluid.layers.conv2d(
input=input,
num_filters=num_filters,
filter_size=filter_size,
stride=stride,
padding=dilation*(int((filter_size - 1) / 2)),
dilation=dilation,
groups=groups,
act=act,
param_attr=fluid.initializer.Xavier(uniform=False),
bias_attr=False)
# param_attr=fluid.initializer.Normal(loc=0.0, scale=2.0),
outconv = fluid.layers.batch_norm(input=conv)
return outconv
def deconv_layer(input, num_filters, filter_size=4, stride=2,
act='relu'):
deconv = fluid.layers.conv2d_transpose(
input=input,
num_filters=num_filters,
filter_size=filter_size,
stride=stride,
padding=1,
act=act,
param_attr=fluid.initializer.Xavier(uniform=False),
bias_attr=False)
return deconv
def deconv_bn_layer(input, num_filters, filter_size=4, stride=2,
act='relu'):
deconv = fluid.layers.conv2d_transpose(
input=input,
num_filters=num_filters,
filter_size=filter_size,
stride=stride,
padding=1,
act=act,
param_attr=fluid.initializer.Xavier(uniform=False),
bias_attr=False)
outconv = fluid.layers.batch_norm(input=deconv)
return outconv
def depthwise_layer(input, filter_size=3, stride=1, padding = 1, dilation=1,
act='relu'):
depthwise = fluid.layers.conv2d(
input=input,
num_filters=input.shape[1],
filter_size=filter_size,
stride=stride,
padding=dilation*(int((filter_size - 1) / 2)),
dilation=dilation,
groups=input.shape[1],
act=act,
param_attr=fluid.initializer.Xavier(uniform=False),
bias_attr=False)
return depthwise
def depthwise_bn_layer(input, filter_size=3, stride=1, padding = 1, dilation=1,
act='relu'):
depthwise = fluid.layers.conv2d(
input=input,
num_filters=input.shape[1],
filter_size=filter_size,
stride=stride,
padding=dilation*(int((filter_size - 1) / 2)),
dilation=dilation,
groups=input.shape[1], #implement depthwise by set groups
act=act,
param_attr=fluid.initializer.Xavier(uniform=False),
bias_attr=False)
outconv = fluid.layers.batch_norm(input=depthwise)
return outconv