Skip to content

Commit 00236b1

Browse files
authored
Merge branch 'topic-recognition' into topic-recognition
2 parents 054f3bc + 9e710a4 commit 00236b1

File tree

825 files changed

+47993
-19
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

825 files changed

+47993
-19
lines changed

.gitignore

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ __pycache__/
66

77
# C extensions
88
*.so
9-
9+
.idea
1010
# Distribution / packaging
1111
.Python
1212
build/
@@ -128,22 +128,8 @@ dmypy.json
128128
# vscode config file
129129
.vscode/
130130

131+
# pycharm project settings
132+
.idea/
133+
131134
# no tracking mypy config file
132-
mypy.ini
133-
*.png
134-
recognition/s4641500/pirate/driver_script.ipynb
135-
recognition/ISICs_UNet/README.md
136-
recognition/XUE4645768/Readme.md
137-
recognition/s4641500/pirate/yes.py
138-
recognition/s4641500/pirate/train.py
139-
recognition/s4641500/pirate/predict.py
140-
recognition/s4641500/pirate/modules.py
141-
recognition/s4641500/pirate/dataset.py
142-
recognition/ISICs_UNet/README.md
143-
recognition/s4641500/keras_metadata.pb
144-
recognition/s4641500/saved model/model/keras_metadata.pb
145-
recognition/s4641500/saved model/model/saved_model.pb
146-
recognition/s4641500/saved model/model/variables/variables.data-00000-of-00001
147-
recognition/s4641500/pirate/VQVAE.py
148-
recognition/s4641500/pirate/pixelcnn.py
149-
recognition/ISICs_Unet/README.md
135+
mypy.ini

.gitmodules

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[submodule "recognition/46616780_YOLO_Hannah_Scholz/yolov5"]
2+
path = recognition/46616780_YOLO_Hannah_Scholz/yolov5
3+
url = https://github.com/hannahscholz/yolov5.git
4+
[submodule "recognition/46616780_YOLO_Hannah_Scholz/--force"]
5+
path = recognition/46616780_YOLO_Hannah_Scholz/--force
6+
url = https://github.com/hannahscholz/yolov5
7+
[submodule "recognition/46616780_YOLO_Hannah_Scholz/--yolov5"]
8+
path = recognition/46616780_YOLO_Hannah_Scholz/--yolov5
9+
url = https://github.com/hannahscholz/yolov5

