-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmri_gan.py
83 lines (68 loc) · 2.99 KB
/
mri_gan.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
import tensorflow as tf
class InstanceNormalization(tf.keras.layers.Layer):
def __init__(self, epsilon=1e-5):
super(InstanceNormalization, self).__init__()
self.epsilon = epsilon
def build(self, input_shape):
self.scale = self.add_weight(
shape=input_shape[-1:], initializer='random_normal', trainable=True)
self.offset = self.add_weight(
shape=input_shape[-1:], initializer='zeros', trainable=True)
def call(self, x):
mean, variance = tf.nn.moments(x, axes=[1, 2], keepdims=True)
return self.scale * (x - mean) / tf.math.sqrt(variance + self.epsilon) + self.offset
def downsample(filters, size, apply_norm=True):
initializer = tf.random_normal_initializer(0., 0.02)
result = tf.keras.Sequential()
result.add(tf.keras.layers.Conv2D(filters, size, strides=2,
padding='same', kernel_initializer=initializer))
if apply_norm:
result.add(InstanceNormalization())
result.add(tf.keras.layers.LeakyReLU())
return result
def upsample(filters, size, apply_dropout=False):
initializer = tf.random_normal_initializer(0., 0.02)
result = tf.keras.Sequential()
result.add(tf.keras.layers.Conv2DTranspose(filters, size, strides=2, padding='same',
kernel_initializer=initializer))
result.add(InstanceNormalization())
if apply_dropout:
result.add(tf.keras.layers.Dropout(0.5))
result.add(tf.keras.layers.ReLU())
return result
def unet_generator():
down_stack = [downsample(64, 4, False), downsample(
128, 4), downsample(256, 4)]
up_stack = [upsample(128, 4), upsample(64, 4)]
initializer = tf.random_normal_initializer(0., 0.02)
last = tf.keras.layers.Conv2DTranspose(3, 4, strides=2, padding='same', kernel_initializer=initializer,
activation='tanh')
concat = tf.keras.layers.Concatenate()
inputs = tf.keras.layers.Input(shape=[256, 256, 3])
x = inputs
skips = []
for down in down_stack:
x = down(x)
skips.append(x)
skips = reversed(skips[:-1])
for up, skip in zip(up_stack, skips):
x = up(x)
x = concat([x, skip])
x = last(x)
return tf.keras.Model(inputs=inputs, outputs=x)
def discriminator():
initializer = tf.random_normal_initializer(0., 0.02)
inputs = tf.keras.layers.Input(shape=[256, 256, 3])
x = inputs
down1 = downsample(64, 4, False)(x)
down2 = downsample(128, 4)(down1)
down3 = downsample(256, 4)(down2)
zero_pad1 = tf.keras.layers.ZeroPadding2D()(down3)
conv = tf.keras.layers.Conv2D(
512, 4, strides=1, kernel_initializer=initializer)(zero_pad1)
norm1 = InstanceNormalization()(conv)
leaky_relu = tf.keras.layers.LeakyReLU()(norm1)
zero_pad2 = tf.keras.layers.ZeroPadding2D()(leaky_relu)
last = tf.keras.layers.Conv2D(
1, 4, strides=1, kernel_initializer=initializer)(zero_pad2)
return tf.keras.Model(inputs=inputs, outputs=last)