-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_utils.py
59 lines (40 loc) · 1.77 KB
/
io_utils.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
import tensorflow as tf
import numpy as np
import imageio
from PIL import Image
def read_image(img_path):
# Reads an image in .npy format, and normalizes it via tf.keras.applications.imagenet_utils.preprocess_input
assert '.npy' in img_path
img = np.load(img_path)
# Tensorflow normalization
# Each image is divided by 127.5, and decreased by 1
# Source: https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/keras/applications/imagenet_utils.py#L103-L119
img = tf.keras.applications.imagenet_utils.preprocess_input(img, mode='tf')
return img
def read_label(label_path):
# Reads an array of labels in .npy format
assert '.npy' in label_path
label = np.load(label_path)
return label
def read_and_reshape(img_path, labels_path, img_shape=(1024,512), dataset='CITYSCAPES'):
# Reads an image/label pair and reshapes it to the desired shape
assert dataset in ['CITYSCAPES', 'SYNTHIA', 'GTA5']
try:
# Read Images
img = np.asarray(imageio.imread(img_path, format='PNG'))
labels = np.asarray(imageio.imread(labels_path, format='PNG'))
if dataset == 'SYNTHIA':
# Synthia labels come in a 2-channel format
labels = labels[:,:,0]
if dataset == 'GTA5':
labels = np.asarray(Image.open(labels_path))
# Resize Images
img = Image.fromarray(img).resize((img_shape[0], img_shape[1]), Image.LANCZOS)
img = np.asarray(img)
labels = Image.fromarray(labels).resize((img_shape[0], img_shape[1]), Image.NEAREST)
labels = np.asarray(labels)
return img, labels
except Exception as e:
print(e, img_path, labels_path)
# print("Failed on", labels_path)
return None, None