1
1
using MxNet . Initializers ;
2
+ using MxNet . Sym . Numpy ;
2
3
using System ;
3
4
using System . Collections . Generic ;
5
+ using System . Diagnostics ;
6
+ using System . Linq ;
4
7
using System . Text ;
5
8
6
9
namespace MxNet . Gluon . NN
7
10
{
8
11
public class DeformableConvolution : HybridBlock
9
12
{
13
+ public int _channels ;
14
+
15
+ public int _in_channels ;
16
+
17
+ public Activation act ;
18
+
19
+ public Parameter deformable_conv_bias ;
20
+
21
+ public Parameter deformable_conv_weight ;
22
+
23
+ public Parameter offset_bias ;
24
+
25
+ public Parameter offset_weight ;
26
+
27
+ public int offset_channels ;
28
+
29
+ public int [ ] kernel ;
30
+
31
+ public int [ ] strides ;
32
+
33
+ public int [ ] padding ;
34
+
35
+ public int [ ] dilation ;
36
+
37
+ public int num_filter ;
38
+
39
+ public int num_group ;
40
+
41
+ public bool no_bias ;
42
+
43
+ public string layout ;
44
+
45
+ public int num_deformable_group ;
46
+
47
+ public int [ ] adj ;
48
+
10
49
public DeformableConvolution ( int channels , ( int , int ) ? kernel_size = null , ( int , int ) ? strides = null , ( int , int ) ? padding = null , ( int , int ) ? dilation = null ,
11
50
int groups = 1 , int num_deformable_group = 1 , string layout = "NCHW" , bool use_bias = true , int in_channels = 0 , ActivationType ? activation = null ,
12
- Initializer weight_initializer = null , string bias_initializer = "zeros" , bool offset_use_bias = true , int [ ] adj = null ,
13
- string op_name = "DeformableConvolution" )
51
+ Initializer weight_initializer = null , string bias_initializer = "zeros" , string offset_weight_initializer = "zeros" ,
52
+ string offset_bias_initializer = "zeros" , bool offset_use_bias = true , int [ ] adj = null , string op_name = "DeformableConvolution" )
14
53
{
15
- throw new NotImplementedException ( ) ;
54
+ this . _channels = channels ;
55
+ this . _in_channels = in_channels ;
56
+ Debug . Assert ( new string [ ] { "NCHW" , "NHWC" } . Contains ( layout ) , "Only supports 'NCHW' and 'NHWC' layout for now" ) ;
57
+ var offset_channels = 2 * kernel_size . Value . Item1 * kernel_size . Value . Item2 * num_deformable_group ;
58
+
59
+ this . kernel = kernel_size . HasValue ? new int [ ] { kernel_size . Value . Item1 , kernel_size . Value . Item2 } : new int [ ] { 1 , 1 } ;
60
+ this . strides = strides . HasValue ? new int [ ] { strides . Value . Item1 , strides . Value . Item2 } : new int [ ] { 1 , 1 } ;
61
+ this . padding = padding . HasValue ? new int [ ] { padding . Value . Item1 , padding . Value . Item2 } : new int [ ] { 0 , 0 } ;
62
+ this . dilation = dilation . HasValue ? new int [ ] { dilation . Value . Item1 , dilation . Value . Item2 } : new int [ ] { 0 , 0 } ;
63
+ this . num_filter = offset_channels ;
64
+ this . num_group = groups ;
65
+ this . no_bias = ! offset_use_bias ;
66
+ this . layout = layout ;
67
+ this . num_deformable_group = num_deformable_group ;
68
+ this . adj = adj ;
69
+ var dshape = new int [ kernel . Length + 2 ] ;
70
+ dshape [ layout . IndexOf ( 'N' ) ] = 1 ;
71
+ dshape [ layout . IndexOf ( 'C' ) ] = in_channels ;
72
+
73
+
74
+ var offsetshapes = _infer_weight_shape ( "convolution" , new Shape ( dshape ) ) ;
75
+ this . offset_weight = new Parameter ( "offset_weight" , shape : offsetshapes [ 1 ] , init : Initializer . Get ( offset_weight_initializer ) , allow_deferred_init : true ) ;
76
+ if ( offset_use_bias )
77
+ {
78
+ this . offset_bias = new Parameter ( "offset_bias" , shape : offsetshapes [ 2 ] , init : Initializer . Get ( offset_bias_initializer ) , allow_deferred_init : true ) ;
79
+ }
80
+ else
81
+ {
82
+ this . offset_bias = null ;
83
+ }
84
+ var deformable_conv_weight_shape = new int [ kernel . Length + 2 ] ;
85
+ deformable_conv_weight_shape [ 0 ] = channels ;
86
+ deformable_conv_weight_shape [ 2 ] = kernel [ 0 ] ;
87
+ deformable_conv_weight_shape [ 3 ] = kernel [ 1 ] ;
88
+ this . deformable_conv_weight = new Parameter ( "deformable_conv_weight" , shape : new Shape ( deformable_conv_weight_shape ) , init : weight_initializer , allow_deferred_init : true ) ;
89
+ if ( use_bias )
90
+ {
91
+ this . deformable_conv_bias = new Parameter ( "deformable_conv_bias" , shape : new Shape ( channels ) , init : bias_initializer , allow_deferred_init : true ) ;
92
+ }
93
+ else
94
+ {
95
+ this . deformable_conv_bias = null ;
96
+ }
97
+
98
+ if ( activation . HasValue )
99
+ {
100
+ this . act = new Activation ( activation . Value ) ;
101
+ }
102
+ else
103
+ {
104
+ this . act = null ;
105
+ }
106
+
107
+ this [ "deformable_conv_bias" ] = deformable_conv_bias ;
108
+ this [ "deformable_conv_weight" ] = deformable_conv_weight ;
109
+ this [ "offset_bias" ] = offset_bias ;
110
+ this [ "offset_weight" ] = offset_weight ;
16
111
}
17
112
18
113
public override string Alias ( )
@@ -22,7 +117,46 @@ public override string Alias()
22
117
23
118
public override NDArrayOrSymbol HybridForward ( NDArrayOrSymbol x , params NDArrayOrSymbol [ ] args )
24
119
{
25
- return base . HybridForward ( x , args ) ;
120
+ //object act;
121
+ //object offset;
122
+ //if (offset_bias == null)
123
+ //{
124
+ // offset = F.convolution(x, offset_weight, kernel: kernel, stride: strides, cudnn_off: true, this._kwargs_offset);
125
+ //}
126
+ //else
127
+ //{
128
+ // offset = F.convolution(x, offset_weight, offset_bias, cudnn_off: true, this._kwargs_offset);
129
+ //}
130
+ //if (deformable_conv_bias == null)
131
+ //{
132
+ // act = F.npx.deformable_convolution(data: x, offset: offset, weight: deformable_conv_weight, name: "fwd", this._kwargs_deformable_conv);
133
+ //}
134
+ //else
135
+ //{
136
+ // act = F.npx.deformable_convolution(data: x, offset: offset, weight: deformable_conv_weight, bias: deformable_conv_bias, name: "fwd", this._kwargs_deformable_conv);
137
+ //}
138
+
139
+ //if (this.act)
140
+ //{
141
+ // using (var np_array(true))
142
+ // {
143
+ // act = this.act(act);
144
+ // }
145
+ //}
146
+
147
+ //return is_np_array() ? act : act.as_nd_ndarray();
148
+
149
+ throw new NotImplementedException ( ) ;
150
+ }
151
+
152
+ internal Shape [ ] _infer_weight_shape ( string op_name , Shape data_shape )
153
+ {
154
+ var conv = sym . Convolution ( _Symbol . Var ( "data" , shape : data_shape ) , null , kernel : new Shape ( kernel ) ,
155
+ num_filter : num_filter ,
156
+ stride : new Shape ( strides ) , dilate : new Shape ( dilation ) , pad : new Shape ( padding ) , no_bias : no_bias ,
157
+ num_group : num_group , bias : null ) ;
158
+
159
+ return conv . InferShapePartial ( new Dictionary < string , Shape > ( ) ) . Item1 ;
26
160
}
27
161
}
28
162
}
0 commit comments