recognition/45183735/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Style-Based Generative Adversarial Networks (StyleGAN) - 45183735
2+
This StyleGAN model is implemented by using tensorflow for the OASIS brain dataset which provide a "reasonably clear image".
3+
The structure of this model is from the original StyleGan paper. [here](https://arxiv.org/pdf/1812.04948.pdf)
4+
## Description of the Algorithm
5+
StyleGAN is one of the state-of-the-art extension for the Generative Adversarial Networks (GAN) which use for generating
6+
high-quality images. it also contains two model generator model and discriminator model, generator model will learn
7+
to create a fake image cheat the discriminator, and the discriminator is a classifier to identify the real and fake
8+
images. StyleGAN make an improvement for the generator model and does not modify the discriminator model.
9+
10+
![styleGan generator structure](./readme_png/styleGAN_generator_structure.PNG)
11+
12+
![AdaIN formula](./readme_png/AdaIN.png)
13+
14+
### Generator model
15+
The generator model for StyleGAN include two part, Mapping network and Synthesis network,
16+
Compare to the traditional generator, Sytle-based generator map the Latent input z through the mapping network to an
17+
intermediate latent space w instead of feeds directly into the input layer only.
18+
and then feeds into the adaptive instance normalisation at each convolution layer in the synthesis network.
19+
the formula of the AdaIN is provide in above picture. the noise will add as a source of variation for each
20+
section of the generator model.
21+
22+
![styleGan discriminator structure](./readme_png/styleGAN_discriminator_structure.PNG)
23+
### Discriminator model
24+
25+
As StyleGAN does not change anything about the discriminator, it still uses the discriminator model
26+
in Progressive Growing of GAN (PGGAN). Therefore, the discriminator structure include two convolution layer
27+
with LeakReLU activation in each section of the network. the aim of discriminator is to distinguish the fake and real images.
28+
29+
## Environment and Dataset Setup
30+
### Dependencies
31+
- Tensorflow 2.10.0
32+
- numpy 1.23.3
33+
- matplotlib 3.5.2
34+
- python 3.10.6
35+
36+
### Dataset preparation
37+
The Preprocessed OASIS MRI Dataset need to be extracted to the same level as the ```train.py```.
38+
39+
## Training / Predicting
40+
### Training the model
41+
42+
Run this code to train the model:
43+
44+
```python train.py```
45+
46+
or if you want to choose the number of epochs, run this code:
47+
48+
```python train.py -e EPOCHS```
49+
50+
Note: Replace EPOCHS to the number you choose.
51+
52+
The training step will generate an image and a plot of loss for each epoch. During the training, it will print the progress of the training.
53+
54+
55+
### Prediction using the trained model
56+
After the model training is finished, it will generate a checkpoint folder and do not remove it.
57+
the prediction will use the latest trained model and will generate an image for the prediction.
58+
To make a prediction, run this code:
59+
60+
```python predict.py```
61+
62+
## Result
63+
64+
### Training
65+
66+
![Training progress](./readme_png/training_progress.png)
67+
68+
### Visualise the loss for each training epoch
69+
70+
![Loss for each training epoch](./readme_png/Loss_output_epoch.png)
71+
72+
### Visualise the loss for entire training step
73+
74+
![entire loss for each epoch](./readme_png/Loss_Training_output.png)
75+
76+
### Prediction output
77+
![Prediction using trained model](./readme_png/Prediction.png)
78+
79+
## Reference
80+
[1] TensorFlow. 2022. Deep Convolutional Generative Adversarial Network | TensorFlow Core. [online] Available at: <https://www.tensorflow.org/tutorials/generative/dcgan> [Accessed 11 October 2022].
81+
82+
[2] Karras, T., Laine, S. and Aila, T., 2019. A Style-Based Generator Architecture for Generative Adversarial Networks. [online] arXiv.org. Available at: <https://arxiv.org/abs/1812.04948> [Accessed 9 October 2022].
83+
84+
[3] Karras, T., Aila, T., Laine, S. and Lehtinen, J., 2018. Progressive Growing of GANs for Improved Quality, Stability, and Variation. [online] arXiv.org. Available at: <https://arxiv.org/abs/1710.10196> [Accessed 10 October 2022].

recognition/45183735/dataset.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import tensorflow as tf
2+
import matplotlib.pyplot as plt
3+
import os
4+
5+
6+
# normalise the data between -1 to 1
7+
def normalise(data):
8+
data = tf.cast(data/255., tf.float32)
9+
return data
10+
11+
12+
class Dataset:
13+
14+
def __init__(self, ds_path, batch_size, image_size):
15+
# path for the dataset folder
16+
self.path = ds_path
17+
self.batch_size = batch_size
18+
# image must be squared e.g. image_size = 256 -> image is 256x256
19+
self.image_size = image_size
20+
# dataset for training
21+
self.train_ds = self.get_train_ds()
22+
# dataset for testing
23+
self.test_ds = self.get_test_ds()
24+
# dataset for validation
25+
self.val_ds = self.get_val_ds()
26+
27+
# get the training dataset from the path
28+
def get_train_ds(self):
29+
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
30+
directory=self.path + '/keras_png_slices_train',
31+
label_mode=None,
32+
color_mode='grayscale',
33+
batch_size=self.batch_size,
34+
image_size=(self.image_size, self.image_size)
35+
)
36+
37+
train_ds = train_ds.map(normalise).prefetch(buffer_size=tf.data.experimental.AUTOTUNE).cache()
38+
39+
return train_ds
40+
41+
# get the test dataset from the path
42+
def get_test_ds(self):
43+
test_ds = tf.keras.preprocessing.image_dataset_from_directory(
44+
directory=self.path + '/keras_png_slices_test',
45+
label_mode=None,
46+
color_mode='grayscale',
47+
batch_size=self.batch_size,
48+
image_size=(self.image_size, self.image_size)
49+
)
50+
test_ds = test_ds.map(normalise).prefetch(buffer_size=tf.data.experimental.AUTOTUNE).cache()
51+
return test_ds
52+
53+
# get the validation dataset from the path
54+
def get_val_ds(self):
55+
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
56+
directory=self.path + '/keras_png_slices_validate',
57+
label_mode=None,
58+
color_mode='grayscale',
59+
batch_size=self.batch_size,
60+
image_size=(self.image_size, self.image_size)
61+
)
62+
val_ds = val_ds.map(normalise).prefetch(buffer_size=tf.data.experimental.AUTOTUNE).cache()
63+
return val_ds
64+

