-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg-segmentation.py
More file actions
90 lines (71 loc) · 3.27 KB
/
img-segmentation.py
File metadata and controls
90 lines (71 loc) · 3.27 KB
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
import datetime
import os
import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.layers import (
Flatten, Dense, Reshape, Conv2D, MaxPool2D, Conv2DTranspose)
import matplotlib.pyplot as plt
plt.rcParams['image.cmap'] = 'Greys_r'
# loading the training and test sets from TFRecords
raw_training_dataset = tf.data.TFRecordDataset('data/train_images.tfrecords')
raw_val_dataset = tf.data.TFRecordDataset('data/val_images.tfrecords')
# dictionary describing the fields stored in TFRecord, and used to extract the date from the TFRecords
image_feature_description = {
'height': tf.io.FixedLenFeature([], tf.int64),
'width': tf.io.FixedLenFeature([], tf.int64),
'depth': tf.io.FixedLenFeature([], tf.int64),
'name' : tf.io.FixedLenFeature([], tf.string),
'image_raw': tf.io.FixedLenFeature([], tf.string),
'label_raw': tf.io.FixedLenFeature([], tf.string),
}
# helper function to extract an image from the dictionary
def _parse_image_function(example_proto):
return tf.io.parse_single_example(example_proto, image_feature_description)
parsed_training_dataset = raw_training_dataset.map(_parse_image_function)
parsed_val_dataset = raw_val_dataset.map(_parse_image_function)
print(len(list(parsed_training_dataset)))
print(len(list(parsed_val_dataset)))
# function to read and decode an example from the parsed dataset
@tf.function
def read_and_decode(example):
image_raw = tf.io.decode_raw(example['image_raw'], tf.int64)
image_raw.set_shape([65536])
image = tf.reshape(image_raw, [256, 256, 1])
image = tf.cast(image, tf.float32) * (1. / 1024)
label_raw = tf.io.decode_raw(example['label_raw'], tf.uint8)
label_raw.set_shape([65536])
label = tf.reshape(label_raw, [256, 256, 1])
return image, label
# get datasets read and decoded, and into a state usable by TensorFlow
tf_autotune = tf.data.experimental.AUTOTUNE
train = parsed_training_dataset.map(
read_and_decode, num_parallel_calls=tf_autotune)
val = parsed_val_dataset.map(read_and_decode)
train.element_spec
# setup the buffer size and batch size for data reading and training
BUFFER_SIZE = 10
BATCH_SIZE = 1
# setup the train and test data by shuffling, prefetching, etc
train_dataset = train.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat()
train_dataset = train_dataset.prefetch(buffer_size=tf_autotune)
test_dataset = val.batch(BATCH_SIZE)
train_dataset
# helper function to display an image, it's label and the prediction
def display(display_list):
plt.figure(figsize=(10, 10))
title = ['Input Image', 'Label', 'Predicted Label']
for i in range(len(display_list)):
display_resized = tf.reshape(display_list[i], [256, 256])
plt.subplot(1, len(display_list), i+1)
plt.title(title[i])
plt.imshow(display_resized)
plt.axis('off')
plt.show()
# display an image and label from the training set
for image, label in train.take(2):
sample_image, sample_label = image, label
display([sample_image, sample_label])
# display an image and label from the test set
for image, label in val.take(2):
sample_image, sample_label = image, label
display([sample_image, sample_label])