-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining_binary_crossentropy.py
192 lines (137 loc) · 5.59 KB
/
training_binary_crossentropy.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
183
184
185
186
187
188
189
190
191
192
print('[INFO] loading packages...')
import os
import numpy as np
import h5py
from tqdm import tqdm
import glob
import csv
print('[INFO] loading tf...')
import tensorflow as tf
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import layers
from tensorflow.keras.callbacks import TensorBoard
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
#tf.config.experimental.set_memory_growth(physical_devices[1], True)
#tf.config.experimental.set_memory_growth(physical_devices[2], True)
#tf.config.experimental.set_memory_growth(physical_devices[3], True)
SIZE = (549, 549)
SHUFFLE = True
BATCH = 32
EPOCHS = 50
MODEL_NAME = 'model_14'
#fsx for lustre :)
PATH = '/home/ec2-user/fsx/splits/100/bool'
files = glob.glob(PATH + '/*.h5')
#files = glob.glob(PATH)
print(len(files))
'''
nn_files = []
for file in tqdm(files):
r0 = file+"|"+'0'
r90 = file+"|"+'90'
r180 = file+"|"+'180'
r270 = file+"|"+'270'
rud = file+"|"+'ud'
rlr = file+"|"+'lr'
nn_files.extend([r0, r90, r180, r270, rud, rlr])
np.random.shuffle(nn_files)
train_files = nn_files[:300000]
test_files = nn_files[300000:450000]
new = nn_files[450000:]
with open(f'{MODEL_NAME}_new.csv', 'w') as f:
write = csv.writer(f)
write.writerow(new)
'''
start_file = 36000
end_file = 45000
np.random.shuffle(files)
train_files = files[:start_file]
test_files = files[start_file:end_file]
new = files[end_file:]
with open(f'{MODEL_NAME}_unseen.csv', 'w') as f:
write = csv.writer(f)
write.writerow(new)
print('[INFO] creating data pipeline...')
def make_dataset(filenames, batch_size):
def load_file(filename):
f = h5py.File(filename.numpy(), 'r', driver='core')
B12 = f['B12'][:]
NDVI = f['NDVI'][:]
#not going to augment data at this time
'''
if r == "90":
B12 = np.rot90(B12)
NDVI = np.rot90(NDVI)
elif r == "180":
B12 = np.rot90(B12, 2)
NDVI = np.rot90(NDVI, 2)
elif r == "270":
B12 = np.rot90(B12, 3)
NDVI = np.rot90(NDVI, 3)
elif r == "ud":
B12 = np.flipud(B12)
NDVI = np.flipud(NDVI)
elif r == "lr":
B12 = np.fliplr(B12)
NDVI = np.fliplr(NDVI)
'''
# HxWxC Heigth width Channel
NDVI = NDVI.reshape(SIZE[0], SIZE[1], 1)
B12 = B12.reshape(SIZE[0], SIZE[1], 1)
return NDVI, B12
# cuz tensorflow
def parse_file_tf(filename):
return tf.py_function(load_file, [filename], [tf.float16, tf.float16])
def configure_for_performance(ds, batch_size):
ds = ds.shuffle(buffer_size=500)
ds = ds.batch(batch_size)
ds = ds.cache() # for instnces with greater than 60GB memory
ds = ds.prefetch(buffer_size=tf.data.experimental.AUTOTUNE) # gets next batch ready
ds = ds.repeat() # continuously loads
return ds
filenames_ds = tf.data.Dataset.from_tensor_slices(filenames)
ds = filenames_ds.map(parse_file_tf, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds = configure_for_performance(ds, batch_size)
return ds
train_ds = make_dataset(train_files, BATCH)
test_ds = make_dataset(test_files, BATCH)
def ssim(y_true, y_pred):
return 1 - tf.reduce_mean(tf.image.ssim(y_true, y_pred, 1.0))
print('[INFO] creating model...')
#strategy = tf.distribute.MirroredStrategy()
#print('Number of devices: {}'.format(strategy.num_replicas_in_sync))
#with strategy.scope():
loss_function = tf.keras.losses.binary_crossentropy
optimizer = tf.keras.optimizers.Adam(learning_rate = .0001)
inp = layers.Input(shape=(549, 549, 1))
x = layers.Conv2D(filters=32, kernel_size=(5,5), padding="same", activation="relu")(inp)
#x = tf.keras.layers.MaxPooling2D(pool_size=(3, 3), padding="valid")(x)
x = layers.BatchNormalization()(x)
x = layers.Conv2D(filters=32, kernel_size=(3, 3), padding="same", activation="relu")(x)
#x = tf.keras.layers.MaxPooling2D(pool_size=(3, 3), padding="valid")(x)
x = layers.BatchNormalization()(x)
x = layers.Conv2D(filters=32, kernel_size=(3, 3), padding="same", activation="relu")(x)
x = layers.BatchNormalization()(x)
x = layers.Conv2D(filters=32, kernel_size=(1, 1), padding="same", activation="relu")(x)
x = layers.BatchNormalization()(x)
x = layers.Conv2D(filters=1, kernel_size=(3, 3), padding="same", activation="sigmoid")(x)
#x = tf.nn.depth_to_space(x, 9)
#Compiling the model
model = tf.keras.Model(inp, x)
model.compile(optimizer=optimizer, loss=loss_function)
tensorboard = TensorBoard(log_dir = f'logs/{MODEL_NAME}', profile_batch = 2 )
early_stopping = tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=5)
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor="val_loss", patience=3)
model_checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=f'checkpoints/{MODEL_NAME}')
#Fit the model to the training data.
print('[INFO] fitting model...')
#model.fit(X, y, batch_size = BATCH, epochs=EPOCHS, validation_data=(Xt, yt), callbacks=[early_stopping, reduce_lr, tensorboard])
#model.fit(train_datagen, batch_size = BATCH, epochs=EPOCHS, validation_data=test_datagen, callbacks = [tensorboard,early_stopping, reduce_lr, model_checkpoint ])
model.fit(train_ds, epochs=EPOCHS, validation_data=test_ds,
steps_per_epoch = len(train_files)// BATCH, validation_steps = len(test_files)// BATCH,
callbacks=[early_stopping, reduce_lr, tensorboard, model_checkpoint])
print('[INFO] saving model...')
model.save(f'{MODEL_NAME}')