-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregator.py
177 lines (145 loc) · 5.93 KB
/
aggregator.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
from sklearn.datasets import make_blobs
from keras.models import load_model
from keras.utils import to_categorical
from sklearn.linear_model import LogisticRegression
from keras.models import Sequential
from keras.layers import Dense
from matplotlib import pyplot
from numpy import dstack
import cv2
import tensorflow as tf
import os
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import numpy as np
#from tensorflow.keras.models import Sequential, Model
test_data_dir = "InputData/"
IMG_SIZE = 128
N_CHANNELS = 3
N_CLASSES = 1
SEED = 1234567890 #SOMETHING RANDOM HERE
input_shape = (IMG_SIZE,IMG_SIZE, N_CHANNELS) # SHOULD BE IMG_HEIGHT and IMG_WIDTH but lets see
# Function to load image and return a dictionary
def parse_image(img_path: str) -> dict:
image = tf.io.read_file(img_path)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.convert_image_dtype(image, tf.uint8)
print(image)
# Three types of img paths: um, umm, uu
# gt image paths: um_road, umm_road, uu_road
mask_path = tf.strings.regex_replace(img_path, "image_2", "gt_image_2")
mask_path = tf.strings.regex_replace(mask_path, "um_", "um_road_")
mask_path = tf.strings.regex_replace(mask_path, "umm_", "umm_road_")
mask_path = tf.strings.regex_replace(mask_path, "uu_", "uu_road_")
mask = tf.io.read_file(mask_path)
mask = tf.image.decode_png(mask, channels=3)
non_road_label = np.array([255, 0, 0])
road_label = np.array([255, 0, 255])
other_road_label = np.array([0, 0, 0])
# Convert to mask to binary mask
mask = tf.experimental.numpy.all(mask == road_label, axis = 2)
mask = tf.cast(mask, tf.uint8)
mask = tf.expand_dims(mask, axis=-1)
return {'image': image, 'segmentation_mask': mask}
test_dataset = tf.data.Dataset.list_files(test_data_dir + "*.png", seed=SEED)
test_dataset = test_dataset.map(parse_image)
@tf.function
def normalize(input_image: tf.Tensor, input_mask: tf.Tensor) -> tuple:
input_image = tf.cast(input_image, tf.float32) / 255.0
return input_image, input_mask
# Tensorflow function to preprocess validation images
@tf.function
def load_image_test(datapoint: dict) -> tuple:
input_image = tf.image.resize(datapoint['image'], (IMG_SIZE, IMG_SIZE))
input_mask = tf.image.resize(datapoint['segmentation_mask'], (IMG_SIZE, IMG_SIZE))
input_image, input_mask = normalize(input_image, input_mask)
return input_image, input_mask
BATCH_SIZE = 32
#-- Testing Dataset --#
test_dataset = test_dataset.map(load_image_test)
test_dataset = test_dataset.batch(BATCH_SIZE)
test_dataset = test_dataset.prefetch(buffer_size=tf.data.AUTOTUNE)
models = []
print(test_dataset)
# Function to calculate mask over image
def weighted_img(img, initial_img, α=1., β=0.5, γ=0.):
return cv2.addWeighted(initial_img, α, img, β, γ)
# Function to process an individual image and it's mask
def process_image_mask(image, mask):
# Round to closest
mask = tf.math.round(mask)
# Convert to mask image
zero_image = np.zeros_like(mask)
mask = np.dstack((mask, zero_image, zero_image))
mask = np.asarray(mask, np.float32)
# Convert to image image
image = np.asarray(image, np.float32)
# Get the final image
final_image = weighted_img(mask, image)
return final_image
# Function to save predictions
def save_predictions(dataset,model):
# Predict and save image the from input dataset
index = 0
for batch_image, batch_mask in dataset:
for image, mask in zip(batch_image, batch_mask):
print(f"Processing image : {index}")
pred_mask = model.predict(tf.expand_dims(image, axis = 0))
save_sample([image, process_image_mask(image, pred_mask[0])], index)
index += 1
# Function to save the images as a plot
def save_sample(display_list, index):
plt.figure(figsize=(18, 18))
title = ['Input Image', 'Predicted Mask']
for i in range(len(display_list)):
plt.subplot(1, len(display_list), i+1)
plt.title(title[i])
plt.imshow(tf.keras.preprocessing.image.array_to_img(display_list[i]))
plt.axis('off')
plt.savefig(f"outputs/{index}.png")
plt.show()
# Only for storing images of the input data
# Replace dataset with test data
os.mkdir("outputs")
# load models from file
def load_all_models(n_models):
all_models = list()
for i in range(n_models):
# load model from file
model = load_model(f'Models/Trained{i+1}.h5')
# add to list of members
all_models.append(model)
print(f'>loaded: Models/Trained{i+1}.h5')
return all_models
save_predictions(test_dataset,load_all_models(models)[0])
# create stacked model input dataset as outputs from the ensemble
def stacked_dataset(members, inputX):
stackX = None
for model in members:
# make prediction
yhat = model.predict(inputX, verbose=0)
# stack predictions into [rows, members, probabilities]
if stackX is None:
stackX = yhat
else:
stackX = dstack((stackX, yhat))
# flatten predictions to [rows, members x probabilities]
stackX = stackX.reshape((stackX.shape[0], stackX.shape[1]*stackX.shape[2]))
return stackX
# fit a model based on the outputs from the ensemble members
def fit_stacked_model(members, inputX, inputy):
# create dataset using ensemble
stackedX = stacked_dataset(members, inputX)
# fit standalone model
model = LogisticRegression()
model.fit(stackedX, inputy)
return model
# make a prediction with the stacked model
def stacked_prediction(members, model, inputX):
# create dataset using ensemble
stackedX = stacked_dataset(members, inputX)
# make a prediction
yhat = model.predict(stackedX)
return yhat
# evaluate model on test set
yhat = stacked_prediction(models, fit_stacked_model(models,test_dataset), test_dataset)