-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgan_layer_architecture_shapes.py
174 lines (145 loc) · 6.1 KB
/
gan_layer_architecture_shapes.py
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
# Copyright 2020 Google Inc. 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.
# ==============================================================================
def calc_generator_discriminator_conv_layer_properties(
conv_num_filters, conv_kernel_sizes, conv_strides, depth):
"""Calculates generator and discriminator conv layer properties.
Args:
num_filters: list, nested list of ints of the number of filters
for each conv layer.
kernel_sizes: list, nested list of ints of the kernel sizes for
each conv layer.
strides: list, nested list of ints of the strides for each conv
layer.
depth: int, depth dimension of images.
Returns:
Nested lists of conv layer properties for both generator and
discriminator.
"""
def make_generator(num_filters, kernel_sizes, strides, depth):
"""Calculates generator conv layer properties.
Args:
num_filters: list, nested list of ints of the number of filters
for each conv layer.
kernel_sizes: list, nested list of ints of the kernel sizes for
each conv layer.
strides: list, nested list of ints of the strides for each conv
layer.
depth: int, depth dimension of images.
Returns:
Nested list of conv layer properties for generator.
"""
# Get the number of growths.
num_growths = len(num_filters) - 1
# Make base block.
in_out = num_filters[0]
base = [
[kernel_sizes[0][i]] * 2 + in_out + [strides[0][i]] * 2
for i in range(len(num_filters[0]))
]
blocks = [base]
# Add growth blocks.
for i in range(1, num_growths + 1):
in_out = [[blocks[i - 1][-1][-3], num_filters[i][0]]]
block = [[kernel_sizes[i][0]] * 2 + in_out[0] + [strides[i][0]] * 2]
for j in range(1, len(num_filters[i])):
in_out.append([block[-1][-3], num_filters[i][j]])
block.append(
[kernel_sizes[i][j]] * 2 + in_out[j] + [strides[i][j]] * 2
)
blocks.append(block)
# Add toRGB conv.
blocks[-1].append([1, 1, blocks[-1][-1][-3], depth] + [1] * 2)
return blocks
def make_discriminator(generator):
"""Calculates discriminator conv layer properties.
Args:
generator: list, nested list of conv layer properties for
generator.
Returns:
Nested list of conv layer properties for discriminator.
"""
# Reverse generator.
discriminator = generator[::-1]
# Reverse input and output shapes.
discriminator = [
[
conv[0:2] + conv[2:4][::-1] + conv[-2:]
for conv in block[::-1]
]
for block in discriminator
]
return discriminator
# Calculate conv layer properties for generator using args.
generator = make_generator(
conv_num_filters, conv_kernel_sizes, conv_strides, depth
)
# Calculate conv layer properties for discriminator using generator
# properties.
discriminator = make_discriminator(generator)
return generator, discriminator
def split_up_generator_conv_layer_properties(
generator, num_filters, strides, depth):
"""Splits up generator conv layer properties into lists.
Args:
generator: list, nested list of conv layer properties for
generator.
num_filters: list, nested list of ints of the number of filters
for each conv layer.
strides: list, nested list of ints of the strides for each conv
layer.
depth: int, depth dimension of images.
Returns:
Nested lists of conv layer properties for generator.
"""
generator_base_conv_blocks = [generator[0][0:len(num_filters[0])]]
generator_growth_conv_blocks = []
if len(num_filters) > 1:
generator_growth_conv_blocks = generator[1:-1] + [generator[-1][:-1]]
generator_to_rgb_layers = [
[[1] * 2 + [num_filters[i][0]] + [depth] + [strides[i][0]] * 2]
for i in range(len(num_filters))
]
return (generator_base_conv_blocks,
generator_growth_conv_blocks,
generator_to_rgb_layers)
def split_up_discriminator_conv_layer_properties(
discriminator, num_filters, strides, depth):
"""Splits up discriminator conv layer properties into lists.
Args:
discriminator: list, nested list of conv layer properties for
discriminator.
num_filters: list, nested list of ints of the number of filters
for each conv layer.
strides: list, nested list of ints of the strides for each conv
layer.
depth: int, depth dimension of images.
Returns:
Nested lists of conv layer properties for discriminator.
"""
discriminator_from_rgb_layers = [
[[1] * 2 + [depth] + [num_filters[i][0]] + [strides[i][0]] * 2]
for i in range(len(num_filters))
]
if len(num_filters) > 1:
discriminator_base_conv_blocks = [discriminator[-1]]
else:
discriminator_base_conv_blocks = [discriminator[-1][1:]]
discriminator_growth_conv_blocks = []
if len(num_filters) > 1:
discriminator_growth_conv_blocks = [discriminator[0][1:]] + discriminator[1:-1]
discriminator_growth_conv_blocks = discriminator_growth_conv_blocks[::-1]
return (discriminator_from_rgb_layers,
discriminator_base_conv_blocks,
discriminator_growth_conv_blocks)