recognition/45183735/modules.py

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import tensorflow as tf
2+
3+
4+
# Mapping Network
5+
class G_Mapping:
6+
7+
def __init__(self, latent_size):
8+
self.latent_size = latent_size
9+
# Mapping network for generator
10+
self.model = self.mapping_nw()
11+
12+
# build the mapping network
13+
def mapping_nw(self):
14+
fc_input = tf.keras.layers.Input(shape=(self.latent_size,))
15+
fc1 = tf.keras.layers.Dense(self.latent_size, activation="relu")(fc_input)
16+
fc2 = tf.keras.layers.Dense(self.latent_size, activation="relu")(fc1)
17+
fc3 = tf.keras.layers.Dense(self.latent_size, activation="relu")(fc2)
18+
fc4 = tf.keras.layers.Dense(self.latent_size, activation="relu")(fc3)
19+
fc5 = tf.keras.layers.Dense(self.latent_size, activation="relu")(fc4)
20+
fc6 = tf.keras.layers.Dense(self.latent_size, activation="relu")(fc5)
21+
fc7 = tf.keras.layers.Dense(self.latent_size, activation="relu")(fc6)
22+
return tf.keras.Model(inputs=[fc_input], outputs=[fc7])
23+
24+
# Synthesis network
25+
class G_Synthesis:
26+
def __init__(self, latent_size, g_mapping, input_size):
27+
# input image size
28+
self.input_size = input_size
29+
self.latent_size = latent_size
30+
# Latent inputs
31+
self.z = self.get_latent_inputs()
32+
# Mapping network
33+
self.g_mapping = g_mapping
34+
# non_liner mapping network to map z -> w
35+
self.w = self.get_w()
36+
# Noises input
37+
self.noises_input = self.get_noises_input()
38+
# Noises
39+
self.noises = self.get_noises()
40+
# Synthesis network
41+
self.nw = self.get_synthesis_nw()
42+
43+
def get_latent_inputs(self):
44+
z = []
45+
for i in range(7):
46+
z.append(tf.keras.layers.Input(shape=(self.latent_size,)))
47+
return z
48+
49+
def get_w(self):
50+
w = []
51+
for i in range(7):
52+
w.append(self.g_mapping.model(self.z[i]))
53+
return w
54+
55+
def get_noises_input(self):
56+
noises_input = []
57+
for i in range(7):
58+
noises_input.append(tf.keras.layers.Input(shape=(4 * 2 ** i, 4 * 2 ** i, 1)))
59+
return noises_input
60+
61+
def get_noises(self):
62+
noises = []
63+
for i in range(7):
64+
noises.append(tf.keras.layers.Dense(32, activation="relu")(self.noises_input[i]))
65+
return noises
66+
67+
# Adaptive instance normalisation
68+
def get_AdaIN(self, x, ys, yb):
69+
x_mean, x_std = tf.keras.backend.mean(x), tf.keras.backend.std(x)
70+
ys = tf.reshape(ys, (-1, 1, 1, tf.shape(ys)[-1]))
71+
yb = tf.reshape(yb, (-1, 1, 1, tf.shape(yb)[-1]))
72+
return tf.add(tf.multiply(ys, tf.divide(x - x_mean, x_std + 1e-7)), yb)
73+
74+
def get_synthesis_nw(self):
75+
layer = tf.keras.layers.Dense(4 * 4 * 32, activation="relu")(self.z[0])
76+
layer = tf.keras.layers.Reshape((4, 4, 32))(layer)
77+
noise_b = tf.keras.layers.Dense(32)(self.noises[0])
78+
# add noise
79+
layer = tf.keras.layers.Add()([layer, noise_b])
80+
# add the style in AdaIN
81+
layer = self.get_AdaIN(layer, tf.keras.layers.Dense(32)(self.w[0]), tf.keras.layers.Dense(32)(self.w[0]))
82+
layer = tf.keras.layers.Conv2D(32, (3, 3), padding="same")(layer)
83+
# add noise
84+
layer = tf.keras.layers.Add()([layer, noise_b])
85+
# add the style in AdaIN
86+
layer = self.get_AdaIN(layer, tf.keras.layers.Dense(32)(self.w[0]), tf.keras.layers.Dense(32)(self.w[0]))
87+
88+
# for 8x8 to 256x256
89+
for i in range(6):
90+
layer = tf.keras.layers.UpSampling2D()(layer)
91+
layer = tf.keras.layers.Conv2D(32, (3, 3), padding="same")(layer)
92+
noise_b = tf.keras.layers.Dense(32)(self.noises[i + 1])
93+
layer = tf.keras.layers.Add()([layer, noise_b])
94+
layer = self.get_AdaIN(layer, tf.keras.layers.Dense(32)(self.w[i + 1]),
95+
tf.keras.layers.Dense(32)(self.w[i + 1]))
96+
layer = tf.keras.layers.Conv2D(32, (3, 3), padding="same")(layer)
97+
layer = tf.keras.layers.Add()([layer, noise_b])
98+
layer = self.get_AdaIN(layer, tf.keras.layers.Dense(32)(self.w[i + 1]),
99+
tf.keras.layers.Dense(32)(self.w[i + 1]))
100+
101+
layer = tf.keras.layers.Dense(1)(layer)
102+
layer = tf.keras.layers.Activation("sigmoid")(layer)
103+
return layer
104+
105+
106+
# generator model
107+
class G_style:
108+
109+
def __init__(self, latent_size, input_size, g_synthesis):
110+
self.input_size = input_size
111+
self.latent_size = latent_size
112+
self.g_synthesis = g_synthesis
113+
self.model = self.generation_model()
114+
115+
def generation_model(self):
116+
model = tf.keras.Model(inputs=self.g_synthesis.z + self.g_synthesis.noises_input, outputs=[self.g_synthesis.nw])
117+
model.summary()
118+
return model
119+
120+
121+
# as the styleGan does not modify discriminator in any way, so we using the discriminator structure of PGGan
122+
class Discriminator:
123+
124+
def __init__(self, input_size):
125+
self.input_size = input_size
126+
self.d_model = self.generate_discriminator_model()
127+
128+
def generate_discriminator_model(self):
129+
D_model = tf.keras.models.Sequential()
130+
131+
# 256x256 -> 128x128
132+
D_model.add(tf.keras.layers.Conv2D(16, (3, 3), strides=(1, 1), padding='same',
133+
input_shape=[self.input_size, self.input_size, 1]))
134+
D_model.add(tf.keras.layers.LeakyReLU())
135+
D_model.add(tf.keras.layers.Conv2D(16, (3, 3), strides=(1, 1), padding='same'))
136+
D_model.add(tf.keras.layers.LeakyReLU())
137+
D_model.add(tf.keras.layers.Conv2D(32, (3, 3), strides=(2, 2), padding='same'))
138+
D_model.add(tf.keras.layers.LeakyReLU())
139+
D_model.add(tf.keras.layers.Dropout(0.2))
140+
141+
# 128x128 -> 64x64
142+
D_model.add(tf.keras.layers.Conv2D(32, (3, 3), strides=(1, 1), padding='same'))
143+
D_model.add(tf.keras.layers.LeakyReLU())
144+
D_model.add(tf.keras.layers.Conv2D(64, (3, 3), strides=(2, 2), padding='same'))
145+
D_model.add(tf.keras.layers.LeakyReLU())
146+
D_model.add(tf.keras.layers.Dropout(0.3))
147+
148+
# 64x64 -> 32x32
149+
D_model.add(tf.keras.layers.Conv2D(64, (3, 3), strides=(1, 1), padding='same'))
150+
D_model.add(tf.keras.layers.LeakyReLU())
151+
D_model.add(tf.keras.layers.Conv2D(128, (3, 3), strides=(2, 2), padding='same'))
152+
D_model.add(tf.keras.layers.LeakyReLU())
153+
D_model.add(tf.keras.layers.Dropout(0.4))
154+
155+
# 32x32 -> 16x16
156+
D_model.add(tf.keras.layers.Conv2D(128, (3, 3), strides=(1, 1), padding='same'))
157+
D_model.add(tf.keras.layers.LeakyReLU())
158+
D_model.add(tf.keras.layers.Conv2D(256, (3, 3), strides=(2, 2), padding='same'))
159+
D_model.add(tf.keras.layers.LeakyReLU())
160+
D_model.add(tf.keras.layers.Dropout(0.4))
161+
162+
# 16x16 -> 8x8
163+
D_model.add(tf.keras.layers.Conv2D(256, (3, 3), strides=(1, 1), padding='same'))
164+
D_model.add(tf.keras.layers.LeakyReLU())
165+
D_model.add(tf.keras.layers.Conv2D(512, (3, 3), strides=(2, 2), padding='same'))
166+
D_model.add(tf.keras.layers.LeakyReLU())
167+
D_model.add(tf.keras.layers.Dropout(0.4))
168+
169+
# 8x8 -> 4x4
170+
D_model.add(tf.keras.layers.Conv2D(512, (3, 3), strides=(1, 1), padding='same'))
171+
D_model.add(tf.keras.layers.LeakyReLU())
172+
D_model.add(tf.keras.layers.Conv2D(512, (3, 3), strides=(2, 2), padding='same'))
173+
D_model.add(tf.keras.layers.LeakyReLU())
174+
D_model.add(tf.keras.layers.Dropout(0.4))
175+
176+
D_model.add(tf.keras.layers.Flatten())
177+
D_model.add(tf.keras.layers.Dense(1, activation="sigmoid"))
178+
179+
return D_model
180+

0 commit comments

Comments
 (0)