-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_greyscale.py
182 lines (137 loc) · 6.31 KB
/
train_greyscale.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
175
176
177
178
179
180
181
182
import matplotlib.pyplot as plt
import numpy as np
from glob import glob
import time
import tensorflow as tf
from tensorflow import keras
from keras import Sequential
from keras.models import Model
from keras.layers import Conv2D, Input, MaxPool2D, Conv2DTranspose, concatenate, Lambda, BatchNormalization, Activation, LeakyReLU, ReLU
from keras.utils import img_to_array, load_img, plot_model
from tensorflow.keras.preprocessing.image import img_to_array, load_img
from keras.optimizers import Adam
from keras.initializers import RandomNormal
save_path = "" #path to save model
path = "/kaggle/working/maps/train/" #path to training data
combined_images = sorted(glob(path + "*.jpg"))
images = np.zeros(shape=(len(combined_images), 256, 256, 1))
masks = np.zeros(shape=(len(combined_images), 256, 256, 3))
for idx, path in enumerate(combined_images):
combined_image = tf.cast(img_to_array(load_img(path)), tf.float32)
image = combined_image[:,:600,:]
mask = combined_image[:,600:,:]
images[idx] = tf.image.rgb_to_grayscale(tf.image.resize(image,(256,256)))/255
masks[idx] = (tf.image.resize(mask,(256,256)))/255
print(len(images))
print(len(masks))
#greyscale
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Input, Conv2D, Conv2DTranspose, LeakyReLU, BatchNormalization, ReLU, concatenate
from tensorflow.keras.initializers import RandomNormal
import tensorflow as tf
def downscale(num_filters):
block = Sequential()
block.add(Conv2D(num_filters, kernel_size=4, strides=2, padding='same', kernel_initializer='he_normal', use_bias=False))
block.add(LeakyReLU(alpha=0.2))
block.add(BatchNormalization())
return block
def upscale(num_filters):
block = Sequential()
block.add(Conv2DTranspose(num_filters, kernel_size=4, strides=2, padding='same', kernel_initializer='he_normal', use_bias=False))
block.add(LeakyReLU(alpha=0.2))
block.add(BatchNormalization())
block.add(ReLU())
return block
def Generator():
inputs = Input(shape=(256,256,1), name="InputLayer")
encoder = [
downscale(64),
downscale(128),
downscale(256),
downscale(512),
downscale(512),
downscale(512),
downscale(512),
]
latent_space = downscale(512)
decoder = [
upscale(512),
upscale(512),
upscale(512),
upscale(512),
upscale(256),
upscale(128),
upscale(64),
]
x = inputs
skips = []
for layer in encoder:
x = layer(x)
skips.append(x)
x = latent_space(x)
skips = reversed(skips)
for up, skip in zip(decoder, skips):
x = up(x)
x = concatenate([x, skip])
initializer = RandomNormal(stddev=0.02, seed=42)
outputs = Conv2DTranspose(3, kernel_size=4, strides=2, kernel_initializer = initializer, activation = 'tanh', padding = 'same')
outputs = outputs(x)
generator = Model(inputs = inputs, outputs = outputs, name="Generator")
return generator
#greyscale
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, concatenate, Conv2D, LeakyReLU, BatchNormalization
from tensorflow.keras.initializers import RandomNormal
def Discriminator():
image = Input(shape = (256,256,1), name = "ImageInput")
target = Input(shape = (256,256,3), name = "TargetInput")
x = concatenate([image, target])
x = downscale(64)(x)
x = downscale(128)(x)
x = downscale(512)(x)
initializer = RandomNormal(stddev = 0.02, seed=42)
x = Conv2D(512, kernel_size = 4, strides = 1, kernel_initializer = initializer, use_bias=False)(x)
x = BatchNormalization()(x)
x = LeakyReLU()(x)
x = Conv2D(1, kernel_size = 4, kernel_initializer = initializer)(x)
discriminator = Model(inputs = [image, target], outputs = x, name = "Discriminator")
return discriminator
generator = Generator()
discriminator = Discriminator()
adversarial_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True)
generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)
discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)
def generator_loss(discriminator_generated, generated_output, target_image):
gan_loss = adversarial_loss(tf.ones_like(discriminator_generated), discriminator_generated)
l1_loss = tf.reduce_mean(tf.abs(target_image - generated_output))
total_loss = (100 * l1_loss) + gan_loss
return total_loss, gan_loss, l1_loss
def discriminator_loss(discriminator_real_output, discriminator_generated_output):
real_loss = adversarial_loss(tf.ones_like(discriminator_real_output), discriminator_real_output)
fake_loss = adversarial_loss(tf.zeros_like(discriminator_generated_output), discriminator_generated_output)
total_loss = real_loss + fake_loss
return total_loss
def train_step(inputs, target):
with tf.GradientTape() as generator_tape, tf.GradientTape() as discriminator_tape:
generated_output = generator(inputs, training=True)
discriminator_real_output = discriminator([inputs, target], training=True)
discriminator_generated_output = discriminator([inputs, generated_output], training=True)
generator_total_loss, generator_gan_loss, generator_l1_loss = generator_loss(discriminator_generated_output, generated_output, target)
discriminator_Loss = discriminator_loss(discriminator_real_output, discriminator_generated_output)
print(generator_total_loss, discriminator_Loss)
generator_gradients = generator_tape.gradient(generator_total_loss, generator.trainable_variables)
generator_optimizer.apply_gradients(zip(generator_gradients, generator.trainable_variables))
discriminator_gradients = discriminator_tape.gradient(discriminator_Loss, discriminator.trainable_variables)
discriminator_optimizer.apply_gradients(zip(discriminator_gradients, discriminator.trainable_variables))
sat_image, map_image = tf.cast(images, tf.float32), tf.cast(masks, tf.float32)
dataset = (sat_image,map_image)
data = tf.data.Dataset.from_tensor_slices(dataset).batch(32, drop_remainder=True)
def fit(data, epochs):
for epoch in range(epochs):
start = time.time()
print("Current epoch: ", epoch+1)
for image, mask in data:
train_step(image, mask)
print(f"Time taken to complete the epoch {epoch + 1} is {(time.time() - start):.2f} seconds \n")
fit(data, 100)
generator.save(save_path)