-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_load.py
44 lines (39 loc) · 1.75 KB
/
data_load.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
import math
import os
import re
import cv2
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
from random import sample
from PIL import Image
# some constants
PATCH_SIZE = 16 # pixels per side of square patches
VAL_SIZE = 10 # size of the validation set (number of images)
CUTOFF = 0.25 # minimum average brightness for a mask patch to be classified as containing road
# unzip the dataset, split it and organize it in folders
if not os.path.isdir('validation'): # make sure this has not been executed yet
try:
!unzip cil-road-segmentation-2021.zip
!mv training/training/* training
!rm -rf training/training
!mkdir validation
!mkdir validation/images
!mkdir validation/groundtruth
for img in sample(glob("training/images/*.png"), VAL_SIZE):
os.rename(img, img.replace('training', 'validation'))
mask = img.replace('images', 'groundtruth')
os.rename(mask, mask.replace('training', 'validation'))
except:
print('Please upload a .zip file containing your datasets.')
def load_all_from_path(path):
# loads all HxW .pngs contained in path as a 4D np.array of shape (n_images, H, W, 3)
# images are loaded as floats with values in the interval [0., 1.]
return np.stack([np.array(Image.open(f)) for f in sorted(glob(path + '/*.png'))]).astype(np.float32) / 255.
# paths to training and validation datasets
train_path = 'training'
val_path = 'validation'
train_images = load_all_from_path(os.path.join(train_path, 'images'))
train_masks = load_all_from_path(os.path.join(train_path, 'groundtruth'))
val_images = load_all_from_path(os.path.join(val_path, 'images'))
val_masks = load_all_from_path(os.path.join(val_path, 'groundtruth'))