-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathShuffleNetV2.swift
214 lines (193 loc) · 8.72 KB
/
ShuffleNetV2.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2019 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import TensorFlow
// Original V2 paper
// "ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
// Ningning Ma, Xiangyu Zhang, Hai-Tao Zheng, Jian Sun
public struct ChannelShuffle: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector
@noDerivative public var groups: Int
public init(groups: Int = 2) {
self.groups = groups
}
@differentiable
public func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
let batchSize = input.shape[0], height = input.shape[1], width = input.shape[2],
channels = input.shape[3]
let channelsPerGroup: Int = channels / groups
var output = input.reshaped(to: [batchSize, height, width, groups, channelsPerGroup])
output = output.transposed(permutation: [0, 1, 2, 4, 3])
output = output.reshaped(to: [batchSize, height, width, channels])
return output
}
}
public struct InvertedResidual: Layer {
@noDerivative public var includeBranch: Bool = true
@noDerivative public var zeropad: ZeroPadding2D = ZeroPadding2D<Float>(padding: ((1, 1), (1, 1)))
public var branch: Sequential<ZeroPadding2D<Float>, Sequential<DepthwiseConv2D<Float>,
Sequential<BatchNorm<Float>, Sequential<Conv2D<Float>, BatchNorm<Float>>>>>
public var conv1: Conv2D<Float>
public var batchNorm1: BatchNorm<Float>
public var depthwiseConv: DepthwiseConv2D<Float>
public var batchNorm2: BatchNorm<Float>
public var conv2: Conv2D<Float>
public var batchNorm3: BatchNorm<Float>
public init(filters: (Int, Int), stride: Int) {
if stride == 1 {
includeBranch = false
}
let branchChannels = filters.1 / 2
branch = Sequential {
ZeroPadding2D<Float>(padding: ((1, 1), (1, 1)))
DepthwiseConv2D<Float>(
filterShape: (3, 3, filters.0, 1), strides: (stride, stride),
padding: .valid
)
BatchNorm<Float>(featureCount: filters.0)
Conv2D<Float>(
filterShape: (1, 1, filters.0, branchChannels), strides: (1, 1), padding: .valid,
useBias: false
)
BatchNorm<Float>(featureCount: branchChannels)
}
let inputChannels = includeBranch ? filters.0: branchChannels
conv1 = Conv2D<Float>(
filterShape: (1, 1, inputChannels, branchChannels), strides: (1, 1), padding: .valid,
useBias: false
)
conv2 = Conv2D<Float>(
filterShape: (1, 1, branchChannels, branchChannels), strides: (1, 1), padding: .valid,
useBias: false
)
depthwiseConv = DepthwiseConv2D<Float>(
filterShape: (3, 3, branchChannels, 1), strides: (stride, stride), padding: .valid
)
batchNorm1 = BatchNorm<Float>(featureCount: branchChannels)
batchNorm2 = BatchNorm<Float>(featureCount: branchChannels)
batchNorm3 = BatchNorm<Float>(featureCount: branchChannels)
}
@differentiable
public func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
if !includeBranch {
let splitInput = input.split(count: 2, alongAxis: 3)
let input1 = splitInput[0]
let input2 = splitInput[1]
var output2 = relu(input2.sequenced(through: conv1, batchNorm1))
output2 = relu(output2.sequenced(through: zeropad, depthwiseConv, batchNorm2, conv2,
batchNorm3))
return ChannelShuffle()(input1.concatenated(with: output2, alongAxis: 3))
} else {
let output1 = branch(input)
var output2 = relu(input.sequenced(through: conv1, batchNorm1))
output2 = relu(output2.sequenced(through: zeropad, depthwiseConv, batchNorm2, conv2,
batchNorm3))
return ChannelShuffle()(output1.concatenated(with: output2, alongAxis: 3))
}
}
}
public struct ShuffleNetV2: Layer {
@noDerivative public var zeroPad: ZeroPadding2D<Float> = ZeroPadding2D<Float>(padding: ((1, 1), (1, 1)))
public var conv1: Conv2D<Float>
public var batchNorm1: BatchNorm<Float>
public var maxPool: MaxPool2D<Float>
public var invertedResidualBlocksStage1: [InvertedResidual]
public var invertedResidualBlocksStage2: [InvertedResidual]
public var invertedResidualBlocksStage3: [InvertedResidual]
public var conv2: Conv2D<Float>
public var globalPool: GlobalAvgPool2D<Float> = GlobalAvgPool2D()
public var dense: Dense<Float>
public init(stagesRepeat: (Int, Int, Int), stagesOutputChannels: (Int, Int, Int, Int, Int),
classCount: Int) {
var inputChannels = 3
var outputChannels = stagesOutputChannels.0
conv1 = Conv2D<Float>(
filterShape: (3, 3, inputChannels, outputChannels), strides: (1, 1)
)
maxPool = MaxPool2D(poolSize: (3, 3), strides: (2, 2))
conv2 = Conv2D<Float>(
filterShape: (1, 1, stagesOutputChannels.3, stagesOutputChannels.4), strides: (1, 1),
useBias: false
)
dense = Dense<Float>(inputSize: stagesOutputChannels.4, outputSize: classCount)
batchNorm1 = BatchNorm(featureCount: outputChannels)
inputChannels = outputChannels
outputChannels = stagesOutputChannels.1
invertedResidualBlocksStage1 = [InvertedResidual(filters: (inputChannels, outputChannels),
stride: 2)]
for _ in 1...stagesRepeat.0 {
invertedResidualBlocksStage1.append(InvertedResidual(
filters: (outputChannels, outputChannels), stride: 1)
)
}
inputChannels = outputChannels
outputChannels = stagesOutputChannels.2
invertedResidualBlocksStage2 = [InvertedResidual(filters: (inputChannels, outputChannels),
stride: 2)]
for _ in 1...stagesRepeat.1 {
invertedResidualBlocksStage2.append(InvertedResidual(
filters: (outputChannels, outputChannels), stride: 1)
)
}
inputChannels = outputChannels
outputChannels = stagesOutputChannels.3
invertedResidualBlocksStage3 = [InvertedResidual(filters: (inputChannels, outputChannels),
stride: 2)]
for _ in 1...stagesRepeat.2 {
invertedResidualBlocksStage3.append(InvertedResidual(
filters: (outputChannels, outputChannels), stride: 1)
)
}
}
@differentiable
public func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
var output = relu(input.sequenced(through: zeroPad, conv1, batchNorm1, zeroPad, maxPool))
output = invertedResidualBlocksStage1.differentiableReduce(output) {$1($0)}
output = invertedResidualBlocksStage2.differentiableReduce(output) {$1($0)}
output = invertedResidualBlocksStage3.differentiableReduce(output) {$1($0)}
output = relu(conv2(output))
return output.sequenced(through: globalPool, dense)
}
}
extension ShuffleNetV2 {
public enum Kind {
case shuffleNetV2x05
case shuffleNetV2x10
case shuffleNetV2x15
case shuffleNetV2x20
}
public init(kind: Kind) {
switch kind {
case .shuffleNetV2x05:
self.init(
stagesRepeat: (4, 8, 4), stagesOutputChannels: (24, 48, 96, 192, 1024),
classCount: 1000
)
case .shuffleNetV2x10:
self.init(
stagesRepeat: (4, 8, 4), stagesOutputChannels: (24, 116, 232, 464, 1024),
classCount: 1000
)
case .shuffleNetV2x15:
self.init(
stagesRepeat: (4, 8, 4), stagesOutputChannels: (24, 176, 352, 704, 1024),
classCount: 1000
)
case .shuffleNetV2x20:
self.init(
stagesRepeat: (4, 8, 4), stagesOutputChannels: (24, 244, 488, 976, 2048),
classCount: 1000
)
}
}
}