-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
155 lines (127 loc) · 5.99 KB
/
model.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
"""
Authors : inzapp
Github url : https://github.com/inzapp/super-resolution
Copyright (c) 2023 Inzapp
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import os
import tensorflow as tf
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
class Model:
def __init__(self, input_shape, output_shape, use_gan):
self.input_shape = input_shape
self.output_shape = output_shape
self.target_scale = output_shape[0] / input_shape[0]
self.use_gan = use_gan
self.gan = None
self.g_model = None
self.d_model = None
def build(self, pretrained_g_model=None):
assert self.output_shape[0] % self.target_scale == 0 and self.output_shape[1] % self.target_scale == 0
if pretrained_g_model is None:
g_input, g_output = self.build_g(bn=self.use_gan)
self.g_model = tf.keras.models.Model(g_input, g_output)
else:
g_input, g_output = pretrained_g_model.input, pretrained_g_model.output
self.g_model = pretrained_g_model
if self.use_gan:
d_input, d_output = self.build_d(bn=False)
self.d_model = tf.keras.models.Model(d_input, d_output)
gan_output = self.d_model(g_output)
self.gan = tf.keras.models.Model(g_input, gan_output)
return self.g_model, self.d_model, self.gan
def build_g(self, bn):
g_input = tf.keras.layers.Input(shape=self.input_shape)
x = g_input
end_filters = 16
initial_filters = end_filters * (self.target_scale // 2)
if self.target_scale >= 2:
x = self.upsampling(x)
x = self.conv2d(x, initial_filters // 1, 3, 1, activation='relu', bn=bn)
x = self.conv2d(x, initial_filters // 1, 3, 1, activation='relu', bn=bn)
if self.target_scale >= 4:
x = self.upsampling(x)
x = self.conv2d(x, initial_filters // 2, 3, 1, activation='relu', bn=bn)
x = self.conv2d(x, initial_filters // 2, 3, 1, activation='relu', bn=bn)
if self.target_scale >= 8:
x = self.upsampling(x)
x = self.conv2d(x, initial_filters // 4, 3, 1, activation='relu', bn=bn)
x = self.conv2d(x, initial_filters // 4, 3, 1, activation='relu', bn=bn)
if self.target_scale >= 16:
x = self.upsampling(x)
x = self.conv2d(x, initial_filters // 8, 3, 1, activation='relu', bn=bn)
x = self.conv2d(x, initial_filters // 8, 3, 1, activation='relu', bn=bn)
if self.target_scale >= 32:
x = self.upsampling(x)
x = self.conv2d(x, initial_filters // 16, 3, 1, activation='relu', bn=bn)
x = self.conv2d(x, initial_filters // 16, 3, 1, activation='relu', bn=bn)
g_output = self.conv2d(x, self.output_shape[-1], 1, 1, activation='sigmoid', bn=False)
return g_input, g_output
def build_d(self, bn):
d_input = tf.keras.layers.Input(shape=self.output_shape)
x = d_input
x = self.conv2d(x, 16, 3, 2, activation='leaky', bn=bn)
x = self.conv2d(x, 32, 3, 2, activation='leaky', bn=bn)
x = self.conv2d(x, 64, 3, 2, activation='leaky', bn=bn)
x = self.conv2d(x, 128, 3, 2, activation='leaky', bn=bn)
x = self.conv2d(x, 256, 3, 2, activation='leaky', bn=bn)
x = self.flatten(x)
d_output = self.dense(x, 1, activation='linear', bn=False)
return d_input, d_output
def conv2d(self, x, filters, kernel_size, strides, bn=False, activation='relu'):
x = tf.keras.layers.Conv2D(
strides=strides,
filters=filters,
padding='same',
kernel_size=kernel_size,
use_bias=not bn,
kernel_initializer=self.kernel_initializer())(x)
if bn:
x = self.batch_normalization(x)
return self.activation(x, activation)
def dense(self, x, units, bn=False, activation='relu'):
x = tf.keras.layers.Dense(
units=units,
use_bias=not bn,
kernel_initializer=self.kernel_initializer())(x)
if bn:
x = self.batch_normalization(x)
return self.activation(x, activation)
def batch_normalization(self, x):
return tf.keras.layers.BatchNormalization(momentum=0.8 if self.use_gan else 0.98)(x)
def kernel_initializer(self):
return tf.keras.initializers.RandomNormal(mean=0.0, stddev=0.02) if self.use_gan else 'glorot_normal'
def activation(self, x, activation):
if activation == 'leaky':
x = tf.keras.layers.LeakyReLU(alpha=0.2)(x)
else:
x = tf.keras.layers.Activation(activation=activation)(x)
return x
def max_pool(self, x):
return tf.keras.layers.MaxPool2D()(x)
def upsampling(self, x):
return tf.keras.layers.UpSampling2D()(x)
def add(self, layers):
return tf.keras.layers.Add()(layers)
def flatten(self, x):
return tf.keras.layers.Flatten()(x)
def summary(self):
self.g_model.summary()
if self.use_gan:
print()
self.gan.summary()