|
| 1 | +// Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Checkpoints |
| 16 | +import TensorFlow |
| 17 | + |
| 18 | +public struct DepthwiseSeparableConvBlock: Layer { |
| 19 | + var dConv: DepthwiseConv2D<Float> |
| 20 | + var conv: Conv2D<Float> |
| 21 | + |
| 22 | + public init( |
| 23 | + depthWiseFilter: Tensor<Float>, |
| 24 | + depthWiseBias: Tensor<Float>, |
| 25 | + pointWiseFilter: Tensor<Float>, |
| 26 | + pointWiseBias: Tensor<Float>, |
| 27 | + strides: (Int, Int) |
| 28 | + ) { |
| 29 | + |
| 30 | + dConv = DepthwiseConv2D<Float>( |
| 31 | + filter: depthWiseFilter, |
| 32 | + bias: depthWiseBias, |
| 33 | + activation: relu6, |
| 34 | + strides: strides, |
| 35 | + padding: .same |
| 36 | + ) |
| 37 | + |
| 38 | + conv = Conv2D<Float>( |
| 39 | + filter: pointWiseFilter, |
| 40 | + bias: pointWiseBias, |
| 41 | + activation: relu6, |
| 42 | + padding: .same |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + @differentiable |
| 47 | + public func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> { |
| 48 | + return input.sequenced(through: dConv, conv) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +public struct MobileNetLikeBackbone: Layer { |
| 53 | + @noDerivative let ckpt: CheckpointReader |
| 54 | + |
| 55 | + public var convBlock0: Conv2D<Float> |
| 56 | + public var dConvBlock1: DepthwiseSeparableConvBlock |
| 57 | + public var dConvBlock2: DepthwiseSeparableConvBlock |
| 58 | + public var dConvBlock3: DepthwiseSeparableConvBlock |
| 59 | + public var dConvBlock4: DepthwiseSeparableConvBlock |
| 60 | + public var dConvBlock5: DepthwiseSeparableConvBlock |
| 61 | + public var dConvBlock6: DepthwiseSeparableConvBlock |
| 62 | + public var dConvBlock7: DepthwiseSeparableConvBlock |
| 63 | + public var dConvBlock8: DepthwiseSeparableConvBlock |
| 64 | + public var dConvBlock9: DepthwiseSeparableConvBlock |
| 65 | + public var dConvBlock10: DepthwiseSeparableConvBlock |
| 66 | + public var dConvBlock11: DepthwiseSeparableConvBlock |
| 67 | + public var dConvBlock12: DepthwiseSeparableConvBlock |
| 68 | + public var dConvBlock13: DepthwiseSeparableConvBlock |
| 69 | + |
| 70 | + public init(checkpoint: CheckpointReader) { |
| 71 | + self.ckpt = checkpoint |
| 72 | + |
| 73 | + self.convBlock0 = Conv2D<Float>( |
| 74 | + filter: ckpt.load(from: "Conv2d_0/weights"), |
| 75 | + bias: ckpt.load(from: "Conv2d_0/biases"), |
| 76 | + activation: relu6, |
| 77 | + strides: (2, 2), |
| 78 | + padding: .same |
| 79 | + ) |
| 80 | + self.dConvBlock1 = DepthwiseSeparableConvBlock( |
| 81 | + depthWiseFilter: ckpt.load(from: "Conv2d_1_depthwise/depthwise_weights"), |
| 82 | + depthWiseBias: ckpt.load(from: "Conv2d_1_depthwise/biases"), |
| 83 | + pointWiseFilter: ckpt.load(from: "Conv2d_1_pointwise/weights"), |
| 84 | + pointWiseBias: ckpt.load(from: "Conv2d_1_pointwise/biases"), |
| 85 | + strides: (1, 1) |
| 86 | + ) |
| 87 | + self.dConvBlock2 = DepthwiseSeparableConvBlock( |
| 88 | + depthWiseFilter: ckpt.load(from: "Conv2d_2_depthwise/depthwise_weights"), |
| 89 | + depthWiseBias: ckpt.load(from: "Conv2d_2_depthwise/biases"), |
| 90 | + pointWiseFilter: ckpt.load(from: "Conv2d_2_pointwise/weights"), |
| 91 | + pointWiseBias: ckpt.load(from: "Conv2d_2_pointwise/biases"), |
| 92 | + strides: (2, 2) |
| 93 | + ) |
| 94 | + self.dConvBlock3 = DepthwiseSeparableConvBlock( |
| 95 | + depthWiseFilter: ckpt.load(from: "Conv2d_3_depthwise/depthwise_weights"), |
| 96 | + depthWiseBias: ckpt.load(from: "Conv2d_3_depthwise/biases"), |
| 97 | + pointWiseFilter: ckpt.load(from: "Conv2d_3_pointwise/weights"), |
| 98 | + pointWiseBias: ckpt.load(from: "Conv2d_3_pointwise/biases"), |
| 99 | + strides: (1, 1) |
| 100 | + ) |
| 101 | + self.dConvBlock4 = DepthwiseSeparableConvBlock( |
| 102 | + depthWiseFilter: ckpt.load(from: "Conv2d_4_depthwise/depthwise_weights"), |
| 103 | + depthWiseBias: ckpt.load(from: "Conv2d_4_depthwise/biases"), |
| 104 | + pointWiseFilter: ckpt.load(from: "Conv2d_4_pointwise/weights"), |
| 105 | + pointWiseBias: ckpt.load(from: "Conv2d_4_pointwise/biases"), |
| 106 | + strides: (2, 2) |
| 107 | + ) |
| 108 | + self.dConvBlock5 = DepthwiseSeparableConvBlock( |
| 109 | + depthWiseFilter: ckpt.load(from: "Conv2d_5_depthwise/depthwise_weights"), |
| 110 | + depthWiseBias: ckpt.load(from: "Conv2d_5_depthwise/biases"), |
| 111 | + pointWiseFilter: ckpt.load(from: "Conv2d_5_pointwise/weights"), |
| 112 | + pointWiseBias: ckpt.load(from: "Conv2d_5_pointwise/biases"), |
| 113 | + strides: (1, 1) |
| 114 | + ) |
| 115 | + self.dConvBlock6 = DepthwiseSeparableConvBlock( |
| 116 | + depthWiseFilter: ckpt.load(from: "Conv2d_6_depthwise/depthwise_weights"), |
| 117 | + depthWiseBias: ckpt.load(from: "Conv2d_6_depthwise/biases"), |
| 118 | + pointWiseFilter: ckpt.load(from: "Conv2d_6_pointwise/weights"), |
| 119 | + pointWiseBias: ckpt.load(from: "Conv2d_6_pointwise/biases"), |
| 120 | + strides: (2, 2) |
| 121 | + ) |
| 122 | + self.dConvBlock7 = DepthwiseSeparableConvBlock( |
| 123 | + depthWiseFilter: ckpt.load(from: "Conv2d_7_depthwise/depthwise_weights"), |
| 124 | + depthWiseBias: ckpt.load(from: "Conv2d_7_depthwise/biases"), |
| 125 | + pointWiseFilter: ckpt.load(from: "Conv2d_7_pointwise/weights"), |
| 126 | + pointWiseBias: ckpt.load(from: "Conv2d_7_pointwise/biases"), |
| 127 | + strides: (1, 1) |
| 128 | + ) |
| 129 | + self.dConvBlock8 = DepthwiseSeparableConvBlock( |
| 130 | + depthWiseFilter: ckpt.load(from: "Conv2d_8_depthwise/depthwise_weights"), |
| 131 | + depthWiseBias: ckpt.load(from: "Conv2d_8_depthwise/biases"), |
| 132 | + pointWiseFilter: ckpt.load(from: "Conv2d_8_pointwise/weights"), |
| 133 | + pointWiseBias: ckpt.load(from: "Conv2d_8_pointwise/biases"), |
| 134 | + strides: (1, 1) |
| 135 | + ) |
| 136 | + self.dConvBlock9 = DepthwiseSeparableConvBlock( |
| 137 | + depthWiseFilter: ckpt.load(from: "Conv2d_9_depthwise/depthwise_weights"), |
| 138 | + depthWiseBias: ckpt.load(from: "Conv2d_9_depthwise/biases"), |
| 139 | + pointWiseFilter: ckpt.load(from: "Conv2d_9_pointwise/weights"), |
| 140 | + pointWiseBias: ckpt.load(from: "Conv2d_9_pointwise/biases"), |
| 141 | + strides: (1, 1) |
| 142 | + ) |
| 143 | + self.dConvBlock10 = DepthwiseSeparableConvBlock( |
| 144 | + depthWiseFilter: ckpt.load(from: "Conv2d_10_depthwise/depthwise_weights"), |
| 145 | + depthWiseBias: ckpt.load(from: "Conv2d_10_depthwise/biases"), |
| 146 | + pointWiseFilter: ckpt.load(from: "Conv2d_10_pointwise/weights"), |
| 147 | + pointWiseBias: ckpt.load(from: "Conv2d_10_pointwise/biases"), |
| 148 | + strides: (1, 1) |
| 149 | + ) |
| 150 | + self.dConvBlock11 = DepthwiseSeparableConvBlock( |
| 151 | + depthWiseFilter: ckpt.load(from: "Conv2d_11_depthwise/depthwise_weights"), |
| 152 | + depthWiseBias: ckpt.load(from: "Conv2d_11_depthwise/biases"), |
| 153 | + pointWiseFilter: ckpt.load(from: "Conv2d_11_pointwise/weights"), |
| 154 | + pointWiseBias: ckpt.load(from: "Conv2d_11_pointwise/biases"), |
| 155 | + strides: (1, 1) |
| 156 | + ) |
| 157 | + self.dConvBlock12 = DepthwiseSeparableConvBlock( |
| 158 | + depthWiseFilter: ckpt.load(from: "Conv2d_12_depthwise/depthwise_weights"), |
| 159 | + depthWiseBias: ckpt.load(from: "Conv2d_12_depthwise/biases"), |
| 160 | + pointWiseFilter: ckpt.load(from: "Conv2d_12_pointwise/weights"), |
| 161 | + pointWiseBias: ckpt.load(from: "Conv2d_12_pointwise/biases"), |
| 162 | + strides: (1, 1) |
| 163 | + ) |
| 164 | + self.dConvBlock13 = DepthwiseSeparableConvBlock( |
| 165 | + depthWiseFilter: ckpt.load(from: "Conv2d_13_depthwise/depthwise_weights"), |
| 166 | + depthWiseBias: ckpt.load(from: "Conv2d_13_depthwise/biases"), |
| 167 | + pointWiseFilter: ckpt.load(from: "Conv2d_13_pointwise/weights"), |
| 168 | + pointWiseBias: ckpt.load(from: "Conv2d_13_pointwise/biases"), |
| 169 | + strides: (1, 1) |
| 170 | + ) |
| 171 | + } |
| 172 | + |
| 173 | + @differentiable |
| 174 | + public func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> { |
| 175 | + var x = convBlock0(input) |
| 176 | + x = dConvBlock1(x) |
| 177 | + x = dConvBlock2(x) |
| 178 | + x = dConvBlock3(x) |
| 179 | + x = dConvBlock4(x) |
| 180 | + x = dConvBlock5(x) |
| 181 | + x = dConvBlock6(x) |
| 182 | + x = dConvBlock7(x) |
| 183 | + x = dConvBlock8(x) |
| 184 | + x = dConvBlock9(x) |
| 185 | + x = dConvBlock10(x) |
| 186 | + x = dConvBlock11(x) |
| 187 | + x = dConvBlock12(x) |
| 188 | + x = dConvBlock13(x) |
| 189 | + return x |
| 190 | + } |
| 191 | + |
| 192 | +} |
0 commit comments