This repository was archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathVGG16.swift
167 lines (137 loc) · 7.16 KB
/
VGG16.swift
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import XCTest
import Serrano
/**
This code shows how to construct a VGG16 network using graph's low-level API.
[vgg16](http://book.paddlepaddle.org/03.image_classification/image/vgg16.png)
*/
func configureVGG16() -> InferenceGraph {
let g = InferenceGraph()
// input [244, 244, 3]
let shape = TensorShape(dataType: .float, shape: [244, 244, 3])
let input = g.tensor(shape: shape)
// block 1
let convOp = ConvOperator2D(numFilters: 64,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: input.shape)
let (out, _, _) = g.operation(inputs: [input], op: convOp)
let convOp1 = ConvOperator2D(numFilters: 64,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out.first!.shape)
let (out1, _, _) = g.operation(inputs: out, op: convOp1)
let poo1 = MaxPool2DOperator(kernelSize: [2, 2],
channelPosition: TensorChannelOrder.Last,
paddingMode: PaddingMode.Valid)
let (out_block_1, _, _) = g.operation(inputs: out1, op: poo1)
// block 2
let convOp2 = ConvOperator2D(numFilters: 128,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out_block_1.first!.shape)
let (out2, _, _) = g.operation(inputs: out_block_1, op: convOp2)
let convOp3 = ConvOperator2D(numFilters: 128,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out2.first!.shape)
let (out3, _, _) = g.operation(inputs: out2, op: convOp3)
let poo2 = MaxPool2DOperator(kernelSize: [2, 2],
channelPosition: TensorChannelOrder.Last,
paddingMode: PaddingMode.Valid)
let (out_block_2, _, _) = g.operation(inputs: out3, op: poo2)
// block 3
let convOp4 = ConvOperator2D(numFilters: 256,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out_block_2.first!.shape)
let (out4, _, _) = g.operation(inputs: out_block_2, op: convOp4)
let convOp5 = ConvOperator2D(numFilters: 256,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out4.first!.shape)
let (out5, _, _) = g.operation(inputs: out4, op: convOp5)
let convOp6 = ConvOperator2D(numFilters: 256,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out5.first!.shape)
let (out6, _, _) = g.operation(inputs: out5, op: convOp6)
let poo3 = MaxPool2DOperator(kernelSize: [2, 2],
channelPosition: TensorChannelOrder.Last,
paddingMode: PaddingMode.Valid)
let (out_block_3, _, _) = g.operation(inputs: out6, op: poo3)
// bloack 4
let convOp7 = ConvOperator2D(numFilters: 512,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out_block_3.first!.shape)
let (out7, _, _) = g.operation(inputs: out_block_3, op: convOp7)
let convOp8 = ConvOperator2D(numFilters: 512,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out7.first!.shape)
let (out8, _, _) = g.operation(inputs: out7, op: convOp8)
let convOp9 = ConvOperator2D(numFilters: 512,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out8.first!.shape)
let (out9, _, _) = g.operation(inputs: out8, op: convOp9)
let poo4 = MaxPool2DOperator(kernelSize: [2, 2],
channelPosition: TensorChannelOrder.Last,
paddingMode: PaddingMode.Valid)
let (out_block_4, _, _) = g.operation(inputs: out9, op: poo4)
// block 5
let convOp10 = ConvOperator2D(numFilters: 512,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out_block_4.first!.shape)
let (out10, _, _) = g.operation(inputs: out_block_4, op: convOp10)
let convOp11 = ConvOperator2D(numFilters: 512,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out10.first!.shape)
let (out11, _, _) = g.operation(inputs: out10, op: convOp11)
let convOp12 = ConvOperator2D(numFilters: 512,
kernelSize: [3, 3],
padMode: PaddingMode.Same,
channelPosition: TensorChannelOrder.Last,
inputShape: out11.first!.shape)
let (out12, _, _) = g.operation(inputs: out11, op: convOp12)
let poo5 = MaxPool2DOperator(kernelSize: [2, 2],
channelPosition: TensorChannelOrder.Last,
paddingMode: PaddingMode.Valid)
let (out_block_5, _, _) = g.operation(inputs: out12, op: poo5)
// block 6
let fc13 = FullyconnectedOperator(inputDim: 25088, numUnits: 4096)
let (out13, _, _) = g.operation(inputs: out_block_5, op: fc13)
let fc14 = FullyconnectedOperator(inputDim: 4096, numUnits: 4096)
let (out14, _, _) = g.operation(inputs: out13, op: fc14)
let fc15 = FullyconnectedOperator(inputDim: 4096, numUnits: 4096)
let (out15, _, _) = g.operation(inputs: out14, op: fc15)
return g
}
class Example_VGG16: XCTestCase {
// Test vgg16 network forward
// Suggestion: run this function on macOS or real iOS devices supporting GPU. It goona be very slow on CPU mode.
func testVGG16Forawad() {
SerranoLogging.release = true
let _ = SerranoEngine.configuredEngine.configureEngine(computationMode: .GPU)
let vgg16 = configureVGG16()
vgg16.forwardPrepare()
let start = CFAbsoluteTimeGetCurrent()
// vgg16.forward(mode: .CPU)
vgg16.forward(mode: .GPU)
print("Forward Execution Time : \((CFAbsoluteTimeGetCurrent() - start) * 100) ms")
}
}