|
| 1 | +// Copyright 2019 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 TensorFlow |
| 16 | + |
| 17 | +public struct ResNetGenerator<NormalizationType: FeatureChannelInitializable>: Layer where NormalizationType.TangentVector.VectorSpaceScalar == Float, NormalizationType.Input == Tensorf, NormalizationType.Output == Tensorf { |
| 18 | + var conv1: Conv2D<Float> |
| 19 | + var norm1: NormalizationType |
| 20 | + |
| 21 | + var conv2: Conv2D<Float> |
| 22 | + var norm2: NormalizationType |
| 23 | + |
| 24 | + var conv3: Conv2D<Float> |
| 25 | + var norm3: NormalizationType |
| 26 | + |
| 27 | + var resblocks: [ResNetBlock<NormalizationType>] |
| 28 | + |
| 29 | + var upConv1: TransposedConv2D<Float> |
| 30 | + var upNorm1: NormalizationType |
| 31 | + |
| 32 | + var upConv2: TransposedConv2D<Float> |
| 33 | + var upNorm2: NormalizationType |
| 34 | + |
| 35 | + var lastConv: Conv2D<Float> |
| 36 | + |
| 37 | + public init(inputChannels: Int, |
| 38 | + outputChannels: Int, |
| 39 | + blocks: Int, |
| 40 | + ngf: Int, |
| 41 | + normalization: NormalizationType.Type, |
| 42 | + useDropout: Bool = false) { |
| 43 | + norm1 = NormalizationType(featureCount: ngf) |
| 44 | + let useBias = norm1 is InstanceNorm2D<Float> |
| 45 | + |
| 46 | + let filterInit: (TensorShape) -> Tensorf = { Tensorf(randomNormal: $0, standardDeviation: Tensorf(0.02)) } |
| 47 | + let biasInit: (TensorShape) -> Tensorf = useBias ? filterInit : zeros() |
| 48 | + |
| 49 | + conv1 = Conv2D(filterShape: (7, 7, inputChannels, ngf), |
| 50 | + strides: (1, 1), |
| 51 | + filterInitializer: filterInit, |
| 52 | + biasInitializer: biasInit) |
| 53 | + |
| 54 | + var mult = 1 |
| 55 | + |
| 56 | + conv2 = Conv2D(filterShape: (3, 3, ngf * mult, ngf * mult * 2), |
| 57 | + strides: (2, 2), |
| 58 | + padding: .same, |
| 59 | + filterInitializer: filterInit, |
| 60 | + biasInitializer: biasInit) |
| 61 | + norm2 = NormalizationType(featureCount: ngf * mult * 2) |
| 62 | + |
| 63 | + mult = 2 |
| 64 | + |
| 65 | + conv3 = Conv2D(filterShape: (3, 3, ngf * mult, ngf * mult * 2), |
| 66 | + strides: (2, 2), |
| 67 | + padding: .same, |
| 68 | + filterInitializer: filterInit, |
| 69 | + biasInitializer: biasInit) |
| 70 | + norm3 = NormalizationType(featureCount: ngf * mult * 2) |
| 71 | + |
| 72 | + mult = 4 |
| 73 | + |
| 74 | + resblocks = (0 ..< blocks).map { _ in |
| 75 | + ResNetBlock(channels: ngf * mult, |
| 76 | + paddingMode: .reflect, |
| 77 | + normalization: normalization, |
| 78 | + useDropOut: useDropout, |
| 79 | + filterInit: filterInit, |
| 80 | + biasInit: biasInit) |
| 81 | + } |
| 82 | + |
| 83 | + mult = 4 |
| 84 | + |
| 85 | + upConv1 = TransposedConv2D(filterShape: (3, 3, ngf * mult / 2, ngf * mult), |
| 86 | + strides: (2, 2), |
| 87 | + padding: .same, |
| 88 | + filterInitializer: filterInit, |
| 89 | + biasInitializer: biasInit) |
| 90 | + upNorm1 = NormalizationType(featureCount: ngf * mult / 2) |
| 91 | + |
| 92 | + mult = 2 |
| 93 | + |
| 94 | + upConv2 = TransposedConv2D(filterShape: (3, 3, ngf * mult / 2, ngf * mult), |
| 95 | + strides: (2, 2), |
| 96 | + padding: .same, |
| 97 | + filterInitializer: filterInit, |
| 98 | + biasInitializer: biasInit) |
| 99 | + upNorm2 = NormalizationType(featureCount: ngf * mult / 2) |
| 100 | + |
| 101 | + lastConv = Conv2D(filterShape: (7, 7, ngf, outputChannels), |
| 102 | + padding: .same, |
| 103 | + filterInitializer: filterInit, |
| 104 | + biasInitializer: biasInit) |
| 105 | + } |
| 106 | + |
| 107 | + @differentiable |
| 108 | + public func callAsFunction(_ input: Tensorf) -> Tensorf { |
| 109 | + var x = input.padded(forSizes: [(0, 0), (3, 3), (3, 3), (0, 0)], mode: .reflect) |
| 110 | + x = relu(x.sequenced(through: conv1, norm1)) |
| 111 | + x = relu(x.sequenced(through: conv2, norm2)) |
| 112 | + x = relu(x.sequenced(through: conv3, norm3)) |
| 113 | + |
| 114 | + x = resblocks(x) |
| 115 | + |
| 116 | + x = relu(x.sequenced(through: upConv1, upNorm1)) |
| 117 | + x = relu(x.sequenced(through: upConv2, upNorm2)) |
| 118 | + |
| 119 | + x = lastConv(x) |
| 120 | + x = tanh(x) |
| 121 | + |
| 122 | + return x |
| 123 | + } |
| 124 | +} |
0 commit comments