-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
159 lines (126 loc) · 5.89 KB
/
main.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
import glob
import tensorflow as tf
import numpy as np
import os
import sys
import wandb
from wandb.keras import WandbCallback
from d169 import DepthEstimate
RESTORE_SAVE = False
tf.get_logger().setLevel('ERROR')
#os.environ['WANDB_MODE'] = 'dryrun' # Testing for funcitonality
if RESTORE_SAVE:
wandb.init(project="mc_depthnn", resume=True)
else:
wandb.init(project="mc_depthnn")
physical_devices = tf.config.list_physical_devices('GPU')
try:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
except:
# Invalid device or cannot modify virtual devices once initialized
pass
AUTOTUNE = tf.data.experimental.AUTOTUNE
DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data", "images")
BATCH_SIZE = 8
EPOCHS = 50
image_count = len(glob.glob(DATA_DIR + os.path.sep + "inputs"))
image_width = 512
image_height = 512
input_shape = (image_height, image_width, 3,)
normalization_layer = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)
def decode_colored_img(img):
img = tf.image.decode_png(img, channels=3)
return tf.image.resize(img, [image_height, image_width])
def decode_bw_img(img):
img = tf.image.decode_png(img, channels=1)
return tf.image.resize(img, [image_height, image_width])
def process_path(file_path):
input_image = tf.io.read_file(file_path)
output_image = tf.io.read_file(tf.strings.join([DATA_DIR, "/outputs/", tf.strings.substr(file_path, -8, -1)]))
input_image = decode_colored_img(input_image)
output_image = decode_bw_img(output_image)
input_image = normalization_layer(input_image)
output_image = normalization_layer(output_image)
return input_image, output_image
def process_validation_path(file_path):
input_image = tf.io.read_file(file_path)
output_image = tf.io.read_file(tf.strings.join([DATA_DIR, "/validation_outputs/", tf.strings.substr(file_path, -8, -1)]))
input_image = decode_colored_img(input_image)
output_image = decode_bw_img(output_image)
input_image = normalization_layer(input_image)
output_image = normalization_layer(output_image)
return input_image, output_image
def configure_for_performance(ds):
#ds = ds.cache()
ds = ds.batch(BATCH_SIZE)
ds = ds.prefetch(buffer_size=AUTOTUNE)
return ds
print("Creating model...")
"""
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(8, (3, 3), activation='relu', input_shape=(input_shape)))
model.add(tf.keras.layers.MaxPooling2D((3, 3)))
model.add(tf.keras.layers.Conv2D(16, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D((3, 3)))
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D((3, 3)))
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D((3, 3)))
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(512, activation='relu'))
model.add(tf.keras.layers.Dense(1024, activation='relu'))
model.add(tf.keras.layers.Reshape((16, 16, 4)))
model.add(tf.keras.layers.Conv2DTranspose(32, (3, 3), strides=2, padding="same", activation='relu'))
model.add(tf.keras.layers.Conv2DTranspose(32, (3, 3), strides=2, padding="same", activation='relu'))
model.add(tf.keras.layers.Conv2DTranspose(16, (3, 3), strides=2, padding="same", activation='relu'))
model.add(tf.keras.layers.Conv2DTranspose(8, (3, 3), strides=2, padding="same", activation='relu'))
model.add(tf.keras.layers.Conv2DTranspose(1, (3, 3), strides=2, padding="same", activation='sigmoid'))
"""
"""
input_layer = tf.keras.Input(shape=(input_shape))
x = tf.keras.layers.Conv2D(8, (3, 3), activation='relu')(input_layer)
x = tf.keras.layers.MaxPooling2D((2, 2))(x)
x = tf.keras.layers.Conv2D(16, (3, 3), activation='relu')(x)
x = tf.keras.layers.MaxPooling2D((2, 2))(x)
x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(x)
x = tf.keras.layers.MaxPooling2D((2, 2))(x)
x = tf.keras.layers.Conv2DTranspose(32, (3, 3), padding="valid", activation='relu')(x)
x = tf.keras.layers.Conv2DTranspose(32, (3, 3), strides=2, padding="same", activation='relu')(x)
x = tf.keras.layers.Conv2DTranspose(32, (3, 3), strides=2, padding="same", activation='relu')(x)
x = tf.keras.layers.Conv2DTranspose(32, (3, 3), strides=2, padding="same", activation='relu')(x)
x = tf.keras.layers.Concatenate()([x, input_layer])
output_layer = tf.keras.layers.Conv2D(1, (1, 1), activation="sigmoid")(x)
model = tf.keras.Model(inputs=input_layer, outputs=output_layer)
"""
model = None
if RESTORE_SAVE:
model = tf.keras.models.load_model("wandb/run-20200805_215504-j6qvvapg/model.tf")
else:
model = DepthEstimate()
model.build((None, 512, 512, 3,))
print(model)
model.compile(loss='MSE', optimizer=tf.keras.optimizers.Adadelta(), metrics=[tf.keras.metrics.Accuracy()])
checkpoint_path = os.path.realpath(__file__)[:len(os.path.realpath(__file__)) - 8] + "/content/training/cp-{epoch:04d}.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_path,
verbose=1,
period=25,
)
model.save_weights(checkpoint_path.format(epoch=0))
print("Compiling data...")
train_ds = tf.data.Dataset.list_files(str(DATA_DIR + '/inputs/*.png'), shuffle=False).take(4096)
train_ds = train_ds.shuffle(4096, reshuffle_each_iteration=True)
train_ds = train_ds.map(process_path, num_parallel_calls=AUTOTUNE)
train_ds = configure_for_performance(train_ds)
test_ds = tf.data.Dataset.list_files(str(DATA_DIR + '/validation_inputs/*.png'), shuffle=False)
test_ds = test_ds.shuffle(4096, reshuffle_each_iteration=True)
test_ds = test_ds.map(process_validation_path, num_parallel_calls=AUTOTUNE)
test_ds = configure_for_performance(test_ds)
#print(type(train_ds))
#print(train_ds)
#print(type(test_ds))
#print(test_ds)
model.fit(train_ds, batch_size=BATCH_SIZE, epochs=EPOCHS, verbose=2, callbacks=[WandbCallback()], validation_data=test_ds, initial_epoch=wandb.run.step)
model.save(os.path.join(wandb.run.dir, "model.tf"), save_format="tf")