@@ -20,41 +20,46 @@ import LayerInit
20
20
// Karen Simonyan, Andrew Zisserman
21
21
// https://arxiv.org/abs/1409.1556
22
22
23
- public typealias AutoVGGBlock = AutoSequenced < AutoSequencedMany < AutoConv2D < Float > > , AutoMaxPool2D < Float > >
24
- func makeVGGBlock( featureCounts: ( Int , Int , Int , Int ) , blockCount: Int ) -> AutoVGGBlock {
25
- var blocks : [ AutoConv2D < Float > ] = [
26
- AutoConv2D < Float > ( filterShape: ( 3 , 3 ) , outputChannels: featureCounts. 1 ,
27
- padding: . same,
28
- activation: relu) ]
29
- for _ in 1 ..< blockCount {
30
- blocks += [ AutoConv2D ( filterShape: ( 3 , 3 ) , outputChannels: featureCounts. 3 ,
31
- padding: . same,
32
- activation: relu) ]
33
- }
23
+ public struct AutoVGGBlock : AutoModule {
24
+ let featureCounts : ( Int , Int , Int , Int )
25
+ let blockCount : Int
34
26
35
- return AutoSequencedMany ( layers: blocks)
36
- . then ( AutoMaxPool2D ( poolSize: ( 2 , 2 ) , strides: ( 2 , 2 ) ) )
27
+ public typealias LayerType = AutoSequenced < AutoSequencedMany < AutoConv2D < Float > > , AutoMaxPool2D < Float > >
28
+ public lazy var initializeLayer : LayerType = {
29
+ var blocks : [ AutoConv2D < Float > ] = [
30
+ AutoConv2D < Float > ( filterShape: ( 3 , 3 ) , outputChannels: featureCounts. 1 ,
31
+ padding: . same,
32
+ activation: relu) ]
33
+
34
+ for _ in 1 ..< blockCount {
35
+ blocks += [ AutoConv2D ( filterShape: ( 3 , 3 ) , outputChannels: featureCounts. 3 ,
36
+ padding: . same,
37
+ activation: relu) ]
38
+ }
39
+
40
+ return AutoSequencedMany ( layers: blocks)
41
+ . then ( AutoMaxPool2D ( poolSize: ( 2 , 2 ) , strides: ( 2 , 2 ) ) )
42
+ } ( )
37
43
}
38
44
39
- // TODO(shadaj): oh no
40
- public typealias AutoVGG16Backbone = AutoSequenced < AutoSequenced < AutoSequenced < AutoSequenced < AutoVGGBlock , AutoVGGBlock > , AutoVGGBlock > , AutoVGGBlock > , AutoVGGBlock >
41
- public typealias AutoVGG16 = AutoSequenced < AutoSequenced < AutoSequenced < AutoSequenced < AutoVGG16Backbone , AutoFlatten < Float > > , AutoDense < Float > > , AutoDense < Float > > , AutoDense < Float > >
42
-
43
- public func makeVGG16( classCount: Int = 1000 ) -> AutoVGG16 {
44
- let layer1 = makeVGGBlock ( featureCounts: ( 3 , 64 , 64 , 64 ) , blockCount: 2 )
45
- let layer2 = makeVGGBlock ( featureCounts: ( 64 , 128 , 128 , 128 ) , blockCount: 2 )
46
- let layer3 = makeVGGBlock ( featureCounts: ( 128 , 256 , 256 , 256 ) , blockCount: 3 )
47
- let layer4 = makeVGGBlock ( featureCounts: ( 256 , 512 , 512 , 512 ) , blockCount: 3 )
48
- let layer5 = makeVGGBlock ( featureCounts: ( 512 , 512 , 512 , 512 ) , blockCount: 3 )
49
-
50
- let flatten = AutoFlatten < Float > ( )
51
- let dense1 = AutoDense < Float > ( outputSize: 4096 , activation: relu)
52
- let dense2 = AutoDense < Float > ( outputSize: 4096 , activation: relu)
53
- let output = AutoDense < Float > ( outputSize: classCount)
54
-
55
- let backbone = layer1. then ( layer2) . then ( layer3) . then ( layer4) . then ( layer5)
56
- let fullModel = backbone. then ( flatten) . then ( dense1) . then ( dense2) . then ( output)
57
- return fullModel
45
+ public struct AutoVGG16 : AutoModule {
46
+ let classCount : Int
47
+
48
+ public init ( classCount: Int = 1000 ) {
49
+ self . classCount = classCount
50
+ }
51
+
52
+ public lazy var initializeLayer = {
53
+ return AutoVGGBlock ( featureCounts: ( 3 , 64 , 64 , 64 ) , blockCount: 2 )
54
+ . then ( AutoVGGBlock ( featureCounts: ( 64 , 128 , 128 , 128 ) , blockCount: 2 ) )
55
+ . then ( AutoVGGBlock ( featureCounts: ( 128 , 256 , 256 , 256 ) , blockCount: 3 ) )
56
+ . then ( AutoVGGBlock ( featureCounts: ( 256 , 512 , 512 , 512 ) , blockCount: 3 ) )
57
+ . then ( AutoVGGBlock ( featureCounts: ( 512 , 512 , 512 , 512 ) , blockCount: 3 ) )
58
+ . then ( AutoFlatten ( ) )
59
+ . then ( AutoDense ( outputSize: 4096 , activation: relu) )
60
+ . then ( AutoDense ( outputSize: 4096 , activation: relu) )
61
+ . then ( AutoDense ( outputSize: classCount) )
62
+ } ( )
58
63
}
59
64
60
65
public struct VGGBlock : Layer {
0 commit comments