-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
70 lines (56 loc) · 2.33 KB
/
preprocess.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
# preprocess image and masks
img_size = [256,256]
def data_augmentation(car_img, mask_img):
if tf.random.uniform(()) > 0.5:
car_img = tf.image.flip_left_right(car_img)
mask_img = tf.image.flip_left_right(mask_img)
return car_img, mask_img
def preprocessing(image_path, mask_path):
car_img = tf.io.read_file(image_path)
car_img = tf.image.decode_png(car_img, channels=3)
car_img = tf.image.resize(car_img, img_size)
car_img = tf.cast(car_img, tf.float32) / 255.0
mask_img = tf.io.read_file(mask_path)
mask_img = tf.image.decode_png(mask_img, channels=3)
mask_img = tf.image.resize(mask_img, img_size)
mask_img = mask_img[:,:,:1]
mask_img = tf.math.sign(mask_img)
return car_img, mask_img
# create dataset
def create_dataset(df, train = False):
if not train:
ds = tf.data.Dataset.from_tensor_slices((df["image_path"].values, df["mask_path"].values))
ds = ds.map(preprocessing, tf.data.AUTOTUNE)
else:
ds = tf.data.Dataset.from_tensor_slices((df["image_path"].values, df["mask_path"].values))
ds = ds.map(preprocessing, tf.data.AUTOTUNE)
ds = ds.map(data_augmentation, tf.data.AUTOTUNE)
return ds
# define train, valid and test datasets
train_df, valid_df = train_test_split(df, random_state=42, test_size=.25)
train = create_dataset(train_df, train = True)
valid = create_dataset(valid_df)
test = create_dataset(test_df)
# set training params
TRAIN_LENGTH = len(train_df)
BATCH_SIZE = 16
BUFFER_SIZE = 1000
train_dataset = train.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat()
train_dataset = train_dataset.prefetch(buffer_size=tf.data.AUTOTUNE)
valid_dataset = valid.batch(BATCH_SIZE)
test_dataset = test.batch(BATCH_SIZE)
# visualize data
def display(display_list):
plt.figure(figsize=(12,12))
title = ['Input Image','True Mask','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.show()
# plot image and true mask
for i in range(5):
for image, mask in train.take(i):
sample_image, sample_mask = image, mask
display([sample_image, sample_mask])