@@ -14,35 +14,6 @@ namespace NeuralColor
1414 public static partial class Layers
1515 {
1616
17- private static Tensor [ ] DilateFilters ( Tensor [ ] T , int dilation = 1 )
18- {
19- if ( dilation == 1 )
20- {
21- return T ;
22- }
23- else
24- {
25- var Result = new Tensor [ T . Length ] ;
26- Parallel . For ( 0 , T . Length , ( int i ) =>
27- {
28- Result [ i ] = new Tensor ( T [ i ] . Width * dilation + dilation - 1 , T [ i ] . Height * dilation + dilation - 1 , T [ i ] . Depth ) ;
29- var F = Result [ i ] ;
30- var f = T [ i ] ;
31- for ( int y = 0 ; y < f . Height ; y ++ )
32- {
33- for ( int x = 0 ; x < f . Width ; x ++ )
34- {
35- for ( int d = 0 ; d < f . Depth ; d ++ )
36- {
37- F . Set ( x * dilation + dilation - 1 , y * dilation + dilation - 1 , d , f . Get ( x , y , d ) ) ;
38- }
39- }
40- }
41- } ) ;
42- return Result ;
43- }
44- }
45-
4617 ///<summary>Реализует слой двумерной свёртки.</summary>
4718 ///<param name="input">Входной тензор.</param>
4819 ///<param name="Filters">Фильтры.</param>
@@ -51,25 +22,24 @@ private static Tensor[] DilateFilters(Tensor[] T, int dilation = 1)
5122 ///<param name="dilation">Разрежение.</param>
5223 public static Tensor Conv2D ( Tensor input , Tensor [ ] Filters , Tensor Biases , int stride = 1 , int dilation = 1 )
5324 {
54- Filters = DilateFilters ( Filters , dilation ) ;
5525 var OutputDepth = Filters . Length ;
5626 var Result = new Tensor ( input . Width / stride , input . Height / stride , OutputDepth ) ;
5727 Parallel . For ( 0 , OutputDepth , ( int d ) =>
5828 {
5929 var f = Filters [ d ] ;
6030 for ( int ay = 0 ; ay < Result . Height ; ay ++ )
6131 {
62- var y = ay * stride - f . Height / 2 ;
32+ var y = ay * stride - ( f . Height * dilation + dilation - 1 ) / 2 ;
6333 for ( int ax = 0 ; ax < Result . Width ; ax ++ )
6434 {
65- var x = ax * stride - f . Width / 2 ;
35+ var x = ax * stride - ( f . Width * dilation + dilation - 1 ) / 2 ;
6636 var a = 0.0f ;
6737 for ( int fy = 0 ; fy < f . Height ; fy ++ )
6838 {
69- var oy = y + fy ;
39+ var oy = y + fy * dilation + dilation - 1 ;
7040 for ( int fx = 0 ; fx < f . Width ; fx ++ )
7141 {
72- var ox = x + fx ;
42+ var ox = x + fx * dilation + dilation - 1 ;
7343 if ( ( oy >= 0 ) && ( oy < input . Height ) && ( ox >= 0 ) && ( ox < input . Width ) )
7444 {
7545 var fi = ( ( f . Width * fy ) + fx ) * f . Depth ;
0 commit comments