-
Notifications
You must be signed in to change notification settings - Fork 148
/
Backbone.swift
192 lines (180 loc) · 7.56 KB
/
Backbone.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
// Copyright 2020 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 Checkpoints
import TensorFlow
public struct DepthwiseSeparableConvBlock: Layer {
var dConv: DepthwiseConv2D<Float>
var conv: Conv2D<Float>
public init(
depthWiseFilter: Tensor<Float>,
depthWiseBias: Tensor<Float>,
pointWiseFilter: Tensor<Float>,
pointWiseBias: Tensor<Float>,
strides: (Int, Int)
) {
dConv = DepthwiseConv2D<Float>(
filter: depthWiseFilter,
bias: depthWiseBias,
activation: relu6,
strides: strides,
padding: .same
)
conv = Conv2D<Float>(
filter: pointWiseFilter,
bias: pointWiseBias,
activation: relu6,
padding: .same
)
}
@differentiable
public func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
return input.sequenced(through: dConv, conv)
}
}
public struct MobileNetLikeBackbone: Layer {
@noDerivative let ckpt: CheckpointReader
public var convBlock0: Conv2D<Float>
public var dConvBlock1: DepthwiseSeparableConvBlock
public var dConvBlock2: DepthwiseSeparableConvBlock
public var dConvBlock3: DepthwiseSeparableConvBlock
public var dConvBlock4: DepthwiseSeparableConvBlock
public var dConvBlock5: DepthwiseSeparableConvBlock
public var dConvBlock6: DepthwiseSeparableConvBlock
public var dConvBlock7: DepthwiseSeparableConvBlock
public var dConvBlock8: DepthwiseSeparableConvBlock
public var dConvBlock9: DepthwiseSeparableConvBlock
public var dConvBlock10: DepthwiseSeparableConvBlock
public var dConvBlock11: DepthwiseSeparableConvBlock
public var dConvBlock12: DepthwiseSeparableConvBlock
public var dConvBlock13: DepthwiseSeparableConvBlock
public init(checkpoint: CheckpointReader) {
self.ckpt = checkpoint
self.convBlock0 = Conv2D<Float>(
filter: ckpt.load(from: "Conv2d_0/weights"),
bias: ckpt.load(from: "Conv2d_0/biases"),
activation: relu6,
strides: (2, 2),
padding: .same
)
self.dConvBlock1 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_1_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_1_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_1_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_1_pointwise/biases"),
strides: (1, 1)
)
self.dConvBlock2 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_2_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_2_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_2_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_2_pointwise/biases"),
strides: (2, 2)
)
self.dConvBlock3 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_3_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_3_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_3_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_3_pointwise/biases"),
strides: (1, 1)
)
self.dConvBlock4 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_4_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_4_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_4_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_4_pointwise/biases"),
strides: (2, 2)
)
self.dConvBlock5 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_5_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_5_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_5_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_5_pointwise/biases"),
strides: (1, 1)
)
self.dConvBlock6 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_6_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_6_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_6_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_6_pointwise/biases"),
strides: (2, 2)
)
self.dConvBlock7 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_7_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_7_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_7_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_7_pointwise/biases"),
strides: (1, 1)
)
self.dConvBlock8 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_8_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_8_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_8_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_8_pointwise/biases"),
strides: (1, 1)
)
self.dConvBlock9 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_9_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_9_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_9_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_9_pointwise/biases"),
strides: (1, 1)
)
self.dConvBlock10 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_10_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_10_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_10_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_10_pointwise/biases"),
strides: (1, 1)
)
self.dConvBlock11 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_11_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_11_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_11_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_11_pointwise/biases"),
strides: (1, 1)
)
self.dConvBlock12 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_12_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_12_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_12_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_12_pointwise/biases"),
strides: (1, 1)
)
self.dConvBlock13 = DepthwiseSeparableConvBlock(
depthWiseFilter: ckpt.load(from: "Conv2d_13_depthwise/depthwise_weights"),
depthWiseBias: ckpt.load(from: "Conv2d_13_depthwise/biases"),
pointWiseFilter: ckpt.load(from: "Conv2d_13_pointwise/weights"),
pointWiseBias: ckpt.load(from: "Conv2d_13_pointwise/biases"),
strides: (1, 1)
)
}
@differentiable
public func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
var x = convBlock0(input)
x = dConvBlock1(x)
x = dConvBlock2(x)
x = dConvBlock3(x)
x = dConvBlock4(x)
x = dConvBlock5(x)
x = dConvBlock6(x)
x = dConvBlock7(x)
x = dConvBlock8(x)
x = dConvBlock9(x)
x = dConvBlock10(x)
x = dConvBlock11(x)
x = dConvBlock12(x)
x = dConvBlock13(x)
return x
}
}